1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- /*
- * 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_MYAGENT2";
- int agent_message_queue_id;
- agent_message_queue_id = myco_agent_register(agent_name);
- // MYCO create
- // #MYCO create("RESOURCE_2", 1024, transactional)
- char *resource_name_2 = "RESOURCE_2";
- int resource_size_2 = 1024;
- void *resource_pointer_2 = myco_agent_register_resource(agent_message_queue_id, agent_name, resource_name_2, RESOURCE_TRANSACTIONAL, getpid(), resource_size_2);
- // Put some random numbers into RESOURCE_2
- int i;
- srand(time(NULL));
- for (i=0; i < resource_size_2/sizeof(int); i++) {
- ((int *)resource_pointer_2)[i] = rand();
- }
- printf("The first integer of RESOURCE_2 is: %d\n", ((int *)resource_pointer_2)[0]);
- // MYCO release
- //myco_agent_release_resource(agent_message_queue_id, agent_name, resource_name_2, resource_pointer_2, resource_size_2);
- // MYCO create
- // #MYCO create("RESOURCE_3", 1024, nottransactional)
- char *resource_name_3 = "RESOURCE_3";
- int resource_size_3 = 1024;
- void *resource_pointer_3 = myco_agent_register_resource(agent_message_queue_id, agent_name, resource_name_3, RESOURCE_TRANSACTIONAL, getpid(), resource_size_3);
- // Put some random numbers into RESOURCE_3
- int j;
- for (j=0; j < resource_size_3/sizeof(int); j++) {
- ((int*)resource_pointer_3)[j] = rand();
- }
- printf("The first integer of RESOURCE_3 is: %d\n", ((int*)resource_pointer_3)[0]);
- // Sleep, so the showcase program can request RESOURCE_2 and read/push RESOURCE_3
- sleep(30);
- // RESOURCE_3 has been pushed, lets check
- printf("The first integer of RESOURCE_3 is now: %d\n", ((int*)resource_pointer_3)[0]);
- // MYCO free
- // #MYCO free("RESOURCE_2")
- myco_agent_unregister_resource(agent_message_queue_id, resource_name_2);
- // MYCO free
- // #MYCO free("RESOURCE_3")
- myco_agent_unregister_resource(agent_message_queue_id, resource_name_3);
-
- // At the end of its runtime the agent unregisters with the agent.
- myco_agent_unregister(agent_name, agent_message_queue_id);
- return EXIT_SUCCESS;
- }
|