mycoagent.c 2.9 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 = "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, 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, "RESOURCE_2");
  37. if (myresource2.size != -1) {
  38. printf("The first integer of RESOURCE_2 is: %d\n", *((int*)myresource2.pointer));
  39. } else {
  40. fprintf(stderr, "Transfer did not work!\n");
  41. }
  42. // MYCO read
  43. // #MYCO read("RESOURCE_3")
  44. struct resource myresource3;
  45. myresource3 = myco_agent_read_remote_resource(agent_message_queue_id, "RESOURCE_3");
  46. if (myresource3.size != -1) {
  47. printf("The first integer of RESOURCE_3 is: %d\n", *((int*)myresource3.pointer));
  48. } else {
  49. fprintf(stderr, "Transfer did not work!\n");
  50. }
  51. // MYCO push
  52. // TODO
  53. // MYCO free
  54. // #MYCO free("RESOURCE_1")
  55. myco_agent_unregister_resource(agent_message_queue_id, resource_name);
  56. // MYCO free
  57. // #MYCO free("RESOURCE_2")
  58. /*
  59. myco_agent_unregister_resource(agent_message_queue_id, "RESOURCE_2");
  60. myco_free(myresource2.pointer);
  61. */
  62. // At the end of its runtime the agent unregisters with the daemon.
  63. myco_agent_unregister(agent_name, agent_message_queue_id);
  64. return EXIT_SUCCESS;
  65. }