00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef _WIN32
00036 #include <unistd.h>
00037 #include "config.h"
00038 #else
00039 #include "winconfig.h"
00040 #endif
00041
00042 #include <embedch.h>
00043
00044 #include "include/libmc.h"
00045 #include "include/agent.h"
00046 #include "include/mc_platform.h"
00047 #include "include/message.h"
00048 #include "include/agent_lib.h"
00049 #include "include/interpreter_variable_data.h"
00050 #include "include/xml_parser.h"
00051
00052 int agent_AddPersistentVariable(agent_p agent, int task_num, const char* var_name)
00053 {
00054 int i;
00055 int size;
00056 int data_type_size;
00057 int progress;
00058 interpreter_variable_data_t *agent_variable_data;
00059 agent_variable_data = (interpreter_variable_data_t*)malloc(sizeof(interpreter_variable_data_t));
00060 agent_variable_data->name = strdup(var_name);
00061
00062
00063 agent_variable_data->data_type = Ch_DataType(
00064 *agent->agent_interp,
00065 var_name );
00066
00067 if (agent_variable_data->data_type == CH_UNDEFINETYPE) {
00068 free(agent_variable_data);
00069 fprintf(stderr, "Warning: agent %s attempted saving of invalid variable, %s. %s:%d\n",
00070 agent->name, var_name, __FILE__, __LINE__);
00071 return MC_ERR;
00072 }
00073
00074 agent_variable_data->array_dim = Ch_ArrayDim(
00075 *agent->agent_interp,
00076 var_name );
00077
00078 agent_variable_data->array_extent = (int*)malloc(
00079 sizeof(int) * agent_variable_data->array_dim );
00080 for (i=0; i<agent_variable_data->array_dim; i++) {
00081 agent_variable_data->array_extent[i] =
00082 Ch_ArrayExtent(
00083 *agent->agent_interp,
00084 var_name,
00085 i );
00086 }
00087
00088 size = 1;
00089 for (i=0; i < agent_variable_data->array_dim; i++) {
00090 size *= agent_variable_data->array_extent[i];
00091 }
00092
00093
00094 CH_DATATYPE_SIZE(agent_variable_data->data_type, data_type_size);
00095
00096 agent_variable_data->data = (void*)malloc(size * data_type_size);
00097 CHECK_NULL(agent_variable_data->data, exit(0));
00098
00099
00100 progress = agent->datastate->task_progress;
00101 i = 0;
00102
00103 if (agent_variable_data->array_dim == 0) {
00104 memcpy(
00105 agent_variable_data->data,
00106 (void*)Ch_GlobalSymbolAddrByName(
00107 *agent->agent_interp,
00108 var_name),
00109 size*data_type_size
00110 );
00111
00112 } else {
00113 memcpy(
00114 agent_variable_data->data,
00115 (void*)Ch_GlobalSymbolAddrByName(
00116 *agent->agent_interp,
00117 var_name),
00118 size*data_type_size
00119 );
00120 }
00121 agent_variable_data->size = size*data_type_size;
00122
00123
00124 agent_variable_list_Remove(
00125 agent->datastate->tasks[task_num]->agent_variable_list,
00126 var_name);
00127 agent_variable_list_Add(
00128 agent->datastate->tasks[task_num]->agent_variable_list,
00129 agent_variable_data);
00130 return 0;
00131 }
00132
00133 agent_p
00134 agent_Copy(const agent_p agent)
00135 {
00136 agent_p cp_agent;
00137 cp_agent = (agent_p)malloc(sizeof(agent_t));
00138
00139 MUTEX_LOCK(agent->lock);
00140
00141 cp_agent->id = agent->id;
00142
00143 cp_agent->name = (char*)malloc
00144 (
00145 sizeof(char) *
00146 (strlen(agent->name) + 1)
00147 );
00148 strcpy(cp_agent->name, agent->name);
00149
00150
00151 cp_agent->arrival_time = agent->arrival_time;
00152
00153 cp_agent->owner = (char*)malloc
00154 (
00155 sizeof(char) *
00156 (strlen(agent->owner) + 1)
00157 );
00158 strcpy(cp_agent->owner, agent->owner);
00159
00160 cp_agent->home = (char*)malloc
00161 (
00162 sizeof(char) *
00163 (strlen(agent->home) + 1)
00164 );
00165 strcpy(cp_agent->home, agent->home);
00166
00167 cp_agent->home_port = agent->home_port;
00168
00169 cp_agent->datastate = agent_datastate_Copy(agent->datastate);
00170
00171 cp_agent->orphan = 1;
00172
00173 cp_agent->agent_type = agent->agent_type;
00174
00175 cp_agent->agent_status = agent->agent_status;
00176
00177 cp_agent->return_data = agent->return_data;
00178
00179 cp_agent->agent_interp = NULL;
00180
00181 cp_agent->run_lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00182 MUTEX_INIT(cp_agent->run_lock);
00183
00184 cp_agent->agent_persistent = agent->agent_persistent;
00185
00186
00187 cp_agent->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00188 MUTEX_INIT(cp_agent->lock);
00189
00190
00191 cp_agent->mailbox = agent_mailbox_New();
00192
00193 return cp_agent;
00194 }
00195
00196 agent_p
00197 agent_New(void)
00198 {
00199 agent_p agent;
00200 agent = (agent_p)malloc(sizeof(agent_t));
00201 if(agent==NULL) {
00202 fprintf(stderr, "Memory error at %s:%d\n",
00203 __FILE__, __LINE__);
00204 return NULL;
00205 }
00206
00207 memset(agent, 0, sizeof(agent_t));
00208
00209
00210 MUTEX_NEW(agent->run_lock);
00211 MUTEX_INIT(agent->run_lock);
00212
00213 MUTEX_NEW(agent->lock);
00214 MUTEX_INIT(agent->lock);
00215
00216 return agent;
00217 }
00218
00219 agent_p
00220 agent_NewBinary( struct mc_platform_s *mc_platform)
00221 {
00222 agent_p agent;
00223
00224
00225 agent = (MCAgent_t)malloc(sizeof(agent_t));
00226 memset(agent, 0, sizeof(agent_t));
00227
00228
00229 agent->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00230 MUTEX_INIT(agent->lock);
00231
00232
00233 agent->run_lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00234 MUTEX_INIT(agent->run_lock);
00235
00236
00237 agent->id = rand();
00238 #ifndef _WIN32
00239 agent->arrival_time = time(NULL);
00240 #else
00241 GetSystemTime( &(agent->arrival_time) );
00242 #endif
00243
00244 agent->home = NULL;
00245 agent->sender = NULL;
00246
00247
00248 agent->orphan = 0;
00249 agent->agent_script_ready = 1;
00250 agent->agent_pipe_ready_to_read = 0;
00251 agent->agent_ready_to_send = 0;
00252 agent->agent_pipe_active = 0;
00253 agent->binary = 1;
00254
00255
00256 agent->agent_thread_id = 0;
00257
00258
00259 agent->mailbox = agent_mailbox_New();
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 agent->agent_status = MC_AGENT_ACTIVE;
00273
00274 agent->mc_platform = mc_platform;
00275
00276 agent->agent_address = (char*)malloc(sizeof(char) *
00277 (strlen(agent->mc_platform->hostname) + 12 + 10)
00278 );
00279 if (agent->agent_address == NULL) {
00280 fprintf(stderr, "Memory error. %s:%d\n", __FILE__, __LINE__);
00281 exit(-1);
00282 }
00283 sprintf(agent->agent_address,
00284 "http://%s:%d/acc",
00285 agent->mc_platform->hostname,
00286 agent->mc_platform->port
00287 );
00288
00289 return agent;
00290 }
00291 agent_p
00292 agent_Initialize(
00293 struct mc_platform_s *mc_platform,
00294 message_p message,
00295 int id)
00296 {
00297 agent_p agent;
00298 int err_code;
00299
00300
00301 agent = (MCAgent_t)malloc(sizeof(agent_t));
00302 memset(agent, 0, sizeof(agent_t));
00303
00304
00305 agent->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00306 MUTEX_INIT(agent->lock);
00307
00308
00309 agent->run_lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00310 MUTEX_INIT(agent->run_lock);
00311
00312
00313 agent->id = id;
00314 #ifndef _WIN32
00315 agent->arrival_time = time(NULL);
00316 #else
00317 GetSystemTime( &(agent->arrival_time) );
00318 #endif
00319
00320 agent->home = NULL;
00321 agent->sender = NULL;
00322
00323
00324 agent->orphan = 0;
00325 agent->agent_script_ready = 1;
00326 agent->agent_pipe_ready_to_read = 0;
00327 agent->agent_ready_to_send = 0;
00328 agent->agent_pipe_active = 0;
00329
00330
00331 agent->agent_thread_id = 0;
00332
00333
00334 agent->mailbox = agent_mailbox_New();
00335
00336
00337 agent->datastate = agent_datastate_New();
00338 agent->datastate->xml_agent_root = message->xml_payload;
00339 agent->datastate->xml_root = message->xml_root;
00340 message->agent_xml_flag = 1;
00341
00342 if (agent->datastate->xml_agent_root != NULL) {
00343 switch(message->message_type) {
00344 case MOBILE_AGENT:
00345 agent->agent_type = MC_REMOTE_AGENT;
00346 if( (err_code = agent_xml_parse(agent))) {
00347 fprintf(stderr, "error code %d. %s:%d\n",
00348 err_code, __FILE__, __LINE__ );
00349 agent_Destroy(agent);
00350 return NULL;
00351 }
00352 if (mc_platform->default_agentstatus != -1) {
00353 agent->agent_status = (enum MC_AgentStatus_e)mc_platform->default_agentstatus;
00354 }
00355 break;
00356 case RETURN_MSG:
00357 agent->agent_type = MC_RETURN_AGENT;
00358 if( (err_code = agent_xml_parse(agent))) {
00359 fprintf(stderr, "error code %d. %s:%d\n",
00360 err_code, __FILE__, __LINE__ );
00361 agent_Destroy(agent);
00362 return NULL;
00363 }
00364 break;
00365 default:
00366 fprintf(stderr, "Invalid agent type: %d %s:%d\n",
00367 agent->agent_type, __FILE__, __LINE__ );
00368 }
00369 } else {
00370 mc_platform->err = MC_ERR_PARSE;
00371
00372 MUTEX_DESTROY(agent->lock);
00373 free(agent->lock);
00374 MUTEX_DESTROY(agent->run_lock);
00375 free(agent->run_lock);
00376
00377 free(agent);
00378 return NULL;
00379 }
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 agent->agent_status = MC_WAIT_CH;
00393
00394 agent->mc_platform = mc_platform;
00395
00396 agent->agent_address = (char*)malloc(sizeof(char) *
00397 (strlen(agent->mc_platform->hostname) + 12 + 10)
00398 );
00399 if (agent->agent_address == NULL) {
00400 fprintf(stderr, "Memory error. %s:%d\n", __FILE__, __LINE__);
00401 exit(-1);
00402 }
00403 sprintf(agent->agent_address,
00404 "http://%s:%d/acc",
00405 agent->mc_platform->hostname,
00406 agent->mc_platform->port
00407 );
00408
00409 return agent;
00410 }
00411
00412 int
00413 agent_Destroy(agent_p agent)
00414 {
00415 if (agent == NULL) {
00416 return MC_SUCCESS;
00417 }
00418 MUTEX_LOCK(agent->lock);
00419 if (agent->name != NULL) {
00420 free(agent->name);
00421 }
00422 if (agent->owner != NULL) {
00423 free(agent->owner);
00424 }
00425 if (agent->home != NULL) {
00426 free(agent->home);
00427 }
00428 if (agent->sender != NULL) {
00429 free(agent->sender);
00430 }
00431 if (agent->wg_code != NULL) {
00432 free(agent->wg_code);
00433 }
00434
00435 MUTEX_DESTROY(agent->lock);
00436 if (agent->agent_status == MC_AGENT_NEUTRAL) {
00437 if ((agent->agent_interp) != NULL) {
00438 Ch_Reset(*agent->agent_interp);
00439 interpreter_queue_Add(agent->mc_platform->interpreter_queue, (struct AP_GENERIC_s*)agent->agent_interp);
00440 }
00441 }
00442 free(agent->lock);
00443 agent_datastate_Destroy(agent->datastate);
00444 free(agent->run_lock);
00445 agent_mailbox_Destroy(agent->mailbox);
00446
00447 free(agent);
00448 agent = NULL;
00449 return MC_SUCCESS;
00450 }
00451
00452 extern void
00453 agent_RunChScript(agent_p agent, mc_platform_p mc_platform)
00454 {
00455 #ifndef _WIN32
00456 pthread_attr_t attr;
00457 pthread_attr_init(&attr);
00458 if(mc_platform->stack_size[MC_THREAD_AGENT] != -1) {
00459 pthread_attr_setstacksize
00460 (
00461 &attr,
00462 mc_platform->stack_size[MC_THREAD_AGENT]
00463 );
00464 }
00465 pthread_attr_setdetachstate
00466 (
00467 &attr,
00468 PTHREAD_CREATE_DETACHED
00469 );
00470 #else
00471 int stack_size;
00472 if (mc_platform->stack_size[MC_THREAD_AGENT] < 1) {
00473 stack_size = mc_platform->stack_size[MC_THREAD_AGENT]+1;
00474 } else {
00475 stack_size = mc_platform->stack_size[MC_THREAD_AGENT];
00476 }
00477 #endif
00478 agent->agent_status = MC_AGENT_ACTIVE;
00479 agent->mc_platform = mc_platform;
00480
00481 THREAD_CREATE(&agent->agent_thread,
00482 agent_RunChScriptThread,
00483 agent );
00484
00485 return;
00486 }
00487
00488
00489
00490 void *
00491 agent_ChScriptInitVar(ChInterp_t* interp)
00492 {
00493 char *tmp_buf;
00494
00495
00496 tmp_buf = (char*)malloc(sizeof(char) * 200);
00497 tmp_buf[0] = '\0';
00498 sprintf(tmp_buf, "int mc_agent_id = -1;");
00499 Ch_DeclareVar(
00500 *interp,
00501 tmp_buf
00502 );
00503
00504 tmp_buf[0] = '\0';
00505
00506 sprintf(tmp_buf, "char* mc_agent_name = NULL;");
00507 Ch_DeclareVar(
00508 *interp,
00509 tmp_buf
00510 );
00511
00512 tmp_buf[0] = '\0';
00513 sprintf(tmp_buf, "void* mc_current_agent = NULL;");
00514 Ch_DeclareVar(
00515 *interp,
00516 tmp_buf
00517 );
00518
00519 tmp_buf[0] = '\0';
00520
00521 sprintf(tmp_buf, "char* mc_host_name = NULL;");
00522 Ch_DeclareVar(
00523 *interp,
00524 tmp_buf
00525 );
00526
00527 tmp_buf[0] = '\0';
00528 sprintf(tmp_buf, "int mc_host_port = -1;\n");
00529 Ch_DeclareVar(
00530 *interp,
00531 tmp_buf
00532 );
00533
00534 tmp_buf[0] = '\0';
00535 sprintf(tmp_buf, "int mc_task_progress = -1;\n");
00536 Ch_DeclareVar(
00537 *interp,
00538 tmp_buf
00539 );
00540
00541 tmp_buf[0] = '\0';
00542 sprintf(tmp_buf, "int mc_num_tasks = -1;\n");
00543 Ch_DeclareVar(
00544 *interp,
00545 tmp_buf
00546 );
00547
00548
00549 tmp_buf[0] = '\0';
00550 sprintf(tmp_buf, "enum error_code_e {MC_SUCCESS = 0, MC_ERR, MC_ERR_CONNECT, MC_ERR_PARSE, MC_ERR_EMPTY, MC_ERR_INVALID, MC_ERR_INVALID_ARGS, MC_ERR_NOT_FOUND, MC_ERR_MEMORY, MC_ERR_SEND, MC_WARN_DUPLICATE };" );
00551 Ch_DeclareVar(
00552 *interp,
00553 tmp_buf
00554 );
00555
00556 Ch_DeclareVar(
00557 *interp,
00558 "char* mc_agent_address;"
00559 );
00560
00561
00562 tmp_buf[0] = '\0';
00563 sprintf(tmp_buf, "enum MC_SteerCommand_e {MC_RUN = 0, MC_SUSPEND, MC_RESTART, MC_STOP};" );
00564 Ch_DeclareVar(
00565 *interp,
00566 tmp_buf
00567 );
00568
00569 tmp_buf[0] = '\0';
00570 sprintf(tmp_buf, "enum mc_AgentStatus_e { MC_WAIT_CH, MC_WAIT_MESSGSEND, MC_AGENT_ACTIVE, MC_AGENT_NEUTRAL, MC_AGENT_SUSPENDED, MC_WAIT_FINISHED};");
00571 Ch_DeclareVar(
00572 *interp,
00573 tmp_buf
00574 );
00575
00576 free(tmp_buf);
00577
00578 Ch_DeclareVar(
00579 *interp,
00580 "void* MCAgent_t;"
00581 );
00582 Ch_DeclareTypedef(
00583 *interp,
00584 "MCAgent_t"
00585 );
00586
00587
00588 Ch_DeclareFunc(
00589 *interp,
00590 "int mc_AclDestroy(void* acl_message);",
00591 (ChFuncdl_t)MC_AclDestroy_chdl
00592 );
00593 Ch_DeclareFunc(
00594 *interp,
00595 "void* mc_AclNew(void);",
00596 (ChFuncdl_t)MC_AclNew_chdl
00597 );
00598 Ch_DeclareFunc(
00599 *interp,
00600 "int mc_AclPost(void* agent, void* acl_message);",
00601 (ChFuncdl_t)MC_AclPost_chdl
00602 );
00603 Ch_DeclareFunc(
00604 *interp,
00605 "void* mc_AclRetrieve(void* agent);",
00606 (ChFuncdl_t)MC_AclRetrieve_chdl
00607 );
00608 Ch_DeclareFunc(
00609 *interp,
00610 "void* mc_AclReply(void* acl_message);",
00611 (ChFuncdl_t)MC_AclReply_chdl
00612 );
00613 Ch_DeclareFunc(
00614 *interp,
00615 "int mc_AclSend(void* acl_message);",
00616 (ChFuncdl_t)MC_AclSend_chdl
00617 );
00618 Ch_DeclareFunc(
00619 *interp,
00620 "void* mc_AclWaitRetrieve(void* agent);",
00621 (ChFuncdl_t)MC_AclWaitRetrieve_chdl
00622 );
00623
00624 Ch_DeclareFunc(
00625 *interp,
00626 "int mc_AclGetProtocol(void* acl_message);",
00627 (ChFuncdl_t)MC_AclGetProtocol_chdl
00628 );
00629 Ch_DeclareFunc(
00630 *interp,
00631 "char* mc_AclGetConversationID(void* acl_message);",
00632 (ChFuncdl_t)MC_AclGetConversationID_chdl
00633 );
00634 Ch_DeclareFunc(
00635 *interp,
00636 "int mc_AclGetPerformative(void* acl_message);",
00637 (ChFuncdl_t)MC_AclGetPerformative_chdl
00638 );
00639 Ch_DeclareFunc(
00640 *interp,
00641 "int mc_AclGetSender(void* acl_message, char** name, char** address);",
00642 (ChFuncdl_t)MC_AclGetSender_chdl
00643 );
00644 Ch_DeclareFunc(
00645 *interp,
00646 "char* mc_AclGetContent(void* acl_message);",
00647 (ChFuncdl_t)MC_AclGetContent_chdl
00648 );
00649
00650 Ch_DeclareFunc(
00651 *interp,
00652 "int mc_AclSetProtocol(void* acl_message, int protocol);",
00653 (ChFuncdl_t)MC_AclSetProtocol_chdl
00654 );
00655 Ch_DeclareFunc(
00656 *interp,
00657 "int mc_AclSetConversationID(void* acl_message, char* id);",
00658 (ChFuncdl_t)MC_AclSetConversationID_chdl
00659 );
00660 Ch_DeclareFunc(
00661 *interp,
00662 "int mc_AclSetPerformative(void* acl_message, int performative);",
00663 (ChFuncdl_t)MC_AclSetPerformative_chdl
00664 );
00665 Ch_DeclareFunc(
00666 *interp,
00667 "int mc_AclSetSender(void* acl_message, char* name, char* address);",
00668 (ChFuncdl_t)MC_AclSetSender_chdl
00669 );
00670 Ch_DeclareFunc(
00671 *interp,
00672 "int mc_AclAddReceiver(void* acl_message, char* name, char* address);",
00673 (ChFuncdl_t)MC_AclAddReceiver_chdl
00674 );
00675 Ch_DeclareFunc(
00676 *interp,
00677 "int mc_AclAddReplyTo(void* acl_message, char* name, char* address);",
00678 (ChFuncdl_t)MC_AclAddReplyTo_chdl
00679 );
00680 Ch_DeclareFunc(
00681 *interp,
00682 "int mc_AclSetContent(void* acl_message, char* content);",
00683 (ChFuncdl_t)MC_AclSetContent_chdl
00684 );
00685
00686 Ch_DeclareFunc(
00687 *interp,
00688 "int mc_AddAgent(void* agent);",
00689 (ChFuncdl_t)MC_AddAgent_chdl
00690 );
00691 Ch_DeclareFunc(
00692 *interp,
00693 "int mc_AgentAddTask(void* agent, const char* code, const char* return_var_name, const char* server, int persistent);",
00694 (ChFuncdl_t)MC_AgentAddTask_chdl
00695 );
00696 Ch_DeclareFunc(
00697 *interp,
00698 "const void* mc_AgentVariableRetrieve(void* agent, const char* var_name, int task_num);",
00699 (ChFuncdl_t)MC_AgentVariableRetrieve_chdl
00700 );
00701 Ch_DeclareFunc(
00702 *interp,
00703 "int mc_AgentVariableSave(void* agent, const char* var_name);",
00704 (ChFuncdl_t)MC_AgentVariableSave_chdl
00705 );
00706 Ch_DeclareFunc(
00707 *interp,
00708 "int mc_Barrier(int id);",
00709 (ChFuncdl_t)MC_Barrier_chdl
00710 );
00711 Ch_DeclareFunc(
00712 *interp,
00713 "int mc_BarrierDelete(int id);",
00714 (ChFuncdl_t)MC_BarrierDelete_chdl
00715 );
00716 Ch_DeclareFunc(
00717 *interp,
00718 "int mc_BarrierInit(int id, int num_procs);",
00719 (ChFuncdl_t)MC_BarrierInit_chdl
00720 );
00721 Ch_DeclareFunc(
00722 *interp,
00723 "int mc_CallAgentFunc(char* agentName, const char* funcName, void* returnVal, ...);",
00724 (ChFuncdl_t)MC_CallAgentFunc_chdl
00725 );
00726 Ch_DeclareFunc(
00727 *interp,
00728 "MCAgent_t mc_ComposeAgent(const char* name, *home, *owner, *code, *return_var_name, *server, int persistent);",
00729 (ChFuncdl_t)MC_ComposeAgent_chdl
00730 );
00731 Ch_DeclareFunc(
00732 *interp,
00733 "MCAgent_t mc_ComposeAgentS(const char* name, *home, *owner, *code, *return_var_name, *server, *workgroup_code, int persistent);",
00734 (ChFuncdl_t)MC_ComposeAgentWithWorkgroup_chdl
00735 );
00736 Ch_DeclareFunc(
00737 *interp,
00738 "MCAgent_t mc_ComposeAgentWithWorkgroup(const char* name, *home, *owner, *code, *return_var_name, *server, *workgroup_code, int persistent);",
00739 (ChFuncdl_t)MC_ComposeAgentWithWorkgroup_chdl
00740 );
00741 Ch_DeclareFunc(
00742 *interp,
00743 "MCAgent_t mc_ComposeAgentFromFile(const char* name, *home, *owner, *filename, *return_var_name, *server, int persistent);",
00744 (ChFuncdl_t)MC_ComposeAgentFromFile_chdl
00745 );
00746 Ch_DeclareFunc(
00747 *interp,
00748 "MCAgent_t mc_ComposeAgentFromFileWithWorkgroup(const char* name, *home, *owner, *filename, *return_var_name, *server, *workgroup_code, int persistent);",
00749 (ChFuncdl_t)MC_ComposeAgentFromFileWithWorkgroup_chdl
00750 );
00751 Ch_DeclareFunc(
00752 *interp,
00753 "int mc_CondBroadcast(int id);",
00754 (ChFuncdl_t)MC_CondBroadcast_chdl
00755 );
00756 Ch_DeclareFunc(
00757 *interp,
00758 "int mc_CondSignal(int id);",
00759 (ChFuncdl_t)MC_CondSignal_chdl
00760 );
00761 Ch_DeclareFunc(
00762 *interp,
00763 "int mc_CondReset(int id);",
00764 (ChFuncdl_t)MC_CondReset_chdl
00765 );
00766 Ch_DeclareFunc(
00767 *interp,
00768 "int mc_CondWait(int id);",
00769 (ChFuncdl_t)MC_CondWait_chdl
00770 );
00771 Ch_DeclareFunc(
00772 *interp,
00773 "int mc_DeleteAgent(const char* agentName);",
00774 (ChFuncdl_t)MC_DeleteAgent_chdl
00775 );
00776 Ch_DeclareFunc(
00777 *interp,
00778 "int mc_DeleteAgentWG(MCAgent_t calling_agent, const char* agentName);",
00779 (ChFuncdl_t)MC_DeleteAgentWG_chdl
00780 );
00781 Ch_DeclareFunc(
00782 *interp,
00783 "int mc_DeregisterService(int agentID, char* serviceName);",
00784 (ChFuncdl_t)MC_DeregisterService_chdl
00785 );
00786 Ch_DeclareFunc(
00787 *interp,
00788 "int mc_DestroyServiceSearchResult( char** agentName, char** serviceName, int* agentID, int numResult);",
00789 (ChFuncdl_t)MC_DestroyServiceSearchResult_chdl
00790 );
00791 Ch_DeclareFunc(
00792 *interp,
00793 "int mc_End(void);",
00794 (ChFuncdl_t)MC_End_chdl
00795 );
00796 Ch_DeclareFunc(
00797 *interp,
00798 "void *mc_FindAgentByID(int id);",
00799 (ChFuncdl_t)MC_FindAgentByID_chdl
00800 );
00801
00802 Ch_DeclareFunc(
00803 *interp,
00804 "void *mc_FindAgentByName(const char *name);",
00805 (ChFuncdl_t)MC_FindAgentByName_chdl
00806 );
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816 Ch_DeclareFunc(
00817 *interp,
00818 "int MC_GetAgentID(void* agent);",
00819 (ChFuncdl_t)MC_GetAgentStatus_chdl
00820 );
00821 Ch_DeclareFunc(
00822 *interp,
00823 "char* MC_GetAgentName(void* agent);",
00824 (ChFuncdl_t)MC_GetAgentStatus_chdl
00825 );
00826 Ch_DeclareFunc(
00827 *interp,
00828 "int mc_GetAgentStatus(void* agent);",
00829 (ChFuncdl_t)MC_GetAgentStatus_chdl
00830 );
00831 Ch_DeclareFunc(
00832 *interp,
00833 "char *mc_GetAgentXMLString(void* agent);",
00834 (ChFuncdl_t)MC_GetAgentXMLString_chdl
00835 );
00836
00837 #ifndef _WIN32
00838 Ch_DeclareFunc(
00839 *interp,
00840 "int mc_gettimeofday(void* tv);",
00841 (ChFuncdl_t)MC_GetTimeOfDay_chdl
00842 );
00843 #endif
00844
00845 Ch_DeclareFunc(
00846 *interp,
00847 "int mc_HaltAgency(void);",
00848 (ChFuncdl_t)MC_HaltAgency_chdl
00849 );
00850 Ch_DeclareFunc(
00851 *interp,
00852 "int mc_MigrateAgent(void* agent, const char* hostname, int port);",
00853 (ChFuncdl_t)MC_MigrateAgent_chdl
00854 );
00855 Ch_DeclareFunc(
00856 *interp,
00857 "int mc_MutexLock(int id);",
00858 (ChFuncdl_t)MC_MutexLock_chdl
00859 );
00860 Ch_DeclareFunc(
00861 *interp,
00862 "int mc_MutexUnlock(int id);",
00863 (ChFuncdl_t)MC_MutexUnlock_chdl
00864 );
00865 Ch_DeclareFunc(
00866 *interp,
00867 "int mc_PrintAgentCode(void* agent);",
00868 (ChFuncdl_t)MC_PrintAgentCode_chdl
00869 );
00870 Ch_DeclareFunc(
00871 *interp,
00872 "int mc_ResumeAgency(void);",
00873 (ChFuncdl_t)MC_ResumeAgency_chdl
00874 );
00875 Ch_DeclareFunc(
00876 *interp,
00877 "int mc_SearchForService(const char* searchString, char*** agentNames, char*** serviceNames, int** agentIDs, int* numResults);",
00878 (ChFuncdl_t)MC_SearchForService_chdl
00879 );
00880 Ch_DeclareFunc(
00881 *interp,
00882 "int mc_SendSteerCommand(enum MC_SteerCommand_e command);",
00883 (ChFuncdl_t)MC_SendSteerCommand_chdl
00884 );
00885 Ch_DeclareFunc(
00886 *interp,
00887 "int mc_RegisterService(MCAgent_t agent, char **serviceNames, int numServices);",
00888 (ChFuncdl_t)MC_RegisterService_chdl
00889 );
00890 Ch_DeclareFunc(
00891 *interp,
00892 "void *mc_RetrieveAgent(void);",
00893 (ChFuncdl_t)MC_RetrieveAgent_chdl
00894 );
00895 Ch_DeclareFunc(
00896 *interp,
00897 "char *mc_RetrieveAgentCode(void* agent);",
00898 (ChFuncdl_t)MC_RetrieveAgentCode_chdl
00899 );
00900 Ch_DeclareFunc(
00901 *interp,
00902 "int mc_SaveData(MCAgent_t agent, char* name, int size, void* data);",
00903 (ChFuncdl_t)MC_SaveData_chdl
00904 );
00905 Ch_DeclareFunc(
00906 *interp,
00907 "int mc_SemaphoreWait(int id);",
00908 (ChFuncdl_t)MC_SemaphoreWait_chdl
00909 );
00910 Ch_DeclareFunc(
00911 *interp,
00912 "int mc_SemaphorePost(int id);",
00913 (ChFuncdl_t)MC_SemaphorePost_chdl
00914 );
00915 Ch_DeclareFunc(
00916 *interp,
00917 "int mc_SendAgentMigrationMessage(char *message, char *hostname, int port);",
00918 (ChFuncdl_t)MC_SendAgentMigrationMessage_chdl
00919 );
00920 Ch_DeclareFunc(
00921 *interp,
00922 "int mc_SendAgentMigrationMessageFile(char *filename, char *hostname, int port);",
00923 (ChFuncdl_t)MC_SendAgentMigrationMessageFile_chdl
00924 );
00925 Ch_DeclareFunc(
00926 *interp,
00927 "int mc_SetAgentStatus(void* agent, int status);",
00928 (ChFuncdl_t)MC_SetAgentStatus_chdl
00929 );
00930 Ch_DeclareFunc(
00931 *interp,
00932 "int mc_SetDefaultAgentStatus(int status);",
00933 (ChFuncdl_t)MC_SetDefaultAgentStatus_chdl
00934 );
00935 Ch_DeclareFunc(
00936 *interp,
00937 "int mc_SyncDelete(int id);",
00938 (ChFuncdl_t)MC_SyncDelete_chdl
00939 );
00940 Ch_DeclareFunc(
00941 *interp,
00942 "int mc_SyncInit(int id);",
00943 (ChFuncdl_t)MC_SyncInit_chdl
00944 );
00945 Ch_DeclareFunc(
00946 *interp,
00947 "int mc_TerminateAgent(const char* agentName);",
00948 (ChFuncdl_t)MC_TerminateAgent_chdl
00949 );
00950 Ch_DeclareFunc(
00951 *interp,
00952 "int mc_TerminateAgentWG(void* callingAgent, const char* agentName);",
00953 (ChFuncdl_t)MC_TerminateAgentWG_chdl
00954 );
00955 Ch_DeclareFunc(
00956 *interp,
00957 "int mc_GetAgentID(void* agent);",
00958 (ChFuncdl_t)MC_GetAgentID_chdl
00959 );
00960 Ch_DeclareFunc(
00961 *interp,
00962 "char *mc_GetAgentName(void* agent);",
00963 (ChFuncdl_t)MC_GetAgentName_chdl
00964 );
00965
00966 return NULL;
00967 }
00968
00969 #ifndef _WIN32
00970 void*
00971 agent_RunChScriptThread(void * ChAgent)
00972 #else
00973 DWORD WINAPI
00974 agent_RunChScriptThread(void* ChAgent)
00975 #endif
00976 {
00977 #ifndef _WIN32
00978 int fd;
00979 #endif
00980 MCAgent_t agent;
00981 mc_platform_p mc_platform;
00982 int i,n;
00983 FILE *TEMP_FILE;
00984 char *temp_store_file;
00985 char *ChShellArg[2];
00986 void *result;
00987 int progress;
00988 int callbackErrCode;
00989 interpreter_variable_data_t* temp_interp_data;
00990
00991
00992 agent = (MCAgent_t)ChAgent;
00993 progress = agent->datastate->task_progress;
00994 mc_platform = agent->mc_platform;
00995
00996 setbuf(stdout, NULL);
00997 setbuf(stderr, NULL);
00998
00999 if(ChAgent == NULL)
01000 {
01001 printf("ERROR, AGENT NULL \n");
01002 #ifndef _WIN32
01003 return NULL;
01004 #else
01005 return 0;
01006 #endif
01007 }
01008
01009
01010 agent->agent_interp = (ChInterp_t *)interpreter_queue_CreateRetrieve(mc_platform->interpreter_queue,
01011 agent->mc_platform->interp_options );
01012 if(agent->agent_interp == NULL) {
01013
01014 WARN("Could not initialize another Ch interperter. Please make more copies of the chmt*.dl file.\n");
01015 return NULL;
01016 }
01017
01018
01019
01020 Ch_SetVar(*agent->agent_interp, "mc_agent_id",
01021 CH_INTTYPE, (int)agent->id);
01022
01023 Ch_SetVar(*agent->agent_interp, "mc_agent_name",
01024 CH_CHARPTRTYPE, agent->name);
01025
01026 Ch_SetVar(*agent->agent_interp, "mc_current_agent",
01027 CH_VOIDPTRTYPE, (void*)((size_t)agent));
01028
01029 Ch_SetVar(*agent->agent_interp, "mc_host_name",
01030 CH_CHARPTRTYPE, agent->mc_platform->hostname);
01031
01032 Ch_SetVar(*agent->agent_interp, "mc_host_port",
01033 CH_INTTYPE, (int)agent->mc_platform->port);
01034
01035 Ch_SetVar(*agent->agent_interp, "mc_task_progress",
01036 CH_INTTYPE, (int)agent->datastate->task_progress);
01037
01038 Ch_SetVar(*agent->agent_interp, "mc_num_tasks",
01039 CH_INTTYPE, (int)agent->datastate->number_of_tasks);
01040
01041 Ch_SetVar(*agent->agent_interp, "mc_agent_address",
01042 CH_CHARPTRTYPE, agent->agent_address);
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053 if(strlen(agent->datastate->agent_code) < 5)
01054 {
01055 MUTEX_LOCK(agent->run_lock);
01056 if(Ch_AppendRunScript(
01057 *((MCAgent_t)ChAgent)->agent_interp,
01058 ((MCAgent_t)ChAgent)->datastate->agent_code))
01059 {
01060
01061 printf("CH Failure \n");
01062 printf("***************************************************\nCode was:\n%s\n\n", agent->datastate->agent_code);
01063
01064 }
01065 if(Ch_CallFuncByName(*((MCAgent_t)ChAgent)->agent_interp, "main", NULL))
01066 {
01067 printf("CH2 failure \n");
01068 exit(EXIT_FAILURE);
01069 }
01070 MUTEX_UNLOCK(agent->run_lock);
01071 }
01072 else
01073 {
01074
01075
01076 if(mc_platform->agency->agentInitCallback) {
01077 callbackErrCode = (mc_platform->agency->agentInitCallback)(
01078 *agent->agent_interp,
01079 (struct agent_s*)agent,
01080 mc_platform->agency->agentInitUserData );
01081 if(callbackErrCode) {
01082
01083 ((MCAgent_t) ChAgent)->agent_status = MC_AGENT_NEUTRAL;
01084 #ifndef _WIN32
01085 pthread_exit(ChAgent);
01086 #else
01087 return 0;
01088 #endif
01089 }
01090 }
01091
01092 #ifndef _WIN32
01093
01094 temp_store_file = (char *)malloc(sizeof(char)*30);
01095
01096 strcpy(temp_store_file, "agentchscriptXXXXXX");
01097 fd = mkstemp(temp_store_file);
01098 if (fd == -1) {
01099 fprintf(stderr, "Could not create temporary file:%s. %s:%d\n",
01100 temp_store_file,
01101 __FILE__,
01102 __LINE__ );
01103 exit(EXIT_FAILURE);
01104 }
01105 close(fd);
01106 #else
01107 temp_store_file = _tempnam(".", "agentchscript");
01108 #endif
01109 TEMP_FILE = fopen(temp_store_file, "w");
01110
01111
01112 n = fwrite(
01113 (void *)agent->datastate->agent_code,
01114 sizeof(char),
01115 strlen(agent->datastate->agent_code),
01116 TEMP_FILE);
01117
01118 fclose(TEMP_FILE);
01119
01120
01121 ChShellArg[0] = temp_store_file;
01122 ChShellArg[1] = NULL;
01123 MUTEX_LOCK(agent->run_lock);
01124 if(Ch_RunScript(*agent->agent_interp, ChShellArg) < 0) {
01125 fprintf(stderr, "Ch_RunScript error. %s:%d\n", __FILE__, __LINE__);
01126 } else {
01127
01128 fflush(stdout);
01129 }
01130
01131
01132
01133
01134
01135
01136
01137
01138 remove(temp_store_file);
01139 #ifndef _WIN32
01140 free(temp_store_file);
01141 #endif
01142 MUTEX_UNLOCK(agent->run_lock);
01143 }
01144
01145
01146 if(
01147 agent->datastate->tasks[progress]->var_name != NULL &&
01148 strcmp(agent->datastate->tasks[progress]->var_name, "no-return")
01149 )
01150 {
01151 result = interpreter_variable_data_InitializeFromAgent(agent);
01152
01153 interpreter_variable_data_Destroy(
01154 agent->datastate->tasks[progress]->agent_return_data
01155 );
01156
01157 agent->datastate->tasks[progress]->agent_return_data =
01158 (interpreter_variable_data_t*)result;
01159 } else {
01160 interpreter_variable_data_Destroy(
01161 agent->datastate->tasks[progress]->agent_return_data );
01162 agent->datastate->tasks[progress]->agent_return_data = NULL;
01163 }
01164
01165
01166
01167 while (
01168 (
01169 temp_interp_data =
01170 agent_variable_list_Pop(agent->datastate->tasks[progress]->agent_variable_list)
01171 )
01172 )
01173 {
01174 interpreter_variable_data_Destroy(temp_interp_data);
01175 }
01176 for(i = 0; i < agent->datastate->tasks[progress]->num_saved_variables; i++) {
01177
01178
01179
01180
01181 agent_variable_list_Add(
01182 agent->datastate->tasks[progress]->agent_variable_list,
01183 interpreter_variable_data_Initialize(
01184 agent,
01185 agent->datastate->tasks[progress]->saved_variables[i] )
01186 );
01187 }
01188
01189
01190 agent->datastate->task_progress += agent->datastate->progress_modifier;
01191
01192 if (agent->datastate->persistent ||
01193 agent->datastate->tasks[progress]->persistent ) {
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205 ((MCAgent_t) ChAgent)->agent_status = MC_AGENT_NEUTRAL;
01206 } else {
01207 if ((((MCAgent_t)ChAgent)->agent_interp) != NULL) {
01208
01209 Ch_Reset(*agent->agent_interp);
01210 interpreter_queue_Add(mc_platform->interpreter_queue, (struct AP_GENERIC_s*)agent->agent_interp);
01211 }
01212
01213
01214 if (
01215 (agent->datastate->task_progress ==
01216 (agent->datastate->number_of_tasks-1))
01217 )
01218 {
01219
01220 ((MCAgent_t) ChAgent)->agent_status = MC_WAIT_FINISHED;
01221
01222
01223 for(i = 0;
01224 i < agent->datastate->number_of_tasks;
01225 i++)
01226 {
01227 if (agent->datastate->tasks[i]->agent_return_data != NULL) {
01228 ((MCAgent_t) ChAgent)->agent_status = MC_WAIT_MESSGSEND;
01229 }
01230 }
01231 }
01232 else {
01233 ((MCAgent_t) ChAgent)->agent_status = MC_WAIT_MESSGSEND;
01234 }
01235 }
01236
01237
01238 agent->datastate->task_progress++;
01239
01240 if (
01241 (agent->datastate->task_progress >= agent->datastate->number_of_tasks)
01242 )
01243 {
01244 agent->agent_type = MC_RETURN_AGENT;
01245 }
01246
01247 SIGNAL(
01248 mc_platform->MC_signal_cond,
01249 mc_platform->MC_signal_lock,
01250 mc_platform->MC_signal = MC_EXEC_AGENT;
01251 );
01252
01253 MUTEX_LOCK( mc_platform->MC_signal_lock);
01254 MUTEX_UNLOCK( mc_platform->MC_signal_lock );
01255 MUTEX_LOCK(mc_platform->ams->runflag_lock);
01256 mc_platform->ams->run = 1;
01257 COND_SIGNAL(mc_platform->ams->runflag_cond);
01258 MUTEX_UNLOCK(mc_platform->ams->runflag_lock);
01259
01260 #ifndef _WIN32
01261 pthread_exit(ChAgent);
01262 #else
01263 return 0;
01264 #endif
01265 }