/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 \"mobilec.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      "MOBILEC_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__tasks(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__tasks(agent_p agent)
00251 {
00252   char buf[30];
00253   int i;
00254   mxml_node_t* node;
00255   mxml_node_t* tmp_node;
00256 
00257   node=mxmlNewElement(
00258       NULL,
00259       "TASKS" );
00260 
00261   sprintf(buf, "%d", agent->datastate->number_of_tasks);
00262   mxmlElementSetAttr(
00263       node,
00264       "task",
00265       buf
00266       );
00267 
00268   buf[0] = '\0';
00269   sprintf(buf, "%d", agent->datastate->task_progress);
00270   mxmlElementSetAttr(
00271       node,
00272       "num",
00273       buf
00274       );
00275 
00276   for (i = 0; i < agent->datastate->number_of_tasks; i++) {
00277     tmp_node = agent_xml_compose__task(agent, i);
00278     if (tmp_node != NULL) {
00279       mxmlAdd(
00280           node,
00281           MXML_ADD_AFTER,
00282           NULL,
00283           tmp_node
00284           );
00285     } else {
00286       return NULL;
00287     }
00288   }
00289 
00290   i=0;
00291   tmp_node = agent_xml_compose__agent_code(agent, i);
00292   while (tmp_node != NULL) {
00293     mxmlAdd(
00294         node,
00295         MXML_ADD_AFTER,
00296         NULL,
00297         tmp_node
00298         );
00299     i++;
00300     tmp_node = agent_xml_compose__agent_code(agent, i);
00301   }
00302 
00303   return node;
00304 }
00305 
00306   mxml_node_t*
00307 agent_xml_compose__task(agent_p agent, int index)
00308 {
00309   char buf[30];
00310   mxml_node_t* node;
00311   mxml_node_t* tmp_node;
00312   interpreter_variable_data_t* tmp_interp_var;
00313   node = mxmlNewElement(
00314       NULL,
00315       "TASK"
00316       );
00317 
00318   buf[0] = '\0';
00319   sprintf(buf, "%d", index);
00320   mxmlElementSetAttr(
00321       node,
00322       "num",
00323       buf );
00324 
00325   /* Set up server name attribute */
00326   mxmlElementSetAttr(
00327       node,
00328       "server",
00329       agent->datastate->tasks[index]->server_name
00330       );
00331 
00332   /* Set up task completion attribute */
00333   sprintf(buf, "%d", agent->datastate->tasks[index]->task_completed);
00334   mxmlElementSetAttr(
00335       node,
00336       "complete",
00337       buf
00338       );
00339 
00340   /* Set up the code id */
00341   if (agent->datastate->tasks[index]->code_id) {
00342     mxmlElementSetAttr(
00343         node,
00344         "code_id",
00345         agent->datastate->tasks[index]->code_id
00346         );
00347   }
00348 
00349   /* Set up persistent flag, only if it _is_ persistent */
00350   if (agent->datastate->persistent || agent->datastate->tasks[index]->persistent) {
00351     mxmlElementSetAttr(
00352         node,
00353         "persistent",
00354         "1"
00355         );
00356   }
00357 
00358   /* Add 'DATA' nodes */
00359   /* First, add the return data */
00360   if(agent->datastate->tasks[index]->var_name != NULL) {
00361     mxmlElementSetAttr(
00362         node,
00363         "return",
00364         agent->datastate->tasks[index]->var_name );
00365 
00366     if(
00367         strcmp( 
00368           "no-return",
00369           agent->datastate->tasks[index]->var_name
00370           )
00371         &&
00372         agent->datastate->tasks[index]->task_completed
00373       )
00374     {
00375 
00376       if (agent->datastate->tasks[index]->agent_return_data != NULL) {
00377         tmp_node = agent_xml_compose__data(
00378             agent, 
00379             index, 
00380             agent->datastate->tasks[index]->agent_return_data);
00381       } else {tmp_node = NULL;}
00382 
00383       if(tmp_node == NULL) {
00384         fprintf(stderr, "Compose error. %s:%d\n", __FILE__, __LINE__);
00385         return NULL;
00386       }
00387       mxmlAdd(
00388           node,
00389           MXML_ADD_AFTER,
00390           NULL,
00391           tmp_node );
00392     }
00393   }
00394 
00395   /* Now, add all the variables that the agent wants to save */
00396   while
00397     (
00398      ( 
00399       tmp_interp_var = agent_variable_list_Pop(
00400         agent->datastate->tasks[index]->agent_variable_list )
00401      ) != NULL 
00402     )
00403   {
00404     tmp_node = agent_xml_compose__data(
00405         agent,
00406         index,
00407         tmp_interp_var);
00408     free(tmp_interp_var);
00409     if(tmp_node == NULL) {
00410       fprintf(stderr, "Compose error. %s:%d\n", __FILE__, __LINE__);
00411       return NULL;
00412     }
00413     mxmlAdd(
00414         node,
00415         MXML_ADD_AFTER,
00416         NULL,
00417         tmp_node );
00418   }
00419 
00420   return node;
00421 }
00422 
00423   mxml_node_t*
00424 agent_xml_compose__data(agent_p agent, int index, interpreter_variable_data_t* interp_variable)
00425 {
00426   char buf[30];
00427   mxml_node_t* node;
00428   mxml_node_t* tmp_node;
00429 
00430   if (interp_variable == NULL) { return NULL; }
00431 
00432   node = mxmlNewElement(
00433       NULL,
00434       "DATA"
00435       );
00436   /* Set up return variable name attribute */
00437   mxmlElementSetAttr(
00438       node,
00439       "name",
00440       interp_variable->name
00441       );
00442 
00443   if (interp_variable != NULL)
00444   {
00445     /* Set up the 'dim' attribute */
00446     sprintf(
00447         buf, 
00448         "%d", 
00449         interp_variable->array_dim
00450         );
00451     mxmlElementSetAttr(
00452         node,
00453         "dim",
00454         buf
00455         );
00456 
00457     /* set up the 'type' attribute */
00458     CH_DATATYPE_STRING(
00459         interp_variable->data_type,
00460         buf
00461         );
00462     mxmlElementSetAttr(
00463         node,
00464         "type",
00465         buf
00466         );
00467 
00468     if (interp_variable->array_dim == 0)
00469     {
00470       CH_DATATYPE_VALUE_STRING
00471         (
00472          interp_variable->data_type,
00473          buf,
00474          interp_variable->data
00475         );
00476       mxmlElementSetAttr(
00477           node,
00478           "value",
00479           buf
00480           );
00481     } else {
00482       /* Set up row nodes */
00483       tmp_node = agent_xml_compose__row(interp_variable, index);
00484       if (tmp_node != NULL) {
00485         mxmlAdd(
00486             node,
00487             MXML_ADD_AFTER,
00488             NULL,
00489             tmp_node
00490             );
00491       }
00492     }
00493   }
00494   return node;
00495 }
00496 
00497   mxml_node_t*
00498 agent_xml_compose__agent_code(agent_p agent, int index)
00499 {
00500   mxml_node_t* node;
00501   if (agent->datastate->agent_codes[index] == NULL) {
00502     return NULL;
00503   }
00504 
00505   node = mxmlNewElement (
00506       MXML_NO_PARENT,
00507       "AGENT_CODE"
00508       );
00509 
00510   xml_new_cdata(
00511       node,
00512       agent->datastate->agent_codes[index]
00513       );
00514   if (strlen(agent->datastate->agent_code_ids[index]) > 0) {
00515     mxmlElementSetAttr
00516       (
00517        node,
00518        "id",
00519        agent->datastate->agent_code_ids[index]
00520       );
00521   }
00522   return node;
00523 }
00524 
00525   mxml_node_t*
00526 agent_xml_compose__row(interpreter_variable_data_t* interp_variable, int index)
00527 {
00528   mxml_node_t* node;
00529 
00530   if (interp_variable == NULL) {
00531     return NULL;
00532   }
00533 
00534   node = agent_xml_compose__create_row_nodes
00535     (
00536      interp_variable->data,
00537      0,
00538      interp_variable->array_extent,
00539      interp_variable->data_type,
00540      interp_variable->array_dim,
00541      0
00542     );
00543   return node;
00544 }
00545 
00546 /* This recursive function actually creates the tree of row nodes. */
00547 mxml_node_t* 
00548 agent_xml_compose__create_row_nodes
00549 (
00550  void* data, 
00551  int index,
00552  int *extent,
00553  ChType_t type,
00554  int dim,
00555  int extent_index
00556  )
00557 {
00558   mxml_node_t* node;
00559   char *buf;
00560   char *varstring;
00561   int size;
00562   int i;
00563   if (dim == 1) { 
00564     buf = (char*)malloc(sizeof(char) * 20);
00565     CH_DATATYPE_SIZE(type, size);
00566 
00567     varstring = malloc(
00568         (sizeof(char)*64) * *extent);
00569     varstring[0] = '\0';
00570     for(i = 0; i < *extent; i++) {
00571       buf[0] = '\0';
00572 #ifndef _WIN32
00573       CH_DATATYPE_VALUE_STRING(type, buf, (data+ size*(index+i)));
00574 #else
00575       CH_DATATYPE_VALUE_STRING(type, buf, ((char*)data+ size*(index+i)));
00576 #endif
00577       strcat(varstring, buf);
00578       strcat(varstring, ",");
00579     }
00580     node = mxmlNewElement(
00581         MXML_NO_PARENT,
00582         "ROW" );
00583     buf[0] = '\0';
00584     sprintf(buf, "%d", extent_index);
00585     mxmlElementSetAttr(
00586         node,
00587         "index",
00588         buf );
00589 
00590 
00591     mxmlNewText(
00592         node,
00593         1,
00594         varstring );
00595     free(buf);
00596     free(varstring);
00597     return node;
00598   } else if (dim < 0) {
00599     fprintf(stderr, "INTERNAL ERROR: %s:%d\n",
00600         __FILE__, __LINE__);
00601     return NULL;
00602   } else if (dim == 0) {
00603     return NULL;
00604   } else {
00605     /* Create my node, but need to attach node of lower dimensions */
00606     size = 1;
00607     for (i = 1; i < dim; i++) {
00608       size *= extent[i];  /* size is the size of a dimension */
00609     }
00610     node = mxmlNewElement(MXML_NO_PARENT, "ROW");
00611     buf = (char*)malloc(sizeof(char)*10);
00612     sprintf(buf, "%d", extent_index);
00613     mxmlElementSetAttr(
00614         node,
00615         "index",
00616         buf );
00617     for (i = 0; i < *extent; i++) {
00618       mxmlAdd( 
00619           node,
00620           MXML_ADD_AFTER,
00621           MXML_ADD_TO_PARENT,
00622           agent_xml_compose__create_row_nodes(
00623             data,
00624             index + (size*i),
00625             extent+1,
00626             type,
00627             dim-1,
00628             i
00629             )
00630           );
00631     }
00632     free (buf);
00633     return node;
00634   }
00635 }
00636 

Generated on Mon Jun 23 16:01:12 2008 for Mobile-C by  doxygen 1.5.4