Index: branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_service_loader.c
===================================================================
--- branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 553)
+++ branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 604)
@@ -29,5 +29,6 @@
 
 #include "cgic.h"
-
+#include "zoo_amqp.h"
+#include "zoo_sql.h"
 extern "C"
 {
@@ -39,4 +40,6 @@
 #include <libgen.h>
 }
+
+
 #include "ulinet.h"
 #include "service_zcfg.h"
@@ -272,4 +275,5 @@
 }
 
+#include "zoo_json.h"
 void
 loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs,
@@ -277,4 +281,26 @@
 {
  
+ json_object *obj;
+ mapstojson(&obj,*myMap);
+ fprintf(stderr,"myMaps: %s\n",json_object_to_json_string(obj));
+
+
+json_object *obj3;
+mapstojson(&obj3,*inputs);
+fprintf(stderr,"inputs: %s\n",json_object_to_json_string(obj3));
+
+
+json_object *obj1;
+ maptojson(&obj1,request_inputs);
+ fprintf(stderr,"request_inputs: %s\n",json_object_to_json_string(obj1));
+
+json_object *obj2;
+mapstojson(&obj2,*ioutputs);
+fprintf(stderr,"ioutputs: %s\n",json_object_to_json_string(obj2));
+
+
+
+ fflush(stderr);
+
 
  char tmps1[1024];
@@ -425,5 +451,4 @@
 
                   *eres = -1;
-                  return;
                 }
 
