/home/dko/projects/mobilec/trunk/src/xml_parser.c

Go to the documentation of this file.
00001 /*[
00002  * Copyright (c) 2007 Integration Engineering Laboratory
00003                       University of California, Davis
00004  *
00005  * Permission to use, copy, and distribute this software and its
00006  * documentation for any purpose with or without fee is hereby granted,
00007  * provided that the above copyright notice appear in all copies and
00008  * that both that copyright notice and this permission notice appear
00009  * in supporting documentation.
00010  *
00011  * Permission to modify the software is granted, but not the right to
00012  * distribute the complete modified source code.  Modifications are to
00013  * be distributed as patches to the released version.  Permission to
00014  * distribute binaries produced by compiling modified sources is granted,
00015  * provided you
00016  *   1. distribute the corresponding source modifications from the
00017  *    released version in the form of a patch file along with the binaries,
00018  *   2. add special version identification to distinguish your version
00019  *    in addition to the base release version number,
00020  *   3. provide your name and address as the primary contact for the
00021  *    support of your modified version, and
00022  *   4. retain our contact information in regard to use of the base
00023  *    software.
00024  * Permission to distribute the released version of the source code along
00025  * with corresponding source modifications in the form of a patch file is
00026  * granted with same provisions 2 through 4 for binary distributions.
00027  *
00028  * This software is provided "as is" without express or implied warranty
00029  * to the extent permitted by applicable law.
00030 ]*/
00031 
00032 #include <mxml.h>
00033 #include <string.h>
00034 #define _XOPEN_SOURCE 600
00035 #include <stdlib.h>
00036 #ifndef _WIN32
00037 #include "config.h"
00038 #else
00039 #include "winconfig.h"
00040 #endif
00041 #include "include/agent_return_data.h"
00042 #include "include/message.h"
00043 #include "include/xml_parser.h"
00044 #include "include/xml_helper.h"
00045 /* *** */
00046 /* agent_xml_parse */
00047 error_code_t agent_xml_parse(agent_p agent)
00048 {
00049   xml_parser_t xml_parser;
00050   xml_parser.root = agent->datastate->xml_agent_root;
00051   xml_parser.node = xml_parser.root;
00052   agent_xml_parse__mobile_agent(agent, &xml_parser);
00053   return MC_SUCCESS;
00054 }
00055 
00056 /* *** */
00057 /* agent_xml_parse__gaf_message */
00058 error_code_t 
00059 agent_xml_parse__mobile_agent
00060 (
00061  agent_p agent, 
00062  xml_parser_p xml_parser
00063  )
00064 {
00065   /* make sure this is the 'MOBILE_AGENT' tag */
00066   if ( 
00067       strcmp(
00068         (const char*)xml_get_element_name(xml_parser->node),
00069         "MOBILE_AGENT"
00070         )
00071      )
00072   {
00073     return MC_ERR_PARSE;
00074   }
00075   /* There is only one child node: AGENT_DATA*/
00076   xml_parser->node = (const mxml_node_t*)xml_get_child(
00077       xml_parser->node,
00078       "AGENT_DATA",
00079       1);
00080 
00081   return agent_xml_parse__agent_data(agent, xml_parser);
00082 }
00083 
00084 /* *** */
00085 /* agent_xml_parse__message */
00086 error_code_t
00087 agent_xml_parse__agent_data
00088 (
00089  agent_p agent,
00090  xml_parser_p xml_parser
00091  )
00092 {
00093   const mxml_node_t* agent_data_node;
00094   error_code_t err_code;
00095   
00096   if (xml_parser->node == NULL) {
00097     return MC_ERR_PARSE;
00098   }
00099 
00100   agent_data_node = xml_parser->node;
00101 
00102   xml_parser->node = xml_get_child(
00103       agent_data_node,
00104       "NAME", 
00105       1
00106       );
00107   if ( (err_code = agent_xml_parse__name(agent, xml_parser)) ) {
00108     return err_code;
00109   }
00110 
00111   xml_parser->node = xml_get_child(
00112       agent_data_node,
00113       "OWNER",
00114       1
00115       );
00116   if ( (err_code = agent_xml_parse__owner(agent, xml_parser)) ) {
00117     return err_code;
00118   }
00119 
00120   xml_parser->node = xml_get_child(
00121       agent_data_node,
00122       "HOME",
00123       1
00124       );
00125   if ( (err_code = agent_xml_parse__home(agent, xml_parser)) ) {
00126     return err_code;
00127   }
00128 
00129   xml_parser->node = xml_get_child(
00130       agent_data_node,
00131       "TASK",
00132       1
00133       );
00134   if ( (err_code = agent_xml_parse__task(agent, xml_parser)) ) {
00135     return err_code;
00136   }
00137   return MC_SUCCESS;
00138 }
00139 
00140 /* *** */
00141 /* agent_xml_parse__name */
00142 error_code_t
00143 agent_xml_parse__name(agent_p agent, xml_parser_p xml_parser)
00144 {
00145   char* text;
00146   const mxml_node_t* name_node;
00147   if (xml_parser->node == NULL) {
00148     return MC_ERR_PARSE;
00149   }
00150   name_node = xml_parser->node;
00151 
00152   text = xml_get_text( name_node );
00153   CHECK_NULL(text, return MC_ERR_PARSE;);
00154 
00155   agent->name = (char*)malloc(
00156       sizeof(char)*(strlen(text)+1)
00157       );
00158   strcpy(
00159       agent->name,
00160       text
00161       );
00162   free(text);
00163   return MC_SUCCESS;
00164 }
00165 
00166 /* *** */
00167 /* agent_xml_parse__owner */
00168 error_code_t
00169 agent_xml_parse__owner(agent_p agent, xml_parser_p xml_parser)
00170 {
00171   char *text;
00172   const mxml_node_t* owner_node;
00173   if (xml_parser->node == NULL) {
00174     /* It's ok if there is no owner node: It is not a required field. */
00175     agent->owner = NULL;
00176     return MC_SUCCESS;
00177   }
00178   owner_node = xml_parser->node;
00179 
00180   text = xml_get_text( owner_node );
00181   CHECK_NULL(text, agent->owner=NULL;return MC_SUCCESS;);
00182   agent->owner = (char*)malloc(
00183       sizeof(char)*(strlen(text)+1)
00184       );
00185   strcpy(
00186       agent->owner,
00187       text
00188       );
00189   free(text);
00190   return MC_SUCCESS;
00191 }
00192 
00193 /* *** */
00194 /* agent_xml_parse__home */
00195 error_code_t
00196 agent_xml_parse__home(agent_p agent, xml_parser_p xml_parser)
00197 {
00198   char *text;
00199   const mxml_node_t* home_node;
00200   if (xml_parser->node == NULL) {
00201     /* It's ok if there is no home node: It is not a required field. */
00202     agent->home= NULL;
00203     return MC_SUCCESS;
00204   }
00205   home_node = xml_parser->node;
00206   text = xml_get_text( home_node );
00207   CHECK_NULL(text, agent->home=NULL;return MC_SUCCESS;);
00208   agent->home = (char*)malloc(
00209       sizeof(char)*(strlen(text)+1)
00210       );
00211   strcpy(
00212       agent->home,
00213       text
00214       );
00215   free(text);
00216   return MC_SUCCESS;
00217 }
00218 
00219 /* *** */
00220 /* agent_xml_parse__task */
00221 error_code_t
00222 agent_xml_parse__task(agent_p agent, xml_parser_p xml_parser)
00223 {
00224   const char* attribute;
00225   const mxml_node_t* task_node;
00226   int i;
00227   int task_num;
00228   int code_num = 0;
00229   error_code_t err_code = MC_SUCCESS;
00230   CHECK_NULL(xml_parser->node, return MC_ERR_PARSE;);
00231   task_node = xml_parser->node;
00232   /* parse the 'task' attribute */
00233   attribute = mxmlElementGetAttr(
00234       (mxml_node_t*)task_node,
00235       "task"
00236       );
00237   if (attribute == NULL) {
00238     agent->datastate->number_of_tasks = 1;
00239   } else {
00240     agent->datastate->number_of_tasks = atoi((char*)attribute);
00241   }
00242   agent->datastate->tasks = (agent_task_p*)malloc(
00243       sizeof(agent_task_p) * agent->datastate->number_of_tasks
00244       );
00245 
00246   /* Allocate each task */
00247   for(i = 0; i<agent->datastate->number_of_tasks; i++) {
00248     agent->datastate->tasks[i] = (agent_task_p)malloc
00249       (
00250        sizeof(agent_task_t)
00251       );
00252   }
00253 
00254   /* parse the 'num' attribute */
00255   attribute = mxmlElementGetAttr(
00256       (mxml_node_t*)task_node,
00257       "num"
00258       );
00259   if (attribute == NULL) {
00260     agent->datastate->task_progress = 0;
00261   } else {
00262     agent->datastate->task_progress = atoi((char*)attribute);
00263   }
00264 
00265   /* Parse the multiple DATA nodes */
00266   for (i = 0; i < agent->datastate->number_of_tasks; i++) {
00267     if (i == 0) {
00268       xml_parser->node = xml_get_child(
00269           task_node,
00270           "DATA",
00271           1
00272           );
00273     }
00274     if(i != 0) {
00275       xml_parser->node = xml_get_next_element(xml_parser->node);
00276     }
00277     if ((err_code = agent_xml_parse__data(agent, xml_parser, i)))
00278     {
00279       return err_code;
00280     }
00281   }
00282 
00283   /* Need to get all of the agent codes. Even though we may execute only
00284    * one right now, in the future, the agent may decide at runtime which block
00285    * to execute, so we need them all. */
00286   xml_parser->node = mxmlFindElement
00287       (
00288        task_node,
00289        task_node,
00290        "AGENT_CODE",
00291        NULL,
00292        NULL,
00293        MXML_DESCEND
00294       );
00295 
00296   /* First we count the number of code blocks */
00297   while(xml_parser->node != NULL) {
00298     code_num++;
00299     xml_parser->node = mxmlFindElement
00300       (
00301        xml_parser->node,
00302        task_node,
00303        "AGENT_CODE",
00304        NULL,
00305        NULL,
00306        MXML_NO_DESCEND
00307       );
00308   }
00309 
00310   /* Allocate correct amount of memory for code blocks. */
00311   agent->datastate->agent_code_ids = (char**)malloc
00312     (
00313      (code_num+1) * sizeof(char*)
00314     );
00315   agent->datastate->agent_codes = (char**)malloc
00316     (
00317      (code_num+1) * sizeof(char*)
00318     );
00319   /* Set sigil */
00320   agent->datastate->agent_code_ids[code_num] = NULL;
00321   agent->datastate->agent_codes[code_num] = NULL;
00322 
00323   /* Go through all code again and parse */
00324   xml_parser->node = mxmlFindElement
00325       (
00326        task_node,
00327        task_node,
00328        "AGENT_CODE",
00329        NULL,
00330        NULL,
00331        MXML_DESCEND
00332       );
00333   i = 0;
00334   while(xml_parser->node != NULL) {
00335     err_code = agent_xml_parse__agent_code(agent, i, xml_parser);
00336     i++;
00337     xml_parser->node = mxmlFindElement
00338       (
00339        xml_parser->node,
00340        task_node,
00341        "AGENT_CODE",
00342        NULL,
00343        NULL,
00344        MXML_NO_DESCEND
00345       );
00346   }
00347 
00348   if (agent->datastate->agent_code == NULL) {
00349     /* Something is wrong. */
00350     fprintf(stderr, "Parse error: Agent code not found. %s:%d\n", __FILE__, __LINE__);
00351     return MC_ERR_PARSE;
00352   }
00353 
00354   return err_code;
00355 }
00356 
00357 /* *** */
00358 /* agent_xml_parse__data */
00359 error_code_t
00360 agent_xml_parse__data(agent_p agent, xml_parser_p xml_parser, int index)
00361 {
00362   const char* attribute;
00363   const mxml_node_t *data_node;
00364   int data_type_size;
00365   if (xml_parser->node == NULL) {
00366     return MC_ERR_PARSE;
00367   }
00368   if (strcmp(
00369         "DATA",
00370         xml_get_element_name(xml_parser->node) )
00371      )
00372   {
00373     return MC_ERR_PARSE;
00374   }
00375   data_node = xml_parser->node;
00376 
00377   /* Allocate Return data structure */
00378   /* FIXME: This may not be the right place to do this,
00379    * but it is safe and leak free. */
00380   agent->datastate->tasks[index]->agent_return_data = 
00381      agent_return_data_New(); 
00382 
00383 
00384   /* Attributes we need to parse:
00385    * complete       - is the task complete?
00386    * server         - The host to perform the task
00387    * O dim          - dimension of the return data
00388    * O name         - name of the return variable
00389    * O persistent   - is the agent persistent for this task?
00390    * O type         - return variable type
00391    * O return_value - return value, if not an array
00392    * O code_id      - ID of the code block to execute. If this
00393    *                  attribute is missing, execute the first 
00394    *                  code block available.
00395    *
00396    * 'O' denotes optional attribute. */
00397 
00398   /* 'complete' */
00399   attribute = mxmlElementGetAttr(
00400       (mxml_node_t*)data_node,
00401       "complete"
00402       );
00403   CHECK_NULL(attribute, return MC_ERR_PARSE;);
00404   agent->datastate->tasks[index]->task_completed = 
00405     atoi((char*)attribute);
00406   
00407   /* 'server' */
00408   attribute = mxmlElementGetAttr(
00409       (mxml_node_t*)data_node,
00410       "server"
00411       );
00412   CHECK_NULL(attribute, return MC_ERR_PARSE;);
00413   agent->datastate->tasks[index]->server_name = 
00414     (char*)malloc(sizeof(char) * (strlen(attribute)+1) );
00415   strcpy(
00416       agent->datastate->tasks[index]->server_name,
00417       attribute
00418       );
00419 
00420   /* 'dim' */
00421   attribute = mxmlElementGetAttr(
00422       (mxml_node_t*)xml_parser->node,
00423       "dim"
00424       );
00425   if (attribute != NULL) {
00426     agent->datastate->tasks[index]->agent_return_data->array_dim = 
00427       atoi((char*)attribute);
00428   } else {
00429     agent->datastate->tasks[index]->agent_return_data->array_dim = 
00430       0;
00431   }
00432 
00433   /* 'name' */
00434   attribute = mxmlElementGetAttr(
00435       (mxml_node_t*)xml_parser->node,
00436       "name"
00437       );
00438   if (attribute != NULL) {
00439     agent->datastate->tasks[index]->var_name = 
00440       (char*)malloc(sizeof(char)*(strlen(attribute)+1));
00441     strcpy(
00442         agent->datastate->tasks[index]->var_name,
00443         attribute
00444         );
00445   } else {
00446     agent->datastate->tasks[index]->var_name = 
00447       (char*)malloc(sizeof(char)*(strlen("no-return")+1));
00448     strcpy(
00449         agent->datastate->tasks[index]->var_name,
00450         "no-return"
00451         );
00452   }
00453 
00454   /* 'persistent' */
00455   attribute = mxmlElementGetAttr(
00456       (mxml_node_t*)data_node,
00457       "persistent"
00458       );
00459   if (attribute != NULL) {
00460     agent->datastate->tasks[index]->persistent = 
00461       atoi((char*)attribute);
00462   } else {
00463     agent->datastate->tasks[index]->persistent = 0;
00464   }
00465 
00466   /* 'type' */
00467   attribute = mxmlElementGetAttr(
00468       (mxml_node_t*)data_node,
00469       "type"
00470       );
00471   if (attribute != NULL) {
00472     CH_STRING_DATATYPE(
00473         attribute,
00474         agent->datastate->tasks[index]->agent_return_data->data_type
00475         );
00476     CH_DATATYPE_SIZE(
00477         agent->datastate->tasks[index]->agent_return_data->data_type,
00478         data_type_size
00479         );
00480   } else {
00481     agent->datastate->tasks[index]->agent_return_data->data_type = 
00482       CH_UNDEFINETYPE;
00483     data_type_size = 0;
00484   }
00485 
00486   if (agent->datastate->tasks[index]->agent_return_data->array_dim == 0) {
00487   /* 'return_value' */
00488     attribute = mxmlElementGetAttr(
00489         (mxml_node_t*)data_node,
00490         "return_value" );
00491     if (attribute != NULL && data_type_size != 0) {
00492       agent->datastate->tasks[index]->agent_return_data->return_data =
00493         malloc(data_type_size);
00494       CH_DATATYPE_STR_TO_VAL(
00495           agent->datastate->tasks[index]->agent_return_data->data_type,
00496           attribute,
00497           agent->datastate->tasks[index]->agent_return_data->return_data
00498           );
00499     }
00500   } else {
00501     /* The only possible child node to parse are row nodes. */
00502     xml_parser->node = xml_get_child(
00503         xml_parser->node,
00504         "ROW",
00505         1
00506         );
00507     agent_xml_parse__row(agent, xml_parser, index);
00508   }
00509   /* 'code_id' */
00510   attribute = mxmlElementGetAttr(
00511       (mxml_node_t*)data_node,
00512       "code_id");
00513   if (attribute != NULL) {
00514     agent->datastate->tasks[index]->code_id = malloc
00515       (
00516        sizeof(char) * 
00517        (strlen(attribute) + 1)
00518       );
00519     strcpy(agent->datastate->tasks[index]->code_id, attribute);
00520   } else {
00521     agent->datastate->tasks[index]->code_id = NULL;
00522   }
00523   xml_parser->node = data_node;
00524   return MC_SUCCESS;
00525 }
00526 
00527 /* *** */
00528 /* agent_xml_parse__row */
00529 error_code_t
00530 agent_xml_parse__row(agent_p agent, xml_parser_p xml_parser, int index)
00531 {
00532   int j;
00533   int data_type_size;
00534   int tmp;
00535   int num_elements;
00536   agent_task_p task;
00537   const mxml_node_t* row_node;
00538 
00539   if (xml_parser->node == NULL) {
00540     return MC_SUCCESS;
00541   }
00542 
00543   if (strcmp(
00544         xml_get_element_name(xml_parser->node),
00545         "ROW" )
00546      )
00547   {
00548     return MC_SUCCESS;
00549   }
00550   row_node = xml_parser->node;
00551 
00552   task = agent->datastate->tasks[index];
00553   /* malloc mem for the task data elements */
00554   /* First, find the extents of the dimensions */
00555   if (task->agent_return_data->array_dim != 0) {
00556     task->agent_return_data->array_extent = (int*)
00557       malloc
00558       (
00559        sizeof(int) * 
00560        task->agent_return_data->array_dim
00561       );
00562     tmp = 0;
00563     agent_xml_parse__fill_row_data(NULL,
00564         task->agent_return_data->data_type,
00565         task->agent_return_data->array_extent,
00566         row_node,
00567         &tmp);
00568     num_elements = 1;
00569     for (j = 0; j<task->agent_return_data->array_dim; j++) {
00570       num_elements *= task->agent_return_data->array_extent[j];
00571     }
00572 
00573     /* Allocate space for the return data */
00574     CH_DATATYPE_SIZE
00575       (
00576        task->agent_return_data->data_type,
00577        data_type_size
00578       );
00579     task->agent_return_data->return_data =
00580       malloc(num_elements * data_type_size);
00581 
00582     /* Get the data */
00583     tmp = 0;
00584     agent_xml_parse__fill_row_data(
00585         task->agent_return_data->return_data,
00586         task->agent_return_data->data_type,
00587         task->agent_return_data->array_extent,
00588         row_node,
00589         &tmp );
00590   } else { 
00591     return MC_SUCCESS;
00592   }
00593   return MC_SUCCESS;
00594 }
00595 
00596 void agent_xml_parse__fill_row_data(
00597         void *data,
00598         ChType_t type,
00599         int *extent,
00600         const mxml_node_t* node,
00601         int *index) 
00602 {
00603   mxml_node_t* tmp_node;
00604   int i=0;
00605   char *buf;
00606   char *tmp;
00607 #ifndef _WIN32
00608   char *saveptr;
00609 #endif
00610   int datasize;
00611   /* Check to see if the child is an element or text. All children must be
00612    * either an element or text. If it is text, that means we are at the very bottom 
00613    * and we need to retrive data. */
00614   (*extent) = 0; 
00615   if (node->child->type == MXML_TEXT) {
00616     node = node->child;
00617     /* Now we parse the data */
00618     CH_DATATYPE_SIZE(type, datasize);
00619     buf = (char*)malloc(
00620         sizeof(char) +
00621         (strlen(node->value.text.string) + 1));
00622     strcpy(buf, node->value.text.string);
00623     /* Tokenize by commas */
00624 #ifndef _WIN32
00625     tmp = strtok_r(buf, ",", &saveptr);
00626 #else
00627     tmp = strtok(buf, ",");
00628 #endif
00629     while ( tmp != NULL) {
00630       switch(type) {
00631         case CH_INTTYPE:
00632           if (data != NULL)
00633             ((int*)data)[*index] = strtol(tmp, NULL, 0);
00634           (*index)++;
00635           break;
00636         case CH_UINTTYPE:
00637           if (data != NULL)
00638             ((unsigned int*)data)[*index] = strtoul(tmp, NULL, 0);
00639           (*index)++;
00640           break;
00641         case CH_SHORTTYPE:
00642           if (data != NULL)
00643             ((short*)data)[*index] = (short)strtol(tmp, NULL, 0);
00644           (*index)++;
00645           break;
00646         case CH_USHORTTYPE:
00647           if (data != NULL)
00648             ((unsigned short*)data)[*index] = (unsigned short)strtol(tmp, NULL, 0);
00649           (*index)++;
00650           break;
00651         case CH_FLOATTYPE:
00652           if (data != NULL)
00653 #ifndef _WIN32
00654             ((float*)data)[*index] = strtof(tmp, NULL);
00655 #else   
00656           ((float*)data)[*index] = (float)strtod(tmp, NULL);
00657 #endif
00658           (*index)++;
00659           break;
00660         case CH_DOUBLETYPE:
00661           if (data != NULL)
00662             ((double*)data)[*index] = strtod(tmp, NULL);
00663           (*index)++;
00664           break;
00665         default:
00666           fprintf(stderr, 
00667               "Unsupported data type: %d %s:%d\n",
00668               type, __FILE__, __LINE__);
00669       }
00670 #ifndef _WIN32
00671       tmp = strtok_r(NULL, ",", &saveptr);
00672 #else
00673       tmp = strtok(NULL, ",");
00674 #endif
00675       (*extent)++;
00676     }
00677     free(buf);
00678   } else if (node->type == MXML_ELEMENT) {
00679     buf = (char*)malloc(sizeof(char)*10);
00680     buf[0] = '\0';
00681     sprintf(buf, "%d", i);
00682     tmp_node = mxmlFindElement(
00683         (mxml_node_t*)node,
00684         (mxml_node_t*)node,
00685         "ROW",
00686         "index",
00687         buf,
00688         MXML_DESCEND_FIRST);
00689     while (tmp_node != NULL) {
00690       (*extent)++;
00691       agent_xml_parse__fill_row_data(data, type,(extent+1), tmp_node, index);
00692       i++;
00693       buf[0] = '\0';
00694       sprintf(buf, "%d", i);
00695       tmp_node = mxmlFindElement(
00696           (mxml_node_t*)node,
00697           (mxml_node_t*)node,
00698           "ROW",
00699           "index",
00700           buf,
00701           MXML_DESCEND_FIRST);
00702     }
00703     free(buf);
00704   }
00705 }
00706 
00707 /* *** */
00708 /* agent_xml_parse__agent_code */
00709 error_code_t
00710 agent_xml_parse__agent_code(agent_p agent, int index, xml_parser_p xml_parser)
00711 {
00712   char *attribute;
00713   int cur_task = agent->datastate->task_progress;
00714   if( cur_task == agent->datastate->number_of_tasks )
00715     cur_task--;
00716   agent->datastate->agent_codes[index] = 
00717     xml_get_text
00718     (
00719      xml_parser->node
00720     );
00721 
00722   /* Get the code id */
00723   attribute = mxmlElementGetAttr
00724     (
00725      (mxml_node_t*)xml_parser->node,
00726      "id"
00727     );
00728   if (attribute) {
00729     agent->datastate->agent_code_ids[index] = malloc
00730       (
00731        sizeof(char) * 
00732        (strlen(attribute) + 1)
00733       );
00734     strcpy(agent->datastate->agent_code_ids[index], attribute);
00735   } else {
00736     agent->datastate->agent_code_ids[index] = malloc(sizeof(char));
00737     *(agent->datastate->agent_code_ids[index]) = '\0';
00738   }
00739   if (agent->datastate->tasks[cur_task]->code_id && attribute != NULL) {
00740     if (!strcmp(attribute, agent->datastate->tasks[cur_task]->code_id)) {
00741       agent->datastate->agent_code = agent->datastate->agent_codes[index];
00742     }
00743   } else {
00744     agent->datastate->agent_code = agent->datastate->agent_codes[0];
00745   }
00746   return MC_SUCCESS;
00747 }
00748 
00749 /* agent return parsing ******************************************************/
00750 error_code_t
00751 agent_return_xml_parse(agent_p agent)
00752 {
00753   xml_parser_t xml_parser;
00754   xml_parser.root = agent->datastate->xml_root;
00755   xml_parser.node = (const mxml_node_t*)xml_get_child(
00756       xml_parser.root,
00757       "NAME",
00758       1);
00759       
00760   agent_xml_parse__name(agent, &xml_parser);
00761 
00762   xml_parser.node = (const mxml_node_t*)xml_get_child(
00763       xml_parser.root,
00764       "OWNER",
00765       1);
00766 
00767   agent_xml_parse__owner(agent, &xml_parser);
00768 
00769   xml_parser.node = (const mxml_node_t*)xml_get_child(
00770       xml_parser.root,
00771       "HOME",
00772       1);
00773 
00774   agent_xml_parse__home(agent, &xml_parser);
00775 
00776   xml_parser.node = (const mxml_node_t*)xml_get_child(
00777       xml_parser.root,
00778       "TASK",
00779       1);
00780 
00781   agent_xml_parse__task(agent, &xml_parser);
00782   return MC_SUCCESS;
00783 }
00784 /* message parsing  **********************************************************/
00785 error_code_t
00786 message_xml_parse(message_p message)
00787 {
00788   int err_code;
00789   xml_parser_p xml_parser;
00790   xml_parser = (xml_parser_p)malloc(sizeof(xml_parser_t));
00791   xml_parser->root = message->xml_root;
00792   xml_parser->node = mxmlFindElement
00793     (
00794      (mxml_node_t*)xml_parser->root,
00795      (mxml_node_t*)xml_parser->root,
00796      "GAF_MESSAGE",
00797      NULL,
00798      NULL,
00799      MXML_NO_DESCEND
00800     );
00801   if (xml_parser->node == NULL) {
00802     xml_parser->node = mxmlFindElement
00803       (
00804        (mxml_node_t*)xml_parser->root,
00805        (mxml_node_t*)xml_parser->root,
00806        "GAF_MESSAGE",
00807        NULL,
00808        NULL,
00809        MXML_DESCEND
00810       );
00811   }
00812   if (xml_parser->node == NULL) {
00813     err_code = MC_ERR_PARSE;
00814     goto cleanup;
00815   }
00816   xml_parser->root = xml_parser->node;
00817   if(
00818       strcmp(
00819         (const char*)xml_get_element_name(xml_parser->node),
00820         "GAF_MESSAGE"
00821         )
00822     )
00823   {
00824     fprintf(stderr, "Parse error. %s:%d\n", __FILE__, __LINE__);
00825     err_code = MC_ERR_PARSE;
00826     goto cleanup;
00827   }
00828   xml_parser->node = (const mxml_node_t*)xml_get_child
00829     (
00830      xml_parser->node,
00831      "MESSAGE",
00832      1
00833     );
00834   err_code = message_xml_parse__message(message, xml_parser);
00835 cleanup:
00836   free(xml_parser);
00837   return err_code;
00838 }
00839 
00840 error_code_t
00841 message_xml_parse__message(message_p message, xml_parser_p xml_parser)
00842 {
00843   const char* attribute;
00844   char* buf;
00845   char* hostname;
00846   char* port_str;
00847 #ifndef _WIN32
00848   char* save_ptr; /* Save ptr for re-entrant strtok */
00849 #endif
00850   int port;
00851   if (xml_parser->node == NULL) {
00852     return MC_ERR_PARSE;
00853   }
00854   attribute = mxmlElementGetAttr
00855     (
00856      (mxml_node_t*)xml_parser->node,
00857      "message"
00858     );
00859   if (!strcmp(attribute, "MOBILE_AGENT")) {
00860     message->message_type = MOBILE_AGENT;
00861     message->xml_payload = xml_get_child
00862       (
00863        xml_parser->node,
00864        "MOBILE_AGENT",
00865        1
00866       );
00867   } else if (!strcmp(attribute, "RETURN_MSG")) {
00868     message->message_type = RETURN_MSG;
00869     message->xml_payload = xml_get_child
00870       (
00871        xml_parser->node,
00872        "MOBILE_AGENT",
00873        1
00874       );
00875   } else if (!strcmp(attribute, "ACL")) {
00876     message->message_type = FIPA_ACL;
00877   } else if (!strcmp(attribute, "ENCRYPTION_INITIALIZE")) {
00878     message->message_type = ENCRYPTION_INITIALIZE;
00879     message->xml_payload = xml_get_child
00880       (
00881        xml_parser->node,
00882        "ENCRYPTION_DATA",
00883        1
00884       );
00885   } else if (!strcmp(attribute, "ENCRYPTED_DATA")) {
00886     message->message_type = ENCRYPTED_DATA;
00887     message->xml_payload = xml_get_child
00888       (
00889        xml_parser->node,
00890        "ENCRYPTED_DATA",
00891        1
00892       );
00893   } else if (!strcmp(attribute, "REQUEST_ENCRYPTION_INITIALIZE")) {
00894     message->message_type = REQUEST_ENCRYPTION_INITIALIZE;
00895   } else {
00896     fprintf(stderr, "Parse error. %s:%d\n", __FILE__, __LINE__);
00897     return MC_ERR_PARSE;
00898   }
00899   attribute = mxmlElementGetAttr
00900     (
00901      (mxml_node_t*)xml_parser->node,
00902      "from"
00903     );
00904   if(attribute != NULL) {
00905     /* Free 'from_address' first, if we need. */
00906     if(message->from_address) free(message->from_address);
00907     message->from_address = (char*)malloc
00908       (
00909        sizeof(char) * 
00910        (strlen(attribute)+1)
00911       );
00912     CHECK_NULL(message->from_address, exit(0););
00913     strcpy(message->from_address, attribute);
00914     buf = (char*)malloc
00915       (
00916        sizeof(char) * 
00917        (strlen(message->from_address)+1)
00918       );
00919     CHECK_NULL(buf, exit(0););
00920     strcpy(buf, message->from_address);
00921     hostname = strtok_r(buf, ":", &save_ptr);
00922     port_str = strtok_r(NULL, ":", &save_ptr);
00923     port = atoi(port_str);
00924     message->addr->sin_port = htons(port);
00925     free(buf);
00926   }
00927   return MC_SUCCESS;
00928 }
00929   
00930 

Generated on Fri May 16 14:49:55 2008 for Mobile-C by  doxygen 1.5.4