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