services.alc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. include "primitives.alh"
  2. include "utils.alh"
  3. include "core_algorithm.alh"
  4. include "modelling.alh"
  5. include "object_operations.alh"
  6. Integer services = 0
  7. String function comm_newPort():
  8. // Create a new username, though prefixed with __ to prevent execution: this is merely a communication port!
  9. // The specified port needs an input and output queue, like ordinary users.
  10. Element root
  11. root = read_root()
  12. // Initialize, but we know that __hierarchy is already taken!
  13. String attempt
  14. attempt = "__hierarchy"
  15. while (dict_in(root, attempt)):
  16. attempt = (("__" + get_taskname()) + "_") + cast_v2s(services)
  17. services = services + 1
  18. // Create queues
  19. Element queues
  20. queues = create_node()
  21. dict_add(queues, "input", create_node())
  22. dict_add(queues, "output", create_node())
  23. // Make it visible
  24. dict_add(root, attempt, queues)
  25. return attempt!
  26. Boolean function comm_hasInput(comm : String):
  27. // Check if there is any input on the specified pseudo-username
  28. Element root
  29. root = read_root()
  30. return dict_in(root[comm]["input"], "value")!
  31. Void function comm_set(comm : String, value : Element):
  32. // Set input to the specified service
  33. // For the Mv, this is actually output!
  34. Element root
  35. root = read_root()
  36. Element new
  37. new = create_node()
  38. dict_add(root[comm]["last_output"], "value", value)
  39. dict_add(root[comm]["last_output"], "next", new)
  40. dict_overwrite(root[comm], "last_output", new)
  41. return!
  42. Element function comm_get(comm : String):
  43. // Fetch input from the service
  44. while (bool_not(comm_hasInput(comm))):
  45. sleep(0.01)
  46. // Return the value that we got
  47. Element root
  48. root = read_root()
  49. Element value
  50. value = root["input"]["value"]
  51. dict_overwrite(root, "input", root["input"]["next"])
  52. return value!
  53. String function comm_connect(service : String):
  54. // Connect to an existing service
  55. log("Resolving service location for " + cast_e2s(service))
  56. service = get_service_id(service)
  57. log("Resolved service ID: " + service)
  58. service = read_attribute(core, service, "port")
  59. log("Resolved port: " + service)
  60. String port
  61. port = comm_newPort()
  62. log("Request new port: " + port)
  63. comm_set(service, port)
  64. log("Communication OK")
  65. return port!