mycoagent3.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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_MYAGENT3";
  19. int agent_message_queue_id = myco_agent_register(agent_name);
  20. // MYCO read
  21. // #MYCO read("RESOURCE_3")
  22. struct resource myresource3;
  23. myresource3 = myco_agent_read_remote_resource(agent_message_queue_id, "RESOURCE_3");
  24. if (myresource3.size != -1) {
  25. printf("The first integer of RESOURCE_3 is: %d\n", *((int*)myresource3.pointer));
  26. }
  27. // Change some values in RESOURCE_3
  28. ((int*)myresource3.pointer)[0] = 1;
  29. sleep(10);
  30. // MYCO push
  31. // #MYCO push("RESOURCE_3")
  32. myco_agent_write_remote_resource(agent_message_queue_id, "RESOURCE_3", myresource3.pointer, myresource3.size, 0);
  33. // At the end of its runtime the agent unregisters with the daemon.
  34. myco_agent_unregister(agent_name, agent_message_queue_id);
  35. return EXIT_SUCCESS;
  36. }