1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- /*
- * 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 = "DOMAIN1_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 lock
- myco_agent_lock_resource(agent_message_queue_id, agent_name, resource_name);
- // MYCO unlock
- myco_agent_unlock_resource(agent_message_queue_id, agent_name, resource_name);
- // MYCO release
- myco_agent_release_resource(agent_message_queue_id, agent_name, resource_name, resource_pointer, resource_size);
- // View available resources
- myco_agent_request_resource_list(agent_message_queue_id);
- // MYCO fetch
- // #MYCO fetch("RESOURCE_2")
- struct resource myresource2;
- myresource2 = myco_agent_request_resource(agent_message_queue_id, agent_name, "RESOURCE_2");
- if (myresource2.size != -1) {
- printf("The first integer of RESOURCE_2 is: %d\n", *((int*)myresource2.pointer));
- }
- // 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));
- }
- // Change some values in RESOURCE_3
- if (myresource3.size != -1) {
- ((int*)myresource3.pointer)[0] = 0;
- }
- // MYCO push
- // #MYCO push("RESOURCE_3")
- myco_agent_write_remote_resource(agent_message_queue_id, "RESOURCE_3", myresource3.pointer, myresource3.size, 0);
- // 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");
- // At the end of its runtime the agent unregisters with the daemon.
- myco_agent_unregister(agent_name, agent_message_queue_id);
- return EXIT_SUCCESS;
- }
|