main.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import sys
  2. sys.path.append("wrappers")
  3. from modelverse import *
  4. import random, re, json, uuid
  5. from pprint import pprint
  6. from multiprocessing import Process, Pipe, freeze_support
  7. sys.path.append('services/DEVS/pypdevs/src')
  8. sys.path.append('services/DEVS/pypdevs/examples')
  9. from simulator import Controller
  10. from ps_model import Root
  11. import threading, time
  12. init(sys.argv[1])
  13. login("pypdevs_service", "my_password")
  14. def pypdevs_service(port):
  15. try:
  16. exec service_get(port) in globals()
  17. except SyntaxError:
  18. exec(service_get(port) in globals())
  19. controller = Controller(Root())
  20. def inputter():
  21. print("Waiting for input...")
  22. while 1:
  23. try:
  24. input = service_get(port)
  25. print('raw input = %s' % input)
  26. params = json.loads(input)
  27. print('json parsed = %s' % params)
  28. if not isinstance(params, list):
  29. params = [params]
  30. print("Sending input to simulator: %s" % params)
  31. if (len(params) > 1):
  32. controller.addInput(Event(params[0], "request", params[1:]))
  33. else:
  34. controller.addInput(Event(params[0], "request", []))
  35. except:
  36. import traceback
  37. traceback.print_exc()
  38. input_thread = threading.Thread(target=inputter)
  39. input_thread.daemon = True
  40. input_thread.start()
  41. output_listener = controller.addOutputListener(["reply"])
  42. def outputter():
  43. print("Waiting for output...")
  44. while 1:
  45. output_val = output_listener.fetch(-1)
  46. print("Got output from simulator: %s" % output_val)
  47. service_set(port, json.dumps({"name": output_val.getName(), "port": output_val.getPort(), "params": output_val.getParameters()}))
  48. output_thread = threading.Thread(target=outputter)
  49. output_thread.daemon = True
  50. output_thread.start()
  51. controller.start()
  52. service_register("pypdevs_simulator", pypdevs_service)
  53. try:
  54. while True:
  55. # Stay active, as we shouldn't exit while the service is running!
  56. time.sleep(1)
  57. finally:
  58. service_stop()