@@ -681,5 +706,4 @@
 {
 
-
 struct cgi_env *cgi = *c;
 #ifndef USE_GDB
@@ -694,16 +718,50 @@
   map *r_inputs = NULL;
   map *request_inputs = *inputs;
-  maps *m = NULL;
+  maps *m = (maps *) malloc (MAPS_SIZE);
+ 
+  if (m == NULL)
+    {
+      return errorException (m, _("Unable to allocate memory."),
+                             "InternalError", NULL,request->out);
+    }
+
   char *REQUEST = NULL;
+   map *store = getMap (request_inputs,"storeExecuteResponse");
+   map *status = getMap (request_inputs, "status");
+
+ /**
+   * 05-007r7 WPS 1.0.0 page 57 :
+   * 'If status="true" and storeExecuteResponse is "false" then the service 
+   * shall raise an exception.'
+   */
+  if (status != NULL && strcmp(status->value,"true") == 0 && store != NULL && strcmp (store->value, "false") == 0)
+    {
+      errorException (m,
+                      _
+                      ("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."),
+                      "InvalidParameterValue", "storeExecuteResponse",request->out);
+      //freeService (&s1);
+      //free (s1);
+      freeMaps (&m);
+      free (m);
+      free (REQUEST);
+      return 1;
+    }
+
+
+
+/*
+ Si status =! NULLMAP le service est executÃ© enbackground
+*/
+ if (status != NULL)
+    if (strcasecmp (status->value, "false") == 0)
+      status = NULLMAP;
+
+
+
   /**
    * Parsing service specfic configuration file
    */
   
-  m = (maps *) malloc (MAPS_SIZE);
-  if (m == NULL)
-    {
-      return errorException (m, _("Unable to allocate memory."),
-                             "InternalError", NULL,request->out);
-    }
    
   char ntmp[1024];
@@ -1091,47 +1149,7 @@
 
   s1 = NULL;
-  /**s1 = (service *) malloc (SERVICE_SIZE);
-  if (s1 == NULL)
-    {
-      freeMaps (&m);
-      free (m);
-      free (REQUEST);
-      free (SERVICE_URL);
-      return errorException (m, _("Unable to allocate memory."),
-                             "InternalError", NULL);
-    }
-  r_inputs = getMap (request_inputs, "MetaPath");
-  if (r_inputs != NULL)
-    snprintf (tmps1, 1024, "%s/%s", ntmp, r_inputs->value);
-  else
-    snprintf (tmps1, 1024, "%s/", ntmp);
-   **/
   r_inputs = getMap (request_inputs, "Identifier");
-/**
-  char *ttmp = zStrdup (tmps1);
-  snprintf (tmps1, 1024, "%s/%s.zcfg", ttmp, r_inputs->value);
-  free (ttmp);
-#ifdef DEBUG
-  fprintf (stderr, "Trying to load %s\n", tmps1);
-#endif
-  if (strstr (r_inputs->value, ".") != NULL)
-    {
-      char *identifier = zStrdup (r_inputs->value);
-      parseIdentifier (m, conf_dir, identifier, tmps1);
-      map *tmpMap = getMapFromMaps (m, "lenv", "metapath");
-      if (tmpMap != NULL)
-        addToMap (request_inputs, "metapath", tmpMap->value);
-      free (identifier);
-    }
-  else
-    setMapInMaps (m, "lenv", "Identifier", r_inputs->value);
-  int saved_stdout = dup (fileno (stdout));
-  dup2 (fileno (stderr), fileno (stdout));
-  t = readServiceFile (m, tmps1, &s1, r_inputs->value);
-  fflush (stdout);
-  **/
-  //int saved_stdout = dup (fileno (stdout));
-  //dup2 (saved_stdout, fileno (stdout));
-  s1 = search_service (r_inputs->value);
+  char * service_identifier = zStrdup (r_inputs->value);
+  s1 = search_service (service_identifier);
   if (s1 == NULL)
     {
@@ -1369,7 +1387,4 @@
 #endif
           fflush (stderr);
-#ifdef DEBUG
-          fprintf (stderr, "***%s***\n", pToken);
-#endif
           inputs_as_text[i] =
             (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
@@ -1508,8 +1523,11 @@
 #endif
                       addToMap (tmpmaps->content, tmpn1, tmpx2);
+                        
+                      if (status == NULLMAP){
+
 #ifndef WIN32
-                      if (CHECK_INET_HANDLE (hInternet))
-#endif
-                        {
+                        if (CHECK_INET_HANDLE (hInternet))
+#endif
+                        {    
                           if (loadRemoteFile
                               (&m, &tmpmaps->content, &hInternet, tmpx2) < 0)
@@ -1525,4 +1543,6 @@
                             }
                         }
+                        }
+                        
                       free (tmpx2);
                       addToMap (tmpmaps->content, "Reference", tmpv1 + 1);
@@ -1530,8 +1550,5 @@
                   tmpc = strtok (NULL, "@");
                 }
-#ifdef DEBUG
-              dumpMaps (tmpmaps);
-              fflush (stderr);
-#endif
+              
               if (request_input_real_format == NULL)
                 request_input_real_format = dupMaps (&tmpmaps);
@@ -1694,7 +1711,5 @@
 	     * Header and Body gesture should be added here
 	     */
-#ifdef DEBUG
-                      fprintf (stderr, "REFERENCE\n");
-#endif
+
                       const char *refs[5] =
                         { "mimeType", "encoding", "schema",
@@ -1728,4 +1743,5 @@
                                       && CHECK_INET_HANDLE (hInternet))
                                     {
+                                      if (status == NULLMAP)
                                       if (loadRemoteFile
                                           (&m,
@@ -2882,35 +2898,5 @@
    */
   r_inputs = NULL;
-  map *store = getMap (request_inputs,
-                       "storeExecuteResponse");
-  map *status = getMap (request_inputs, "status");
-  /**
-   * 05-007r7 WPS 1.0.0 page 57 :
-   * 'If status="true" and storeExecuteResponse is "false" then the service 
-   * shall raise an exception.'
-   */
-  if (status != NULL
-      &&
-      strcmp
-      (status->value,
-       "true") == 0 && store != NULL && strcmp (store->value, "false") == 0)
-    {
-      errorException (m,
-                      _
-                      ("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."),
-                      "InvalidParameterValue", "storeExecuteResponse",request->out);
-      //freeService (&s1);
-      //free (s1);
-      freeMaps (&m);
-      free (m);
-      freeMaps (&request_input_real_format);
-      free (request_input_real_format);
-      freeMaps (&request_output_real_format);
-      free (request_output_real_format);
-      free (REQUEST);
-      free (SERVICE_URL);
-      return 1;
-    }
-  r_inputs = getMap (request_inputs, "storeExecuteResponse");
+  //r_inputs = getMap (request_inputs, "storeExecuteResponse");
   int eres = SERVICE_STARTED;
   int cpid = getpid ();
@@ -2936,6 +2922,7 @@
   _tmpMaps->name = zStrdup ("lenv");
   char tmpBuff[100];
-  sprintf (tmpBuff, "%i", (cpid + (int) time (NULL)));
-  _tmpMaps->content = createMap ("usid", tmpBuff);
+  //sprintf (tmpBuff, "%i", (cpid + (int) time (NULL)));
+  char *uuid = get_uuid();
+  _tmpMaps->content = createMap ("usid", uuid);
   _tmpMaps->next = NULL;
   sprintf (tmpBuff, "%i", cpid);
@@ -3051,6 +3038,4 @@
   
 
-char *fbkp, *fbkp1;
-  FILE *f0, *f1;
   if (status != NULL)
     if (strcasecmp (status->value, "false") == 0)
@@ -3063,118 +3048,43 @@
          &request_input_real_format, &request_output_real_format, &eres,request->out,request->err);
     }
-  else
-    {
-      int pid;
-#ifdef DEBUG
-      fprintf (stderr, "\nPID : %d\n", cpid);
-#endif
-#ifndef WIN32
-      pid = fork ();
-#else
-      if (cgiSid == NULL)
-        {
-          createProcess (m,
-                         request_inputs,
-                         s1,
-                         NULL,
-                         cpid,
-                         request_input_real_format,
-                         request_output_real_format);
-          pid = cpid;
-        }
-      else
-        {
-          pid = 0;
-          cpid = atoi (cgiSid);
-        }
-#endif
-      if (pid > 0)
-        {
-      /**
-       * dady :
-       * set status to SERVICE_ACCEPTED
-       */
-#ifdef DEBUG
-          fprintf (stderr,
-                   "father pid continue (origin %d) %d ...\n",
-                   cpid, getpid ());
-#endif
-          eres = SERVICE_ACCEPTED;
-        }
-      else if (pid == 0)
-        {
-      /**
-       * son : have to close the stdout, stdin and stderr to let the parent
-       * process answer to http client.
-       */
-          r_inputs = getMapFromMaps (m, "main", "tmpPath");
-          map *r_inputs1 = getMap (s1->content,
-                                   "ServiceProvider");
-          fbkp = (char *)
-            malloc ((strlen
-                     (r_inputs->value) +
-                     strlen (r_inputs1->value) + 1024) * sizeof (char));
-          sprintf (fbkp, "%s/%s_%d.xml", r_inputs->value,
-                   r_inputs1->value, cpid);
-          char *flog =
-            (char *)
-            malloc ((strlen (r_inputs->value) +
-                     strlen (r_inputs1->value) + 1024) * sizeof (char));
-          sprintf (flog, "%s/%s_%d_error.log", r_inputs->value,
-                   r_inputs1->value, cpid);
-#ifdef DEBUG
-          fprintf (stderr, "RUN IN BACKGROUND MODE \n");
-          fprintf (stderr,
-                   "son pid continue (origin %d) %d ...\n", cpid, getpid ());
-          fprintf (stderr, "\nFILE TO STORE DATA %s\n", r_inputs->value);
-#endif
-          freopen (flog, "w+", stderr);
-          f0 = freopen (fbkp, "w+", stdout);
-#ifndef WIN32
-          fclose (stdin);
-#endif
-          free (flog);
-      /**
-       * set status to SERVICE_STARTED and flush stdout to ensure full 
-       * content was outputed (the file used to store the ResponseDocument).
-       * The rewind stdout to restart writing from the bgining of the file,
-       * this way the data will be updated at the end of the process run.
-       */
-          printProcessResponse
-            (m, request_inputs,
-             cpid, s1,
-             r_inputs1->value,
-             SERVICE_STARTED,
-             request_input_real_format, request_output_real_format,request->out);
-#ifndef WIN32
-          fflush (stdout);
-          rewind (stdout);
-#else
-#endif
-          fbkp1 = (char *)
-            malloc ((strlen
-                     (r_inputs->value) +
-                     strlen (r_inputs1->value) + 1024) * sizeof (char));
-          sprintf (fbkp1, "%s/%s_final_%d.xml", r_inputs->value,
-                   r_inputs1->value, cpid);
-          f1 = freopen (fbkp1, "w+", stdout);
-          loadServiceAndRun
-            (&m, s1,
-             request_inputs,
-             &request_input_real_format, &request_output_real_format, &eres,request->out,request->err);
-        }
-      else
-        {
-      /**
-       * error server don't accept the process need to output a valid 
-       * error response here !!!
-       */
-          eres = -1;
-          errorException (m,
-                          _
-                          ("Unable to run the child process properly"),
-                          "InternalError", NULL,request->out);
-        }
-    }
+
+/* processus en background */
+
+else
+    {
+
+    eres = SERVICE_ACCEPTED;
+    json_object * msg_jobj = json_object_new_object();
+    json_object_object_add(msg_jobj,"service_identifier",json_object_new_string(service_identifier));
+
+
+    json_object *maps_obj;
+    mapstojson(&maps_obj,m);
+    json_object_object_add(msg_jobj,"maps",maps_obj);
+
+
+    json_object *req_format_jobj;
+    mapstojson(&req_format_jobj,request_input_real_format);
+    json_object_object_add(msg_jobj,"request_input_real_format",req_format_jobj);
+
+    json_object *req_jobj;
+    maptojson(&req_jobj,request_inputs);
+    json_object_object_add(msg_jobj,"request_inputs",req_jobj);
+
+
+    json_object *outputs_jobj;
+    mapstojson(&outputs_jobj,request_output_real_format);
+    json_object_object_add(msg_jobj,"request_output_real_format",outputs_jobj);
+   
+    if ( (send_msg(json_object_to_json_string(msg_jobj),"application/json") != 0) || (add_status(uuid) != 0) ){     
+        eres = SERVICE_FAILED;
+    }
+
+}
+
+free(uuid);
+
+/* fin processus background */
+
 
 #ifdef DEBUG
@@ -3201,29 +3111,4 @@
   (void) signal (SIGABRT, donothing);
 #endif
-  if (((int) getpid ()) != cpid || cgi->cgiSid != NULL)
-    {
-      fclose (stdout);
-      fclose (stderr);
-      unhandleStatus (m);
-    /**
-     * Dump back the final file fbkp1 to fbkp
-     */
-      fclose (f0);
-      fclose (f1);
-      FILE *f2 = fopen (fbkp1, "rb");
-      FILE *f3 = fopen (fbkp, "wb+");
-      free (fbkp);
-      fseek (f2, 0, SEEK_END);
-      long flen = ftell (f2);
-      fseek (f2, 0, SEEK_SET);
-      char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
-      fread (tmps1, flen, 1, f2);
-      fwrite (tmps1, 1, flen, f3);
-      fclose (f2);
-      fclose (f3);
-      unlink (fbkp1);
-      free (fbkp1);
-      free (tmps1);
-    }
 
   //freeService (&s1);
