services.alc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_value(services)
  17. services = services + 1
  18. // Create queues
  19. Element queues
  20. queues = dict_create()
  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.1)
  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. if (service == ""):
  59. return ""!
  60. service = read_attribute(core, service, "port")
  61. String port
  62. port = comm_newPort()
  63. comm_set(service, port)
  64. return port!
  65. Void function comm_close(comm : String):
  66. if (string_startswith(comm, "__")):
  67. dict_delete(read_root(), comm)
  68. return!
  69. Element function comm_get_list(comm : String):
  70. Integer length
  71. Element result
  72. length = comm_get(comm)
  73. result = list_create()
  74. while (length > 0):
  75. list_append(result, comm_get(comm))
  76. length = length - 1
  77. return result!