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