mycoagent.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * mycoagent.c
  3. *
  4. * This is an example agent that showcases the use of mycorrhiza.
  5. */
  6. #include "../src/myco-agent.c"
  7. // MYCO fetch: Requests a remote, transactional resource. Blocks until
  8. // the resource has been granted.
  9. // MYCO create: Creates a new resource and registers it with the agent.
  10. // MYCO release: Releases a resource. It can now be taken by other agents via fetch.
  11. // MYCO free: Destroys a resource for the entire system. Only works if the calling agent has ownership (fetch/create) of the resource.
  12. // MYCO read: Creates a local copy of a remote resource.
  13. // MYCO push: Updates the remote resource with the local copy obtained from read.
  14. // MYCO lock: Protects a resource from read access.
  15. // MYCO unlock: Allows read access again.
  16. int main() {
  17. // At the beginning every agent needs to register itself with the agent server.
  18. char *agent_name = "DOMAIN1_MYAGENT";
  19. int agent_message_queue_id = myco_agent_register(agent_name);
  20. // MYCO create
  21. // #MYCO create("RESOURCE_1", 1024, transactional)
  22. char *resource_name = "RESOURCE_1";
  23. int resource_size = 1024;
  24. void *resource_pointer = myco_agent_register_resource(agent_message_queue_id, agent_name, resource_name, RESOURCE_TRANSACTIONAL, getpid(), resource_size);
  25. // MYCO lock
  26. myco_agent_lock_resource(agent_message_queue_id, agent_name, resource_name);
  27. // MYCO unlock
  28. myco_agent_unlock_resource(agent_message_queue_id, agent_name, resource_name);
  29. // MYCO release
  30. myco_agent_release_resource(agent_message_queue_id, agent_name, resource_name, resource_pointer, resource_size);
  31. // View available resources
  32. myco_agent_request_resource_list(agent_message_queue_id);
  33. // MYCO fetch
  34. // #MYCO fetch("RESOURCE_2")
  35. struct resource myresource2;
  36. myresource2 = myco_agent_request_resource(agent_message_queue_id, agent_name, "RESOURCE_2");
  37. if (myresource2.size != -1) {
  38. printf("The first integer of RESOURCE_2 is: %d\n", *((int*)myresource2.pointer));
  39. }
  40. // MYCO read
  41. // #MYCO read("RESOURCE_3")
  42. struct resource myresource3;
  43. myresource3 = myco_agent_read_remote_resource(agent_message_queue_id, "RESOURCE_3");
  44. if (myresource3.size != -1) {
  45. printf("The first integer of RESOURCE_3 is: %d\n", *((int*)myresource3.pointer));
  46. }
  47. // Change some values in RESOURCE_3
  48. if (myresource3.size != -1) {
  49. ((int*)myresource3.pointer)[0] = 0;
  50. }
  51. // MYCO push
  52. // #MYCO push("RESOURCE_3")
  53. myco_agent_write_remote_resource(agent_message_queue_id, "RESOURCE_3", myresource3.pointer, myresource3.size, 0);
  54. // MYCO free
  55. // #MYCO free("RESOURCE_1")
  56. myco_agent_unregister_resource(agent_message_queue_id, resource_name);
  57. // MYCO free
  58. // #MYCO free("RESOURCE_2")
  59. myco_agent_unregister_resource(agent_message_queue_id, "RESOURCE_2");
  60. // At the end of its runtime the agent unregisters with the daemon.
  61. myco_agent_unregister(agent_name, agent_message_queue_id);
  62. return EXIT_SUCCESS;
  63. }