mycoagent2.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_MYAGENT2";
  19. int agent_message_queue_id;
  20. agent_message_queue_id = myco_agent_register(agent_name);
  21. // MYCO create
  22. // #MYCO create("RESOURCE_2", 1024, transactional)
  23. char *resource_name_2 = "RESOURCE_2";
  24. int resource_size_2 = 1024;
  25. void *resource_pointer_2 = myco_agent_register_resource(agent_message_queue_id, agent_name, resource_name_2, RESOURCE_TRANSACTIONAL, getpid(), resource_size_2);
  26. // Put some random numbers into RESOURCE_2
  27. int i;
  28. srand(time(NULL));
  29. for (i=0; i < resource_size_2/sizeof(int); i++) {
  30. ((int *)resource_pointer_2)[i] = rand();
  31. }
  32. printf("The first integer of RESOURCE_2 is: %d\n", ((int *)resource_pointer_2)[0]);
  33. // MYCO release
  34. //myco_agent_release_resource(agent_message_queue_id, agent_name, resource_name_2, resource_pointer_2, resource_size_2);
  35. // MYCO create
  36. // #MYCO create("RESOURCE_3", 1024, nottransactional)
  37. char *resource_name_3 = "RESOURCE_3";
  38. int resource_size_3 = 1024;
  39. void *resource_pointer_3 = myco_agent_register_resource(agent_message_queue_id, agent_name, resource_name_3, RESOURCE_TRANSACTIONAL, getpid(), resource_size_3);
  40. // Put some random numbers into RESOURCE_3
  41. int j;
  42. for (j=0; j < resource_size_3/sizeof(int); j++) {
  43. ((int*)resource_pointer_3)[j] = rand();
  44. }
  45. printf("The first integer of RESOURCE_3 is: %d\n", ((int*)resource_pointer_3)[0]);
  46. // Sleep, so the showcase program can request RESOURCE_2 and read/push RESOURCE_3
  47. sleep(30);
  48. // RESOURCE_3 has been pushed, lets check
  49. printf("The first integer of RESOURCE_3 is now: %d\n", ((int*)resource_pointer_3)[0]);
  50. // MYCO free
  51. // #MYCO free("RESOURCE_2")
  52. myco_agent_unregister_resource(agent_message_queue_id, resource_name_2);
  53. // MYCO free
  54. // #MYCO free("RESOURCE_3")
  55. myco_agent_unregister_resource(agent_message_queue_id, resource_name_3);
  56. // At the end of its runtime the agent unregisters with the agent.
  57. myco_agent_unregister(agent_name, agent_message_queue_id);
  58. return EXIT_SUCCESS;
  59. }