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__wg_code(agent);
00197 if (tmp_node != NULL) {
00198 mxmlAdd(
00199 node,
00200 MXML_ADD_AFTER,
00201 NULL,
00202 tmp_node
00203 );
00204 }
00205
00206
00207 tmp_node = agent_xml_compose__tasks(agent);
00208 if (tmp_node != NULL) {
00209 mxmlAdd(
00210 node,
00211 MXML_ADD_AFTER,
00212 NULL,
00213 tmp_node
00214 );
00215 }
00216
00217 return node;
00218 }
00219
00220 mxml_node_t*
00221 agent_xml_compose__name(agent_p agent)
00222 {
00223 mxml_node_t* node;
00224 node = mxmlNewElement(
00225 NULL,
00226 "NAME"
00227 );
00228 mxmlNewText(
00229 node,
00230 0,
00231 agent->name
00232 );
00233 return node;
00234 }
00235
00236 mxml_node_t*
00237 agent_xml_compose__owner(agent_p agent)
00238 {
00239 mxml_node_t* node;
00240 node = mxmlNewElement(
00241 NULL,
00242 "OWNER"
00243 );
00244 mxmlNewText(
00245 node,
00246 0,
00247 agent->owner
00248 );
00249 return node;
00250 }
00251
00252 mxml_node_t*
00253 agent_xml_compose__home(agent_p agent)
00254 {
00255 mxml_node_t* node;
00256 node = mxmlNewElement(
00257 NULL,
00258 "HOME"
00259 );
00260 mxmlNewText(
00261 node,
00262 0,
00263 agent->home
00264 );
00265 return node;
00266 }
00267
00268 mxml_node_t*
00269 agent_xml_compose__wg_code(agent_p agent)
00270 {
00271 mxml_node_t* node;
00272 node = mxmlNewElement(
00273 NULL,
00274 "WG_CODE"
00275 );
00276 mxmlNewText(
00277 node,
00278 0,
00279 agent->wg_code
00280 );
00281 return node;
00282 }
00283
00284 mxml_node_t*
00285 agent_xml_compose__tasks(agent_p agent)
00286 {
00287 char buf[30];
00288 int i;
00289 mxml_node_t* node;
00290 mxml_node_t* tmp_node;
00291
00292 node=mxmlNewElement(
00293 NULL,
00294 "TASKS" );
00295
00296 sprintf(buf, "%d", agent->datastate->number_of_tasks);
00297 mxmlElementSetAttr(
00298 node,
00299 "task",
00300 buf
00301 );
00302
00303 buf[0] = '\0';
00304 sprintf(buf, "%d", agent->datastate->task_progress);
00305 mxmlElementSetAttr(
00306 node,
00307 "num",
00308 buf
00309 );
00310
00311 for (i = 0; i < agent->datastate->number_of_tasks; i++) {
00312 tmp_node = agent_xml_compose__task(agent, i);
00313 if (tmp_node != NULL) {
00314 mxmlAdd(
00315 node,
00316 MXML_ADD_AFTER,
00317 NULL,
00318 tmp_node
00319 );
00320 } else {
00321 return NULL;
00322 }
00323 }
00324
00325 i=0;
00326 tmp_node = agent_xml_compose__agent_code(agent, i);
00327 while (tmp_node != NULL) {
00328 mxmlAdd(
00329 node,
00330 MXML_ADD_AFTER,
00331 NULL,
00332 tmp_node
00333 );
00334 i++;
00335 tmp_node = agent_xml_compose__agent_code(agent, i);
00336 }
00337
00338 return node;
00339 }
00340
00341 mxml_node_t*
00342 agent_xml_compose__task(agent_p agent, int index)
00343 {
00344 char buf[30];
00345 mxml_node_t* node;
00346 mxml_node_t* tmp_node;
00347 interpreter_variable_data_t* tmp_interp_var;
00348 node = mxmlNewElement(
00349 NULL,
00350 "TASK"
00351 );
00352
00353 buf[0] = '\0';
00354 sprintf(buf, "%d", index);
00355 mxmlElementSetAttr(
00356 node,
00357 "num",
00358 buf );
00359
00360
00361 mxmlElementSetAttr(
00362 node,
00363 "server",
00364 agent->datastate->tasks[index]->server_name
00365 );
00366
00367
00368 if (agent->datastate->tasks[index]->code_id) {
00369 mxmlElementSetAttr(
00370 node,
00371 "code_id",
00372 agent->datastate->tasks[index]->code_id
00373 );
00374 }
00375
00376
00377 if (agent->datastate->persistent || agent->datastate->tasks[index]->persistent) {
00378 mxmlElementSetAttr(
00379 node,
00380 "persistent",
00381 "1"
00382 );
00383 }
00384
00385
00386
00387 if(agent->datastate->tasks[index]->var_name != NULL) {
00388 mxmlElementSetAttr(
00389 node,
00390 "return",
00391 agent->datastate->tasks[index]->var_name );
00392
00393 if(
00394 strcmp(
00395 "no-return",
00396 agent->datastate->tasks[index]->var_name
00397 )
00398 )
00399 {
00400
00401 if (agent->datastate->tasks[index]->agent_return_data != NULL) {
00402 tmp_node = agent_xml_compose__data(
00403 agent,
00404 index,
00405 agent->datastate->tasks[index]->agent_return_data);
00406 } else {tmp_node = NULL;}
00407
00408 if(tmp_node != NULL) {
00409 mxmlAdd(
00410 node,
00411 MXML_ADD_AFTER,
00412 NULL,
00413 tmp_node );
00414 }
00415 }
00416 }
00417
00418
00419 while
00420 (
00421 (
00422 tmp_interp_var = agent_variable_list_Pop(
00423 agent->datastate->tasks[index]->agent_variable_list )
00424 ) != NULL
00425 )
00426 {
00427 tmp_node = agent_xml_compose__data(
00428 agent,
00429 index,
00430 tmp_interp_var);
00431 free(tmp_interp_var);
00432 if(tmp_node == NULL) {
00433 fprintf(stderr, "Compose error. %s:%d\n", __FILE__, __LINE__);
00434 return NULL;
00435 }
00436 mxmlAdd(
00437 node,
00438 MXML_ADD_AFTER,
00439 NULL,
00440 tmp_node );
00441 }
00442
00443 return node;
00444 }
00445
00446 mxml_node_t*
00447 agent_xml_compose__data(agent_p agent, int index, interpreter_variable_data_t* interp_variable)
00448 {
00449 char buf[30];
00450 mxml_node_t* node;
00451 mxml_node_t* tmp_node;
00452
00453 if (interp_variable == NULL) { return NULL; }
00454
00455 node = mxmlNewElement(
00456 NULL,
00457 "DATA"
00458 );
00459
00460 mxmlElementSetAttr(
00461 node,
00462 "name",
00463 interp_variable->name
00464 );
00465
00466 if (interp_variable != NULL)
00467 {
00468
00469 sprintf(
00470 buf,
00471 "%d",
00472 interp_variable->array_dim
00473 );
00474 mxmlElementSetAttr(
00475 node,
00476 "dim",
00477 buf
00478 );
00479
00480
00481 CH_DATATYPE_STRING(
00482 interp_variable->data_type,
00483 buf
00484 );
00485 mxmlElementSetAttr(
00486 node,
00487 "type",
00488 buf
00489 );
00490
00491 if (interp_variable->array_dim == 0)
00492 {
00493 CH_DATATYPE_VALUE_STRING
00494 (
00495 interp_variable->data_type,
00496 buf,
00497 interp_variable->data
00498 );
00499 mxmlElementSetAttr(
00500 node,
00501 "value",
00502 buf
00503 );
00504 } else {
00505
00506 tmp_node = agent_xml_compose__row(interp_variable, index);
00507 if (tmp_node != NULL) {
00508 mxmlAdd(
00509 node,
00510 MXML_ADD_AFTER,
00511 NULL,
00512 tmp_node
00513 );
00514 }
00515 }
00516 }
00517 return node;
00518 }
00519
00520 mxml_node_t*
00521 agent_xml_compose__agent_code(agent_p agent, int index)
00522 {
00523 mxml_node_t* node;
00524 if (agent->datastate->agent_codes[index] == NULL) {
00525 return NULL;
00526 }
00527
00528 node = mxmlNewElement (
00529 MXML_NO_PARENT,
00530 "AGENT_CODE"
00531 );
00532
00533 xml_new_cdata(
00534 node,
00535 agent->datastate->agent_codes[index]
00536 );
00537 if (strlen(agent->datastate->agent_code_ids[index]) > 0) {
00538 mxmlElementSetAttr
00539 (
00540 node,
00541 "id",
00542 agent->datastate->agent_code_ids[index]
00543 );
00544 }
00545 return node;
00546 }
00547
00548 mxml_node_t*
00549 agent_xml_compose__row(interpreter_variable_data_t* interp_variable, int index)
00550 {
00551 mxml_node_t* node;
00552
00553 if (interp_variable == NULL) {
00554 return NULL;
00555 }
00556
00557 node = agent_xml_compose__create_row_nodes
00558 (
00559 interp_variable->data,
00560 0,
00561 interp_variable->array_extent,
00562 interp_variable->data_type,
00563 interp_variable->array_dim,
00564 0
00565 );
00566 return node;
00567 }
00568
00569
00570 mxml_node_t*
00571 agent_xml_compose__create_row_nodes
00572 (
00573 void* data,
00574 int index,
00575 int *extent,
00576 ChType_t type,
00577 int dim,
00578 int extent_index
00579 )
00580 {
00581 mxml_node_t* node;
00582 char *buf;
00583 char *varstring;
00584 int size;
00585 int i;
00586 if (dim == 1) {
00587 buf = (char*)malloc(sizeof(char) * 20);
00588 CH_DATATYPE_SIZE(type, size);
00589
00590 varstring = (char*)malloc(
00591 (sizeof(char)*64) * *extent);
00592 varstring[0] = '\0';
00593 for(i = 0; i < *extent; i++) {
00594 buf[0] = '\0';
00595 #ifndef _WIN32
00596 CH_DATATYPE_VALUE_STRING(type, buf, (data+ size*(index+i)));
00597 #else
00598 CH_DATATYPE_VALUE_STRING(type, buf, ((char*)data+ size*(index+i)));
00599 #endif
00600 strcat(varstring, buf);
00601 strcat(varstring, ",");
00602 }
00603 node = mxmlNewElement(
00604 MXML_NO_PARENT,
00605 "ROW" );
00606 buf[0] = '\0';
00607 sprintf(buf, "%d", extent_index);
00608 mxmlElementSetAttr(
00609 node,
00610 "index",
00611 buf );
00612
00613
00614 mxmlNewText(
00615 node,
00616 1,
00617 varstring );
00618 free(buf);
00619 free(varstring);
00620 return node;
00621 } else if (dim < 0) {
00622 fprintf(stderr, "INTERNAL ERROR: %s:%d\n",
00623 __FILE__, __LINE__);
00624 return NULL;
00625 } else if (dim == 0) {
00626 return NULL;
00627 } else {
00628
00629 size = 1;
00630 for (i = 1; i < dim; i++) {
00631 size *= extent[i];
00632 }
00633 node = mxmlNewElement(MXML_NO_PARENT, "ROW");
00634 buf = (char*)malloc(sizeof(char)*10);
00635 sprintf(buf, "%d", extent_index);
00636 mxmlElementSetAttr(
00637 node,
00638 "index",
00639 buf );
00640 for (i = 0; i < *extent; i++) {
00641 mxmlAdd(
00642 node,
00643 MXML_ADD_AFTER,
00644 MXML_ADD_TO_PARENT,
00645 agent_xml_compose__create_row_nodes(
00646 data,
00647 index + (size*i),
00648 extent+1,
00649 type,
00650 dim-1,
00651 i
00652 )
00653 );
00654 }
00655 free (buf);
00656 return node;
00657 }
00658 }
00659