services.alc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, "last_input", queues["input"])
  23. dict_add(queues, "output", create_node())
  24. dict_add(queues, "last_output", queues["output"])
  25. // Make it visible
  26. dict_add(root, attempt, queues)
  27. return attempt!
  28. Boolean function comm_hasInput(comm : String):
  29. // Check if there is any input on the specified pseudo-username
  30. Element root
  31. root = read_root()
  32. return dict_in(root[comm]["input"], "value")!
  33. Void function comm_set(comm : String, value : Element):
  34. // Set input to the specified service
  35. // For the Mv, this is actually output!
  36. Element root
  37. root = read_root()
  38. Element new
  39. new = create_node()
  40. dict_add(root[comm]["last_output"], "value", value)
  41. dict_add(root[comm]["last_output"], "next", new)
  42. dict_overwrite(root[comm], "last_output", new)
  43. return!
  44. Element function comm_get(comm : String):
  45. // Fetch input from the service
  46. while (bool_not(comm_hasInput(comm))):
  47. sleep(0.01)
  48. // Return the value that we got
  49. Element root
  50. Element value
  51. root = read_root()
  52. value = root[comm]["input"]["value"]
  53. dict_overwrite(root[comm], "input", root[comm]["input"]["next"])
  54. return value!
  55. String function comm_connect(service : String):
  56. // Connect to an existing service
  57. service = get_service_id(service)
  58. service = read_attribute(core, service, "port")
  59. String port
  60. port = comm_newPort()
  61. comm_set(service, port)
  62. return port!
  63. Void function comm_close(comm : String):
  64. if (string_startswith(comm, "__")):
  65. dict_delete(read_root(), comm)
  66. return!