services.alc 1.6 KB

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