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