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

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

Fix header files location

  • Property svn:keywords set to Id
File size: 42.6 KB
RevLine 
[822]1/*
2 * Author : Gérald FENOY
3 *
[839]4 * Copyright (c) 2017 GeoLabs SARL
[822]5 *
[839]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 *
[822]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"
[839]33#include "service_callback.h"
34#include "mimetypes.h"
[822]35#include <sys/un.h>
[899]36#include "cgic.h"
[822]37
[839]38typedef struct {
39  maps* conf;
40  char* local_file;
41  char* target_file;
42} local_params;
43
[846]44/**
45 * Add nested outputs to every outputs that is geographic format
46 * @see isGeographic
47 * @param s the service current definition
48 */ 
[839]49void addNestedOutputs(service** s){
50  if((*s)==NULL){
51    return;
52  }   
[877]53  if(*s==NULL || (*s)->outputs==NULL || (*s)->content==NULL){
[839]54    return;
[822]55  }
[839]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");
[862]63    map* useMS=getMap(cur->defaults->content,"useMapserver");
64    if(mimeType!=NULL && useMS!=NULL && strncasecmp(useMS->value,"true",4)==0){
[839]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);
[850]84          if(tmp[i]->format!=NULL)
85            free(tmp[i]->format);
[839]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);
[854]148          free(cur->defaults);
[839]149          cur->defaults=NULL;
150        }
151        if(cur->supported!=NULL){
152          freeIOType(&cur->supported);
[854]153          free(cur->supported);
[839]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");
[862]163      }     
[894]164    }else{
165      if(mimeType!=NULL){
[895]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);
[894]191      }
[839]192    }
193    cur=cur->next;
194  }
[850]195  //dumpElements((*s)->outputs);
[822]196}
197
198/**
[854]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/**
[822]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
[860]317 * @return SERVICE_SUCCEEDED in case of success, -1 or SERVICE_FAILED when failing.
[822]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;
[854]327  // Get the configuration id depending on service type and defined thresholds
328  // then, set the configId key in the lenv section
[839]329  char *serviceType;
[886]330  map* mServiceType=getMap(s->content,"confId");
[839]331  if(mServiceType!=NULL)
332    serviceType=mServiceType->value;
333  else
334    serviceType="HPC";
[822]335  map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
336  map* uuid=getMapFromMaps(*main_conf,"lenv","usid");
[854]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");
[888]351  map* targetPersistentPathMap=getMapFromMaps(*main_conf,configurationId,"remote_persistent_data_path");
[854]352 
[839]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  }
[822]360
361  maps* input=*real_inputs;
362  char **parameters=NULL;
363  int parameters_cnt=0;
[839]364  while(input!=NULL && input->content!=NULL){
[862]365    map* isInRequest=getMap(input->content,"inRequest");
[886]366    map* minNb=getMap(input->content,"minOccurs");
[862]367    if(getMaps(*real_outputs,input->name)==NULL &&
368       ( (isInRequest!=NULL && strncasecmp(isInRequest->value,"true",4)==0)
369         || (minNb!=NULL && atoi(minNb->value)>0) ) ){
[839]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");
[862]382          //addToMap(input->content,"useMapserver","true");
[839]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);
[888]394            map* origin=getMapArray(input->content,"origin",i);
[839]395            char* targetName=strrchr(tmp->value,'/');
[888]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            }
[839]405            setMapArray(input->content,"targetPath",i,targetPath);
406            setMapArray(input->content,"localPath",i,tmp->value);
[862]407            map* tmp1=getMapArray(input->content,"value",i);
408            if(tmp1!=NULL){
409              free(tmp1->value);
410              tmp1->value=strdup("empty");
411            }
[839]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{
[862]416              fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
417              fflush(stderr);
[839]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);
[862]422              fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
423              fflush(stderr);
[839]424            }
425            free(targetPath);
426          }
[862]427          addToUploadQueue(main_conf,input);
[839]428        }else{
429          // ???
430          fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
431          fflush(stderr);
432        }
[822]433      }else{
[839]434        // LitteralData and BboxData
435        if(getMap(input->content,"dataType")!=NULL){
436          // For LitteralData, simply pass the value
[886]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          }
[839]464        }
[822]465      }
466    }
467    input=input->next;
468  }
469
[860]470#ifdef HPC_DEBUG
[839]471  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
[860]472#endif
473  invokeCallback(m,inputs,NULL,1,1);
[862]474  invokeCallback(m,inputs,NULL,2,0);
[854]475  if(getMapFromMaps(m,"lenv","mapError")!=NULL){
[862]476    invokeCallback(*main_conf,inputs,NULL,7,0);
[854]477    return -1;
478  }
[860]479#ifdef HPC_DEBUG
480  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
481  dumpMaps(inputs);
482#endif
[839]483
484  // Upload data on HPC
[854]485  if(runUpload(main_conf)==false){
[862]486    errorException (*main_conf, _("Unable to lock the file for upload!"),
[854]487                    "InternalError", NULL);
[860]488#ifdef HPC_DEBUG
[854]489    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
[860]490#endif
[862]491    invokeCallback(*main_conf,inputs,NULL,7,0);
[860]492#ifdef HPC_DEBUG
[854]493    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
[860]494#endif
[854]495    return -1;
496  }
[860]497#ifdef HPC_DEBUG
[839]498  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
[860]499#endif
[839]500  invokeCallback(m,inputs,NULL,2,1);
[860]501#ifdef HPC_DEBUG
[839]502  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
[860]503#endif
[839]504
[854]505  // Add the filename to generate for every output to parameters
[822]506  input=*real_outputs;
[860]507#ifdef HPC_DEBUG
[854]508  dumpMaps(input);
[860]509#endif
[822]510  while(input!=NULL){
[862]511    // Parse all outputs including inner outputs if required.
[822]512    if(input->child==NULL){
513      // Name every files that should be produced by the service execution
[839]514      map* mime=getMap(input->content,"mimeType");
515      char* targetName;
516      if(mime!=NULL){
517        bool hasExt=false;
518        map* fileExt=getFileExtensionMap(mime->value,&hasExt);
519        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
520        sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
521        freeMap(&fileExt);
522        free(fileExt);
523      }else{
524        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
525        sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
526      }
[822]527      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
528      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
[862]529      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
530      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
531      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
[851]532      free(targetName);
[822]533      setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
[862]534      addToMap(input->content,"generated_url",targetUrl);
535      free(targetUrl);
536      {
[854]537        parameters_cnt+=1;
538        if(parameters_cnt==1)
539          parameters=(char**)malloc(parameters_cnt*sizeof(char*));
540        else
541          parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
542        // We should verify if any optional tag for output is required
543        // (i.e. -out output.tiff *int8*), meaning that we should search
544        // for a corresponding inputs name.
545        map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
546        if(inValue!=NULL){
547          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
548          sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
549        }else{
550          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
551          sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
552        }
[839]553      }
554      free(targetPath);
555    }// In other case it means we need to return the cache_file as generated_file
556    else{
557      // Name every files that should be produced by the service execution
558      map* mime=getMap(input->child->content,"mimeType");
559      char* targetName;
560      if(mime!=NULL){
561        bool hasExt=false;
562        map* fileExt=getFileExtensionMap(mime->value,&hasExt);
563        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
564        sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
565        freeMap(&fileExt);
566        free(fileExt);
567      }else{
568        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
569        sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
570      }
571      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
572      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
[862]573      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
574      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
575      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
[851]576      free(targetName);
[839]577      addToMap(input->content,"generated_file",targetPath);
[854]578      addToMap(input->content,"storage",targetPath);
[862]579      addToMap(input->content,"generated_url",targetUrl);
580      free(targetUrl);
[854]581      if(strcasecmp(input->name,"wms_link")!=0&&
582         strcasecmp(input->name,"wcs_link")!=0 &&
583         strcasecmp(input->name,"wfs_link")!=0){
584        parameters_cnt+=1;
585        if(parameters_cnt==1)
586          parameters=(char**)malloc(parameters_cnt*sizeof(char*));
587        else
588          parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
589        // We should verify if any optional tag for output is required
590        // (i.e. -out output.tiff *int8*), meaning that we should search
591        // for a corresponding inputs name.
592        map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
593        if(inValue!=NULL){
594          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
595          sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
596        }else{
597          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
598          sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
599        }
[839]600      }
601      free(targetPath);
[822]602    }
603    input=input->next;
604  }
605  // Produce the SBATCH File locally
[839]606  char *scriptPath=(char*)malloc((strlen(s->name)+strlen(tmpPath->value)+strlen(uuid->value)+10)*sizeof(char));
[822]607  sprintf(scriptPath,"%s/zoo_%s_%s.sh",tmpPath->value,s->name,uuid->value);
[839]608  setMapInMaps(*main_conf,"lenv","local_script",scriptPath);
[860]609#ifdef HPC_DEBUG
[839]610  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
611  fflush(stderr);
[860]612#endif
[839]613  invokeCallback(m,inputs,NULL,3,0);
[860]614#ifdef HPC_DEBUG
[839]615  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
616  fflush(stderr);
[860]617#endif
[822]618  FILE* scriptFile=fopen(scriptPath,"w+");
[854]619  map* headerMap=getMapFromMaps(*main_conf,configurationId,"jobscript_header");
[822]620  if(headerMap!=NULL){
621    // Use the header file if defined in the HPC section of the main.cfg file
622    struct stat f_status;
623    int s=stat(headerMap->value, &f_status);
624    if(s==0){
625      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
626      FILE* f=fopen(headerMap->value,"rb");
627      fread(fcontent,f_status.st_size,1,f);
628      int fsize=f_status.st_size;
629      fcontent[fsize]=0;
630      fclose(f);
[839]631      fprintf(scriptFile,"%s\n### --- ZOO-Service HEADER end --- ###\n\n",fcontent);
[822]632      free(fcontent);
633    }else
634      fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER (no header found) *** ###\n\n");
635  }else
636    fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER *** ###\n\n");
[854]637  maps* hpc_opts=getMaps(*main_conf,configurationId);
[839]638  if(hpc_opts!=NULL){
639    map* hpc_opts_content=hpc_opts->content;
640    while(hpc_opts_content!=NULL){
[854]641      if(strncasecmp(hpc_opts_content->name,"sbatch_options_",15)==0)
642        fprintf(scriptFile,"#SBATCH --%s=%s\n",strstr(hpc_opts_content->name,"sbatch_options_")+15,hpc_opts_content->value);
[839]643      hpc_opts_content=hpc_opts_content->next;
644    }
[822]645  }
646  fprintf(scriptFile,"#SBATCH --job-name=ZOO-Project_%s_%s\n\n",uuid->value,s->name);
647  map* mods=getMap(s->content,"hpcModules");
648  if(mods!=NULL)
649    fprintf(scriptFile,"#SBATCH --export=MODULES=%s\n",mods->value);
[839]650
[854]651  map* bodyMap=getMapFromMaps(*main_conf,configurationId,"jobscript_body");
[839]652  if(bodyMap!=NULL){
653    // Use the header file if defined in the HPC section of the main.cfg file
654    struct stat f_status;
655    int s=stat(bodyMap->value, &f_status);
656    if(s==0){
657      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
658      FILE* f=fopen(bodyMap->value,"rb");
659      fread(fcontent,f_status.st_size,1,f);
660      int fsize=f_status.st_size;
661      fcontent[fsize]=0;
662      fclose(f);
663      fprintf(scriptFile,"%s\n### --- ZOO-Service BODY end --- ###\n\n",fcontent);
664      free(fcontent);
665    }else
[860]666      fprintf(scriptFile,"\n### *** Default ZOO-Service BODY (no body found) *** ###\n\n");
[839]667  }else
[860]668    fprintf(scriptFile,"\n### *** Default ZOO-Service BODY *** ###\n\n");
[839]669
[822]670  map* sp=getMap(s->content,"serviceProvider");
[839]671 
[822]672  // Require to produce the command line to be executed
673  fprintf(scriptFile,"\n\necho \"Job started at: $(date)\"\n");
674  fprintf(scriptFile,"echo \"Running service: [%s]\"\n",sp->value);
675  fprintf(scriptFile,"%s ",sp->value);
676  for(int i=0;i<parameters_cnt;i++){
677    fprintf(scriptFile," %s",parameters[i]);
678  }
[839]679  for(int i=parameters_cnt-1;i>=0;i--){
680    free(parameters[i]);
681  }
[822]682  free(parameters);
683  fprintf(scriptFile,"\n");
684  fprintf(scriptFile,"echo \"Job finished at: $(date)\"\n");
[877]685  map* footerMap=getMapFromMaps(*main_conf,configurationId,"jobscript_footer");
686  if(footerMap!=NULL){
687    // Use the footer file if defined in the HPC section of the main.cfg file
688    struct stat f_status;
689    int s=stat(footerMap->value, &f_status);
690    if(s==0){
691      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
692      FILE* f=fopen(footerMap->value,"rb");
693      fread(fcontent,f_status.st_size,1,f);
694      int fsize=f_status.st_size;
695      fcontent[fsize]=0;
696      fclose(f);
697      char* ffcontent=(char*)malloc((strlen(fcontent)+(3*strlen(uuid->value))+1)*sizeof(char));
698      sprintf(ffcontent,fcontent,uuid->value,uuid->value,uuid->value);
699      fprintf(scriptFile,"%s\n### --- ZOO-Service FOOTER end --- ###\n\n",ffcontent);
700      free(fcontent);
701    }else
702      fprintf(scriptFile,"### *** Default ZOO-Service FOOTER (footer file failed to load) *** ###\n\n");
703  }else
704      fprintf(scriptFile,"### *** Default ZOO-Service FOOTER (no footer found) *** ###\n\n");
[822]705  fflush(scriptFile);
706  fclose(scriptFile);
[860]707#ifdef HPC_DEBUG
[839]708  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
[860]709#endif
[839]710  invokeCallback(m,inputs,NULL,3,1);
[860]711#ifdef HPC_DEBUG
[839]712  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
[860]713#endif
[822]714
715  // Upload the SBATCH File to the remote host
[860]716#ifdef HPC_DEBUG
[839]717  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
[860]718#endif
[839]719  invokeCallback(m,inputs,NULL,4,0);
[860]720#ifdef HPC_DEBUG
[839]721  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
[860]722#endif
[854]723  targetPathMap=getMapFromMaps(*main_conf,configurationId,"remote_work_path");
[822]724  if(targetPathMap==NULL){
[854]725    setMapInMaps(*main_conf,"lenv","message",_("There is no remote_work_path defined in your section!"));
726    setMapInMaps(*main_conf,"lenv","status","failed");
[862]727    errorException (*main_conf, _("There is no remote_work_path defined in your section!"),
[854]728                    "InternalError", NULL);
[860]729#ifdef HPC_DEBUG
[854]730    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
731    fflush(stderr);
[860]732#endif
[862]733    invokeCallback(*main_conf,NULL,NULL,7,0);
[860]734#ifdef HPC_DEBUG
[854]735    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
736    fflush(stderr);
[860]737#endif
[822]738    return SERVICE_FAILED;
739  }
740  char* targetName=strrchr(scriptPath,'/');
741  char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
742  sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
[839]743  setMapInMaps(*main_conf,"lenv","remote_script",targetPath);
[822]744  SSHCON *test=ssh_connect(*main_conf);
[860]745  int copy0=ssh_copy(*main_conf,scriptPath,targetPath,ssh_get_cnt(*main_conf));
[854]746  unlink(scriptPath);
747  free(scriptPath);
[860]748  if(copy0!=true){
[862]749    setMapInMaps(*main_conf,"lenv","message",_("Unable to upload the script"));
750    invokeCallback(*main_conf,NULL,NULL,7,0);
751    errorException(*main_conf,_("Unable to upload the script"),"NoApplicableCode",NULL);
[860]752    return -1;
753  }
[822]754  // Execute the SBATCH script remotely
[854]755  addReadLocks(main_conf);
756  map* subStr=getMapFromMaps(*main_conf,configurationId,"sbatch_substr");
[822]757  char *command=(char*)malloc((strlen(targetPath)+strlen(targetPathMap->value)+strlen(subStr->value)+strlen(uuid->value)+137)*sizeof(char));
758  sprintf(command,"sbatch %s 2> %s/error_%s.log | sed \"s:%s::g\"",targetPath,targetPathMap->value,uuid->value,subStr->value);
[860]759  if(ssh_exec(*main_conf,command,ssh_get_cnt(m))<=0){
[822]760    // The sbatch command has failed!
761    // Download the error log file from the HPC server
762    char tmpS[1024];
763    free(command);
[839]764    command=(char*)malloc((strlen(targetPathMap->value)+strlen(uuid->value)+11)*sizeof(char));
[822]765    sprintf(command,"%s/error_%s.log",targetPathMap->value,uuid->value);
766    targetName=strrchr(command,'/');
767    free(targetPath);
768    targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(targetName)+2)*sizeof(char));
769    sprintf(targetPath,"%s/%s",tmpPath->value,targetName);
[839]770    if(ssh_fetch(*main_conf,targetPath,command,ssh_get_cnt(m))==0){
[822]771      struct stat f_status;
772      int ts=stat(targetPath, &f_status);
773      if(ts==0) {
774        char* fcontent = NULL;
775        fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
776        FILE* f=fopen(targetPath,"rb");
777        fread(fcontent,f_status.st_size,1,f);
778        int fsize=f_status.st_size;
779        fcontent[fsize]=0;
780        fclose(f);
781        setMapInMaps(*main_conf,"lenv","message",fcontent);
782        free(fcontent);
783      }else
784        setMapInMaps(*main_conf,"lenv","message",_("No message provided"));
785    }else
786      setMapInMaps(*main_conf,"lenv","message",_("Unable to fetch the remote error log file"));
[860]787    tmpPath=getMapFromMaps(m,"lenv","message");
788#ifdef HPC_DEBUG
[850]789    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
790    fflush(stderr);
[860]791#endif
[862]792    invokeCallback(*main_conf,NULL,NULL,7,0);
[860]793#ifdef HPC_DEBUG
[850]794    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
795    fflush(stderr);
[860]796#endif
797    sprintf(tmpS, "Cannot execute the HPC ZOO-Service %s using %s: %s", s->name, configurationId, tmpPath->value);
[862]798    errorException(*main_conf,tmpS,"NoApplicableCode",NULL);
[822]799    free(command);
800    free(targetPath);
801    ssh_close(*main_conf);
[860]802    removeReadLocks(main_conf);
[822]803    return -1;
804  }
[851]805  free(targetPath);
[860]806#ifdef HPC_DEBUG
[839]807  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
808  fflush(stderr);
[860]809#endif
[839]810  invokeCallback(m,NULL,NULL,4,1);
[860]811#ifdef HPC_DEBUG
[839]812  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
813  fflush(stderr);
[860]814#endif
[839]815  free(command);
[860]816#ifdef HPC_DEBUG
[854]817  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
818  fflush(stderr);
[860]819#endif
[822]820
821  struct sockaddr_un addr;
822  memset(&addr, 0, sizeof(addr));
823  addr.sun_family = AF_UNIX;
824  int rc, cl, fd = socket(AF_UNIX, SOCK_STREAM, 0);
[839]825  char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+20));
[822]826  sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,uuid->value);
827  strncpy(addr.sun_path, sname, sizeof(addr.sun_path)-1);
[839]828 
[822]829  if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
830    perror("bind error");
[862]831    setMapInMaps(*main_conf,"lenv","message",_("Unable to bind socket!"));
832    errorException (*main_conf, _("Unable to bind socket!"),
[854]833                    "InternalError", NULL);
[860]834#ifdef HPC_DEBUG
[854]835    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
836    fflush(stderr);
[860]837#endif
[862]838    invokeCallback(*main_conf,NULL,NULL,7,0);
[860]839    removeReadLocks(main_conf);
840#ifdef HPC_DEBUG
[854]841    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
842    fflush(stderr);
[860]843#endif
[822]844    return -1;
845  }
[860]846#ifdef HPC_DEBUG
[854]847  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
848  fflush(stderr);
[860]849#endif
[822]850  if (listen(fd, 5) == -1) {
[839]851    setMapInMaps(*main_conf,"lenv","message",_("Listen error"));
[862]852    errorException (*main_conf, _("Listen error"),
[854]853                    "InternalError", NULL);
[860]854#ifdef HPC_DEBUG
[854]855    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
856    fflush(stderr);
[860]857#endif
[862]858    invokeCallback(*main_conf,NULL,NULL,7,0);
[860]859    removeReadLocks(main_conf);
860#ifdef HPC_DEBUG
[854]861    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
862    fflush(stderr);
[860]863#endif
[822]864    return -1;
865  }
[839]866  if ( (cl = accept(fd, NULL, NULL)) == -1) {
867    setMapInMaps(*main_conf,"lenv","message",_("Accept error"));
[862]868    errorException (*main_conf, _("Accept error"),
[854]869                    "InternalError", NULL);
[860]870#ifdef HPC_DEBUG
[854]871    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
872    fflush(stderr);
[860]873#endif
[862]874    invokeCallback(*main_conf,NULL,NULL,7,0);
[860]875    removeReadLocks(main_conf);
876#ifdef HPC_DEBUG
[854]877    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
878    fflush(stderr);
[860]879#endif
[822]880    return -1;
[839]881  }else{
[860]882#ifdef HPC_DEBUG
[854]883    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
884    fflush(stderr);
[860]885#endif
[839]886    int hasPassed=-1;
887    char buf[11];
888    memset(&buf,0,11);
889    while ( (rc=read(cl,buf,10)) ) {     
890      if(rc==0){
891        setMapInMaps(*main_conf,"lenv","message",_("Read closed"));
[862]892        errorException (*main_conf, _("Read closed"),
[854]893                        "InternalError", NULL);
[860]894#ifdef HPC_DEBUG
[854]895        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
896        fflush(stderr);
[860]897#endif
[862]898        invokeCallback(*main_conf,NULL,NULL,7,0);
[860]899        removeReadLocks(main_conf);
900#ifdef HPC_DEBUG
[854]901        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
902        fflush(stderr);
[860]903#endif
[822]904        return -1;
[839]905      }else{
906        if(rc<0){
907          setMapInMaps(*main_conf,"lenv","message",_("Read error"));
[862]908          errorException (*main_conf, _("Read error"),
[854]909                          "InternalError", NULL);
[860]910#ifdef HPC_DEBUG
[854]911          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
912          fflush(stderr);
[860]913#endif
[862]914          invokeCallback(*main_conf,NULL,NULL,7,0);
[860]915          removeReadLocks(main_conf);
916#ifdef HPC_DEBUG
[854]917          fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
918          fflush(stderr);
[860]919#endif
[822]920          return -1;
921        }
[839]922      }
923      hasPassed=1;
924      res=atoi(buf);
925      unlink(sname);
[854]926      free(sname);
927      removeReadLocks(main_conf);
928 
[839]929      if(res==3){
[860]930#ifdef HPC_DEBUG
[839]931        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
932        fflush(stderr);
[860]933#endif
[839]934        invokeCallback(m,NULL,outputs,5,0);
[860]935#ifdef HPC_DEBUG
[839]936        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
937        fflush(stderr);
[860]938#endif
939
940        // Read informations provided by FinalizeHPC as a configuration file
941        // then, remove the file.
942        map* jobid=getMapFromMaps(*main_conf,"lenv","usid");
943        map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
944        char *filePath=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+15)*sizeof(char));
945        sprintf(filePath,"%s/exec_status_%s",tmpPath->value,jobid->value);
[887]946        maps* lm = (maps *) malloc (MAPS_SIZE);
947        lm->child=NULL;
948        lm->next=NULL;
[860]949        int saved_stdout = dup (fileno (stdout));
950        dup2 (fileno (stderr), fileno (stdout));
[887]951        conf_read(filePath,lm);
[886]952        //dumpMaps(m);
[860]953        fflush(stdout);
954        dup2 (saved_stdout, fileno (stdout));
955        close(saved_stdout);
956        unlink(filePath);
957        free(filePath);
[887]958        addMapsToMaps(main_conf,lm);
959        freeMaps(&lm);
960        free(lm);
[860]961
[839]962        input=*real_outputs;
963        while(input!=NULL){
964          if(input->child==NULL){
965            map* generatedFile=getMap(input->content,"generated_file");
966            if(generatedFile!=NULL){
967              char* filename=strrchr(generatedFile->value,'/');
968              char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
969              sprintf(targetPath,"%s/%s",tmpPath->value,filename);
970              test=ssh_connect(*main_conf);
971              if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
972                setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
973                free(targetPath);
974              }else{
[886]975                map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr");
976                // Added for using sacct in place of scontrol
977                char *sourcePath=NULL;
978                if(hpcStdErr!=NULL){
979                  sourcePath=(char*)malloc((strlen(targetPathMap->value)+strlen(hpcStdErr->value)+2)*sizeof(char));
980                  sprintf(sourcePath,"%s/%s",targetPathMap->value,hpcStdErr->value);
981                }
982                if(hpcStdErr!=NULL && sourcePath!=NULL && ssh_fetch(*main_conf,targetPath,sourcePath,ssh_get_cnt(m))==0){
983                  free(sourcePath);
[862]984                  struct stat f_status;
985                  int ts=stat(targetPath, &f_status);
986                  if(ts==0) {
987                    char* fcontent = NULL;
988                    fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
989                    FILE* f=fopen(targetPath,"rb");
990                    fread(fcontent,f_status.st_size,1,f);
991                    int fsize=f_status.st_size;
992                    fcontent[fsize]=0;
993                    fclose(f);
994                    setMapInMaps(*main_conf,"lenv","message",fcontent);
995                    free(fcontent);
996                  }else{
[886]997                    char *tmpStr=(char*)malloc((strlen(targetPath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
998                    sprintf(tmpStr,_("Unable to fetch the remote file for %s"),targetPath);
[862]999                    setMapInMaps(*main_conf,"lenv","message",tmpStr);
1000                    free(tmpStr);
1001                  }
1002                }else{
1003                  char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
1004                  sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
1005                  setMapInMaps(*main_conf,"lenv","message",tmpStr);
1006                  free(tmpStr);
1007                }
1008                invokeCallback(*main_conf,NULL,NULL,7,0);
[839]1009                return SERVICE_FAILED;
[822]1010              }
[886]1011            }
[839]1012          }else{
1013            map* generatedFile=getMap(input->content,"generated_file");
[862]1014            map* generatedUrl=getMap(input->content,"generated_url");
[839]1015            if(generatedFile!=NULL){
1016              char* filename=strrchr(generatedFile->value,'/');
1017              char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
1018              sprintf(targetPath,"%s/%s",tmpPath->value,filename);
1019              test=ssh_connect(*main_conf);
1020              if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
1021                maps* tmp=getMaps(*real_outputs,input->name);
[854]1022                char serviceName[9];
[839]1023                freeMap(&tmp->content);
1024                free(tmp->content);
1025                tmp->content=NULL;
1026                maps* output=getMaps(*real_outputs,input->name);
1027                setMapInMaps(output->child,"download_link","generated_file",targetPath);
[862]1028                setMapInMaps(output->child,"download_link","generated_url",generatedUrl->value);
[854]1029                setMapInMaps(output->child,"download_link","storage",targetPath);
[839]1030                setMapInMaps(output->child,"download_link","useMapserver","false");
[854]1031                setMapInMaps(output->child,"download_link","replicateStorageNext","true");
1032                setMapInMaps(output->child,"download_link","asReference","true");
1033                setMapInMaps(output->child,"download_link","inRequest","true");
[851]1034                setMapInMaps(output->child,"wms_link","generated_file",targetPath);
[854]1035                setMapInMaps(output->child,"wms_link","storage",targetPath);
[851]1036                setMapInMaps(output->child,"wms_link","useMapserver","true");
1037                setMapInMaps(output->child,"wms_link","msOgc","WMS");
1038                setMapInMaps(output->child,"wms_link","requestedMimeType","image/png");
[854]1039                setMapInMaps(output->child,"wms_link","asReference","true");
1040                if(getMaps(output->child,"wcs_link")!=NULL){
1041                  sprintf(serviceName,"wcs_link");
[862]1042                  setMapInMaps(output->child,"wcs_link","msOgc","WCS");
1043                }else{
[854]1044                  sprintf(serviceName,"wfs_link");
[862]1045                  setMapInMaps(output->child,"wfs_link","msOgc","WFS");
1046                }
[854]1047                setMapInMaps(output->child,serviceName,"storage",targetPath);
1048                setMapInMaps(output->child,serviceName,"generated_file",targetPath);
1049                setMapInMaps(output->child,serviceName,"useMapserver","true");
1050                setMapInMaps(output->child,serviceName,"asReference","true");
1051              }else{
[860]1052                map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr");
[886]1053                char *sourcePath=NULL;
1054                if(hpcStdErr!=NULL){
1055                  dumpMap(hpcStdErr);
1056                  sourcePath=(char*)malloc((strlen(targetPathMap->value)+strlen(hpcStdErr->value)+2)*sizeof(char));
1057                  sprintf(sourcePath,"%s/%s",targetPathMap->value,hpcStdErr->value);
1058                }
1059                if(hpcStdErr!=NULL && sourcePath!=NULL && ssh_fetch(*main_conf,targetPath,sourcePath,ssh_get_cnt(m))==0){
1060                  free(sourcePath);
[860]1061                  struct stat f_status;
1062                  int ts=stat(targetPath, &f_status);
1063                  if(ts==0) {
1064                    char* fcontent = NULL;
1065                    fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
1066                    FILE* f=fopen(targetPath,"rb");
1067                    fread(fcontent,f_status.st_size,1,f);
1068                    int fsize=f_status.st_size;
1069                    fcontent[fsize]=0;
1070                    fclose(f);
1071                    setMapInMaps(*main_conf,"lenv","message",fcontent);
1072                    free(fcontent);
1073                  }else{
[886]1074                    char *tmpStr=(char*)malloc((strlen(targetPath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
1075                    sprintf(tmpStr,_("Unable to fetch the remote file for %s"),targetPath);
[860]1076                    setMapInMaps(*main_conf,"lenv","message",tmpStr);
1077                    free(tmpStr);
1078                  }
1079                }else{
[886]1080                  char *tmpStr=(char*)malloc((strlen(sourcePath)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
1081                  sprintf(tmpStr,_("Unable to fetch the remote file for %s"),sourcePath);
[860]1082                  setMapInMaps(*main_conf,"lenv","message",tmpStr);
1083                  free(tmpStr);
1084                }
1085                invokeCallback(*main_conf,NULL,NULL,7,0);
[854]1086                return SERVICE_FAILED;
[839]1087              }
[851]1088              free(targetPath);
[839]1089            }
[822]1090          }
[839]1091          input=input->next;
[822]1092        }
[854]1093
1094      }else{
1095        // Try to access remotely to the log file and return a more relevant error message
[862]1096        setMapInMaps(*main_conf,"lenv","message",_("HPC Execution failed!"));
1097        errorException (*main_conf, _("HPC Execution failed!"),
[854]1098                        "InternalError", NULL);
[860]1099#ifdef HPC_DEBUG
[854]1100        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1101        fflush(stderr);
[860]1102#endif
[862]1103        invokeCallback(*main_conf,NULL,NULL,7,0);
[860]1104#ifdef HPC_DEBUG
[854]1105        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1106        fflush(stderr);
[860]1107#endif
[822]1108      }
1109    }
[839]1110    if(hasPassed<0){
1111      perror("Failed to read");
1112      setMapInMaps(*main_conf,"lenv","message",_("Unable to parse the value returned by remote execution"));
[862]1113      errorException (*main_conf, _("Unable to parse the value returned by remote execution"),
[854]1114                      "InternalError", NULL);
[860]1115#ifdef HPC_DEBUG
[854]1116      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1117      fflush(stderr);
[860]1118#endif
[862]1119      invokeCallback(*main_conf,NULL,NULL,7,0);
[860]1120#ifdef HPC_DEBUG
[854]1121      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1122      fflush(stderr);
[860]1123#endif
[839]1124      return SERVICE_FAILED;
1125    }
1126  }
[860]1127#ifdef HPC_DEBUG
[850]1128  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1129  fflush(stderr);
[860]1130#endif
[822]1131  ssh_close(*main_conf);
[860]1132#ifdef HPC_DEBUG
[850]1133  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
1134  fflush(stderr);
[860]1135#endif
[822]1136  return res;
1137}
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