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