|
@@ -0,0 +1,132 @@
|
|
|
+/*
|
|
|
+ * 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 = "MYAGENT2";
|
|
|
+ int agent_message_queue_id;
|
|
|
+ agent_message_queue_id = myco_agent_register(agent_name);
|
|
|
+
|
|
|
+ // MYCO create
|
|
|
+ // #MYCO create("RESOURCE_2", 1024, transactional)
|
|
|
+ int resource_size_2 = 1024;
|
|
|
+ int *resource_pointer_2;
|
|
|
+ resource_pointer_2 = myco_malloc(resource_size_2);
|
|
|
+ myco_agent_register_resource(agent_message_queue_id, agent_name, "RESOURCE_2", RESOURCE_TRANSACTIONAL, getpid(), resource_size_2, resource_pointer_2);
|
|
|
+
|
|
|
+ // Put some random numbers into RESOURCE_2
|
|
|
+ int i;
|
|
|
+ for (i=0; i < resource_size_2/sizeof(int); i++) {
|
|
|
+ resource_pointer_2[i] = 1337;
|
|
|
+ }
|
|
|
+
|
|
|
+ // MYCO release
|
|
|
+ myco_agent_release_resource(agent_message_queue_id, "RESOURCE_2");
|
|
|
+
|
|
|
+ // MYCO create
|
|
|
+ // #MYCO create("RESOURCE_2", 1024, nottransactional)
|
|
|
+ int resource_size_3 = 1024;
|
|
|
+ void *resource_pointer_3;
|
|
|
+ resource_pointer_3 = myco_malloc(resource_size_3);
|
|
|
+ myco_agent_register_resource(agent_message_queue_id, agent_name, "RESOURCE_3", RESOURCE_NOT_TRANSACTIONAL, getpid(), resource_size_3, resource_pointer_3);
|
|
|
+
|
|
|
+
|
|
|
+ // MYCO free
|
|
|
+ // #MYCO free("RESOURCE_2")
|
|
|
+ myco_agent_unregister_resource(agent_message_queue_id, "RESOURCE_2");
|
|
|
+ myco_free(resource_pointer_2);
|
|
|
+
|
|
|
+ // MYCO free
|
|
|
+ // #MYCO free("RESOURCE_3")
|
|
|
+ myco_agent_unregister_resource(agent_message_queue_id, "RESOURCE_3");
|
|
|
+ myco_free(resource_pointer_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;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ struct resource myresource;
|
|
|
+ pid_t worker_pid;
|
|
|
+ int agent_message_queue_id, worker_message_queue_id;
|
|
|
+ message msg;
|
|
|
+ char *split;
|
|
|
+ int split_count;
|
|
|
+
|
|
|
+ if (argv[1] == NULL) {
|
|
|
+ fprintf(stderr, "ERROR: executed mycoagent without worker (try ./mycoagent mycoworker)\n");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ worker_message_queue_id = myco_create_private_message_queue();
|
|
|
+ worker_pid = myco_worker_start(worker_message_queue_id, argv[1]);
|
|
|
+
|
|
|
+ agent_message_queue_id = myco_agent_register(argv[1]);
|
|
|
+
|
|
|
+ //while (myco_worker_is_running(worker_pid) == 0) {
|
|
|
+ while (1) {
|
|
|
+ msg = myco_receive(worker_message_queue_id);
|
|
|
+ if (strcmp(msg.message, "WORKER: ALLOCATED MEMORY") == 0) {
|
|
|
+ if (DEBUG) {
|
|
|
+ printf("%s\n", msg.message);
|
|
|
+ }
|
|
|
+ myco_agent_register_resource(agent_message_queue_id, argv[1], msg.resource_name, msg.resource_transactional, worker_pid, msg.resource_size, msg.resource_pointer);
|
|
|
+ }
|
|
|
+ if (strcmp(msg.message, "WORKER: FREED MEMORY") == 0) {
|
|
|
+ if (DEBUG) {
|
|
|
+ printf("%s %s\n", msg.message, msg.resource_name);
|
|
|
+ }
|
|
|
+ myco_agent_unregister_resource(agent_message_queue_id, msg.resource_name);
|
|
|
+ }
|
|
|
+ if (strcmp(msg.message, "WORKER: REQUESTING RESOURCE") == 0) {
|
|
|
+ if (DEBUG) {
|
|
|
+ printf("%s\n", msg.message);
|
|
|
+ }
|
|
|
+ // Stop this worker
|
|
|
+
|
|
|
+ // Create shared memory region
|
|
|
+
|
|
|
+ // Start transfer
|
|
|
+
|
|
|
+ // Continue this worker
|
|
|
+
|
|
|
+ //myresource = myco_agent_request_resource(agent_message_queue_id, "RESOURCE_1");
|
|
|
+ }
|
|
|
+ if (strcmp(msg.message, "WORKER: REQUESTING RESOURCE LIST") == 0) {
|
|
|
+ if (DEBUG) {
|
|
|
+ printf("%s\n", msg.message);
|
|
|
+ }
|
|
|
+ myco_agent_request_resource_list(agent_message_queue_id);
|
|
|
+ }
|
|
|
+ if (strcmp(msg.message, "WORKER: SHUTTING DOWN") == 0) {
|
|
|
+ if (DEBUG) {
|
|
|
+ printf("%s\n", msg.message);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ myco_agent_unregister(argv[1], agent_message_queue_id);
|
|
|
+
|
|
|
+
|
|
|
+ return EXIT_SUCCESS;
|
|
|
+}
|
|
|
+*/
|