123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /*
- * mycoagent.c
- *
- * This is an example agent that showcases the use of mycorrhiza.
- */
- #include "../src/myco-agent.c"
- // MYCO fetch: Requests a remote, transactional resource. Blocks until
- // the resource has been granted.
- // MYCO create: Creates a new resource and registers it with the agent.
- // MYCO release: Releases a resource. It can now be taken by other agents via fetch.
- // MYCO free: Destroys a resource for the entire system. Only works if the calling agent has ownership (fetch/create) of the resource.
- // MYCO read: Creates a local copy of a remote resource.
- // MYCO push: Updates the remote resource with the local copy obtained from read.
- // MYCO lock: Protects a resource from read access.
- // MYCO unlock: Allows read access again.
- int main() {
- // At the beginning every agent needs to register itself with the agent server.
- char *agent_name = "MYAGENT";
- int agent_message_queue_id = myco_agent_register(agent_name);
- // MYCO create
- // #MYCO create("RESOURCE_1", 1024, transactional)
- char *resource_name = "RESOURCE_1";
- int resource_size = 1024;
- void *resource_pointer = myco_agent_register_resource(agent_message_queue_id, agent_name, resource_name, RESOURCE_TRANSACTIONAL, getpid(), resource_size);
- myco_agent_request_resource_list(agent_message_queue_id);
- // MYCO lock
- myco_agent_lock_resource(agent_message_queue_id, agent_name, resource_name);
- myco_agent_request_resource_list(agent_message_queue_id);
- // MYCO unlock
- myco_agent_unlock_resource(agent_message_queue_id, agent_name, resource_name);
- myco_agent_request_resource_list(agent_message_queue_id);
- // MYCO release
- // unmap memory
- myco_agent_release_resource(agent_message_queue_id, resource_name, resource_pointer, resource_size);
- // View resources
- myco_agent_request_resource_list(agent_message_queue_id);
- // MYCO fetch
- // #MYCO fetch("RESOURCE_2")
- // open shared memory
- // mmap memory
- /*
- struct resource myresource2;
- myresource2 = myco_agent_request_resource(agent_message_queue_id, "RESOURCE_2");
- if (myresource2.size != -1) {
- printf("The first integer of RESOURCE_2 is: %d\n", *((int*)myresource2.pointer));
- } else {
- fprintf(stderr, "Transfer did not work!\n");
- }
- */
- // MYCO read
- // #MYCO read("RESOURCE_3")
- /*
- struct resource myresource3;
- myresource3 = myco_agent_read_remote_resource(agent_message_queue_id, "RESOURCE_3");
- if (myresource3.size != -1) {
- printf("The first integer of RESOURCE_3 is: %d\n", *((int*)myresource3.pointer));
- } else {
- fprintf(stderr, "Transfer did not work!\n");
- }
- */
- // MYCO push
- // TODO
- // MYCO free
- // #MYCO free("RESOURCE_1")
- myco_agent_unregister_resource(agent_message_queue_id, resource_name);
- // MYCO free
- // #MYCO free("RESOURCE_2")
- /*
- myco_agent_unregister_resource(agent_message_queue_id, "RESOURCE_2");
- myco_free(myresource2.pointer);
- */
- // At the end of its runtime the agent unregisters with the daemon.
- myco_agent_unregister(agent_name, agent_message_queue_id);
- return EXIT_SUCCESS;
- }
|