/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/xml_compose.c

Go to the documentation of this file.
00001 /* SVN FILE INFO
00002  * $Revision: 207 $ : Last Committed Revision
00003  * $Date: 2008-07-11 17:55:19 -0700 (Fri, 11 Jul 2008) $ : Last Committed Date */
00004 /*[
00005  * Copyright (c) 2007 Integration Engineering Laboratory
00006                       University of California, Davis
00007  *
00008  * Permission to use, copy, and distribute this software and its
00009  * documentation for any purpose with or without fee is hereby granted,
00010  * provided that the above copyright notice appear in all copies and
00011  * that both that copyright notice and this permission notice appear
00012  * in supporting documentation.
00013  *
00014  * Permission to modify the software is granted, but not the right to
00015  * distribute the complete modified source code.  Modifications are to
00016  * be distributed as patches to the released version.  Permission to
00017  * distribute binaries produced by compiling modified sources is granted,
00018  * provided you
00019  *   1. distribute the corresponding source modifications from the
00020  *    released version in the form of a patch file along with the binaries,
00021  *   2. add special version identification to distinguish your version
00022  *    in addition to the base release version number,
00023  *   3. provide your name and address as the primary contact for the
00024  *    support of your modified version, and
00025  *   4. retain our contact information in regard to use of the base
00026  *    software.
00027  * Permission to distribute the released version of the source code along
00028  * with corresponding source modifications in the form of a patch file is
00029  * granted with same provisions 2 through 4 for binary distributions.
00030  *
00031  * This software is provided "as is" without express or implied warranty
00032  * to the extent permitted by applicable law.
00033 ]*/
00034 
00035 #include <mxml.h>
00036 #include "include/agent.h"
00037 #include "include/xml_compose.h"
00038 #include "include/xml_helper.h"
00039 
00040 mxml_node_t*
00041 agent_xml_compose(agent_p agent)
00042 {
00043   mxml_node_t* node;
00044   node = mxmlLoadString
00045     (
00046      NULL,
00047      "<?xml version=\"1.0\"?>\n<!DOCTYPE myMessage SYSTEM \"mobilec.dtd\">",
00048      MXML_NO_CALLBACK
00049     );
00050   mxmlAdd
00051     (
00052      node,
00053      MXML_ADD_AFTER,
00054      MXML_ADD_TO_PARENT,
00055      agent_xml_compose__gaf_message(agent)
00056     );
00057   return node;
00058 }
00059 
00060   mxml_node_t*
00061 agent_xml_compose__gaf_message(agent_p agent)
00062 {
00063   mxml_node_t* node;
00064   node = mxmlNewElement
00065     (
00066      NULL,
00067      "MOBILEC_MESSAGE"
00068     );
00069   mxmlAdd
00070     (
00071      node,
00072      MXML_ADD_AFTER,
00073      NULL,
00074      agent_xml_compose__message(agent)
00075     );
00076   return node;
00077 }
00078 
00079   mxml_node_t*
00080 agent_xml_compose__message(agent_p agent)
00081 {
00082   mxml_node_t* node;
00083   node = mxmlNewElement
00084     (
00085      NULL,
00086      "MESSAGE"
00087     );
00088 
00089   if(
00090       agent->agent_type == MC_REMOTE_AGENT ||
00091       agent->agent_type == MC_LOCAL_AGENT 
00092     )
00093   {
00094     mxmlElementSetAttr
00095       (
00096        node,
00097        "message",
00098        "MOBILE_AGENT"
00099       );
00100   } else if 
00101     (
00102      agent->agent_type == MC_RETURN_AGENT
00103     )
00104     {
00105     mxmlElementSetAttr
00106       (
00107        node,
00108        "message",
00109        "RETURN_MSG"
00110       );
00111   }
00112 
00113   mxmlAdd
00114     (
00115      node,
00116      MXML_ADD_AFTER,
00117      NULL,
00118      agent_xml_compose__mobile_agent(agent)
00119     );
00120   return node;
00121 }
00122 
00123   mxml_node_t*
00124 agent_xml_compose__mobile_agent(agent_p agent)
00125 {
00126   mxml_node_t* node;
00127 
00128   node = mxmlNewElement
00129     (
00130      NULL,
00131      "MOBILE_AGENT"
00132     );
00133 
00134   mxmlAdd
00135     (
00136      node,
00137      MXML_ADD_AFTER,
00138      NULL,
00139      agent_xml_compose__agent_data(agent)
00140     );
00141   return node;
00142 }
00143 
00144   mxml_node_t*
00145 agent_xml_compose__agent_data(agent_p agent)
00146 {
00147   mxml_node_t* node;
00148   mxml_node_t* tmp_node;
00149 
00150   node = mxmlNewElement
00151     (
00152      NULL,
00153      "AGENT_DATA"
00154     );
00155 
00156   /* Add the 'name' node */
00157   tmp_node = agent_xml_compose__name(agent);
00158   if (tmp_node == NULL) {
00159     return NULL;
00160   }
00161   mxmlAdd(
00162       node,
00163       MXML_ADD_AFTER,
00164       NULL,
00165       tmp_node
00166       );
00167 
00168   /* Add the 'owner' node */
00169   tmp_node = agent_xml_compose__owner(agent);
00170   if (tmp_node != NULL) {
00171     mxmlAdd(
00172         node,
00173         MXML_ADD_AFTER,
00174         NULL,
00175         tmp_node
00176         );
00177   }
00178 
00179   /* Add the 'home' node */
00180   tmp_node = agent_xml_compose__home(agent);
00181   if (tmp_node != NULL) {
00182     mxmlAdd(
00183         node,
00184         MXML_ADD_AFTER,
00185         NULL,
00186         tmp_node
00187         );
00188   }
00189 
00190   /* Add the 'task' node */
00191   tmp_node = agent_xml_compose__tasks(agent);
00192   if (tmp_node != NULL) {
00193     mxmlAdd(
00194         node,
00195         MXML_ADD_AFTER,
00196         NULL,
00197         tmp_node
00198         );
00199   }
00200 
00201   return node;
00202 }
00203 
00204   mxml_node_t*
00205 agent_xml_compose__name(agent_p agent)
00206 {
00207   mxml_node_t* node;
00208   node = mxmlNewElement(
00209       NULL,
00210       "NAME"
00211       );
00212   mxmlNewText(
00213       node,
00214       0,
00215       agent->name
00216       );
00217   return node;
00218 }
00219 
00220   mxml_node_t*
00221 agent_xml_compose__owner(agent_p agent)
00222 {
00223   mxml_node_t* node;
00224   node = mxmlNewElement(
00225       NULL,
00226       "OWNER"
00227       );
00228   mxmlNewText(
00229       node,
00230       0,
00231       agent->owner
00232       );
00233   return node;
00234 }
00235 
00236   mxml_node_t*
00237 agent_xml_compose__home(agent_p agent)
00238 {
00239   mxml_node_t* node;
00240   node = mxmlNewElement(
00241       NULL,
00242       "HOME"
00243       );
00244   mxmlNewText(
00245       node,
00246       0,
00247       agent->home
00248       );
00249   return node;
00250 }
00251 
00252   mxml_node_t*
00253 agent_xml_compose__tasks(agent_p agent)
00254 {
00255   char buf[30];
00256   int i;
00257   mxml_node_t* node;
00258   mxml_node_t* tmp_node;
00259 
00260   node=mxmlNewElement(
00261       NULL,
00262       "TASKS" );
00263 
00264   sprintf(buf, "%d", agent->datastate->number_of_tasks);
00265   mxmlElementSetAttr(
00266       node,
00267       "task",
00268       buf
00269       );
00270 
00271   buf[0] = '\0';
00272   sprintf(buf, "%d", agent->datastate->task_progress);
00273   mxmlElementSetAttr(
00274       node,
00275       "num",
00276       buf
00277       );
00278 
00279   for (i = 0; i < agent->datastate->number_of_tasks; i++) {
00280     tmp_node = agent_xml_compose__task(agent, i);
00281     if (tmp_node != NULL) {
00282       mxmlAdd(
00283           node,
00284           MXML_ADD_AFTER,
00285           NULL,
00286           tmp_node
00287           );
00288     } else {
00289       return NULL;
00290     }
00291   }
00292 
00293   i=0;
00294   tmp_node = agent_xml_compose__agent_code(agent, i);
00295   while (tmp_node != NULL) {
00296     mxmlAdd(
00297         node,
00298         MXML_ADD_AFTER,
00299         NULL,
00300         tmp_node
00301         );
00302     i++;
00303     tmp_node = agent_xml_compose__agent_code(agent, i);
00304   }
00305 
00306   return node;
00307 }
00308 
00309   mxml_node_t*
00310 agent_xml_compose__task(agent_p agent, int index)
00311 {
00312   char buf[30];
00313   mxml_node_t* node;
00314   mxml_node_t* tmp_node;
00315   interpreter_variable_data_t* tmp_interp_var;
00316   node = mxmlNewElement(
00317       NULL,
00318       "TASK"
00319       );
00320 
00321   buf[0] = '\0';
00322   sprintf(buf, "%d", index);
00323   mxmlElementSetAttr(
00324       node,
00325       "num",
00326       buf );
00327 
00328   /* Set up server name attribute */
00329   mxmlElementSetAttr(
00330       node,
00331       "server",
00332       agent->datastate->tasks[index]->server_name
00333       );
00334 
00335   /* Set up task completion attribute */
00336   sprintf(buf, "%d", agent->datastate->tasks[index]->task_completed);
00337   mxmlElementSetAttr(
00338       node,
00339       "complete",
00340       buf
00341       );
00342 
00343   /* Set up the code id */
00344   if (agent->datastate->tasks[index]->code_id) {
00345     mxmlElementSetAttr(
00346         node,
00347         "code_id",
00348         agent->datastate->tasks[index]->code_id
00349         );
00350   }
00351 
00352   /* Set up persistent flag, only if it _is_ persistent */
00353   if (agent->datastate->persistent || agent->datastate->tasks[index]->persistent) {
00354     mxmlElementSetAttr(
00355         node,
00356         "persistent",
00357         "1"
00358         );
00359   }
00360 
00361   /* Add 'DATA' nodes */
00362   /* First, add the return data */
00363   if(agent->datastate->tasks[index]->var_name != NULL) {
00364     mxmlElementSetAttr(
00365         node,
00366         "return",
00367         agent->datastate->tasks[index]->var_name );
00368 
00369     if(
00370         strcmp( 
00371           "no-return",
00372           agent->datastate->tasks[index]->var_name
00373           )
00374         &&
00375         agent->datastate->tasks[index]->task_completed
00376       )
00377     {
00378 
00379       if (agent->datastate->tasks[index]->agent_return_data != NULL) {
00380         tmp_node = agent_xml_compose__data(
00381             agent, 
00382             index, 
00383             agent->datastate->tasks[index]->agent_return_data);
00384       } else {tmp_node = NULL;}
00385 
00386       if(tmp_node == NULL) {
00387         fprintf(stderr, "Compose error. %s:%d\n", __FILE__, __LINE__);
00388         return NULL;
00389       }
00390       mxmlAdd(
00391           node,
00392           MXML_ADD_AFTER,
00393           NULL,
00394           tmp_node );
00395     }
00396   }
00397 
00398   /* Now, add all the variables that the agent wants to save */
00399   while
00400     (
00401      ( 
00402       tmp_interp_var = agent_variable_list_Pop(
00403         agent->datastate->tasks[index]->agent_variable_list )
00404      ) != NULL 
00405     )
00406   {
00407     tmp_node = agent_xml_compose__data(
00408         agent,
00409         index,
00410         tmp_interp_var);
00411     free(tmp_interp_var);
00412     if(tmp_node == NULL) {
00413       fprintf(stderr, "Compose error. %s:%d\n", __FILE__, __LINE__);
00414       return NULL;
00415     }
00416     mxmlAdd(
00417         node,
00418         MXML_ADD_AFTER,
00419         NULL,
00420         tmp_node );
00421   }
00422 
00423   return node;
00424 }
00425 
00426   mxml_node_t*
00427 agent_xml_compose__data(agent_p agent, int index, interpreter_variable_data_t* interp_variable)
00428 {
00429   char buf[30];
00430   mxml_node_t* node;
00431   mxml_node_t* tmp_node;
00432 
00433   if (interp_variable == NULL) { return NULL; }
00434 
00435   node = mxmlNewElement(
00436       NULL,
00437       "DATA"
00438       );
00439   /* Set up return variable name attribute */
00440   mxmlElementSetAttr(
00441       node,
00442       "name",
00443       interp_variable->name
00444       );
00445 
00446   if (interp_variable != NULL)
00447   {
00448     /* Set up the 'dim' attribute */
00449     sprintf(
00450         buf, 
00451         "%d", 
00452         interp_variable->array_dim
00453         );
00454     mxmlElementSetAttr(
00455         node,
00456         "dim",
00457         buf
00458         );
00459 
00460     /* set up the 'type' attribute */
00461     CH_DATATYPE_STRING(
00462         interp_variable->data_type,
00463         buf
00464         );
00465     mxmlElementSetAttr(
00466         node,
00467         "type",
00468         buf
00469         );
00470 
00471     if (interp_variable->array_dim == 0)
00472     {
00473       CH_DATATYPE_VALUE_STRING
00474         (
00475          interp_variable->data_type,
00476          buf,
00477          interp_variable->data
00478         );
00479       mxmlElementSetAttr(
00480           node,
00481           "value",
00482           buf
00483           );
00484     } else {
00485       /* Set up row nodes */
00486       tmp_node = agent_xml_compose__row(interp_variable, index);
00487       if (tmp_node != NULL) {
00488         mxmlAdd(
00489             node,
00490             MXML_ADD_AFTER,
00491             NULL,
00492             tmp_node
00493             );
00494       }
00495     }
00496   }
00497   return node;
00498 }
00499 
00500   mxml_node_t*
00501 agent_xml_compose__agent_code(agent_p agent, int index)
00502 {
00503   mxml_node_t* node;
00504   if (agent->datastate->agent_codes[index] == NULL) {
00505     return NULL;
00506   }
00507 
00508   node = mxmlNewElement (
00509       MXML_NO_PARENT,
00510       "AGENT_CODE"
00511       );
00512 
00513   xml_new_cdata(
00514       node,
00515       agent->datastate->agent_codes[index]
00516       );
00517   if (strlen(agent->datastate->agent_code_ids[index]) > 0) {
00518     mxmlElementSetAttr
00519       (
00520        node,
00521        "id",
00522        agent->datastate->agent_code_ids[index]
00523       );
00524   }
00525   return node;
00526 }
00527 
00528   mxml_node_t*
00529 agent_xml_compose__row(interpreter_variable_data_t* interp_variable, int index)
00530 {
00531   mxml_node_t* node;
00532 
00533   if (interp_variable == NULL) {
00534     return NULL;
00535   }
00536 
00537   node = agent_xml_compose__create_row_nodes
00538     (
00539      interp_variable->data,
00540      0,
00541      interp_variable->array_extent,
00542      interp_variable->data_type,
00543      interp_variable->array_dim,
00544      0
00545     );
00546   return node;
00547 }
00548 
00549 /* This recursive function actually creates the tree of row nodes. */
00550 mxml_node_t* 
00551 agent_xml_compose__create_row_nodes
00552 (
00553  void* data, 
00554  int index,
00555  int *extent,
00556  ChType_t type,
00557  int dim,
00558  int extent_index
00559  )
00560 {
00561   mxml_node_t* node;
00562   char *buf;
00563   char *varstring;
00564   int size;
00565   int i;
00566   if (dim == 1) { 
00567     buf = (char*)malloc(sizeof(char) * 20);
00568     CH_DATATYPE_SIZE(type, size);
00569 
00570     varstring = malloc(
00571         (sizeof(char)*64) * *extent);
00572     varstring[0] = '\0';
00573     for(i = 0; i < *extent; i++) {
00574       buf[0] = '\0';
00575 #ifndef _WIN32
00576       CH_DATATYPE_VALUE_STRING(type, buf, (data+ size*(index+i)));
00577 #else
00578       CH_DATATYPE_VALUE_STRING(type, buf, ((char*)data+ size*(index+i)));
00579 #endif
00580       strcat(varstring, buf);
00581       strcat(varstring, ",");
00582     }
00583     node = mxmlNewElement(
00584         MXML_NO_PARENT,
00585         "ROW" );
00586     buf[0] = '\0';
00587     sprintf(buf, "%d", extent_index);
00588     mxmlElementSetAttr(
00589         node,
00590         "index",
00591         buf );
00592 
00593 
00594     mxmlNewText(
00595         node,
00596         1,
00597         varstring );
00598     free(buf);
00599     free(varstring);
00600     return node;
00601   } else if (dim < 0) {
00602     fprintf(stderr, "INTERNAL ERROR: %s:%d\n",
00603         __FILE__, __LINE__);
00604     return NULL;
00605   } else if (dim == 0) {
00606     return NULL;
00607   } else {
00608     /* Create my node, but need to attach node of lower dimensions */
00609     size = 1;
00610     for (i = 1; i < dim; i++) {
00611       size *= extent[i];  /* size is the size of a dimension */
00612     }
00613     node = mxmlNewElement(MXML_NO_PARENT, "ROW");
00614     buf = (char*)malloc(sizeof(char)*10);
00615     sprintf(buf, "%d", extent_index);
00616     mxmlElementSetAttr(
00617         node,
00618         "index",
00619         buf );
00620     for (i = 0; i < *extent; i++) {
00621       mxmlAdd( 
00622           node,
00623           MXML_ADD_AFTER,
00624           MXML_ADD_TO_PARENT,
00625           agent_xml_compose__create_row_nodes(
00626             data,
00627             index + (size*i),
00628             extent+1,
00629             type,
00630             dim-1,
00631             i
00632             )
00633           );
00634     }
00635     free (buf);
00636     return node;
00637   }
00638 }
00639 

Generated on Fri Jul 11 17:59:47 2008 for Mobile-C by  doxygen 1.5.4