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

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

Add download_link nested output for every complex data

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