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

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