Changeset 604


Ignore:
Timestamp:
Mar 4, 2015, 11:25:35 AM (9 years ago)
Author:
david
Message:
  • amqp integration
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_service_loader.c

    r553 r604  
    2929
    3030#include "cgic.h"
    31 
     31#include "zoo_amqp.h"
     32#include "zoo_sql.h"
    3233extern "C"
    3334{
     
    3940#include <libgen.h>
    4041}
     42
     43
    4144#include "ulinet.h"
    4245#include "service_zcfg.h"
     
    272275}
    273276
     277#include "zoo_json.h"
    274278void
    275279loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs,
     
    277281{
    278282 
     283 json_object *obj;
     284 mapstojson(&obj,*myMap);
     285 fprintf(stderr,"myMaps: %s\n",json_object_to_json_string(obj));
     286
     287
     288json_object *obj3;
     289mapstojson(&obj3,*inputs);
     290fprintf(stderr,"inputs: %s\n",json_object_to_json_string(obj3));
     291
     292
     293json_object *obj1;
     294 maptojson(&obj1,request_inputs);
     295 fprintf(stderr,"request_inputs: %s\n",json_object_to_json_string(obj1));
     296
     297json_object *obj2;
     298mapstojson(&obj2,*ioutputs);
     299fprintf(stderr,"ioutputs: %s\n",json_object_to_json_string(obj2));
     300
     301
     302
     303 fflush(stderr);
     304
    279305
    280306 char tmps1[1024];
     
    425451
    426452                  *eres = -1;
    427                   return;
    428453                }
    429454
     
    681706{
    682707
    683 
    684708struct cgi_env *cgi = *c;
    685709#ifndef USE_GDB
     
    694718  map *r_inputs = NULL;
    695719  map *request_inputs = *inputs;
    696   maps *m = NULL;
     720  maps *m = (maps *) malloc (MAPS_SIZE);
     721 
     722  if (m == NULL)
     723    {
     724      return errorException (m, _("Unable to allocate memory."),
     725                             "InternalError", NULL,request->out);
     726    }
     727
    697728  char *REQUEST = NULL;
     729   map *store = getMap (request_inputs,"storeExecuteResponse");
     730   map *status = getMap (request_inputs, "status");
     731
     732 /**
     733   * 05-007r7 WPS 1.0.0 page 57 :
     734   * 'If status="true" and storeExecuteResponse is "false" then the service
     735   * shall raise an exception.'
     736   */
     737  if (status != NULL && strcmp(status->value,"true") == 0 && store != NULL && strcmp (store->value, "false") == 0)
     738    {
     739      errorException (m,
     740                      _
     741                      ("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."),
     742                      "InvalidParameterValue", "storeExecuteResponse",request->out);
     743      //freeService (&s1);
     744      //free (s1);
     745      freeMaps (&m);
     746      free (m);
     747      free (REQUEST);
     748      return 1;
     749    }
     750
     751
     752
     753/*
     754 Si status =! NULLMAP le service est executé enbackground
     755*/
     756 if (status != NULL)
     757    if (strcasecmp (status->value, "false") == 0)
     758      status = NULLMAP;
     759
     760
     761
    698762  /**
    699763   * Parsing service specfic configuration file
    700764   */
    701765 
    702   m = (maps *) malloc (MAPS_SIZE);
    703   if (m == NULL)
    704     {
    705       return errorException (m, _("Unable to allocate memory."),
    706                              "InternalError", NULL,request->out);
    707     }
    708766   
    709767  char ntmp[1024];
     
    10911149
    10921150  s1 = NULL;
    1093   /**s1 = (service *) malloc (SERVICE_SIZE);
    1094   if (s1 == NULL)
    1095     {
    1096       freeMaps (&m);
    1097       free (m);
    1098       free (REQUEST);
    1099       free (SERVICE_URL);
    1100       return errorException (m, _("Unable to allocate memory."),
    1101                              "InternalError", NULL);
    1102     }
    1103   r_inputs = getMap (request_inputs, "MetaPath");
    1104   if (r_inputs != NULL)
    1105     snprintf (tmps1, 1024, "%s/%s", ntmp, r_inputs->value);
    1106   else
    1107     snprintf (tmps1, 1024, "%s/", ntmp);
    1108    **/
    11091151  r_inputs = getMap (request_inputs, "Identifier");
    1110 /**
    1111   char *ttmp = zStrdup (tmps1);
    1112   snprintf (tmps1, 1024, "%s/%s.zcfg", ttmp, r_inputs->value);
    1113   free (ttmp);
    1114 #ifdef DEBUG
    1115   fprintf (stderr, "Trying to load %s\n", tmps1);
    1116 #endif
    1117   if (strstr (r_inputs->value, ".") != NULL)
    1118     {
    1119       char *identifier = zStrdup (r_inputs->value);
    1120       parseIdentifier (m, conf_dir, identifier, tmps1);
    1121       map *tmpMap = getMapFromMaps (m, "lenv", "metapath");
    1122       if (tmpMap != NULL)
    1123         addToMap (request_inputs, "metapath", tmpMap->value);
    1124       free (identifier);
    1125     }
    1126   else
    1127     setMapInMaps (m, "lenv", "Identifier", r_inputs->value);
    1128   int saved_stdout = dup (fileno (stdout));
    1129   dup2 (fileno (stderr), fileno (stdout));
    1130   t = readServiceFile (m, tmps1, &s1, r_inputs->value);
    1131   fflush (stdout);
    1132   **/
    1133   //int saved_stdout = dup (fileno (stdout));
    1134   //dup2 (saved_stdout, fileno (stdout));
    1135   s1 = search_service (r_inputs->value);
     1152  char * service_identifier = zStrdup (r_inputs->value);
     1153  s1 = search_service (service_identifier);
    11361154  if (s1 == NULL)
    11371155    {
     
    13691387#endif
    13701388          fflush (stderr);
    1371 #ifdef DEBUG
    1372           fprintf (stderr, "***%s***\n", pToken);
    1373 #endif
    13741389          inputs_as_text[i] =
    13751390            (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
     
    15081523#endif
    15091524                      addToMap (tmpmaps->content, tmpn1, tmpx2);
     1525                       
     1526                      if (status == NULLMAP){
     1527
    15101528#ifndef WIN32
    1511                       if (CHECK_INET_HANDLE (hInternet))
    1512 #endif
    1513                         {
     1529                        if (CHECK_INET_HANDLE (hInternet))
     1530#endif
     1531                        {   
    15141532                          if (loadRemoteFile
    15151533                              (&m, &tmpmaps->content, &hInternet, tmpx2) < 0)
     
    15251543                            }
    15261544                        }
     1545                        }
     1546                       
    15271547                      free (tmpx2);
    15281548                      addToMap (tmpmaps->content, "Reference", tmpv1 + 1);
     
    15301550                  tmpc = strtok (NULL, "@");
    15311551                }
    1532 #ifdef DEBUG
    1533               dumpMaps (tmpmaps);
    1534               fflush (stderr);
    1535 #endif
     1552             
    15361553              if (request_input_real_format == NULL)
    15371554                request_input_real_format = dupMaps (&tmpmaps);
     
    16941711             * Header and Body gesture should be added here
    16951712             */
    1696 #ifdef DEBUG
    1697                       fprintf (stderr, "REFERENCE\n");
    1698 #endif
     1713
    16991714                      const char *refs[5] =
    17001715                        { "mimeType", "encoding", "schema",
     
    17281743                                      && CHECK_INET_HANDLE (hInternet))
    17291744                                    {
     1745                                      if (status == NULLMAP)
    17301746                                      if (loadRemoteFile
    17311747                                          (&m,
     
    28822898   */
    28832899  r_inputs = NULL;
    2884   map *store = getMap (request_inputs,
    2885                        "storeExecuteResponse");
    2886   map *status = getMap (request_inputs, "status");
    2887   /**
    2888    * 05-007r7 WPS 1.0.0 page 57 :
    2889    * 'If status="true" and storeExecuteResponse is "false" then the service
    2890    * shall raise an exception.'
    2891    */
    2892   if (status != NULL
    2893       &&
    2894       strcmp
    2895       (status->value,
    2896        "true") == 0 && store != NULL && strcmp (store->value, "false") == 0)
    2897     {
    2898       errorException (m,
    2899                       _
    2900                       ("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."),
    2901                       "InvalidParameterValue", "storeExecuteResponse",request->out);
    2902       //freeService (&s1);
    2903       //free (s1);
    2904       freeMaps (&m);
    2905       free (m);
    2906       freeMaps (&request_input_real_format);
    2907       free (request_input_real_format);
    2908       freeMaps (&request_output_real_format);
    2909       free (request_output_real_format);
    2910       free (REQUEST);
    2911       free (SERVICE_URL);
    2912       return 1;
    2913     }
    2914   r_inputs = getMap (request_inputs, "storeExecuteResponse");
     2900  //r_inputs = getMap (request_inputs, "storeExecuteResponse");
    29152901  int eres = SERVICE_STARTED;
    29162902  int cpid = getpid ();
     
    29362922  _tmpMaps->name = zStrdup ("lenv");
    29372923  char tmpBuff[100];
    2938   sprintf (tmpBuff, "%i", (cpid + (int) time (NULL)));
    2939   _tmpMaps->content = createMap ("usid", tmpBuff);
     2924  //sprintf (tmpBuff, "%i", (cpid + (int) time (NULL)));
     2925  char *uuid = get_uuid();
     2926  _tmpMaps->content = createMap ("usid", uuid);
    29402927  _tmpMaps->next = NULL;
    29412928  sprintf (tmpBuff, "%i", cpid);
     
    30513038 
    30523039
    3053 char *fbkp, *fbkp1;
    3054   FILE *f0, *f1;
    30553040  if (status != NULL)
    30563041    if (strcasecmp (status->value, "false") == 0)
     
    30633048         &request_input_real_format, &request_output_real_format, &eres,request->out,request->err);
    30643049    }
    3065   else
    3066     {
    3067       int pid;
    3068 #ifdef DEBUG
    3069       fprintf (stderr, "\nPID : %d\n", cpid);
    3070 #endif
    3071 #ifndef WIN32
    3072       pid = fork ();
    3073 #else
    3074       if (cgiSid == NULL)
    3075         {
    3076           createProcess (m,
    3077                          request_inputs,
    3078                          s1,
    3079                          NULL,
    3080                          cpid,
    3081                          request_input_real_format,
    3082                          request_output_real_format);
    3083           pid = cpid;
    3084         }
    3085       else
    3086         {
    3087           pid = 0;
    3088           cpid = atoi (cgiSid);
    3089         }
    3090 #endif
    3091       if (pid > 0)
    3092         {
    3093       /**
    3094        * dady :
    3095        * set status to SERVICE_ACCEPTED
    3096        */
    3097 #ifdef DEBUG
    3098           fprintf (stderr,
    3099                    "father pid continue (origin %d) %d ...\n",
    3100                    cpid, getpid ());
    3101 #endif
    3102           eres = SERVICE_ACCEPTED;
    3103         }
    3104       else if (pid == 0)
    3105         {
    3106       /**
    3107        * son : have to close the stdout, stdin and stderr to let the parent
    3108        * process answer to http client.
    3109        */
    3110           r_inputs = getMapFromMaps (m, "main", "tmpPath");
    3111           map *r_inputs1 = getMap (s1->content,
    3112                                    "ServiceProvider");
    3113           fbkp = (char *)
    3114             malloc ((strlen
    3115                      (r_inputs->value) +
    3116                      strlen (r_inputs1->value) + 1024) * sizeof (char));
    3117           sprintf (fbkp, "%s/%s_%d.xml", r_inputs->value,
    3118                    r_inputs1->value, cpid);
    3119           char *flog =
    3120             (char *)
    3121             malloc ((strlen (r_inputs->value) +
    3122                      strlen (r_inputs1->value) + 1024) * sizeof (char));
    3123           sprintf (flog, "%s/%s_%d_error.log", r_inputs->value,
    3124                    r_inputs1->value, cpid);
    3125 #ifdef DEBUG
    3126           fprintf (stderr, "RUN IN BACKGROUND MODE \n");
    3127           fprintf (stderr,
    3128                    "son pid continue (origin %d) %d ...\n", cpid, getpid ());
    3129           fprintf (stderr, "\nFILE TO STORE DATA %s\n", r_inputs->value);
    3130 #endif
    3131           freopen (flog, "w+", stderr);
    3132           f0 = freopen (fbkp, "w+", stdout);
    3133 #ifndef WIN32
    3134           fclose (stdin);
    3135 #endif
    3136           free (flog);
    3137       /**
    3138        * set status to SERVICE_STARTED and flush stdout to ensure full
    3139        * content was outputed (the file used to store the ResponseDocument).
    3140        * The rewind stdout to restart writing from the bgining of the file,
    3141        * this way the data will be updated at the end of the process run.
    3142        */
    3143           printProcessResponse
    3144             (m, request_inputs,
    3145              cpid, s1,
    3146              r_inputs1->value,
    3147              SERVICE_STARTED,
    3148              request_input_real_format, request_output_real_format,request->out);
    3149 #ifndef WIN32
    3150           fflush (stdout);
    3151           rewind (stdout);
    3152 #else
    3153 #endif
    3154           fbkp1 = (char *)
    3155             malloc ((strlen
    3156                      (r_inputs->value) +
    3157                      strlen (r_inputs1->value) + 1024) * sizeof (char));
    3158           sprintf (fbkp1, "%s/%s_final_%d.xml", r_inputs->value,
    3159                    r_inputs1->value, cpid);
    3160           f1 = freopen (fbkp1, "w+", stdout);
    3161           loadServiceAndRun
    3162             (&m, s1,
    3163              request_inputs,
    3164              &request_input_real_format, &request_output_real_format, &eres,request->out,request->err);
    3165         }
    3166       else
    3167         {
    3168       /**
    3169        * error server don't accept the process need to output a valid
    3170        * error response here !!!
    3171        */
    3172           eres = -1;
    3173           errorException (m,
    3174                           _
    3175                           ("Unable to run the child process properly"),
    3176                           "InternalError", NULL,request->out);
    3177         }
    3178     }
     3050
     3051/* processus en background */
     3052
     3053else
     3054    {
     3055
     3056    eres = SERVICE_ACCEPTED;
     3057    json_object * msg_jobj = json_object_new_object();
     3058    json_object_object_add(msg_jobj,"service_identifier",json_object_new_string(service_identifier));
     3059
     3060
     3061    json_object *maps_obj;
     3062    mapstojson(&maps_obj,m);
     3063    json_object_object_add(msg_jobj,"maps",maps_obj);
     3064
     3065
     3066    json_object *req_format_jobj;
     3067    mapstojson(&req_format_jobj,request_input_real_format);
     3068    json_object_object_add(msg_jobj,"request_input_real_format",req_format_jobj);
     3069
     3070    json_object *req_jobj;
     3071    maptojson(&req_jobj,request_inputs);
     3072    json_object_object_add(msg_jobj,"request_inputs",req_jobj);
     3073
     3074
     3075    json_object *outputs_jobj;
     3076    mapstojson(&outputs_jobj,request_output_real_format);
     3077    json_object_object_add(msg_jobj,"request_output_real_format",outputs_jobj);
     3078   
     3079    if ( (send_msg(json_object_to_json_string(msg_jobj),"application/json") != 0) || (add_status(uuid) != 0) ){     
     3080        eres = SERVICE_FAILED;
     3081    }
     3082
     3083}
     3084
     3085free(uuid);
     3086
     3087/* fin processus background */
     3088
    31793089
    31803090#ifdef DEBUG
     
    32013111  (void) signal (SIGABRT, donothing);
    32023112#endif
    3203   if (((int) getpid ()) != cpid || cgi->cgiSid != NULL)
    3204     {
    3205       fclose (stdout);
    3206       fclose (stderr);
    3207       unhandleStatus (m);
    3208     /**
    3209      * Dump back the final file fbkp1 to fbkp
    3210      */
    3211       fclose (f0);
    3212       fclose (f1);
    3213       FILE *f2 = fopen (fbkp1, "rb");
    3214       FILE *f3 = fopen (fbkp, "wb+");
    3215       free (fbkp);
    3216       fseek (f2, 0, SEEK_END);
    3217       long flen = ftell (f2);
    3218       fseek (f2, 0, SEEK_SET);
    3219       char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
    3220       fread (tmps1, flen, 1, f2);
    3221       fwrite (tmps1, 1, flen, f3);
    3222       fclose (f2);
    3223       fclose (f3);
    3224       unlink (fbkp1);
    3225       free (fbkp1);
    3226       free (tmps1);
    3227     }
    32283113
    32293114  //freeService (&s1);
Note: See TracChangeset for help on using the changeset viewer.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png