Browse Source

Start up the HUTN service with the Modelverse, and split of starting up HTTP client in modelverse.py

Yentl Van Tendeloo 8 years ago
parent
commit
4e8bb6a42e
3 changed files with 52 additions and 34 deletions
  1. 5 1
      scripts/HUTN_service.py
  2. 5 4
      scripts/run_local_modelverse.py
  3. 42 29
      wrappers/modelverse.py

+ 5 - 1
scripts/HUTN_service.py

@@ -7,7 +7,11 @@ from hutn_compiler.compiler import main as do_compile
 from modelverse import *
 import os
 
-init()
+import time
+time.sleep(1)
+print("INIT HUTN")
+init(sys.argv[1])
+print("INITTED HUTN")
 login("HUTN", "HUTN")
 
 def compile_service(port):

+ 5 - 4
scripts/run_local_modelverse.py

@@ -8,6 +8,9 @@ if len(sys.argv) < 2:
 else:
     port = sys.argv[1]
 
+# Start up the HUTN compilation service already
+hutn = subprocess.Popen([sys.executable, "scripts/HUTN_service.py", "127.0.0.1:%s" % port])
+
 os.chdir("hybrid_server")
 subprocess.check_call([sys.executable, "-m", "sccd.compiler.sccdc", "-p", "threads", "server.xml"])
 
@@ -18,7 +21,5 @@ program_to_execute = [sys.executable, "run_mvk_server.py", port]
 #program_to_execute = [sys.executable, "run_mvk_server.py", port, "--kernel=interpreter"]
 #program_to_execute = [sys.executable, "run_mvk_server.py", port, "--kernel=fast-jit"]
 
-if os.name == "nt":
-    subprocess.call(program_to_execute)
-else:
-    os.execv(sys.executable, program_to_execute)
+subprocess.call(program_to_execute)
+hutn.terminate()

+ 42 - 29
wrappers/modelverse.py

@@ -213,7 +213,10 @@ def _compile_model(code):
 
     return do_compile(".model.mvc", COMPILER_PATH + "/grammars/modelling.g", "M")
 
-def _output(expected=None):
+def _output(expected=None, port=None):
+    if port is None:
+        port = taskname
+
     try:
         while len(outputs) < 2:
             time.sleep(0.02)
@@ -225,9 +228,11 @@ def _output(expected=None):
 
     if expected is not None and _last_output() != expected:
         raise InterfaceMismatch(_last_output(), expected)
-    return _last_output()
+    return outputs[0]
 
-def _last_output():
+def _last_output(port=None):
+    if port is None:
+        port = taskname
     return outputs[0]
 
 # Raise common exceptions
@@ -298,11 +303,34 @@ def alter_context(model_name, metamodel_name):
     global registered_metamodels
     registered_metamodels[model_name] = metamodel_name
 
+def _start_http_client(addr, port, timeout):
+    import http_client
+    ctrl = http_client.Controller()
+    listener = ctrl.addOutputListener("request_out")
+    socket2event.boot_translation_service(ctrl)
+    thrd = threading.Thread(target=ctrl.start)
+    thrd.daemon = True
+    thrd.start()
+
+    evt = listener.fetch(-1)
+    if evt.name != "http_client_initialized":
+        raise Exception("HTTP client did not behave as expected during init: " + str(evt.name))
+
+    ctrl.addInput(Event("connect", "request_in", [(addr, port), timeout]))
+
+    evt = listener.fetch(-1)
+
+    if evt.name == "http_client_timeout":
+        raise Exception("HTTP client timeout")
+    if evt.name != "http_client_ready":
+        raise Exception("HTTP client did not behave as expected during connect: " + str(evt.name))
+
+    return ctrl
+
 # Main MvC operations
 def init(address_param="127.0.0.1:8001", timeout=20.0):
     """Starts up the connection to the Modelverse."""
     # Start up the HTTP Client SC
-    import http_client
     global ctrl_input
     global ctrl_output
 
@@ -311,32 +339,12 @@ def init(address_param="127.0.0.1:8001", timeout=20.0):
     if ctrl_output is not None:
         ctrl_output.stop()
 
-    ctrl_input = http_client.Controller()
-    ctrl_output = http_client.Controller()
-    controllers = [ctrl_input, ctrl_output]
-
     addr, port = address_param.split(":", 1)
     port = int(port)
-
-    for ctrl in controllers:
-        listener = ctrl.addOutputListener("request_out")
-        socket2event.boot_translation_service(ctrl)
-        thrd = threading.Thread(target=ctrl.start)
-        thrd.daemon = True
-        thrd.start()
-
-        evt = listener.fetch(-1)
-        if evt.name != "http_client_initialized":
-            raise Exception("HTTP client did not behave as expected during init: " + str(evt.name))
-
-        ctrl.addInput(Event("connect", "request_in", [(addr, port), timeout]))
-
-        evt = listener.fetch(-1)
-
-        if evt.name == "http_client_timeout":
-            raise Exception("HTTP client timeout")
-        if evt.name != "http_client_ready":
-            raise Exception("HTTP client did not behave as expected during connect: " + str(evt.name))
+    
+    ctrl_input = _start_http_client(addr, port, timeout)
+    ctrl_output = _start_http_client(addr, port, timeout) 
+    controllers = [ctrl_input, ctrl_output]
 
     global mode
     start_time = time.time()
@@ -348,9 +356,14 @@ def init(address_param="127.0.0.1:8001", timeout=20.0):
 
     global outputs
     global taskname
+    taskname = task
+    outputs = [None]
 
+    _listen_to_output(task)
+
+def _listen_to_output(task):
+    global outputs
     outputs = [None]
-    taskname = task
 
     # This re-assign also diconnects the previous get_output connections to the outputs variable
     thrd = threading.Thread(target=_output_thread, args=[outputs, task])