/home/dko/projects/mobilec/trunk/src/xml_compose.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 "include/agent.h"
00034 #include "include/xml_compose.h"
00035 #include "include/xml_helper.h"
00036 
00037 mxml_node_t*
00038 agent_xml_compose(agent_p agent)
00039 {
00040   mxml_node_t* node;
00041   node = mxmlLoadString
00042     (
00043      NULL,
00044      "<?xml version=\"1.0\"?>\n<!DOCTYPE myMessage SYSTEM \"gafmessage.dtd\">",
00045      MXML_NO_CALLBACK
00046     );
00047   mxmlAdd
00048     (
00049      node,
00050      MXML_ADD_AFTER,
00051      MXML_ADD_TO_PARENT,
00052      agent_xml_compose__gaf_message(agent)
00053     );
00054   return node;
00055 }
00056 
00057   mxml_node_t*
00058 agent_xml_compose__gaf_message(agent_p agent)
00059 {
00060   mxml_node_t* node;
00061   node = mxmlNewElement
00062     (
00063      NULL,
00064      "GAF_MESSAGE"
00065     );
00066   mxmlAdd
00067     (
00068      node,
00069      MXML_ADD_AFTER,
00070      NULL,
00071      agent_xml_compose__message(agent)
00072     );
00073   return node;
00074 }
00075 
00076   mxml_node_t*
00077 agent_xml_compose__message(agent_p agent)
00078 {
00079   mxml_node_t* node;
00080   node = mxmlNewElement
00081     (
00082      NULL,
00083      "MESSAGE"
00084     );
00085 
00086   if(
00087       agent->agent_type == MC_REMOTE_AGENT ||
00088       agent->agent_type == MC_LOCAL_AGENT 
00089     )
00090   {
00091     mxmlElementSetAttr
00092       (
00093        node,
00094        "message",
00095        "MOBILE_AGENT"
00096       );
00097   } else if 
00098     (
00099      agent->agent_type == MC_RETURN_AGENT
00100     )
00101     {
00102     mxmlElementSetAttr
00103       (
00104        node,
00105        "message",
00106        "RETURN_MSG"
00107       );
00108   }
00109 
00110   mxmlAdd
00111     (
00112      node,
00113      MXML_ADD_AFTER,
00114      NULL,
00115      agent_xml_compose__mobile_agent(agent)
00116     );
00117   return node;
00118 }
00119 
00120   mxml_node_t*
00121 agent_xml_compose__mobile_agent(agent_p agent)
00122 {
00123   mxml_node_t* node;
00124 
00125   node = mxmlNewElement
00126     (
00127      NULL,
00128      "MOBILE_AGENT"
00129     );
00130 
00131   mxmlAdd
00132     (
00133      node,
00134      MXML_ADD_AFTER,
00135      NULL,
00136      agent_xml_compose__agent_data(agent)
00137     );
00138   return node;
00139 }
00140 
00141   mxml_node_t*
00142 agent_xml_compose__agent_data(agent_p agent)
00143 {
00144   mxml_node_t* node;
00145   mxml_node_t* tmp_node;
00146 
00147   node = mxmlNewElement
00148     (
00149      NULL,
00150      "AGENT_DATA"
00151     );
00152 
00153   /* Add the 'name' node */
00154   tmp_node = agent_xml_compose__name(agent);
00155   if (tmp_node == NULL) {
00156     return NULL;
00157   }
00158   mxmlAdd(
00159       node,
00160       MXML_ADD_AFTER,
00161       NULL,
00162       tmp_node
00163       );
00164 
00165   /* Add the 'owner' node */
00166   tmp_node = agent_xml_compose__owner(agent);
00167   if (tmp_node != NULL) {
00168     mxmlAdd(
00169         node,
00170         MXML_ADD_AFTER,
00171         NULL,
00172         tmp_node
00173         );
00174   }
00175 
00176   /* Add the 'home' node */
00177   tmp_node = agent_xml_compose__home(agent);
00178   if (tmp_node != NULL) {
00179     mxmlAdd(
00180         node,
00181         MXML_ADD_AFTER,
00182         NULL,
00183         tmp_node
00184         );
00185   }
00186 
00187   /* Add the 'task' node */
00188   tmp_node = agent_xml_compose__task(agent);
00189   if (tmp_node != NULL) {
00190     mxmlAdd(
00191         node,
00192         MXML_ADD_AFTER,
00193         NULL,
00194         tmp_node
00195         );
00196   }
00197 
00198   return node;
00199 }
00200 
00201   mxml_node_t*
00202 agent_xml_compose__name(agent_p agent)
00203 {
00204   mxml_node_t* node;
00205   node = mxmlNewElement(
00206       NULL,
00207       "NAME"
00208       );
00209   mxmlNewText(
00210       node,
00211       0,
00212       agent->name
00213       );
00214   return node;
00215 }
00216 
00217   mxml_node_t*
00218 agent_xml_compose__owner(agent_p agent)
00219 {
00220   mxml_node_t* node;
00221   node = mxmlNewElement(
00222       NULL,
00223       "OWNER"
00224       );
00225   mxmlNewText(
00226       node,
00227       0,
00228       agent->owner
00229       );
00230   return node;
00231 }
00232 
00233   mxml_node_t*
00234 agent_xml_compose__home(agent_p agent)
00235 {
00236   mxml_node_t* node;
00237   node = mxmlNewElement(
00238       NULL,
00239       "HOME"
00240       );
00241   mxmlNewText(
00242       node,
00243       0,
00244       agent->home
00245       );
00246   return node;
00247 }
00248 
00249   mxml_node_t*
00250 agent_xml_compose__task(agent_p agent)
00251 {
00252   char buf[30];
00253   mxml_node_t* node;
00254   mxml_node_t* tmp_node;
00255   int i;
00256   node = mxmlNewElement(
00257       NULL,
00258       "TASK"
00259       );
00260   sprintf(buf, "%d", agent->datastate->number_of_tasks);
00261   mxmlElementSetAttr(
00262       node,
00263       "task",
00264       buf
00265       );
00266 
00267   buf[0] = '\0';
00268   sprintf(buf, "%d", agent->datastate->task_progress);
00269   mxmlElementSetAttr(
00270       node,
00271       "num",
00272       buf
00273       );
00274 
00275   for (i = 0; i < agent->datastate->number_of_tasks; i++) {
00276     tmp_node = agent_xml_compose__data(agent, i);
00277     if (tmp_node != NULL) {
00278       mxmlAdd(
00279           node,
00280           MXML_ADD_AFTER,
00281           NULL,
00282           tmp_node
00283           );
00284     } else {
00285       return NULL;
00286     }
00287   }
00288   i=0;
00289   tmp_node = agent_xml_compose__agent_code(agent, i);
00290   while (tmp_node != NULL) {
00291     mxmlAdd(
00292         node,
00293         MXML_ADD_AFTER,
00294         NULL,
00295         tmp_node
00296         );
00297     i++;
00298     tmp_node = agent_xml_compose__agent_code(agent, i);
00299   }
00300 
00301   return node;
00302 }
00303 
00304   mxml_node_t*
00305 agent_xml_compose__data(agent_p agent, int index)
00306 {
00307   char buf[30];
00308   mxml_node_t* node;
00309   mxml_node_t* tmp_node;
00310   node = mxmlNewElement(
00311       NULL,
00312       "DATA"
00313       );
00314   /* Set up return variable name attribute */
00315   if ( agent->datastate->tasks[index]->var_name != NULL ) {
00316     mxmlElementSetAttr(
00317         node,
00318         "name",
00319         agent->datastate->tasks[index]->var_name
00320         );
00321   }
00322 
00323   /* Set up task completion attribute */
00324   sprintf(buf, "%d", agent->datastate->tasks[index]->task_completed);
00325   mxmlElementSetAttr(
00326       node,
00327       "complete",
00328       buf
00329       );
00330 
00331   /* Set up server name attribute */
00332   mxmlElementSetAttr(
00333       node,
00334       "server",
00335       agent->datastate->tasks[index]->server_name
00336       );
00337 
00338   /* Set up persistent flag, only if it _is_ persistent */
00339   if (agent->datastate->persistent || agent->datastate->tasks[index]->persistent) {
00340     mxmlElementSetAttr(
00341         node,
00342         "persistent",
00343         "1"
00344         );
00345   }
00346 
00347   /* Set up the code id */
00348   if (agent->datastate->tasks[index]->code_id) {
00349     mxmlElementSetAttr(
00350         node,
00351         "code_id",
00352         agent->datastate->tasks[index]->code_id
00353         );
00354   }
00355 
00356   /* If the task is not complete, we may return here. Otherwise, we have
00357    * to deal with the return data. */
00358   if (!agent->datastate->tasks[index]->task_completed) {
00359     return node;
00360   }
00361 
00362   /* *****************************
00363    * Set up agent return attributes here
00364    * ***************************** */
00365   if (agent->datastate->tasks[index]->var_name != NULL) {
00366     if (
00367         strcmp(
00368           "no-return",
00369           agent->datastate->tasks[index]->var_name
00370           )
00371        )
00372     {
00373       if (agent->datastate->tasks[index]->agent_return_data != NULL)
00374       {
00375         /* Set up the 'dim' attribute */
00376         sprintf(
00377             buf, 
00378             "%d", 
00379             agent->datastate->tasks[index]->agent_return_data->array_dim
00380             );
00381         mxmlElementSetAttr(
00382             node,
00383             "dim",
00384             buf
00385             );
00386 
00387         /* set up the 'type' attribute */
00388         CH_DATATYPE_STRING(
00389             agent->datastate->tasks[index]->agent_return_data->data_type,
00390             buf
00391             );
00392         mxmlElementSetAttr(
00393             node,
00394             "type",
00395             buf
00396             );
00397 
00398         if (agent->datastate->tasks[index]->agent_return_data->array_dim == 0)
00399         {
00400           CH_DATATYPE_VALUE_STRING
00401             (
00402              agent->datastate->tasks[index]->agent_return_data->data_type,
00403              buf,
00404              agent->datastate->tasks[index]->agent_return_data->return_data
00405             );
00406           mxmlElementSetAttr(
00407               node,
00408               "return_value",
00409               buf
00410               );
00411         } else {
00412           /* Set up row nodes */
00413           tmp_node = agent_xml_compose__row(agent, index);
00414           if (tmp_node != NULL) {
00415             mxmlAdd(
00416                 node,
00417                 MXML_ADD_AFTER,
00418                 NULL,
00419                 tmp_node
00420                 );
00421           }
00422         }
00423       }
00424     }
00425   }
00426   return node;
00427 }
00428 
00429   mxml_node_t*
00430 agent_xml_compose__agent_code(agent_p agent, int index)
00431 {
00432   mxml_node_t* node;
00433   if (agent->datastate->agent_codes[index] == NULL) {
00434     return NULL;
00435   }
00436 
00437   node = mxmlNewElement (
00438       MXML_NO_PARENT,
00439       "AGENT_CODE"
00440       );
00441 
00442   xml_new_cdata(
00443       node,
00444       agent->datastate->agent_codes[index]
00445       );
00446   if (strlen(agent->datastate->agent_code_ids[index]) > 0) {
00447     mxmlElementSetAttr
00448       (
00449        node,
00450        "id",
00451        agent->datastate->agent_code_ids[index]
00452       );
00453   }
00454   return node;
00455 }
00456 
00457   mxml_node_t*
00458 agent_xml_compose__row(agent_p agent, int index)
00459 {
00460   mxml_node_t* node;
00461 
00462   if (agent->datastate->tasks[index]->agent_return_data == NULL) {
00463     return NULL;
00464   }
00465 
00466   node = agent_xml_compose__create_row_nodes
00467     (
00468      agent->datastate->tasks[index]->agent_return_data->return_data,
00469      0,
00470      agent->datastate->tasks[index]->agent_return_data->array_extent,
00471      agent->datastate->tasks[index]->agent_return_data->data_type,
00472      agent->datastate->tasks[index]->agent_return_data->array_dim,
00473      0
00474     );
00475   return node;
00476 }
00477 
00478 /* This recursive function actually creates the tree of row nodes. */
00479 mxml_node_t* 
00480 agent_xml_compose__create_row_nodes
00481 (
00482  void* data, 
00483  int index,
00484  int *extent,
00485  ChType_t type,
00486  int dim,
00487  int extent_index
00488  )
00489 {
00490   mxml_node_t* node;
00491   char *buf;
00492   char *varstring;
00493   int size;
00494   int i;
00495   if (dim == 1) { 
00496     buf = (char*)malloc(sizeof(char) * 20);
00497     CH_DATATYPE_SIZE(type, size);
00498 
00499     varstring = malloc(
00500         (sizeof(char)*64) * *extent);
00501     varstring[0] = '\0';
00502     for(i = 0; i < *extent; i++) {
00503       buf[0] = '\0';
00504 #ifndef _WIN32
00505       CH_DATATYPE_VALUE_STRING(type, buf, (data+ size*(index+i)));
00506 #else
00507       CH_DATATYPE_VALUE_STRING(type, buf, ((char*)data+ size*(index+i)));
00508 #endif
00509       strcat(varstring, buf);
00510       strcat(varstring, ",");
00511     }
00512     node = mxmlNewElement(
00513         MXML_NO_PARENT,
00514         "ROW" );
00515     buf[0] = '\0';
00516     sprintf(buf, "%d", extent_index);
00517     mxmlElementSetAttr(
00518         node,
00519         "index",
00520         buf );
00521 
00522 
00523     mxmlNewText(
00524         node,
00525         1,
00526         varstring );
00527     free(buf);
00528     free(varstring);
00529     return node;
00530   } else if (dim < 0) {
00531     fprintf(stderr, "INTERNAL ERROR: %s:%d\n",
00532         __FILE__, __LINE__);
00533     return NULL;
00534   } else if (dim == 0) {
00535     return NULL;
00536   } else {
00537     /* Create my node, but need to attach node of lower dimensions */
00538     size = 1;
00539     for (i = 1; i < dim; i++) {
00540       size *= extent[i];  /* size is the size of a dimension */
00541     }
00542     node = mxmlNewElement(MXML_NO_PARENT, "ROW");
00543     buf = (char*)malloc(sizeof(char)*10);
00544     sprintf(buf, "%d", extent_index);
00545     mxmlElementSetAttr(
00546         node,
00547         "index",
00548         buf );
00549     for (i = 0; i < *extent; i++) {
00550       mxmlAdd( 
00551           node,
00552           MXML_ADD_AFTER,
00553           MXML_ADD_TO_PARENT,
00554           agent_xml_compose__create_row_nodes(
00555             data,
00556             index + (size*i),
00557             extent+1,
00558             type,
00559             dim-1,
00560             i
00561             )
00562           );
00563     }
00564     free (buf);
00565     return node;
00566   }
00567 }
00568 

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