source: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.c @ 902

Last change on this file since 902 was 902, checked in by djay, 5 years ago

Make Callback and HPC support independent.

  • Property svn:keywords set to Id
File size: 43.1 KB
Line 
1/*
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2017 GeoLabs SARL
5 *
6 * This work was supported by public funds received in the framework of GEOSUD,
7 * a project (ANR-10-EQPX-20) of the program "Investissements d'Avenir" managed
8 * by the French National Research Agency
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
27 *
28 */
29
30#include "service_internal_hpc.h"
31#include "response_print.h"
32#include "server_internal.h"
33#include "service_callback.h"
34#include "mimetypes.h"
35#include <sys/un.h>
36#include "cgic.h"
37
38typedef struct {
39  maps* conf;
40  char* local_file;
41  char* target_file;
42} local_params;
43
44/**
45 * Add nested outputs to every outputs that is geographic format
46 * @see isGeographic
47 * @param s the service current definition
48 */ 
49void addNestedOutputs(service** s){
50  if((*s)==NULL){
51    return;
52  }   
53  if(*s==NULL || (*s)->outputs==NULL || (*s)->content==NULL){
54    return;
55  }
56  elements *out=(*s)->outputs;
57  elements* cur=out;
58  map* serviceType=getMap((*s)->content,"ServiceType");
59  if(strncmp(serviceType->value,"HPC",3)!=0)
60    return;
61  while(cur!=NULL && cur->defaults!=NULL){
62    map* mimeType=getMap(cur->defaults->content,"mimeType");
63    map* useMS=getMap(cur->defaults->content,"useMapserver");
64    if(mimeType!=NULL && useMS!=NULL && strncasecmp(useMS->value,"true",4)==0){
65      int geo=isGeographic(mimeType->value);
66      if(geo>0){
67        elements *tmp[3]={
68          dupElements(cur),
69          dupElements(cur),
70          dupElements(cur)
71        };
72        char *geoLink="wcs_link";
73        if(geo==2){
74          geoLink="wfs_link";
75        }
76        int i=0;
77        for(;i<3;i++){
78          if(tmp[i]->next!=NULL){
79            freeElements(&tmp[i]->next);
80            free(tmp[i]->next);
81            tmp[i]->next=NULL;
82          }
83          free(tmp[i]->name);
84          if(tmp[i]->format!=NULL)
85            free(tmp[i]->format);
86          tmp[i]->format=zStrdup("ComplexData");
87          freeMap(&tmp[i]->content);
88          free(tmp[i]->content);
89          tmp[i]->content=NULL;
90          switch(i){
91          case 0:
92            tmp[i]->name=zStrdup("download_link");
93            tmp[i]->content=createMap("Title",_("Download link"));
94            addToMap(tmp[i]->content,"Abstract",_("The download link"));
95            addToMap(tmp[i]->defaults->content,"useMapserver","false");
96            if(tmp[i]->supported!=NULL){
97              freeIOType(&tmp[i]->supported);
98              free(tmp[i]->supported);
99              tmp[i]->supported=NULL;
100            }
101            break;
102          case 1:
103            tmp[i]->name=zStrdup("wms_link");
104            tmp[i]->content=createMap("Title",_("WMS link"));
105            addToMap(tmp[i]->content,"Abstract",_("The WMS link"));
106            if(tmp[i]->supported!=NULL && tmp[i]->supported->next!=NULL){
107              freeIOType(&tmp[i]->supported->next);
108              free(tmp[i]->supported->next);
109              tmp[i]->supported->next=NULL;
110            }else{
111              if(tmp[i]->supported!=NULL)
112                addToMap(tmp[i]->supported->content,"useMapserver","true");
113              addToMap(tmp[i]->defaults->content,"useMapserver","true");
114            }
115            break;
116          case 2:
117            if(geo==2){
118              tmp[i]->name=zStrdup("wfs_link");
119              tmp[i]->content=createMap("Title",_("WFS link"));
120              addToMap(tmp[i]->content,"Abstract",_("The WFS link"));
121            }else{
122              tmp[i]->name=zStrdup("wcs_link");
123              tmp[i]->content=createMap("Title",_("WCS link"));
124              addToMap(tmp[i]->content,"Abstract",_("The WCS link"));
125            }
126            if(tmp[i]->supported!=NULL && tmp[i]->supported->next!=NULL &&
127               tmp[i]->supported->next->content!=NULL){
128              freeIOType(&tmp[i]->supported);
129              free(tmp[i]->supported);
130              tmp[i]->supported=NULL;
131              tmp[i]->supported=createIoType();
132              iotype* cnext=cur->supported->next;
133              tmp[i]->supported->content=createMap(cnext->content->name,cnext->content->value);
134              addMapToMap(&tmp[i]->supported->content,cnext->content->next);
135              addToMap(tmp[i]->supported->content,"useMapserver","true");
136            }else
137              addToMap(tmp[i]->defaults->content,"useMapserver","true");
138            break;
139          }
140        }
141        addToElements(&cur->child,tmp[0]);
142        addToElements(&cur->child,tmp[1]);
143        addToElements(&cur->child,tmp[2]);
144        free(cur->format);
145        cur->format=NULL;
146        if(cur->defaults!=NULL){
147          freeIOType(&cur->defaults);
148          free(cur->defaults);
149          cur->defaults=NULL;
150        }
151        if(cur->supported!=NULL){
152          freeIOType(&cur->supported);
153          free(cur->supported);
154          cur->supported=NULL;
155        }
156        freeElements(&tmp[2]);
157        free(tmp[2]);
158        freeElements(&tmp[1]);
159        free(tmp[1]);
160        freeElements(&tmp[0]);
161        free(tmp[0]);
162        //addToMap(cur->content,"internal","true");
163      }     
164    }else{
165      if(mimeType!=NULL){
166        elements *tmp=dupElements(cur);
167        tmp->name=zStrdup("download_link");
168        tmp->content=createMap("Title",_("Download link"));
169        addToMap(tmp->content,"Abstract",_("The download link"));
170        addToMap(tmp->defaults->content,"useMapserver","false");
171        if(tmp->supported!=NULL){
172          freeIOType(&tmp->supported);
173          free(tmp->supported);
174          tmp->supported=NULL;
175        }
176        addToElements(&cur->child,tmp);
177        free(cur->format);
178        cur->format=NULL;
179        if(cur->defaults!=NULL){
180          freeIOType(&cur->defaults);
181          free(cur->defaults);
182          cur->defaults=NULL;
183        }
184        if(cur->supported!=NULL){
185          freeIOType(&cur->supported);
186          free(cur->supported);
187          cur->supported=NULL;
188        }
189        freeElements(&tmp);
190        free(tmp);
191      }
192    }
193    cur=cur->next;
194  }
195  //dumpElements((*s)->outputs);
196}
197
198/**
199 * Acquire a read lock on every files used as input for executing a service.
200 * @param conf the main configuration file map
201 * @return 0 if every file can be locked, -1 if one lock has failed.
202 */
203int addReadLocks(maps** conf){
204  map* queueLengthMap=getMapFromMaps(*conf,"uploadQueue","length");
205  maps* queueMaps=getMaps(*conf,"uploadQueue");
206  if(queueLengthMap!=NULL){
207    int cnt=atoi(queueLengthMap->value);
208    int i=0;
209    for(i=0;i<cnt;i++){
210      map* argv[2]={
211        getMapArray(queueMaps->content,"input",i),
212        getMapArray(queueMaps->content,"localPath",i)
213      };
214      zooLock* lck;
215      if((lck=lockFile(*conf,argv[1]->value,'r'))==NULL){
216        char* templateStr=_("Unable to lock the file for %s in read mode.");
217        char *tmpMessage=(char*)malloc((strlen(templateStr)+strlen(argv[0]->value)+1)*sizeof(char));
218        sprintf(tmpMessage,templateStr,argv[0]->value);
219        setMapInMaps(*conf,"lenv","message",tmpMessage);
220        free(tmpMessage);
221        return -1;
222      }else{
223        if(zoo_file_locks_cnt==0){
224          zoo_file_locks=(zooLock**)malloc(sizeof(zooLock*));
225        }
226        else{
227          zoo_file_locks=(zooLock**)realloc(zoo_file_locks,(zoo_file_locks_cnt+1)*sizeof(zooLock*));
228        }
229        zoo_file_locks[zoo_file_locks_cnt]=lck;
230        zoo_file_locks_cnt++;
231      }
232    }
233  }
234  return 0;
235}
236
237/**
238 * Remove all read locks set for files used as input for executing the service.
239 * @param conf the main configuration maps pointer
240 * @return 0 in case of success, -1 if any error occured. In case of error, one
241 * can refer to the message map array from the lenv section.
242 */
243int removeReadLocks(maps** conf){
244  int res=0;
245  int nberr=0;
246  map* queueLengthMap=getMapFromMaps(*conf,"uploadQueue","length");
247  maps* queueMaps=getMaps(*conf,"uploadQueue");
248  if(queueLengthMap!=NULL){
249    int cnt=atoi(queueLengthMap->value);
250    int i=0;
251    for(i=0;i<cnt;i++){
252      if(unlockFile(*conf,zoo_file_locks[i])<1){
253        map* argv=getMapArray(queueMaps->content,"input",i);
254        char* templateStr=_("Unable to unlock the file for %s after execution.");
255        char *tmpMessage=(char*)malloc((strlen(templateStr)+strlen(argv->value)+1)*sizeof(char));
256        sprintf(tmpMessage,templateStr,argv->value);
257        maps* lenv=getMaps(*conf,"lenv");
258        setMapArray(lenv->content,"message",nberr,tmpMessage);
259        free(tmpMessage);
260        res=-1;
261        nberr++;
262      }
263    }
264  }
265  free(zoo_file_locks);
266  return res;
267}
268
269/**
270 * Get the section name depending on number of features and/or pixels of each
271 * inputs and the threshold defined in a section.
272 * It supposes that your inputs has been published using MapServer support,
273 * implying that the number of features (nb_features), respectively pixels
274 * (nb_pixels), are defined. The section, identified by confId, should contain
275 * preview_max_features and preview_max_pixels defining the threshold values.
276 * @param conf the main configuration file maps pointer
277 * @param inputs the inputs maps pointer
278 * @param confId the section identifier
279 * @return "preview_conf" in case the numbers are lower than the threshold,
280 * "fullres_conf" in other cases.
281 */
282char* getConfiguration(maps** conf,maps** inputs,const char* confId){
283  maps* input=*inputs;
284  map* max_pixels=getMapFromMaps(*conf,confId,"preview_max_pixels");
285  map* max_features=getMapFromMaps(*conf,confId,"preview_max_features");
286  int i_max_pixels=atoi(max_pixels->value);
287  int i_max_features=atoi(max_features->value);
288  while(input!=NULL && input->content!=NULL){
289    map* tmpMap=getMap(input->content,"geodatatype");
290    if(tmpMap!=NULL){
291      map* currentNb;
292      if(strcasecmp(tmpMap->value,"raster")==0 ){
293        currentNb=getMap(input->content,"nb_pixels");
294        if(atoi(currentNb->value)>i_max_pixels)
295          return "fullres_conf";
296      }else{
297        if(strcasecmp(tmpMap->value,"vector")==0 ){
298          currentNb=getMap(input->content,"nb_features");
299          if(atoi(currentNb->value)>i_max_features)
300            return "fullres_conf";
301        }
302      }
303    }
304    input=input->next;
305  }
306  return "preview_conf";
307}
308
309/**
310 * Load and run a HPC Application corresponding to the service.
311 *
312 * @param main_conf the conf maps containing the main.cfg settings
313 * @param request the map containing the HTTP request
314 * @param s the service structure
315 * @param real_inputs the maps containing the inputs
316 * @param real_outputs the maps containing the outputs
317 * @return SERVICE_SUCCEEDED in case of success, -1 or SERVICE_FAILED when failing.
318 */
319int zoo_hpc_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
320  maps* m=*main_conf;
321  maps* inputs=*real_inputs;
322  maps* outputs=*real_outputs;
323  map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd");
324  char *ntmp=tmp0->value;
325  map* tmp=NULL;
326  int res=-1;
327  // Get the configuration id depending on service type and defined thresholds
328  // then, set the configId key in the lenv section
329  char *serviceType;
330  map* mServiceType=getMap(s->content,"confId");
331  if(mServiceType!=NULL)
332    serviceType=mServiceType->value;
333  else
334    serviceType="HPC";
335  map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
336  map* uuid=getMapFromMaps(*main_conf,"lenv","usid");
337  map* confMap=getMapFromMaps(*main_conf,serviceType,getConfiguration(main_conf,real_inputs,serviceType));
338  char * configurationId=confMap->value;
339  setMapInMaps(*main_conf,"lenv","configId",configurationId);
340  // Dump lenv maps again after having set the configId ...
341  char *flenv =
342    (char *)
343    malloc ((strlen (tmpPath->value) + 
344             strlen (uuid->value) + 12) * sizeof (char));
345  sprintf (flenv, "%s/%s_lenv.cfg", tmpPath->value, uuid->value);
346  maps* lenvMaps=getMaps(m,"lenv");
347  dumpMapsToFile(lenvMaps,flenv,0);
348  free(flenv);
349
350  map* targetPathMap=getMapFromMaps(*main_conf,configurationId,"remote_data_path");
351  map* targetPersistentPathMap=getMapFromMaps(*main_conf,configurationId,"remote_persistent_data_path");
352 
353  pthread_t threads_pool[50];
354  // Force the HPC services to be called asynchronously
355  map* isAsync=getMapFromMaps(*main_conf,"lenv","async");
356  if(isAsync==NULL){
357    errorException(*main_conf,_("The synchronous mode is not supported by this type of service"),"NoSuchMode",s->name);
358    return -1;
359  }
360
361  maps* input=*real_inputs;
362  char **parameters=NULL;
363  int parameters_cnt=0;
364  while(input!=NULL && input->content!=NULL){
365    map* isInRequest=getMap(input->content,"inRequest");
366    map* minNb=getMap(input->content,"minOccurs");
367    if(getMaps(*real_outputs,input->name)==NULL &&
368       ( (isInRequest!=NULL && strncasecmp(isInRequest->value,"true",4)==0)
369         || (minNb!=NULL && atoi(minNb->value)>0) ) ){
370      parameters_cnt+=1;
371      if(parameters_cnt==1)
372        parameters=(char**)malloc(parameters_cnt*sizeof(char*));
373      else
374        parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
375      if(getMap(input->content,"mimeType")!=NULL){
376        // Input is ComplexData
377        if(getMap(input->content,"cache_file")==NULL){
378          // Input data has been passed by value
379          // TODO: publish input through MapServer / use output publication
380          dumpMapsValuesToFiles(main_conf,&input);
381          addToMap(input->content,"toPublish","true");
382          //addToMap(input->content,"useMapserver","true");
383        }
384        if(getMap(input->content,"cache_file")!=NULL){
385          map* length=getMap(input->content,"length");
386          if(length==NULL){
387            addToMap(input->content,"length","1");
388            length=getMap(input->content,"length");
389          }
390          int len=atoi(length->value);
391          int i=0;
392          for(i=0;i<len;i++){
393            map* tmp=getMapArray(input->content,"cache_file",i);
394            map* origin=getMapArray(input->content,"origin",i);
395            char* targetName=strrchr(tmp->value,'/');
396            char *targetPath;
397            if(origin!=NULL && strcasecmp(origin->value,"SHARED")==0 && targetPersistentPathMap!=NULL){
398              targetPath=(char*)malloc((strlen(targetPersistentPathMap->value)+strlen(targetName)+2)*sizeof(char));
399              sprintf(targetPath,"%s/%s",targetPersistentPathMap->value,targetName);
400            }
401            else{
402              targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
403              sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
404            }
405            setMapArray(input->content,"targetPath",i,targetPath);
406            setMapArray(input->content,"localPath",i,tmp->value);
407            map* tmp1=getMapArray(input->content,"value",i);
408            if(tmp1!=NULL){
409              free(tmp1->value);
410              tmp1->value=strdup("empty");
411            }
412            if(i==0){
413              parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
414              sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
415            }else{
416              fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
417              fflush(stderr);
418              char *tmpStr=zStrdup(parameters[parameters_cnt-1]);
419              parameters[parameters_cnt-1]=(char*)realloc(parameters[parameters_cnt-1],(strlen(tmpStr)+strlen(targetPath)+2)*sizeof(char));
420              sprintf(parameters[parameters_cnt-1],"%s %s",tmpStr,targetPath);
421              free(tmpStr);
422              fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
423              fflush(stderr);
424            }
425            free(targetPath);
426          }
427          addToUploadQueue(main_conf,input);
428        }else{
429          // ???
430          fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
431          fflush(stderr);
432        }
433      }else{
434        // LitteralData and BboxData
435        if(getMap(input->content,"dataType")!=NULL){
436          // For LitteralData, simply pass the value
437          map* length=getMap(input->content,"length");
438          if(length!=NULL){
439            char* value=NULL;
440            int len=atoi(length->value);
441            int i=0;
442            for(i=0;i<len;i++){
443              map* val=getMapArray(input->content,"value",i);
444              if(val!=NULL){
445                if(value==NULL){
446                  value=(char*)malloc((strlen(val->value)+3)*sizeof(char));
447                  sprintf(value,"\"%s\"",val->value);
448                }
449                else{
450                  value=(char*)realloc(value,(strlen(value)+strlen(val->value)+4)*sizeof(char));
451                  sprintf(value,"%s \"%s\"",value,val->value);
452                }
453              }
454            }
455            if(value!=NULL){
456              parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(value)+3)*sizeof(char));
457              sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,value);
458            }
459          }else{
460            map* val=getMap(input->content,"value");
461            parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(val->value)+5)*sizeof(char));
462            sprintf(parameters[parameters_cnt-1],"-%s \"%s\"",input->name,val->value);
463          }
464        }
465      }
466    }
467    input=input->next;
468  }
469
470#ifdef HPC_DEBUG
471  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
472#endif
473#ifdef USE_CALLBACK
474  invokeCallback(m,inputs,NULL,1,1);
475  invokeCallback(m,inputs,NULL,2,0);
476#endif 
477  if(getMapFromMaps(m,"lenv","mapError")!=NULL){
478#ifdef USE_CALLBACK
479    invokeCallback(*main_conf,inputs,NULL,7,0);
480#endif
481    return -1;
482  }
483#ifdef HPC_DEBUG
484  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
485  dumpMaps(inputs);
486#endif
487
488  // Upload data on HPC
489  if(runUpload(main_conf)==false){
490    errorException (*main_conf, _("Unable to lock the file for upload!"),
491                    "InternalError", NULL);
492#ifdef HPC_DEBUG
493    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
494#endif
495#ifdef USE_CALLBACK
496    invokeCallback(*main_conf,inputs,NULL,7,0);
497#endif
498#ifdef HPC_DEBUG
499    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
500#endif
501    return -1;
502  }
503#ifdef HPC_DEBUG
504  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
505#endif
506#ifdef USE_CALLBACK
507  invokeCallback(m,inputs,NULL,2,1);
508#endif
509#ifdef HPC_DEBUG
510  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
511#endif
512
513  // Add the filename to generate for every output to parameters
514  input=*real_outputs;
515#ifdef HPC_DEBUG
516  dumpMaps(input);
517#endif
518  while(input!=NULL){
519    // Parse all outputs including inner outputs if required.
520    if(input->child==NULL){
521      // Name every files that should be produced by the service execution
522      map* mime=getMap(input->content,"mimeType");
523      char* targetName;
524      if(mime!=NULL){
525        bool hasExt=false;
526        map* fileExt=getFileExtensionMap(mime->value,&hasExt);
527        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
528        sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
529        freeMap(&fileExt);
530        free(fileExt);
531      }else{
532        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
533        sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
534      }
535      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
536      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
537      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
538      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
539      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
540      free(targetName);
541      setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
542      addToMap(input->content,"generated_url",targetUrl);
543      free(targetUrl);
544      {
545        parameters_cnt+=1;
546        if(parameters_cnt==1)
547          parameters=(char**)malloc(parameters_cnt*sizeof(char*));
548        else
549          parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
550        // We should verify if any optional tag for output is required
551        // (i.e. -out output.tiff *int8*), meaning that we should search
552        // for a corresponding inputs name.
553        map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
554        if(inValue!=NULL){
555          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
556          sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
557        }else{
558          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
559          sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
560        }
561      }
562      free(targetPath);
563    }// In other case it means we need to return the cache_file as generated_file
564    else{
565      // Name every files that should be produced by the service execution
566      map* mime=getMap(input->child->content,"mimeType");
567      char* targetName;
568      if(mime!=NULL){
569        bool hasExt=false;
570        map* fileExt=getFileExtensionMap(mime->value,&hasExt);
571        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
572        sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
573        freeMap(&fileExt);
574        free(fileExt);
575      }else{
576        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
577        sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
578      }
579      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
580      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
581      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
582      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
583      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
584      free(targetName);
585      addToMap(input->content,"generated_file",targetPath);
586      addToMap(input->content,"storage",targetPath);
587      addToMap(input->content,"generated_url",targetUrl);
588      free(targetUrl);
589      if(strcasecmp(input->name,"wms_link")!=0&&
590         strcasecmp(input->name,"wcs_link")!=0 &&
591         strcasecmp(input->name,"wfs_link")!=0){
592        parameters_cnt+=1;
593        if(parameters_cnt==1)
594          parameters=(char**)malloc(parameters_cnt*sizeof(char*));
595        else
596          parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
597        // We should verify if any optional tag for output is required
598        // (i.e. -out output.tiff *int8*), meaning that we should search
599        // for a corresponding inputs name.
600        map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
601        if(inValue!=NULL){
602          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
603          sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
604        }else{
605          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
606          sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
607        }
608      }
609      free(targetPath);
610    }
611    input=input->next;
612  }
613  // Produce the SBATCH File locally
614  char *scriptPath=(char*)malloc((strlen(s->name)+strlen(tmpPath->value)+strlen(uuid->value)+10)*sizeof(char));
615  sprintf(scriptPath,"%s/zoo_%s_%s.sh",tmpPath->value,s->name,uuid->value);
616  setMapInMaps(*main_conf,"lenv","local_script",scriptPath);
617#ifdef HPC_DEBUG
618  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
619  fflush(stderr);
620#endif
621#ifdef USE_CALLBACK
622  invokeCallback(m,inputs,NULL,3,0);
623#endif
624#ifdef HPC_DEBUG
625  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
626  fflush(stderr);
627#endif
628  FILE* scriptFile=fopen(scriptPath,"w+");
629  map* headerMap=getMapFromMaps(*main_conf,configurationId,"jobscript_header");
630  if(headerMap!=NULL){
631    // Use the header file if defined in the HPC section of the main.cfg file
632    struct stat f_status;
633    int s=stat(headerMap->value, &f_status);
634    if(s==0){
635      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
636      FILE* f=fopen(headerMap->value,"rb");
637      fread(fcontent,f_status.st_size,1,f);
638      int fsize=f_status.st_size;
639      fcontent[fsize]=0;
640      fclose(f);
641      fprintf(scriptFile,"%s\n### --- ZOO-Service HEADER end --- ###\n\n",fcontent);
642      free(fcontent);
643    }else
644      fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER (no header found) *** ###\n\n");
645  }else
646    fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER *** ###\n\n");
647  maps* hpc_opts=getMaps(*main_conf,configurationId);
648  if(hpc_opts!=NULL){
649    map* hpc_opts_content=hpc_opts->content;
650    while(hpc_opts_content!=NULL){
651      if(strncasecmp(hpc_opts_content->name,"sbatch_options_",15)==0)
652        fprintf(scriptFile,"#SBATCH --%s=%s\n",strstr(hpc_opts_content->name,"sbatch_options_")+15,hpc_opts_content->value);
653      hpc_opts_content=hpc_opts_content->next;
654    }
655  }
656  fprintf(scriptFile,"#SBATCH --job-name=ZOO-Project_%s_%s\n\n",uuid->value,s->name);
657  map* mods=getMap(s->content,"hpcModules");
658  if(mods!=NULL)
659    fprintf(scriptFile,"#SBATCH --export=MODULES=%s\n",mods->value);
660
661  map* bodyMap=getMapFromMaps(*main_conf,configurationId,"jobscript_body");
662  if(bodyMap!=NULL){
663    // Use the header file if defined in the HPC section of the main.cfg file
664    struct stat f_status;
665    int s=stat(bodyMap->value, &f_status);
666    if(s==0){
667      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
668      FILE* f=fopen(bodyMap->value,"rb");
669      fread(fcontent,f_status.st_size,1,f);
670      int fsize=f_status.st_size;
671      fcontent[fsize]=0;
672      fclose(f);
673      fprintf(scriptFile,"%s\n### --- ZOO-Service BODY end --- ###\n\n",fcontent);
674      free(fcontent);
675    }else
676      fprintf(scriptFile,"\n### *** Default ZOO-Service BODY (no body found) *** ###\n\n");
677  }else
678    fprintf(scriptFile,"\n### *** Default ZOO-Service BODY *** ###\n\n");
679
680  map* sp=getMap(s->content,"serviceProvider");
681 
682  // Require to produce the command line to be executed
683  fprintf(scriptFile,"\n\necho \"Job started at: $(date)\"\n");
684  fprintf(scriptFile,"echo \"Running service: [%s]\"\n",sp->value);
685  fprintf(scriptFile,"%s ",sp->value);
686  for(int i=0;i<parameters_cnt;i++){
687    fprintf(scriptFile," %s",parameters[i]);
688  }
689  for(int i=parameters_cnt-1;i>=0;i--){
690    free(parameters[i]);
691  }
692  free(parameters);
693  fprintf(scriptFile,"\n");
694  fprintf(scriptFile,"echo \"Job finished at: $(date)\"\n");
695  map* footerMap=getMapFromMaps(*main_conf,configurationId,"jobscript_footer");
696  if(footerMap!=NULL){
697    // Use the footer file if defined in the HPC section of the main.cfg file
698    struct stat f_status;
699    int s=stat(footerMap->value, &f_status);
700    if(s==0){
701      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
702      FILE* f=fopen(footerMap->value,"rb");
703      fread(fcontent,f_status.st_size,1,f);
704      int fsize=f_status.st_size;
705      fcontent[fsize]=0;
706      fclose(f);
707      char* ffcontent=(char*)malloc((strlen(fcontent)+(3*strlen(uuid->value))+1)*sizeof(char));
708      sprintf(ffcontent,fcontent,uuid->value,uuid->value,uuid->value);
709      fprintf(scriptFile,"%s\n### --- ZOO-Service FOOTER end --- ###\n\n",ffcontent);
710      free(fcontent);
711    }else
712      fprintf(scriptFile,"### *** Default ZOO-Service FOOTER (footer file failed to load) *** ###\n\n");
713  }else
714      fprintf(scriptFile,"### *** Default ZOO-Service FOOTER (no footer found) *** ###\n\n");
715  fflush(scriptFile);
716  fclose(scriptFile);
717#ifdef HPC_DEBUG
718  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
719#endif
720#ifdef USE_CALLBACK
721  invokeCallback(m,inputs,NULL,3,1);
722#endif
723#ifdef HPC_DEBUG
724  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
725#endif
726
727  // Upload the SBATCH File to the remote host
728#ifdef HPC_DEBUG
729  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
730#endif
731#ifdef USE_CALLBACK
732  invokeCallback(m,inputs,NULL,4,0);
733#endif
734#ifdef HPC_DEBUG
735  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
736#endif
737  targetPathMap=getMapFromMaps(*main_conf,configurationId,"remote_work_path");
738  if(targetPathMap==NULL){
739    setMapInMaps(*main_conf,"lenv","message",_("There is no remote_work_path defined in your section!"));
740    setMapInMaps(*main_conf,"lenv","status","failed");
741    errorException (*main_conf, _("There is no remote_work_path defined in your section!"),
742                    "InternalError", NULL);
743#ifdef HPC_DEBUG
744    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
745    fflush(stderr);
746#endif
747#ifdef USE_CALLBACK
748    invokeCallback(*main_conf,NULL,NULL,7,0);
749#endif
750#ifdef HPC_DEBUG
751    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
752    fflush(stderr);
753#endif
754    return SERVICE_FAILED;
755  }
756  char* targetName=strrchr(scriptPath,'/');
757  char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
758  sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
759  setMapInMaps(*main_conf,"lenv","remote_script",targetPath);
760  SSHCON *test=ssh_connect(*main_conf);
761  int copy0=ssh_copy(*main_conf,scriptPath,targetPath,ssh_get_cnt(*main_conf));
762  unlink(scriptPath);
763  free(scriptPath);
764  if(copy0!=true){
765    setMapInMaps(*main_conf,"lenv","message",_("Unable to upload the script"));
766#ifdef USE_CALLBACK
767    invokeCallback(*main_conf,NULL,NULL,7,0);
768#endif
769    errorException(*main_conf,_("Unable to upload the script"),"NoApplicableCode",NULL);
770    return -1;
771  }
772  // Execute the SBATCH script remotely
773  addReadLocks(main_conf);
774  map* subStr=getMapFromMaps(*main_conf,configurationId,"sbatch_substr");
775  char *command=(char*)malloc((strlen(targetPath)+strlen(targetPathMap->value)+strlen(subStr->value)+strlen(uuid->value)+137)*sizeof(char));
776  sprintf(command,"sbatch %s 2> %s/error_%s.log | sed \"s:%s::g\"",targetPath,targetPathMap->value,uuid->value,subStr->value);
777  if(ssh_exec(*main_conf,command,ssh_get_cnt(m))<=0){
778    // The sbatch command has failed!
779    // Download the error log file from the HPC server
780    char tmpS[1024];
781    free(command);
782    command=(char*)malloc((strlen(targetPathMap->value)+strlen(uuid->value)+11)*sizeof(char));
783    sprintf(command,"%s/error_%s.log",targetPathMap->value,uuid->value);
784    targetName=strrchr(command,'/');
785    free(targetPath);
786    targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(targetName)+2)*sizeof(char));
787    sprintf(targetPath,"%s/%s",tmpPath->value,targetName);
788    if(ssh_fetch(*main_conf,targetPath,command,ssh_get_cnt(m))==0){
789      struct stat f_status;
790      int ts=stat(targetPath, &f_status);
791      if(ts==0) {
792        char* fcontent = NULL;
793        fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
794        FILE* f=fopen(targetPath,"rb");
795        fread(fcontent,f_status.st_size,1,f);
796        int fsize=f_status.st_size;
797        fcontent[fsize]=0;
798        fclose(f);
799        setMapInMaps(*main_conf,"lenv","message",fcontent);
800        free(fcontent);
801      }else
802        setMapInMaps(*main_conf,"lenv","message",_("No message provided"));
803    }else
804      setMapInMaps(*main_conf,"lenv","message",_("Unable to fetch the remote error log file"));
805    tmpPath=getMapFromMaps(m,"lenv","message");
806#ifdef HPC_DEBUG
807    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
808    fflush(stderr);
809#endif
810#ifdef USE_CALLBACK
811    invokeCallback(*main_conf,NULL,NULL,7,0);
812#endif
813#ifdef HPC_DEBUG
814    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
815    fflush(stderr);
816#endif
817    sprintf(tmpS, "Cannot execute the HPC ZOO-Service %s using %s: %s", s->name, configurationId, tmpPath->value);
818    errorException(*main_conf,tmpS,"NoApplicableCode",NULL);
819    free(command);
820    free(targetPath);
821    ssh_close(*main_conf);
822    removeReadLocks(main_conf);
823    return -1;
824  }
825  free(targetPath);
826#ifdef HPC_DEBUG
827  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
828  fflush(stderr);
829#endif
830#ifdef USE_CALLBACK
831  invokeCallback(m,NULL,NULL,4,1);
832#endif
833#ifdef HPC_DEBUG
834  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
835  fflush(stderr);
836#endif
837  free(command);
838#ifdef HPC_DEBUG
839  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
840  fflush(stderr);
841#endif
842
843  struct sockaddr_un addr;
844  memset(&addr, 0, sizeof(addr));
845  addr.sun_family = AF_UNIX;
846  int rc, cl, fd = socket(AF_UNIX, SOCK_STREAM, 0);
847  char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+20));
848  sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,uuid->value);
849  strncpy(addr.sun_path, sname, sizeof(addr.sun_path)-1);
850 
851  if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
852    perror("bind error");
853    setMapInMaps(*main_conf,"lenv","message",_("Unable to bind socket!"));
854    errorException (*main_conf, _("Unable to bind socket!"),
855                    "InternalError", NULL);
856#ifdef HPC_DEBUG
857    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
858    fflush(stderr);
859#endif
860#ifdef USE_CALLBACK
861    invokeCallback(*main_conf,NULL,NULL,7,0);
862#endif
863    removeReadLocks(main_conf);
864#ifdef HPC_DEBUG
865    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
866    fflush(stderr);
867#endif
868    return -1;
869  }
870#ifdef HPC_DEBUG
871  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
872  fflush(stderr);
873#endif
874  if (listen(fd, 5) == -1) {
875    setMapInMaps(*main_conf,"lenv","message",_("Listen error"));
876    errorException (*main_conf, _("Listen error"),
877                    "InternalError", NULL);
878#ifdef HPC_DEBUG
879    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
880    fflush(stderr);
881#endif
882#ifdef USE_CALLBACK
883    invokeCallback(*main_conf,NULL,NULL,7,0);
884#endif
885    removeReadLocks(main_conf);
886#ifdef HPC_DEBUG
887    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
888    fflush(stderr);
889#endif
890    return -1;
891  }
892  if ( (cl = accept(fd, NULL, NULL)) == -1) {
893    setMapInMaps(*main_conf,"lenv","message",_("Accept error"));
894    errorException (*main_conf, _("Accept error"),
895                    "InternalError", NULL);
896#ifdef HPC_DEBUG
897    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
898    fflush(stderr);
899#endif
900#ifdef USE_CALLBACK
901    invokeCallback(*main_conf,NULL,NULL,7,0);
902#endif
903    removeReadLocks(main_conf);
904#ifdef HPC_DEBUG
905    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
906    fflush(stderr);
907#endif
908    return -1;
909  }else{
910#ifdef HPC_DEBUG
911    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
912    fflush(stderr);
913#endif
914    int hasPassed=-1;
915    char buf[11];
916    memset(&buf,0,11);
917    while ( (rc=read(cl,buf,10)) ) {     
918      if(rc==0){
919        setMapInMaps(*main_conf,"lenv","message",_("Read closed"));
920        errorException (*main_conf, _("Read closed"),
921                        "InternalError", NULL);
922#ifdef HPC_DEBUG
923        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
924        fflush(stderr);
925#endif
926#ifdef USE_CALLBACK
927        invokeCallback(*main_conf,NULL,NULL,7,0);
928#endif
929        removeReadLocks(main_conf);
930#ifdef HPC_DEBUG
931        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
932        fflush(stderr);
933#endif
934        return -1;
935      }else{
936        if(rc<0){
937          setMapInMaps(*main_conf,"lenv","message",_("Read error"));
938          errorException (*main_conf, _("Read error"),
939                          "InternalError", NULL);
940#ifdef HPC_DEBUG
941          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
942          fflush(stderr);
943#endif
944#ifdef USE_CALLBACK
945          invokeCallback(*main_conf,NULL,NULL,7,0);
946#endif
947          removeReadLocks(main_conf);
948#ifdef HPC_DEBUG
949          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
950          fflush(stderr);
951#endif
952          return -1;
953        }
954      }
955      hasPassed=1;
956      res=atoi(buf);
957      unlink(sname);
958      free(sname);
959      removeReadLocks(main_conf);
960 
961      if(res==3){
962#ifdef HPC_DEBUG
963        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
964        fflush(stderr);
965#endif
966#ifdef USE_CALLBACK
967        invokeCallback(m,NULL,outputs,5,0);
968#endif
969#ifdef HPC_DEBUG
970        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
971        fflush(stderr);
972#endif
973
974        // Read informations provided by FinalizeHPC as a configuration file
975        // then, remove the file.
976        map* jobid=getMapFromMaps(*main_conf,"lenv","usid");
977        map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
978        char *filePath=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+15)*sizeof(char));
979        sprintf(filePath,"%s/exec_status_%s",tmpPath->value,jobid->value);
980        maps* lm = (maps *) malloc (MAPS_SIZE);
981        lm->child=NULL;
982        lm->next=NULL;
983        int saved_stdout = dup (fileno (stdout));
984        dup2 (fileno (stderr), fileno (stdout));
985        conf_read(filePath,lm);
986        //dumpMaps(m);
987        fflush(stdout);
988        dup2 (saved_stdout, fileno (stdout));
989        close(saved_stdout);
990        unlink(filePath);
991        free(filePath);
992        addMapsToMaps(main_conf,lm);
993        freeMaps(&lm);
994        free(lm);
995
996        input=*real_outputs;
997        while(input!=NULL){
998          if(input->child==NULL){
999            map* generatedFile=getMap(input->content,"generated_file");
1000            if(generatedFile!=NULL){
1001              char* filename=strrchr(generatedFile->value,'/');
1002              char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
1003              sprintf(targetPath,"%s/%s",tmpPath->value,filename);
1004              test=ssh_connect(*main_conf);
1005              if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
1006                setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
1007                free(targetPath);
1008              }else{
1009                map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr");
1010                // Added for using sacct in place of scontrol
1011                char *sourcePath=NULL;
1012                if(hpcStdErr!=NULL){
1013                  sourcePath=(char*)malloc((strlen(targetPathMap->value)+strlen(hpcStdErr->value)+2)*sizeof(char));
1014                  sprintf(sourcePath,"%s/%s",targetPathMap->value,hpcStdErr->value);
1015                }
1016                if(hpcStdErr!=NULL && sourcePath!=NULL && ssh_fetch(*main_conf,targetPath,sourcePath,ssh_get_cnt(m))==0){
1017                  free(sourcePath);
1018                  struct stat f_status;
1019                  int ts=stat(targetPath, &f_status);
1020                  if(ts==0) {
1021                    char* fcontent = NULL;
1022                    fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
1023                    FILE* f=fopen(targetPath,"rb");
1024                    fread(fcontent,f_status.st_size,1,f);
1025                    int fsize=f_status.st_size;
1026                    fcontent[fsize]=0;
1027                    fclose(f);
1028                    setMapInMaps(*main_conf,"lenv","message",fcontent);
1029                    free(fcontent);
1030                  }else{
1031                    char *tmpStr=(char*)malloc((strlen(targetPath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
1032                    sprintf(tmpStr,_("Unable to fetch the remote file for %s"),targetPath);
1033                    setMapInMaps(*main_conf,"lenv","message",tmpStr);
1034                    free(tmpStr);
1035                  }
1036                }else{
1037                  char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
1038                  sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
1039                  setMapInMaps(*main_conf,"lenv","message",tmpStr);
1040                  free(tmpStr);
1041                }
1042#ifdef USE_CALLBACK
1043                invokeCallback(*main_conf,NULL,NULL,7,0);
1044#endif
1045                return SERVICE_FAILED;
1046              }
1047            }
1048          }else{
1049            map* generatedFile=getMap(input->content,"generated_file");
1050            map* generatedUrl=getMap(input->content,"generated_url");
1051            if(generatedFile!=NULL){
1052              char* filename=strrchr(generatedFile->value,'/');
1053              char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
1054              sprintf(targetPath,"%s/%s",tmpPath->value,filename);
1055              test=ssh_connect(*main_conf);
1056              if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
1057                maps* tmp=getMaps(*real_outputs,input->name);
1058                char serviceName[9];
1059                freeMap(&tmp->content);
1060                free(tmp->content);
1061                tmp->content=NULL;
1062                maps* output=getMaps(*real_outputs,input->name);
1063                setMapInMaps(output->child,"download_link","generated_file",targetPath);
1064                setMapInMaps(output->child,"download_link","generated_url",generatedUrl->value);
1065                setMapInMaps(output->child,"download_link","storage",targetPath);
1066                setMapInMaps(output->child,"download_link","useMapserver","false");
1067                setMapInMaps(output->child,"download_link","replicateStorageNext","true");
1068                setMapInMaps(output->child,"download_link","asReference","true");
1069                setMapInMaps(output->child,"download_link","inRequest","true");
1070                setMapInMaps(output->child,"wms_link","generated_file",targetPath);
1071                setMapInMaps(output->child,"wms_link","storage",targetPath);
1072                setMapInMaps(output->child,"wms_link","useMapserver","true");
1073                setMapInMaps(output->child,"wms_link","msOgc","WMS");
1074                setMapInMaps(output->child,"wms_link","requestedMimeType","image/png");
1075                setMapInMaps(output->child,"wms_link","asReference","true");
1076                if(getMaps(output->child,"wcs_link")!=NULL){
1077                  sprintf(serviceName,"wcs_link");
1078                  setMapInMaps(output->child,"wcs_link","msOgc","WCS");
1079                }else{
1080                  sprintf(serviceName,"wfs_link");
1081                  setMapInMaps(output->child,"wfs_link","msOgc","WFS");
1082                }
1083                setMapInMaps(output->child,serviceName,"storage",targetPath);
1084                setMapInMaps(output->child,serviceName,"generated_file",targetPath);
1085                setMapInMaps(output->child,serviceName,"useMapserver","true");
1086                setMapInMaps(output->child,serviceName,"asReference","true");
1087              }else{
1088                map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr");
1089                char *sourcePath=NULL;
1090                if(hpcStdErr!=NULL){
1091                  dumpMap(hpcStdErr);
1092                  sourcePath=(char*)malloc((strlen(targetPathMap->value)+strlen(hpcStdErr->value)+2)*sizeof(char));
1093                  sprintf(sourcePath,"%s/%s",targetPathMap->value,hpcStdErr->value);
1094                }
1095                if(hpcStdErr!=NULL && sourcePath!=NULL && ssh_fetch(*main_conf,targetPath,sourcePath,ssh_get_cnt(m))==0){
1096                  free(sourcePath);
1097                  struct stat f_status;
1098                  int ts=stat(targetPath, &f_status);
1099                  if(ts==0) {
1100                    char* fcontent = NULL;
1101                    fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
1102                    FILE* f=fopen(targetPath,"rb");
1103                    fread(fcontent,f_status.st_size,1,f);
1104                    int fsize=f_status.st_size;
1105                    fcontent[fsize]=0;
1106                    fclose(f);
1107                    setMapInMaps(*main_conf,"lenv","message",fcontent);
1108                    free(fcontent);
1109                  }else{
1110                    char *tmpStr=(char*)malloc((strlen(targetPath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
1111                    sprintf(tmpStr,_("Unable to fetch the remote file for %s"),targetPath);
1112                    setMapInMaps(*main_conf,"lenv","message",tmpStr);
1113                    free(tmpStr);
1114                  }
1115                }else{
1116                  char *tmpStr=(char*)malloc((strlen(sourcePath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
1117                  sprintf(tmpStr,_("Unable to fetch the remote file for %s"),sourcePath);
1118                  setMapInMaps(*main_conf,"lenv","message",tmpStr);
1119                  free(tmpStr);
1120                }
1121#ifdef USE_CALLBACK
1122                invokeCallback(*main_conf,NULL,NULL,7,0);
1123#endif
1124                return SERVICE_FAILED;
1125              }
1126              free(targetPath);
1127            }
1128          }
1129          input=input->next;
1130        }
1131
1132      }else{
1133        // Try to access remotely to the log file and return a more relevant error message
1134        setMapInMaps(*main_conf,"lenv","message",_("HPC Execution failed!"));
1135        errorException (*main_conf, _("HPC Execution failed!"),
1136                        "InternalError", NULL);
1137#ifdef HPC_DEBUG
1138        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1139        fflush(stderr);
1140#endif
1141#ifdef USE_CALLBACK
1142        invokeCallback(*main_conf,NULL,NULL,7,0);
1143#endif
1144#ifdef HPC_DEBUG
1145        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1146        fflush(stderr);
1147#endif
1148      }
1149    }
1150    if(hasPassed<0){
1151      perror("Failed to read");
1152      setMapInMaps(*main_conf,"lenv","message",_("Unable to parse the value returned by remote execution"));
1153      errorException (*main_conf, _("Unable to parse the value returned by remote execution"),
1154                      "InternalError", NULL);
1155#ifdef HPC_DEBUG
1156      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1157      fflush(stderr);
1158#endif
1159#ifdef USE_CALLBACK
1160      invokeCallback(*main_conf,NULL,NULL,7,0);
1161#endif
1162#ifdef HPC_DEBUG
1163      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1164      fflush(stderr);
1165#endif
1166      return SERVICE_FAILED;
1167    }
1168  }
1169#ifdef HPC_DEBUG
1170  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1171  fflush(stderr);
1172#endif
1173  ssh_close(*main_conf);
1174#ifdef HPC_DEBUG
1175  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1176  fflush(stderr);
1177#endif
1178  return res;
1179}
Note: See TracBrowser for help on using the repository browser.

Search

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