mycoagent.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 = "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_agent_request_resource_list(agent_message_queue_id);
  26. // MYCO lock
  27. myco_agent_lock_resource(agent_message_queue_id, agent_name, resource_name);
  28. myco_agent_request_resource_list(agent_message_queue_id);
  29. // MYCO unlock
  30. myco_agent_unlock_resource(agent_message_queue_id, agent_name, resource_name);
  31. myco_agent_request_resource_list(agent_message_queue_id);
  32. // MYCO release
  33. // unmap memory
  34. myco_agent_release_resource(agent_message_queue_id, resource_name, resource_pointer, resource_size);
  35. // View resources
  36. myco_agent_request_resource_list(agent_message_queue_id);
  37. // MYCO fetch
  38. // #MYCO fetch("RESOURCE_2")
  39. // open shared memory
  40. // mmap memory
  41. /*
  42. struct resource myresource2;
  43. myresource2 = myco_agent_request_resource(agent_message_queue_id, "RESOURCE_2");
  44. if (myresource2.size != -1) {
  45. printf("The first integer of RESOURCE_2 is: %d\n", *((int*)myresource2.pointer));
  46. } else {
  47. fprintf(stderr, "Transfer did not work!\n");
  48. }
  49. */
  50. // MYCO read
  51. // #MYCO read("RESOURCE_3")
  52. /*
  53. struct resource myresource3;
  54. myresource3 = myco_agent_read_remote_resource(agent_message_queue_id, "RESOURCE_3");
  55. if (myresource3.size != -1) {
  56. printf("The first integer of RESOURCE_3 is: %d\n", *((int*)myresource3.pointer));
  57. } else {
  58. fprintf(stderr, "Transfer did not work!\n");
  59. }
  60. */
  61. // MYCO push
  62. // TODO
  63. // MYCO free
  64. // #MYCO free("RESOURCE_1")
  65. myco_agent_unregister_resource(agent_message_queue_id, resource_name);
  66. // MYCO free
  67. // #MYCO free("RESOURCE_2")
  68. /*
  69. myco_agent_unregister_resource(agent_message_queue_id, "RESOURCE_2");
  70. myco_free(myresource2.pointer);
  71. */
  72. // At the end of its runtime the agent unregisters with the daemon.
  73. myco_agent_unregister(agent_name, agent_message_queue_id);
  74. return EXIT_SUCCESS;
  75. }