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 "complete"
00472 );
00473 CHECK_NULL(attribute, return MC_ERR_PARSE;);
00474 agent->datastate->tasks[index]->task_completed =
00475 atoi((char*)attribute);
00476
00477
00478 attribute = mxmlElementGetAttr(
00479 (mxml_node_t*)task_node,
00480 "server"
00481 );
00482 CHECK_NULL(attribute, return MC_ERR_PARSE;);
00483 agent->datastate->tasks[index]->server_name =
00484 (char*)malloc(sizeof(char) * (strlen(attribute)+1) );
00485 strcpy(
00486 agent->datastate->tasks[index]->server_name,
00487 attribute
00488 );
00489
00490
00491 attribute = mxmlElementGetAttr(
00492 (mxml_node_t*)task_node,
00493 "return" );
00494 if (attribute == NULL) {
00495 agent->datastate->tasks[index]->var_name = strdup("no-return");
00496 } else {
00497 agent->datastate->tasks[index]->var_name = strdup(attribute);
00498 }
00499 CHECK_NULL(agent->datastate->tasks[index]->var_name, exit(1););
00500
00501 return err_code;
00502 }
00503
00504
00505
00506 error_code_t
00507 agent_xml_parse__data(agent_p agent, xml_parser_p xml_parser, int index)
00508 {
00509 const char* attribute;
00510 const char* attribute2;
00511 const mxml_node_t *data_node;
00512 int data_type_size;
00513 interpreter_variable_data_t* interp_variable;
00514 if (xml_parser->node == NULL) {
00515 return MC_ERR_PARSE;
00516 }
00517 if (strcmp(
00518 "DATA",
00519 xml_get_element_name(xml_parser->node) )
00520 )
00521 {
00522 return MC_ERR_PARSE;
00523 }
00524 data_node = xml_parser->node;
00525
00526
00527 attribute = mxmlElementGetAttr(
00528 data_node->parent,
00529 "return" );
00530 attribute2 = mxmlElementGetAttr(
00531 data_node,
00532 "name" );
00533 if (attribute != NULL && !strcmp(attribute, attribute2)) {
00534
00535
00536
00537
00538
00539 agent->datastate->tasks[index]->agent_return_data =
00540 interpreter_variable_data_New();
00541 interp_variable = agent->datastate->tasks[index]->agent_return_data;
00542 } else {
00543 interp_variable = interpreter_variable_data_New();
00544 agent_variable_list_Add(
00545 agent->datastate->tasks[index]->agent_variable_list,
00546 interp_variable );
00547 }
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565 attribute = mxmlElementGetAttr(
00566 (mxml_node_t*)xml_parser->node,
00567 "dim"
00568 );
00569 if (attribute != NULL) {
00570 interp_variable->array_dim =
00571 atoi((char*)attribute);
00572 } else {
00573 interp_variable->array_dim =
00574 0;
00575 }
00576
00577
00578 attribute = mxmlElementGetAttr(
00579 (mxml_node_t*)xml_parser->node,
00580 "name"
00581 );
00582 if (attribute != NULL) {
00583 interp_variable->name =
00584 (char*)malloc(sizeof(char)*(strlen(attribute)+1));
00585 strcpy(
00586 interp_variable->name,
00587 attribute
00588 );
00589 }
00590
00591
00592 attribute = mxmlElementGetAttr(
00593 (mxml_node_t*)data_node,
00594 "persistent"
00595 );
00596 if (attribute != NULL) {
00597 agent->datastate->tasks[index]->persistent =
00598 atoi((char*)attribute);
00599 } else {
00600 agent->datastate->tasks[index]->persistent = 0;
00601 }
00602
00603
00604 attribute = mxmlElementGetAttr(
00605 (mxml_node_t*)data_node,
00606 "type"
00607 );
00608 if (attribute != NULL) {
00609 CH_STRING_DATATYPE(
00610 attribute,
00611 interp_variable->data_type
00612 );
00613 CH_DATATYPE_SIZE(
00614 interp_variable->data_type,
00615 data_type_size
00616 );
00617 } else {
00618 interp_variable->data_type =
00619 CH_UNDEFINETYPE;
00620 data_type_size = 0;
00621 }
00622
00623 if (interp_variable->array_dim == 0) {
00624
00625 attribute = mxmlElementGetAttr(
00626 (mxml_node_t*)data_node,
00627 "value" );
00628 if (attribute != NULL && data_type_size != 0) {
00629 interp_variable->data =
00630 malloc(data_type_size);
00631 CH_DATATYPE_STR_TO_VAL(
00632 interp_variable->data_type,
00633 attribute,
00634 interp_variable->data
00635 );
00636 }
00637 } else {
00638
00639 xml_parser->node = xml_get_child(
00640 xml_parser->node,
00641 "ROW",
00642 1
00643 );
00644 agent_xml_parse__row(interp_variable, xml_parser, index);
00645 }
00646 xml_parser->node = data_node;
00647 return MC_SUCCESS;
00648 }
00649
00650
00651
00652 error_code_t
00653 agent_xml_parse__row(interpreter_variable_data_t* interp_variable, xml_parser_p xml_parser, int index)
00654 {
00655 int j;
00656 int data_type_size;
00657 int tmp;
00658 int num_elements;
00659 const mxml_node_t* row_node;
00660
00661 if (xml_parser->node == NULL) {
00662 return MC_SUCCESS;
00663 }
00664
00665 if (strcmp(
00666 xml_get_element_name(xml_parser->node),
00667 "ROW" )
00668 )
00669 {
00670 return MC_SUCCESS;
00671 }
00672 row_node = xml_parser->node;
00673
00674
00675
00676 if (interp_variable->array_dim != 0) {
00677 interp_variable->array_extent = (int*)
00678 malloc
00679 (
00680 sizeof(int) *
00681 interp_variable->array_dim
00682 );
00683 tmp = 0;
00684 agent_xml_parse__fill_row_data(NULL,
00685 interp_variable->data_type,
00686 interp_variable->array_extent,
00687 row_node,
00688 &tmp);
00689 num_elements = 1;
00690 for (j = 0; j<interp_variable->array_dim; j++) {
00691 num_elements *= interp_variable->array_extent[j];
00692 }
00693
00694
00695 CH_DATATYPE_SIZE
00696 (
00697 interp_variable->data_type,
00698 data_type_size
00699 );
00700 interp_variable->data =
00701 malloc(num_elements * data_type_size);
00702
00703
00704 tmp = 0;
00705 agent_xml_parse__fill_row_data(
00706 interp_variable->data,
00707 interp_variable->data_type,
00708 interp_variable->array_extent,
00709 row_node,
00710 &tmp );
00711 } else {
00712 return MC_SUCCESS;
00713 }
00714 return MC_SUCCESS;
00715 }
00716
00717 void agent_xml_parse__fill_row_data(
00718 void *data,
00719 ChType_t type,
00720 int *extent,
00721 const mxml_node_t* node,
00722 int *index)
00723 {
00724 mxml_node_t* tmp_node;
00725 int i=0;
00726 char *buf;
00727 char *tmp;
00728 #ifndef _WIN32
00729 char *saveptr;
00730 #endif
00731 int datasize;
00732
00733
00734
00735 (*extent) = 0;
00736 if (node->child->type == MXML_TEXT) {
00737 node = node->child;
00738
00739 CH_DATATYPE_SIZE(type, datasize);
00740 buf = (char*)malloc(
00741 sizeof(char) +
00742 (strlen(node->value.text.string) + 1));
00743 strcpy(buf, node->value.text.string);
00744
00745 #ifndef _WIN32
00746 tmp = strtok_r(buf, ",", &saveptr);
00747 #else
00748 tmp = strtok(buf, ",");
00749 #endif
00750 while ( tmp != NULL) {
00751 switch(type) {
00752 case CH_CHARTYPE:
00753 if (data != NULL)
00754 ((char*)data)[*index] = *(char*)tmp;
00755 (*index)++;
00756 break;
00757 case CH_INTTYPE:
00758 if (data != NULL)
00759 ((int*)data)[*index] = strtol(tmp, NULL, 0);
00760 (*index)++;
00761 break;
00762 case CH_UINTTYPE:
00763 if (data != NULL)
00764 ((unsigned int*)data)[*index] = strtoul(tmp, NULL, 0);
00765 (*index)++;
00766 break;
00767 case CH_SHORTTYPE:
00768 if (data != NULL)
00769 ((short*)data)[*index] = (short)strtol(tmp, NULL, 0);
00770 (*index)++;
00771 break;
00772 case CH_USHORTTYPE:
00773 if (data != NULL)
00774 ((unsigned short*)data)[*index] = (unsigned short)strtol(tmp, NULL, 0);
00775 (*index)++;
00776 break;
00777 case CH_FLOATTYPE:
00778 if (data != NULL)
00779 #ifndef _WIN32
00780 ((float*)data)[*index] = strtof(tmp, NULL);
00781 #else
00782 ((float*)data)[*index] = (float)strtod(tmp, NULL);
00783 #endif
00784 (*index)++;
00785 break;
00786 case CH_DOUBLETYPE:
00787 if (data != NULL)
00788 ((double*)data)[*index] = strtod(tmp, NULL);
00789 (*index)++;
00790 break;
00791 default:
00792 fprintf(stderr,
00793 "Unsupported data type: %d %s:%d\n",
00794 type, __FILE__, __LINE__);
00795 }
00796 #ifndef _WIN32
00797 tmp = strtok_r(NULL, ",", &saveptr);
00798 #else
00799 tmp = strtok(NULL, ",");
00800 #endif
00801 (*extent)++;
00802 }
00803 free(buf);
00804 } else if (node->type == MXML_ELEMENT) {
00805 buf = (char*)malloc(sizeof(char)*10);
00806 buf[0] = '\0';
00807 sprintf(buf, "%d", i);
00808 tmp_node = mxmlFindElement(
00809 (mxml_node_t*)node,
00810 (mxml_node_t*)node,
00811 "ROW",
00812 "index",
00813 buf,
00814 MXML_DESCEND_FIRST);
00815 while (tmp_node != NULL) {
00816 (*extent)++;
00817 agent_xml_parse__fill_row_data(data, type,(extent+1), tmp_node, index);
00818 i++;
00819 buf[0] = '\0';
00820 sprintf(buf, "%d", i);
00821 tmp_node = mxmlFindElement(
00822 (mxml_node_t*)node,
00823 (mxml_node_t*)node,
00824 "ROW",
00825 "index",
00826 buf,
00827 MXML_DESCEND_FIRST);
00828 }
00829 free(buf);
00830 }
00831 }
00832
00833
00834
00835 error_code_t
00836 agent_xml_parse__agent_code(agent_p agent, int index, xml_parser_p xml_parser)
00837 {
00838 char *attribute;
00839 int cur_task = agent->datastate->task_progress;
00840 if( cur_task == agent->datastate->number_of_tasks )
00841 cur_task--;
00842 agent->datastate->agent_codes[index] =
00843 xml_get_text
00844 (
00845 xml_parser->node
00846 );
00847
00848
00849 attribute = mxmlElementGetAttr
00850 (
00851 (mxml_node_t*)xml_parser->node,
00852 "id"
00853 );
00854 if (attribute) {
00855 agent->datastate->agent_code_ids[index] = malloc
00856 (
00857 sizeof(char) *
00858 (strlen(attribute) + 1)
00859 );
00860 strcpy(agent->datastate->agent_code_ids[index], attribute);
00861 } else {
00862 agent->datastate->agent_code_ids[index] = malloc(sizeof(char));
00863 *(agent->datastate->agent_code_ids[index]) = '\0';
00864 }
00865 if (agent->datastate->tasks[cur_task]->code_id && attribute != NULL) {
00866 if (!strcmp(attribute, agent->datastate->tasks[cur_task]->code_id)) {
00867 agent->datastate->agent_code = agent->datastate->agent_codes[index];
00868 }
00869 } else {
00870 agent->datastate->agent_code = agent->datastate->agent_codes[0];
00871 }
00872 return MC_SUCCESS;
00873 }
00874
00875
00876 error_code_t
00877 agent_return_xml_parse(agent_p agent)
00878 {
00879 xml_parser_t xml_parser;
00880 xml_parser.root = agent->datastate->xml_root;
00881 xml_parser.node = (const mxml_node_t*)xml_get_child(
00882 xml_parser.root,
00883 "NAME",
00884 1);
00885
00886 agent_xml_parse__name(agent, &xml_parser);
00887
00888 xml_parser.node = (const mxml_node_t*)xml_get_child(
00889 xml_parser.root,
00890 "OWNER",
00891 1);
00892
00893 agent_xml_parse__owner(agent, &xml_parser);
00894
00895 xml_parser.node = (const mxml_node_t*)xml_get_child(
00896 xml_parser.root,
00897 "HOME",
00898 1);
00899
00900 agent_xml_parse__home(agent, &xml_parser);
00901
00902 xml_parser.node = (const mxml_node_t*)xml_get_child(
00903 xml_parser.root,
00904 "TASK",
00905 1);
00906
00907 agent_xml_parse__tasks(agent, &xml_parser);
00908 return MC_SUCCESS;
00909 }
00910
00911 error_code_t
00912 message_xml_parse(message_p message)
00913 {
00914 int err_code;
00915 xml_parser_p xml_parser;
00916 xml_parser = (xml_parser_p)malloc(sizeof(xml_parser_t));
00917 xml_parser->root = message->xml_root;
00918 xml_parser->node = mxmlFindElement
00919 (
00920 (mxml_node_t*)xml_parser->root,
00921 (mxml_node_t*)xml_parser->root,
00922 "MOBILEC_MESSAGE",
00923 NULL,
00924 NULL,
00925 MXML_NO_DESCEND
00926 );
00927 if (xml_parser->node == NULL) {
00928 xml_parser->node = mxmlFindElement
00929 (
00930 (mxml_node_t*)xml_parser->root,
00931 (mxml_node_t*)xml_parser->root,
00932 "MOBILEC_MESSAGE",
00933 NULL,
00934 NULL,
00935 MXML_DESCEND
00936 );
00937 }
00938 if (xml_parser->node == NULL) {
00939 err_code = MC_ERR_PARSE;
00940 goto cleanup;
00941 }
00942 xml_parser->root = xml_parser->node;
00943 if(
00944 strcmp(
00945 (const char*)xml_get_element_name(xml_parser->node),
00946 "MOBILEC_MESSAGE"
00947 )
00948 )
00949 {
00950 fprintf(stderr, "Parse error. %s:%d\n", __FILE__, __LINE__);
00951 err_code = MC_ERR_PARSE;
00952 goto cleanup;
00953 }
00954 xml_parser->node = (const mxml_node_t*)xml_get_child
00955 (
00956 xml_parser->node,
00957 "MESSAGE",
00958 1
00959 );
00960 err_code = message_xml_parse__message(message, xml_parser);
00961 cleanup:
00962 free(xml_parser);
00963 return err_code;
00964 }
00965
00966 error_code_t
00967 message_xml_parse__message(message_p message, xml_parser_p xml_parser)
00968 {
00969 const char* attribute;
00970 char* buf;
00971 char* hostname;
00972 char* port_str;
00973 #ifndef _WIN32
00974 char* save_ptr;
00975 #endif
00976 int port;
00977 if (xml_parser->node == NULL) {
00978 return MC_ERR_PARSE;
00979 }
00980 attribute = mxmlElementGetAttr
00981 (
00982 (mxml_node_t*)xml_parser->node,
00983 "message"
00984 );
00985 if (!strcmp(attribute, "MOBILE_AGENT")) {
00986 message->message_type = MOBILE_AGENT;
00987 message->xml_payload = xml_get_child
00988 (
00989 xml_parser->node,
00990 "MOBILE_AGENT",
00991 1
00992 );
00993 } else if (!strcmp(attribute, "RETURN_MSG")) {
00994 message->message_type = RETURN_MSG;
00995 message->xml_payload = xml_get_child
00996 (
00997 xml_parser->node,
00998 "MOBILE_AGENT",
00999 1
01000 );
01001 } else if (!strcmp(attribute, "ACL")) {
01002 message->message_type = FIPA_ACL;
01003 } else if (!strcmp(attribute, "ENCRYPTION_INITIALIZE")) {
01004 message->message_type = ENCRYPTION_INITIALIZE;
01005 message->xml_payload = xml_get_child
01006 (
01007 xml_parser->node,
01008 "ENCRYPTION_DATA",
01009 1
01010 );
01011 } else if (!strcmp(attribute, "ENCRYPTED_DATA")) {
01012 message->message_type = ENCRYPTED_DATA;
01013 message->xml_payload = xml_get_child
01014 (
01015 xml_parser->node,
01016 "ENCRYPTED_DATA",
01017 1
01018 );
01019 } else if (!strcmp(attribute, "REQUEST_ENCRYPTION_INITIALIZE")) {
01020 message->message_type = REQUEST_ENCRYPTION_INITIALIZE;
01021 } else {
01022 fprintf(stderr, "Parse error. %s:%d\n", __FILE__, __LINE__);
01023 return MC_ERR_PARSE;
01024 }
01025 attribute = mxmlElementGetAttr
01026 (
01027 (mxml_node_t*)xml_parser->node,
01028 "from"
01029 );
01030 if(attribute != NULL) {
01031
01032 if(message->from_address) free(message->from_address);
01033 message->from_address = (char*)malloc
01034 (
01035 sizeof(char) *
01036 (strlen(attribute)+1)
01037 );
01038 CHECK_NULL(message->from_address, exit(0););
01039 strcpy(message->from_address, attribute);
01040 buf = (char*)malloc
01041 (
01042 sizeof(char) *
01043 (strlen(message->from_address)+1)
01044 );
01045 CHECK_NULL(buf, exit(0););
01046 strcpy(buf, message->from_address);
01047 hostname = strtok_r(buf, ":", &save_ptr);
01048 port_str = strtok_r(NULL, ":", &save_ptr);
01049 port = atoi(port_str);
01050 message->addr->sin_port = htons(port);
01051 free(buf);
01052 }
01053 return MC_SUCCESS;
01054 }
01055
01056