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 #include <mxml.h>
00036 #include <string.h>
00037 #include <stdlib.h>
00038 #define _XOPEN_SOURCE 600
00039 #include <stdlib.h>
00040 #ifndef _WIN32
00041 #include "config.h"
00042 #else
00043 #include "winconfig.h"
00044 #endif
00045 #include "include/agent_file_data.h"
00046 #include "include/interpreter_variable_data.h"
00047 #include "include/message.h"
00048 #include "include/xml_parser.h"
00049 #include "include/xml_helper.h"
00050
00051
00052
00053 error_code_t agent_xml_parse(agent_p agent)
00054 {
00055 xml_parser_t xml_parser;
00056 xml_parser.root = agent->datastate->xml_agent_root;
00057 xml_parser.node = xml_parser.root;
00058 agent_xml_parse__mobile_agent(agent, &xml_parser);
00059 return MC_SUCCESS;
00060 }
00061
00062
00063
00064 error_code_t
00065 agent_xml_parse__mobile_agent
00066 (
00067 agent_p agent,
00068 xml_parser_p xml_parser
00069 )
00070 {
00071
00072 if (
00073 strcmp(
00074 (const char*)xml_get_element_name(xml_parser->node),
00075 "MOBILE_AGENT"
00076 )
00077 )
00078 {
00079 return MC_ERR_PARSE;
00080 }
00081
00082 xml_parser->node = xml_get_child(
00083 xml_parser->node,
00084 "AGENT_DATA",
00085 1);
00086
00087 return agent_xml_parse__agent_data(agent, xml_parser);
00088 }
00089
00090
00091
00092 error_code_t
00093 agent_xml_parse__agent_data
00094 (
00095 agent_p agent,
00096 xml_parser_p xml_parser
00097 )
00098 {
00099 const mxml_node_t* agent_data_node;
00100 error_code_t err_code;
00101
00102 if (xml_parser->node == NULL) {
00103 return MC_ERR_PARSE;
00104 }
00105
00106 agent_data_node = xml_parser->node;
00107
00108 xml_parser->node = xml_get_child(
00109 agent_data_node,
00110 "NAME",
00111 1
00112 );
00113 if ( (err_code = agent_xml_parse__name(agent, xml_parser)) ) {
00114 return err_code;
00115 }
00116
00117 xml_parser->node = xml_get_child(
00118 agent_data_node,
00119 "OWNER",
00120 1
00121 );
00122 if ( (err_code = agent_xml_parse__owner(agent, xml_parser)) ) {
00123 return err_code;
00124 }
00125
00126 xml_parser->node = xml_get_child(
00127 agent_data_node,
00128 "HOME",
00129 1
00130 );
00131 if ( (err_code = agent_xml_parse__home(agent, xml_parser)) ) {
00132 return err_code;
00133 }
00134
00135 xml_parser->node = xml_get_child(
00136 agent_data_node,
00137 "SENDER",
00138 1
00139 );
00140 if( (err_code = agent_xml_parse__sender(agent, xml_parser)) ) {
00141 return err_code;
00142 }
00143
00144 xml_parser->node = xml_get_child(
00145 agent_data_node,
00146 "WG_CODE",
00147 1
00148 );
00149 if ( (err_code = agent_xml_parse__wg_code(agent, xml_parser)) ) {
00150 return err_code;
00151 }
00152
00153 xml_parser->node = xml_get_child(
00154 agent_data_node,
00155 "TASKS",
00156 1
00157 );
00158 if ( (err_code = agent_xml_parse__tasks(agent, xml_parser)) ) {
00159 return err_code;
00160 }
00161 return MC_SUCCESS;
00162 }
00163
00164
00165
00166 error_code_t
00167 agent_xml_parse__name(agent_p agent, xml_parser_p xml_parser)
00168 {
00169 char* text;
00170 const mxml_node_t* name_node;
00171 if (xml_parser->node == NULL) {
00172 return MC_ERR_PARSE;
00173 }
00174 name_node = xml_parser->node;
00175
00176 text = xml_get_text( name_node );
00177 CHECK_NULL(text, return MC_ERR_PARSE;);
00178
00179 agent->name = (char*)malloc(
00180 sizeof(char)*(strlen(text)+1)
00181 );
00182 strcpy(
00183 agent->name,
00184 text
00185 );
00186 free(text);
00187 return MC_SUCCESS;
00188 }
00189
00190
00191
00192 error_code_t
00193 agent_xml_parse__owner(agent_p agent, xml_parser_p xml_parser)
00194 {
00195 char *text;
00196 const mxml_node_t* owner_node;
00197 if (xml_parser->node == NULL) {
00198
00199 agent->owner = NULL;
00200 return MC_SUCCESS;
00201 }
00202 owner_node = xml_parser->node;
00203
00204 text = xml_get_text( owner_node );
00205 CHECK_NULL(text, agent->owner=NULL;return MC_SUCCESS;);
00206 agent->owner = (char*)malloc(
00207 sizeof(char)*(strlen(text)+1)
00208 );
00209 strcpy(
00210 agent->owner,
00211 text
00212 );
00213 free(text);
00214 return MC_SUCCESS;
00215 }
00216
00217
00218
00219 error_code_t
00220 agent_xml_parse__home(agent_p agent, xml_parser_p xml_parser)
00221 {
00222 char *text;
00223 const mxml_node_t* home_node;
00224 if (xml_parser->node == NULL) {
00225
00226 agent->home= NULL;
00227 return MC_SUCCESS;
00228 }
00229 home_node = xml_parser->node;
00230 text = xml_get_text( home_node );
00231 CHECK_NULL(text, agent->home=NULL;return MC_SUCCESS;);
00232 agent->home = (char*)malloc(
00233 sizeof(char)*(strlen(text)+1)
00234 );
00235 strcpy(
00236 agent->home,
00237 text
00238 );
00239 free(text);
00240 return MC_SUCCESS;
00241 }
00242
00243
00244
00245 error_code_t
00246 agent_xml_parse__sender(agent_p agent, xml_parser_p xml_parser)
00247 {
00248 char *text;
00249 const mxml_node_t* sender_node;
00250 if (xml_parser->node == NULL) {
00251
00252 agent->sender = NULL;
00253 return MC_SUCCESS;
00254 }
00255 sender_node = xml_parser->node;
00256 text = xml_get_text( sender_node );
00257
00258 CHECK_NULL(text, agent->sender=NULL;return MC_SUCCESS; );
00259
00260 agent->sender = (char*)malloc(
00261 sizeof(char)*(strlen(text)+1)
00262 );
00263 strcpy(
00264 agent->sender,
00265 text
00266 );
00267 free(text);
00268 return MC_SUCCESS;
00269 }
00270
00271
00272
00273 error_code_t
00274 agent_xml_parse__wg_code(agent_p agent, xml_parser_p xml_parser)
00275 {
00276 char *text;
00277 const mxml_node_t* wg_code_node;
00278 if (xml_parser->node == NULL) {
00279
00280 agent->wg_code = NULL;
00281 return MC_SUCCESS;
00282 }
00283 wg_code_node = xml_parser->node;
00284 text = xml_get_text( wg_code_node );
00285
00286 if (text == NULL) {
00287 agent->wg_code=NULL;
00288 return MC_SUCCESS;
00289 }
00290
00291 agent->wg_code = (char*)malloc(
00292 sizeof(char)*(strlen(text)+1)
00293 );
00294 strcpy(
00295 agent->wg_code,
00296 text
00297 );
00298 free(text);
00299 return MC_SUCCESS;
00300 }
00301
00302
00303
00304 error_code_t
00305 agent_xml_parse__tasks(agent_p agent, xml_parser_p xml_parser)
00306 {
00307 int i;
00308 int code_num=0;
00309 int err_code;
00310 const char* attribute;
00311 mxml_node_t* tasks_node;
00312 char buf[20];
00313
00314 tasks_node = xml_parser->node;
00315
00316
00317 attribute = mxmlElementGetAttr(
00318 (mxml_node_t*)tasks_node,
00319 "task"
00320 );
00321 if (attribute == NULL) {
00322 agent->datastate->number_of_tasks = 1;
00323 } else {
00324 agent->datastate->number_of_tasks = atoi((char*)attribute);
00325 }
00326 agent->datastate->tasks = (agent_task_p*)malloc(
00327 sizeof(agent_task_p) * agent->datastate->number_of_tasks
00328 );
00329
00330
00331 attribute = mxmlElementGetAttr(
00332 tasks_node,
00333 "num"
00334 );
00335 if (attribute == NULL) {
00336 agent->datastate->task_progress = 0;
00337 } else {
00338 agent->datastate->task_progress = atoi((char*)attribute);
00339 }
00340
00341
00342 for(i = 0; i<agent->datastate->number_of_tasks; i++) {
00343 agent->datastate->tasks[i] = agent_task_New();
00344 }
00345
00346
00347 for(i = 0; i < agent->datastate->number_of_tasks; i++) {
00348 sprintf(buf, "%d", i);
00349 xml_parser->node = mxmlFindElement(
00350 tasks_node,
00351 tasks_node,
00352 "TASK",
00353 "num",
00354 buf,
00355 MXML_DESCEND_FIRST );
00356 if(xml_parser->node == NULL) {
00357 fprintf(stderr,
00358 "ERROR: Could not find task num %d! %s:%d\n",
00359 i, __FILE__, __LINE__);
00360 return MC_ERR_PARSE;
00361 }
00362 agent_xml_parse__task(agent, xml_parser, i);
00363 }
00364
00365
00366
00367
00368 xml_parser->node = mxmlFindElement
00369 (
00370 tasks_node,
00371 tasks_node,
00372 "AGENT_CODE",
00373 NULL,
00374 NULL,
00375 MXML_DESCEND
00376 );
00377
00378
00379
00380 while(xml_parser->node != NULL) {
00381 code_num++;
00382 xml_parser->node = mxmlFindElement
00383 (
00384 xml_parser->node,
00385 tasks_node,
00386 "AGENT_CODE",
00387 NULL,
00388 NULL,
00389 MXML_NO_DESCEND
00390 );
00391 }
00392
00393
00394 agent->datastate->agent_code_ids = (char**)malloc
00395 (
00396 (code_num+1) * sizeof(char*)
00397 );
00398 agent->datastate->agent_codes = (char**)malloc
00399 (
00400 (code_num+1) * sizeof(char*)
00401 );
00402
00403 agent->datastate->agent_code_ids[code_num] = NULL;
00404 agent->datastate->agent_codes[code_num] = NULL;
00405
00406
00407 xml_parser->node = mxmlFindElement
00408 (
00409 tasks_node,
00410 tasks_node,
00411 "AGENT_CODE",
00412 NULL,
00413 NULL,
00414 MXML_DESCEND
00415 );
00416 i = 0;
00417 while(xml_parser->node != NULL) {
00418 err_code = agent_xml_parse__agent_code(agent, i, xml_parser);
00419 i++;
00420 xml_parser->node = mxmlFindElement
00421 (
00422 xml_parser->node,
00423 tasks_node,
00424 "AGENT_CODE",
00425 NULL,
00426 NULL,
00427 MXML_NO_DESCEND
00428 );
00429 }
00430
00431 if (agent->datastate->agent_code == NULL) {
00432
00433 fprintf(stderr, "Parse error: Agent code not found. %s:%d\n", __FILE__, __LINE__);
00434 return MC_ERR_PARSE;
00435 }
00436
00437 return (error_code_t)0;
00438 }
00439
00440
00441
00442 error_code_t
00443 agent_xml_parse__task(agent_p agent, xml_parser_p xml_parser, int index)
00444 {
00445 const char* attribute;
00446 mxml_node_t* task_node;
00447 error_code_t err_code = MC_SUCCESS;
00448 CHECK_NULL(xml_parser->node, return MC_ERR_PARSE;);
00449 task_node = xml_parser->node;
00450
00451
00452 xml_parser->node = mxmlFindElement(
00453 task_node,
00454 task_node,
00455 "DATA",
00456 NULL,
00457 NULL,
00458 MXML_DESCEND_FIRST);
00459 while(xml_parser->node != NULL) {
00460
00461 if ((err_code = agent_xml_parse__data(agent, xml_parser, index)))
00462 {
00463 return err_code;
00464 }
00465 xml_parser->node = mxmlFindElement(
00466 xml_parser->node,
00467 task_node,
00468 "DATA",
00469 NULL,
00470 NULL,
00471 MXML_NO_DESCEND );
00472 }
00473
00474
00475 xml_parser->node = mxmlFindElement(
00476 task_node,
00477 task_node,
00478 "FILE",
00479 NULL,
00480 NULL,
00481 MXML_DESCEND_FIRST);
00482 while(xml_parser->node != NULL) {
00483 if ((err_code = agent_xml_parse__file(agent, xml_parser, index)))
00484 {
00485 return err_code;
00486 }
00487 xml_parser->node = mxmlFindElement(
00488 xml_parser->node,
00489 task_node,
00490 "FILE",
00491 NULL,
00492 NULL,
00493 MXML_NO_DESCEND );
00494 }
00495
00496
00497 attribute = mxmlElementGetAttr(
00498 (mxml_node_t*)task_node,
00499 "persistent"
00500 );
00501 if (attribute != NULL) {
00502 agent->datastate->tasks[index]->persistent =
00503 atoi((char*)attribute);
00504 } else {
00505 agent->datastate->tasks[index]->persistent = 0;
00506 }
00507
00508
00509
00510 attribute = mxmlElementGetAttr(
00511 (mxml_node_t*)task_node,
00512 "code_id");
00513 if (attribute != NULL) {
00514 agent->datastate->tasks[index]->code_id = (char*)malloc
00515 (
00516 sizeof(char) *
00517 (strlen(attribute) + 1)
00518 );
00519 strcpy(agent->datastate->tasks[index]->code_id, attribute);
00520 } else {
00521 agent->datastate->tasks[index]->code_id = NULL;
00522 }
00523
00524
00525 attribute = mxmlElementGetAttr(
00526 (mxml_node_t*)task_node,
00527 "num"
00528 );
00529 CHECK_NULL(attribute, return MC_ERR_PARSE;);
00530
00531
00532 attribute = mxmlElementGetAttr(
00533 (mxml_node_t*)task_node,
00534 "server"
00535 );
00536 CHECK_NULL(attribute, return MC_ERR_PARSE;);
00537 agent->datastate->tasks[index]->server_name =
00538 (char*)malloc(sizeof(char) * (strlen(attribute)+1) );
00539 strcpy(
00540 agent->datastate->tasks[index]->server_name,
00541 attribute
00542 );
00543
00544
00545 attribute = mxmlElementGetAttr(
00546 (mxml_node_t*)task_node,
00547 "return" );
00548 if (attribute == NULL) {
00549 agent->datastate->tasks[index]->var_name = strdup("no-return");
00550 } else {
00551 agent->datastate->tasks[index]->var_name = strdup(attribute);
00552 }
00553 CHECK_NULL(agent->datastate->tasks[index]->var_name, exit(1););
00554
00555 return err_code;
00556 }
00557
00558
00559
00560 error_code_t
00561 agent_xml_parse__data(agent_p agent, xml_parser_p xml_parser, int index)
00562 {
00563 const char* attribute;
00564 const char* attribute2;
00565 mxml_node_t *data_node;
00566 int data_type_size;
00567 interpreter_variable_data_t* interp_variable;
00568 if (xml_parser->node == NULL) {
00569 return MC_ERR_PARSE;
00570 }
00571 if (strcmp(
00572 "DATA",
00573 xml_get_element_name(xml_parser->node) )
00574 )
00575 {
00576 return MC_ERR_PARSE;
00577 }
00578 data_node = xml_parser->node;
00579
00580
00581 attribute = mxmlElementGetAttr(
00582 data_node->parent,
00583 "return" );
00584 attribute2 = mxmlElementGetAttr(
00585 data_node,
00586 "name" );
00587 if (attribute != NULL && !strcmp(attribute, attribute2)) {
00588
00589
00590
00591
00592
00593 agent->datastate->tasks[index]->agent_return_data =
00594 interpreter_variable_data_New();
00595 interp_variable = agent->datastate->tasks[index]->agent_return_data;
00596 } else {
00597 interp_variable = interpreter_variable_data_New();
00598 ListWRLock(agent->datastate->tasks[index]->agent_variable_list);
00599 ListAdd(
00600 agent->datastate->tasks[index]->agent_variable_list,
00601 interp_variable );
00602 ListWRUnlock(agent->datastate->tasks[index]->agent_variable_list);
00603 }
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621 attribute = mxmlElementGetAttr(
00622 (mxml_node_t*)xml_parser->node,
00623 "dim"
00624 );
00625 if (attribute != NULL) {
00626 interp_variable->array_dim =
00627 atoi((char*)attribute);
00628 } else {
00629 interp_variable->array_dim =
00630 0;
00631 }
00632
00633
00634 attribute = mxmlElementGetAttr(
00635 (mxml_node_t*)xml_parser->node,
00636 "name"
00637 );
00638 if (attribute != NULL) {
00639 interp_variable->name =
00640 (char*)malloc(sizeof(char)*(strlen(attribute)+1));
00641 strcpy(
00642 interp_variable->name,
00643 attribute
00644 );
00645 }
00646
00647
00648 attribute = mxmlElementGetAttr(
00649 (mxml_node_t*)data_node,
00650 "persistent"
00651 );
00652 if (attribute != NULL) {
00653 agent->datastate->tasks[index]->persistent =
00654 atoi((char*)attribute);
00655 } else {
00656 agent->datastate->tasks[index]->persistent = 0;
00657 }
00658
00659
00660 attribute = mxmlElementGetAttr(
00661 (mxml_node_t*)data_node,
00662 "type"
00663 );
00664 if (attribute != NULL) {
00665 CH_STRING_DATATYPE(
00666 attribute,
00667 interp_variable->data_type
00668 );
00669 CH_DATATYPE_SIZE(
00670 interp_variable->data_type,
00671 data_type_size
00672 );
00673 } else {
00674 interp_variable->data_type =
00675 CH_UNDEFINETYPE;
00676 data_type_size = 0;
00677 }
00678
00679 if (interp_variable->array_dim == 0) {
00680
00681 attribute = mxmlElementGetAttr(
00682 (mxml_node_t*)data_node,
00683 "value" );
00684 if (attribute != NULL && data_type_size != 0) {
00685 interp_variable->data =
00686 malloc(data_type_size);
00687 CH_DATATYPE_STR_TO_VAL(
00688 interp_variable->data_type,
00689 attribute,
00690 interp_variable->data
00691 );
00692 }
00693 } else {
00694
00695 xml_parser->node = xml_get_child(
00696 xml_parser->node,
00697 "ROW",
00698 1
00699 );
00700 agent_xml_parse__row(interp_variable, xml_parser, index);
00701 }
00702 xml_parser->node = data_node;
00703 return MC_SUCCESS;
00704 }
00705
00706
00707
00708 error_code_t
00709 agent_xml_parse__file(agent_p agent, xml_parser_p xml_parser, int index)
00710 {
00711 char* text;
00712 const char* name;
00713 const mxml_node_t* file_node;
00714 agent_file_data_p afd;
00715 if(xml_parser->node == NULL) {
00716 return MC_ERR_PARSE;
00717 }
00718 file_node = xml_parser->node;
00719 text = xml_get_text(file_node);
00720 CHECK_NULL(text, return MC_ERR_PARSE;);
00721
00722
00723 name = mxmlElementGetAttr(
00724 (mxml_node_t*)file_node,
00725 "name"
00726 );
00727 if (name == NULL) {
00728 free(text);
00729 return MC_ERR_PARSE;
00730 }
00731 afd = agent_file_data_NewWithData(name, text);
00732 free(text);
00733 ListWRLock(agent->datastate->tasks[index]->agent_file_list);
00734 ListAdd(
00735 agent->datastate->tasks[index]->agent_file_list,
00736 afd);
00737 ListWRUnlock(agent->datastate->tasks[index]->agent_file_list);
00738 return MC_SUCCESS;
00739 }
00740
00741
00742
00743 error_code_t
00744 agent_xml_parse__row(interpreter_variable_data_t* interp_variable, xml_parser_p xml_parser, int index)
00745 {
00746 int j;
00747 int data_type_size;
00748 int tmp;
00749 int num_elements;
00750 const mxml_node_t* row_node;
00751
00752 if (xml_parser->node == NULL) {
00753 return MC_SUCCESS;
00754 }
00755
00756 if (strcmp(
00757 xml_get_element_name(xml_parser->node),
00758 "ROW" )
00759 )
00760 {
00761 return MC_SUCCESS;
00762 }
00763 row_node = xml_parser->node;
00764
00765
00766
00767 if (interp_variable->array_dim != 0) {
00768 interp_variable->array_extent = (int*)
00769 malloc
00770 (
00771 sizeof(int) *
00772 interp_variable->array_dim
00773 );
00774 tmp = 0;
00775 agent_xml_parse__fill_row_data(NULL,
00776 interp_variable->data_type,
00777 interp_variable->array_extent,
00778 row_node,
00779 &tmp);
00780 num_elements = 1;
00781 for (j = 0; j<interp_variable->array_dim; j++) {
00782 num_elements *= interp_variable->array_extent[j];
00783 }
00784
00785
00786 CH_DATATYPE_SIZE
00787 (
00788 interp_variable->data_type,
00789 data_type_size
00790 );
00791 if (interp_variable->data_type == CH_CHARTYPE) {
00792 num_elements++;
00793 }
00794 interp_variable->data =
00795 malloc(num_elements * data_type_size);
00796
00797
00798 tmp = 0;
00799 agent_xml_parse__fill_row_data(
00800 interp_variable->data,
00801 interp_variable->data_type,
00802 interp_variable->array_extent,
00803 row_node,
00804 &tmp );
00805 } else {
00806 return MC_SUCCESS;
00807 }
00808 return MC_SUCCESS;
00809 }
00810
00811 void agent_xml_parse__fill_row_data(
00812 void *data,
00813 ChType_t type,
00814 int *extent,
00815 const mxml_node_t* node,
00816 int *index)
00817 {
00818 mxml_node_t* tmp_node;
00819 int i=0;
00820 char *buf;
00821 char *tmp;
00822 #ifndef _WIN32
00823 char *saveptr;
00824 #endif
00825 int datasize;
00826
00827
00828
00829 (*extent) = 0;
00830 if (node->child->type == MXML_TEXT) {
00831 node = node->child;
00832
00833 CH_DATATYPE_SIZE(type, datasize);
00834 buf = (char*)malloc(
00835 sizeof(char) +
00836 (strlen(node->value.text.string) + 1));
00837 strcpy(buf, node->value.text.string);
00838
00839 #ifndef _WIN32
00840 tmp = strtok_r(buf, ",", &saveptr);
00841 #else
00842 tmp = strtok(buf, ",");
00843 #endif
00844 while ( tmp != NULL) {
00845 switch(type) {
00846 case CH_CHARTYPE:
00847 if (data != NULL) {
00848 ((char*)data)[*index] = *(char*)tmp;
00849 ((char*)data)[*index+1] = '\0';
00850 }
00851 (*index)++;
00852 break;
00853 case CH_INTTYPE:
00854 if (data != NULL)
00855 ((int*)data)[*index] = strtol(tmp, NULL, 0);
00856 (*index)++;
00857 break;
00858 case CH_UINTTYPE:
00859 if (data != NULL)
00860 ((unsigned int*)data)[*index] = strtoul(tmp, NULL, 0);
00861 (*index)++;
00862 break;
00863 case CH_SHORTTYPE:
00864 if (data != NULL)
00865 ((short*)data)[*index] = (short)strtol(tmp, NULL, 0);
00866 (*index)++;
00867 break;
00868 case CH_USHORTTYPE:
00869 if (data != NULL)
00870 ((unsigned short*)data)[*index] = (unsigned short)strtol(tmp, NULL, 0);
00871 (*index)++;
00872 break;
00873 case CH_FLOATTYPE:
00874 if (data != NULL)
00875 #ifndef _WIN32
00876 ((float*)data)[*index] = strtof(tmp, NULL);
00877 #else
00878 ((float*)data)[*index] = (float)strtod(tmp, NULL);
00879 #endif
00880 (*index)++;
00881 break;
00882 case CH_DOUBLETYPE:
00883 if (data != NULL)
00884 ((double*)data)[*index] = strtod(tmp, NULL);
00885 (*index)++;
00886 break;
00887 default:
00888 fprintf(stderr,
00889 "Unsupported data type: %d %s:%d\n",
00890 type, __FILE__, __LINE__);
00891 }
00892 #ifndef _WIN32
00893 tmp = strtok_r(NULL, ",", &saveptr);
00894 #else
00895 tmp = strtok(NULL, ",");
00896 #endif
00897 (*extent)++;
00898 }
00899 free(buf);
00900 } else if (node->type == MXML_ELEMENT) {
00901 buf = (char*)malloc(sizeof(char)*10);
00902 buf[0] = '\0';
00903 sprintf(buf, "%d", i);
00904 tmp_node = mxmlFindElement(
00905 (mxml_node_t*)node,
00906 (mxml_node_t*)node,
00907 "ROW",
00908 "index",
00909 buf,
00910 MXML_DESCEND_FIRST);
00911 while (tmp_node != NULL) {
00912 (*extent)++;
00913 agent_xml_parse__fill_row_data(data, type,(extent+1), tmp_node, index);
00914 i++;
00915 buf[0] = '\0';
00916 sprintf(buf, "%d", i);
00917 tmp_node = mxmlFindElement(
00918 (mxml_node_t*)node,
00919 (mxml_node_t*)node,
00920 "ROW",
00921 "index",
00922 buf,
00923 MXML_DESCEND_FIRST);
00924 }
00925 free(buf);
00926 }
00927 }
00928
00929
00930
00931 error_code_t
00932 agent_xml_parse__agent_code(agent_p agent, int index, xml_parser_p xml_parser)
00933 {
00934 const char *attribute;
00935 int cur_task = agent->datastate->task_progress;
00936 if( cur_task == agent->datastate->number_of_tasks )
00937 cur_task--;
00938 agent->datastate->agent_codes[index] =
00939 xml_get_text
00940 (
00941 xml_parser->node
00942 );
00943
00944
00945 attribute = mxmlElementGetAttr
00946 (
00947 (mxml_node_t*)xml_parser->node,
00948 "id"
00949 );
00950 if (attribute) {
00951 agent->datastate->agent_code_ids[index] = (char*)malloc
00952 (
00953 sizeof(char) *
00954 (strlen(attribute) + 1)
00955 );
00956 strcpy(agent->datastate->agent_code_ids[index], attribute);
00957 } else {
00958 agent->datastate->agent_code_ids[index] = (char*)malloc(sizeof(char));
00959 *(agent->datastate->agent_code_ids[index]) = '\0';
00960 }
00961 if (agent->datastate->tasks[cur_task]->code_id && attribute != NULL) {
00962 if (!strcmp(attribute, agent->datastate->tasks[cur_task]->code_id)) {
00963 agent->datastate->agent_code = agent->datastate->agent_codes[index];
00964 }
00965 } else {
00966 agent->datastate->agent_code = agent->datastate->agent_codes[0];
00967 }
00968 return MC_SUCCESS;
00969 }
00970
00971
00972 error_code_t
00973 agent_return_xml_parse(agent_p agent)
00974 {
00975 xml_parser_t xml_parser;
00976 xml_parser.root = agent->datastate->xml_root;
00977 xml_parser.node = xml_get_child(
00978 xml_parser.root,
00979 "NAME",
00980 1);
00981
00982 agent_xml_parse__name(agent, &xml_parser);
00983
00984 xml_parser.node = xml_get_child(
00985 xml_parser.root,
00986 "OWNER",
00987 1);
00988
00989 agent_xml_parse__owner(agent, &xml_parser);
00990
00991 xml_parser.node = xml_get_child(
00992 xml_parser.root,
00993 "HOME",
00994 1);
00995
00996 agent_xml_parse__home(agent, &xml_parser);
00997
00998 xml_parser.node = xml_get_child(
00999 xml_parser.root,
01000 "TASK",
01001 1);
01002
01003 agent_xml_parse__tasks(agent, &xml_parser);
01004 return MC_SUCCESS;
01005 }
01006
01007 error_code_t
01008 message_xml_parse(message_p message)
01009 {
01010 error_code_t err_code;
01011 xml_parser_p xml_parser;
01012 xml_parser = (xml_parser_p)malloc(sizeof(xml_parser_t));
01013 xml_parser->root = message->xml_root;
01014 xml_parser->node = mxmlFindElement
01015 (
01016 (mxml_node_t*)xml_parser->root,
01017 (mxml_node_t*)xml_parser->root,
01018 "MOBILEC_MESSAGE",
01019 NULL,
01020 NULL,
01021 MXML_NO_DESCEND
01022 );
01023 if (xml_parser->node == NULL) {
01024 xml_parser->node = mxmlFindElement
01025 (
01026 (mxml_node_t*)xml_parser->root,
01027 (mxml_node_t*)xml_parser->root,
01028 "MOBILEC_MESSAGE",
01029 NULL,
01030 NULL,
01031 MXML_DESCEND
01032 );
01033 }
01034 if (xml_parser->node == NULL) {
01035 err_code = MC_ERR_PARSE;
01036 goto cleanup;
01037 }
01038 xml_parser->root = xml_parser->node;
01039 if(
01040 strcmp(
01041 (const char*)xml_get_element_name(xml_parser->node),
01042 "MOBILEC_MESSAGE"
01043 )
01044 )
01045 {
01046 fprintf(stderr, "Parse error. %s:%d\n", __FILE__, __LINE__);
01047 err_code = MC_ERR_PARSE;
01048 goto cleanup;
01049 }
01050 xml_parser->node = xml_get_child
01051 (
01052 xml_parser->node,
01053 "MESSAGE",
01054 1
01055 );
01056 err_code = message_xml_parse__message(message, xml_parser);
01057 cleanup:
01058 free(xml_parser);
01059 return err_code;
01060 }
01061
01062 error_code_t
01063 message_xml_parse__message(message_p message, xml_parser_p xml_parser)
01064 {
01065 const char* attribute;
01066 char* buf;
01067 char* hostname;
01068 char* port_str;
01069 #ifndef _WIN32
01070 char* save_ptr;
01071 #endif
01072 int port;
01073 if (xml_parser->node == NULL) {
01074 return MC_ERR_PARSE;
01075 }
01076 attribute = mxmlElementGetAttr
01077 (
01078 (mxml_node_t*)xml_parser->node,
01079 "message"
01080 );
01081 if (!strcmp(attribute, "MOBILE_AGENT")) {
01082 message->message_type = MOBILE_AGENT;
01083 message->xml_payload = xml_get_child
01084 (
01085 xml_parser->node,
01086 "MOBILE_AGENT",
01087 1
01088 );
01089 } else if (!strcmp(attribute, "RETURN_MSG")) {
01090 message->message_type = RETURN_MSG;
01091 message->xml_payload = xml_get_child
01092 (
01093 xml_parser->node,
01094 "MOBILE_AGENT",
01095 1
01096 );
01097 } else if (!strcmp(attribute, "ACL")) {
01098 message->message_type = FIPA_ACL;
01099 } else if (!strcmp(attribute, "ENCRYPTION_INITIALIZE")) {
01100 message->message_type = ENCRYPTION_INITIALIZE;
01101 message->xml_payload = xml_get_child
01102 (
01103 xml_parser->node,
01104 "ENCRYPTION_DATA",
01105 1
01106 );
01107 } else if (!strcmp(attribute, "ENCRYPTED_DATA")) {
01108 message->message_type = ENCRYPTED_DATA;
01109 message->xml_payload = xml_get_child
01110 (
01111 xml_parser->node,
01112 "ENCRYPTED_DATA",
01113 1
01114 );
01115 } else if (!strcmp(attribute, "REQUEST_ENCRYPTION_INITIALIZE")) {
01116 message->message_type = REQUEST_ENCRYPTION_INITIALIZE;
01117 } else {
01118 fprintf(stderr, "Parse error. %s:%d\n", __FILE__, __LINE__);
01119 return MC_ERR_PARSE;
01120 }
01121 attribute = mxmlElementGetAttr
01122 (
01123 (mxml_node_t*)xml_parser->node,
01124 "from"
01125 );
01126 if(attribute != NULL) {
01127
01128 if(message->from_address) free(message->from_address);
01129 message->from_address = (char*)malloc
01130 (
01131 sizeof(char) *
01132 (strlen(attribute)+1)
01133 );
01134 CHECK_NULL(message->from_address, exit(0););
01135 strcpy(message->from_address, attribute);
01136 buf = (char*)malloc
01137 (
01138 sizeof(char) *
01139 (strlen(message->from_address)+1)
01140 );
01141 CHECK_NULL(buf, exit(0););
01142 strcpy(buf, message->from_address);
01143 hostname = strtok_r(buf, ":", &save_ptr);
01144 port_str = strtok_r(NULL, ":", &save_ptr);
01145 port = atoi(port_str);
01146 message->addr->sin_port = htons(port);
01147 free(buf);
01148 }
01149 return MC_SUCCESS;
01150 }
01151
01152