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 #ifndef _WIN32
00035 #include "config.h"
00036 #else
00037 #include "winconfig.h"
00038 #endif
00039
00040 #include <mxml.h>
00041 #include "include/agent.h"
00042 #include "include/xml_compose.h"
00043 #include "include/xml_helper.h"
00044
00045 mxml_node_t*
00046 agent_xml_compose(agent_p agent)
00047 {
00048 mxml_node_t* node;
00049 node = mxmlLoadString
00050 (
00051 NULL,
00052 "<?xml version=\"1.0\"?>\n<!DOCTYPE myMessage SYSTEM \"mobilec.dtd\">",
00053 MXML_NO_CALLBACK
00054 );
00055 mxmlAdd
00056 (
00057 node,
00058 MXML_ADD_AFTER,
00059 MXML_ADD_TO_PARENT,
00060 agent_xml_compose__gaf_message(agent)
00061 );
00062 return node;
00063 }
00064
00065 mxml_node_t*
00066 agent_xml_compose__gaf_message(agent_p agent)
00067 {
00068 mxml_node_t* node;
00069 node = mxmlNewElement
00070 (
00071 NULL,
00072 "MOBILEC_MESSAGE"
00073 );
00074 mxmlAdd
00075 (
00076 node,
00077 MXML_ADD_AFTER,
00078 NULL,
00079 agent_xml_compose__message(agent)
00080 );
00081 return node;
00082 }
00083
00084 mxml_node_t*
00085 agent_xml_compose__message(agent_p agent)
00086 {
00087 mxml_node_t* node;
00088 node = mxmlNewElement
00089 (
00090 NULL,
00091 "MESSAGE"
00092 );
00093
00094 if(
00095 agent->agent_type == MC_REMOTE_AGENT ||
00096 agent->agent_type == MC_LOCAL_AGENT
00097 )
00098 {
00099 mxmlElementSetAttr
00100 (
00101 node,
00102 "message",
00103 "MOBILE_AGENT"
00104 );
00105 } else if
00106 (
00107 agent->agent_type == MC_RETURN_AGENT
00108 )
00109 {
00110 mxmlElementSetAttr
00111 (
00112 node,
00113 "message",
00114 "RETURN_MSG"
00115 );
00116 }
00117
00118 mxmlAdd
00119 (
00120 node,
00121 MXML_ADD_AFTER,
00122 NULL,
00123 agent_xml_compose__mobile_agent(agent)
00124 );
00125 return node;
00126 }
00127
00128 mxml_node_t*
00129 agent_xml_compose__mobile_agent(agent_p agent)
00130 {
00131 mxml_node_t* node;
00132
00133 node = mxmlNewElement
00134 (
00135 NULL,
00136 "MOBILE_AGENT"
00137 );
00138
00139 mxmlAdd
00140 (
00141 node,
00142 MXML_ADD_AFTER,
00143 NULL,
00144 agent_xml_compose__agent_data(agent)
00145 );
00146 return node;
00147 }
00148
00149 mxml_node_t*
00150 agent_xml_compose__agent_data(agent_p agent)
00151 {
00152 mxml_node_t* node;
00153 mxml_node_t* tmp_node;
00154
00155 node = mxmlNewElement
00156 (
00157 NULL,
00158 "AGENT_DATA"
00159 );
00160
00161
00162 tmp_node = agent_xml_compose__name(agent);
00163 if (tmp_node == NULL) {
00164 return NULL;
00165 }
00166 mxmlAdd(
00167 node,
00168 MXML_ADD_AFTER,
00169 NULL,
00170 tmp_node
00171 );
00172
00173
00174 tmp_node = agent_xml_compose__owner(agent);
00175 if (tmp_node != NULL) {
00176 mxmlAdd(
00177 node,
00178 MXML_ADD_AFTER,
00179 NULL,
00180 tmp_node
00181 );
00182 }
00183
00184
00185 tmp_node = agent_xml_compose__home(agent);
00186 if (tmp_node != NULL) {
00187 mxmlAdd(
00188 node,
00189 MXML_ADD_AFTER,
00190 NULL,
00191 tmp_node
00192 );
00193 }
00194
00195
00196 tmp_node = agent_xml_compose__tasks(agent);
00197 if (tmp_node != NULL) {
00198 mxmlAdd(
00199 node,
00200 MXML_ADD_AFTER,
00201 NULL,
00202 tmp_node
00203 );
00204 }
00205
00206 return node;
00207 }
00208
00209 mxml_node_t*
00210 agent_xml_compose__name(agent_p agent)
00211 {
00212 mxml_node_t* node;
00213 node = mxmlNewElement(
00214 NULL,
00215 "NAME"
00216 );
00217 mxmlNewText(
00218 node,
00219 0,
00220 agent->name
00221 );
00222 return node;
00223 }
00224
00225 mxml_node_t*
00226 agent_xml_compose__owner(agent_p agent)
00227 {
00228 mxml_node_t* node;
00229 node = mxmlNewElement(
00230 NULL,
00231 "OWNER"
00232 );
00233 mxmlNewText(
00234 node,
00235 0,
00236 agent->owner
00237 );
00238 return node;
00239 }
00240
00241 mxml_node_t*
00242 agent_xml_compose__home(agent_p agent)
00243 {
00244 mxml_node_t* node;
00245 node = mxmlNewElement(
00246 NULL,
00247 "HOME"
00248 );
00249 mxmlNewText(
00250 node,
00251 0,
00252 agent->home
00253 );
00254 return node;
00255 }
00256
00257 mxml_node_t*
00258 agent_xml_compose__tasks(agent_p agent)
00259 {
00260 char buf[30];
00261 int i;
00262 mxml_node_t* node;
00263 mxml_node_t* tmp_node;
00264
00265 node=mxmlNewElement(
00266 NULL,
00267 "TASKS" );
00268
00269 sprintf(buf, "%d", agent->datastate->number_of_tasks);
00270 mxmlElementSetAttr(
00271 node,
00272 "task",
00273 buf
00274 );
00275
00276 buf[0] = '\0';
00277 sprintf(buf, "%d", agent->datastate->task_progress);
00278 mxmlElementSetAttr(
00279 node,
00280 "num",
00281 buf
00282 );
00283
00284 for (i = 0; i < agent->datastate->number_of_tasks; i++) {
00285 tmp_node = agent_xml_compose__task(agent, i);
00286 if (tmp_node != NULL) {
00287 mxmlAdd(
00288 node,
00289 MXML_ADD_AFTER,
00290 NULL,
00291 tmp_node
00292 );
00293 } else {
00294 return NULL;
00295 }
00296 }
00297
00298 i=0;
00299 tmp_node = agent_xml_compose__agent_code(agent, i);
00300 while (tmp_node != NULL) {
00301 mxmlAdd(
00302 node,
00303 MXML_ADD_AFTER,
00304 NULL,
00305 tmp_node
00306 );
00307 i++;
00308 tmp_node = agent_xml_compose__agent_code(agent, i);
00309 }
00310
00311 return node;
00312 }
00313
00314 mxml_node_t*
00315 agent_xml_compose__task(agent_p agent, int index)
00316 {
00317 char buf[30];
00318 mxml_node_t* node;
00319 mxml_node_t* tmp_node;
00320 interpreter_variable_data_t* tmp_interp_var;
00321 node = mxmlNewElement(
00322 NULL,
00323 "TASK"
00324 );
00325
00326 buf[0] = '\0';
00327 sprintf(buf, "%d", index);
00328 mxmlElementSetAttr(
00329 node,
00330 "num",
00331 buf );
00332
00333
00334 mxmlElementSetAttr(
00335 node,
00336 "server",
00337 agent->datastate->tasks[index]->server_name
00338 );
00339
00340
00341 if (agent->datastate->tasks[index]->code_id) {
00342 mxmlElementSetAttr(
00343 node,
00344 "code_id",
00345 agent->datastate->tasks[index]->code_id
00346 );
00347 }
00348
00349
00350 if (agent->datastate->persistent || agent->datastate->tasks[index]->persistent) {
00351 mxmlElementSetAttr(
00352 node,
00353 "persistent",
00354 "1"
00355 );
00356 }
00357
00358
00359
00360 if(agent->datastate->tasks[index]->var_name != NULL) {
00361 mxmlElementSetAttr(
00362 node,
00363 "return",
00364 agent->datastate->tasks[index]->var_name );
00365
00366 if(
00367 strcmp(
00368 "no-return",
00369 agent->datastate->tasks[index]->var_name
00370 )
00371 )
00372 {
00373
00374 if (agent->datastate->tasks[index]->agent_return_data != NULL) {
00375 tmp_node = agent_xml_compose__data(
00376 agent,
00377 index,
00378 agent->datastate->tasks[index]->agent_return_data);
00379 } else {tmp_node = NULL;}
00380
00381 if(tmp_node != NULL) {
00382 mxmlAdd(
00383 node,
00384 MXML_ADD_AFTER,
00385 NULL,
00386 tmp_node );
00387 }
00388 }
00389 }
00390
00391
00392 while
00393 (
00394 (
00395 tmp_interp_var = agent_variable_list_Pop(
00396 agent->datastate->tasks[index]->agent_variable_list )
00397 ) != NULL
00398 )
00399 {
00400 tmp_node = agent_xml_compose__data(
00401 agent,
00402 index,
00403 tmp_interp_var);
00404 free(tmp_interp_var);
00405 if(tmp_node == NULL) {
00406 fprintf(stderr, "Compose error. %s:%d\n", __FILE__, __LINE__);
00407 return NULL;
00408 }
00409 mxmlAdd(
00410 node,
00411 MXML_ADD_AFTER,
00412 NULL,
00413 tmp_node );
00414 }
00415
00416 return node;
00417 }
00418
00419 mxml_node_t*
00420 agent_xml_compose__data(agent_p agent, int index, interpreter_variable_data_t* interp_variable)
00421 {
00422 char buf[30];
00423 mxml_node_t* node;
00424 mxml_node_t* tmp_node;
00425
00426 if (interp_variable == NULL) { return NULL; }
00427
00428 node = mxmlNewElement(
00429 NULL,
00430 "DATA"
00431 );
00432
00433 mxmlElementSetAttr(
00434 node,
00435 "name",
00436 interp_variable->name
00437 );
00438
00439 if (interp_variable != NULL)
00440 {
00441
00442 sprintf(
00443 buf,
00444 "%d",
00445 interp_variable->array_dim
00446 );
00447 mxmlElementSetAttr(
00448 node,
00449 "dim",
00450 buf
00451 );
00452
00453
00454 CH_DATATYPE_STRING(
00455 interp_variable->data_type,
00456 buf
00457 );
00458 mxmlElementSetAttr(
00459 node,
00460 "type",
00461 buf
00462 );
00463
00464 if (interp_variable->array_dim == 0)
00465 {
00466 CH_DATATYPE_VALUE_STRING
00467 (
00468 interp_variable->data_type,
00469 buf,
00470 interp_variable->data
00471 );
00472 mxmlElementSetAttr(
00473 node,
00474 "value",
00475 buf
00476 );
00477 } else {
00478
00479 tmp_node = agent_xml_compose__row(interp_variable, index);
00480 if (tmp_node != NULL) {
00481 mxmlAdd(
00482 node,
00483 MXML_ADD_AFTER,
00484 NULL,
00485 tmp_node
00486 );
00487 }
00488 }
00489 }
00490 return node;
00491 }
00492
00493 mxml_node_t*
00494 agent_xml_compose__agent_code(agent_p agent, int index)
00495 {
00496 mxml_node_t* node;
00497 if (agent->datastate->agent_codes[index] == NULL) {
00498 return NULL;
00499 }
00500
00501 node = mxmlNewElement (
00502 MXML_NO_PARENT,
00503 "AGENT_CODE"
00504 );
00505
00506 xml_new_cdata(
00507 node,
00508 agent->datastate->agent_codes[index]
00509 );
00510 if (strlen(agent->datastate->agent_code_ids[index]) > 0) {
00511 mxmlElementSetAttr
00512 (
00513 node,
00514 "id",
00515 agent->datastate->agent_code_ids[index]
00516 );
00517 }
00518 return node;
00519 }
00520
00521 mxml_node_t*
00522 agent_xml_compose__row(interpreter_variable_data_t* interp_variable, int index)
00523 {
00524 mxml_node_t* node;
00525
00526 if (interp_variable == NULL) {
00527 return NULL;
00528 }
00529
00530 node = agent_xml_compose__create_row_nodes
00531 (
00532 interp_variable->data,
00533 0,
00534 interp_variable->array_extent,
00535 interp_variable->data_type,
00536 interp_variable->array_dim,
00537 0
00538 );
00539 return node;
00540 }
00541
00542
00543 mxml_node_t*
00544 agent_xml_compose__create_row_nodes
00545 (
00546 void* data,
00547 int index,
00548 int *extent,
00549 ChType_t type,
00550 int dim,
00551 int extent_index
00552 )
00553 {
00554 mxml_node_t* node;
00555 char *buf;
00556 char *varstring;
00557 int size;
00558 int i;
00559 if (dim == 1) {
00560 buf = (char*)malloc(sizeof(char) * 20);
00561 CH_DATATYPE_SIZE(type, size);
00562
00563 varstring = malloc(
00564 (sizeof(char)*64) * *extent);
00565 varstring[0] = '\0';
00566 for(i = 0; i < *extent; i++) {
00567 buf[0] = '\0';
00568 #ifndef _WIN32
00569 CH_DATATYPE_VALUE_STRING(type, buf, (data+ size*(index+i)));
00570 #else
00571 CH_DATATYPE_VALUE_STRING(type, buf, ((char*)data+ size*(index+i)));
00572 #endif
00573 strcat(varstring, buf);
00574 strcat(varstring, ",");
00575 }
00576 node = mxmlNewElement(
00577 MXML_NO_PARENT,
00578 "ROW" );
00579 buf[0] = '\0';
00580 sprintf(buf, "%d", extent_index);
00581 mxmlElementSetAttr(
00582 node,
00583 "index",
00584 buf );
00585
00586
00587 mxmlNewText(
00588 node,
00589 1,
00590 varstring );
00591 free(buf);
00592 free(varstring);
00593 return node;
00594 } else if (dim < 0) {
00595 fprintf(stderr, "INTERNAL ERROR: %s:%d\n",
00596 __FILE__, __LINE__);
00597 return NULL;
00598 } else if (dim == 0) {
00599 return NULL;
00600 } else {
00601
00602 size = 1;
00603 for (i = 1; i < dim; i++) {
00604 size *= extent[i];
00605 }
00606 node = mxmlNewElement(MXML_NO_PARENT, "ROW");
00607 buf = (char*)malloc(sizeof(char)*10);
00608 sprintf(buf, "%d", extent_index);
00609 mxmlElementSetAttr(
00610 node,
00611 "index",
00612 buf );
00613 for (i = 0; i < *extent; i++) {
00614 mxmlAdd(
00615 node,
00616 MXML_ADD_AFTER,
00617 MXML_ADD_TO_PARENT,
00618 agent_xml_compose__create_row_nodes(
00619 data,
00620 index + (size*i),
00621 extent+1,
00622 type,
00623 dim-1,
00624 i
00625 )
00626 );
00627 }
00628 free (buf);
00629 return node;
00630 }
00631 }
00632