Example 1: Change an algorithm in a running application
client.c
#include <stdio.h> #include <unistd.h> #include <libmc.h> int main() { int i, local_port = 5050, server_port = 5130; char *server_name = "bird1.engr.ucdavis.edu"; char mobagent[2][20] = {"mobileagent1.xml", "mobileagent2.xml"}; MCAgency_t agency; agency = MC_Initialize(local_port, NULL); for(i=0; i<2; i++) { MC_SendAgentMigrationMessageFile(agency, mobagent[i], server_name, server_port); sleep(3); } MC_End(agency); return 0; }
server.c
#include <stdio.h> #include <unistd.h> #include <libmc.h> struct Matrix { double a[2][2], b[2][2]; }matrix; int main() { int i, j, local_port = 5130, mutex_id = 55, *agentID, numResult; char *funcname = "matrix_operate", **agentName, **serviceName; double c[2][2]; MCAgency_t agency; MCAgent_t agent; for(i=0; i<2; i++) { for(j=0; j<2; j++) { matrix.a[i][j] = 2*i+j+1; matrix.b[i][j] = 2*i+j+5; } } agency = MC_Initialize(local_port, NULL); MC_SyncInit(agency, mutex_id); while(1) { MC_MutexLock(agency, mutex_id); MC_SearchForService(agency, funcname, &agentName, &serviceName, &agentID, &numResult); while(numResult == 0) { /* If no agent is found to have provided the desired service, unlock the Mutex variable to allow an agent to register the desired service with the DF before locking the same Mutex variable again for the next search of the desired service. */ MC_MutexUnlock(agency, mutex_id); MC_MutexLock(agency, mutex_id); MC_SearchForService(agency, funcname, &agentName, &serviceName, &agentID, &numResult); } agent = MC_FindAgentByID(agency, agentID[0]); MC_CallAgentFunc(agent, funcname, &(c[0][0]), &matrix); MC_MutexUnlock(agency, mutex_id); MC_DestroyServiceSearchResult(agentName, serviceName, agentID, numResult); /* Output array c containing the matrix computation result to the screen. */ printf("\n"); for(i=0; i<2; i++) { for(j=0; j<2; j++) { printf("%8.2f", c[i][j]); } printf("\n"); } /* Wait for 1 second to make the screen output of array c for each iteration clearly readable. */ sleep(1); } MC_End(agency); return 0; }
mobileagent1.xml
<?xml version="1.0"?> <!DOCTYPE myMessage SYSTEM "gafmessage.dtd"> <GAF_MESSAGE> <MESSAGE message="MOBILE_AGENT"> <MOBILE_AGENT> <AGENT_DATA> <NAME>mobileagent1</NAME> <OWNER>IEL</OWNER> <HOME>iel2.engr.ucdavis.edu:5050</HOME> <TASKS task="1" num="0"> <TASK num="0" persistent="1" return="no-return" complete="0" server="bird1.engr.ucdavis.edu:5130"> </TASK> <AGENT_CODE> <![CDATA[ #include <stdlib.h> #include <string.h> #include <array.h> struct Matrix { double a[2][2], b[2][2]; }; int main() { int i, numService = 1, mutex_id = 55, *agentID, numResult; char *funcname = "matrix_operate", **service, **agentName, **serviceName; MCAgent_t agent; service = (char **)malloc(sizeof(char *)*numService); for(i=0; i<numService; i++) { service[i] = (char *)malloc(sizeof(char)*(strlen(funcname)+1)); } strcpy(service[0], funcname); mc_SearchForService(service[0], &agentName, &serviceName, &agentID, &numResult); if(numResults < 1) { /* No agent is found to have provided such a service. */ mc_RegisterService(mc_current_agent, service, numService); } else { /* An existing agent is found to have provided such a service. */ mc_MutexLock(mutex_id); mc_DeregisterService(agentID[0], service[0]); mc_RegisterService(mc_current_agent, service, numService); mc_MutexUnlock(mutex_id); mc_DestroyServiceSearchResult(agentName, serviceName, agentID, numResult); } for(i=0; i<numService; i++) { free(service[i]); } free(service); return 0; } array double matrix_operate(struct Matrix *matrix)[2][2] { array double a[2][2], b[2][2]; a = (array double[2][2])matrix->a; b = (array double[2][2])matrix->b; return b*inverse(a) + 3*a*transpose(b); } ]]> </AGENT_CODE> </TASKS> </AGENT_DATA> </MOBILE_AGENT> </MESSAGE> </GAF_MESSAGE>
mobileagent2.xml
<?xml version="1.0"?> <!DOCTYPE myMessage SYSTEM "gafmessage.dtd"> <GAF_MESSAGE> <MESSAGE message="MOBILE_AGENT"> <MOBILE_AGENT> <AGENT_DATA> <NAME>mobileagent2</NAME> <OWNER>IEL</OWNER> <HOME>iel2.engr.ucdavis.edu:5050</HOME> <TASKS task="1" num="0"> <TASK num="0" persistent="1" return="no-return" complete="0" server="bird1.engr.ucdavis.edu:5130"> </TASK> <AGENT_CODE> <![CDATA[ #include <stdlib.h> #include <string.h> #include <array.h> struct Matrix { double a[2][2], b[2][2]; }; int main() { int i, numService = 1, mutex_id = 55, *agentID, numResult; char *funcname = "matrix_operate", **service, **agentName, **serviceName; MCAgent_t agent; service = (char **)malloc(sizeof(char *)*numService); for(i=0; i<numService; i++) { service[i] = (char *)malloc(sizeof(char)*(strlen(funcname)+1)); } strcpy(service[0], funcname); mc_SearchForService(service[0], &agentName, &serviceName, &agentID, &numResult); if(numResults < 1) { /* No agent is found to have provided such a service. */ mc_RegisterService(mc_current_agent, service, numService); } else { /* An existing agent is found to have provided such a service. */ mc_MutexLock(mutex_id); mc_DeregisterService(agentID[0], service[0]); mc_RegisterService(mc_current_agent, service, numService); mc_MutexUnlock(mutex_id); mc_DestroyServiceSearchResult(agentName, serviceName, agentID, numResult); } for(i=0; i<numService; i++) { free(service[i]); } free(service); return 0; } array double matrix_operate(struct Matrix *matrix)[2][2] { array double a[2][2], b[2][2]; a = (array double[2][2])matrix->a; b = (array double[2][2])matrix->b; return 5*transpose(a) - 7*inverse(b)*a*transpose(b); } ]]> </AGENT_CODE> </TASKS> </AGENT_DATA> </MOBILE_AGENT> </MESSAGE> </GAF_MESSAGE> |
Integration Engineering Laboratory | UCD MTU Sandia |