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

Last change on this file since 851 was 851, checked in by djay, 7 years ago

Invoke callback asynchronously. Still the ZOO-Kernel has still to wait for every requests to finish before stoping its execution.

  • Property svn:keywords set to Id
File size: 23.8 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){
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    if(mimeType!=NULL){
63      int geo=isGeographic(mimeType->value);
64      if(geo>0){
65        elements *tmp[3]={
66          dupElements(cur),
67          dupElements(cur),
68          dupElements(cur)
69        };
70        char *geoLink="wcs_link";
71        if(geo==2){
72          geoLink="wfs_link";
73        }
74        int i=0;
75        for(;i<3;i++){
76          if(tmp[i]->next!=NULL){
77            freeElements(&tmp[i]->next);
78            free(tmp[i]->next);
79            tmp[i]->next=NULL;
80          }
81          free(tmp[i]->name);
82          if(tmp[i]->format!=NULL)
83            free(tmp[i]->format);
84          tmp[i]->format=zStrdup("ComplexData");
85          freeMap(&tmp[i]->content);
86          free(tmp[i]->content);
87          tmp[i]->content=NULL;
88          switch(i){
89          case 0:
90            tmp[i]->name=zStrdup("download_link");
91            tmp[i]->content=createMap("Title",_("Download link"));
92            addToMap(tmp[i]->content,"Abstract",_("The download link"));
93            addToMap(tmp[i]->defaults->content,"useMapserver","false");
94            if(tmp[i]->supported!=NULL){
95              freeIOType(&tmp[i]->supported);
96              free(tmp[i]->supported);
97              tmp[i]->supported=NULL;
98            }
99            break;
100          case 1:
101            tmp[i]->name=zStrdup("wms_link");
102            tmp[i]->content=createMap("Title",_("WMS link"));
103            addToMap(tmp[i]->content,"Abstract",_("The WMS link"));
104            if(tmp[i]->supported!=NULL && tmp[i]->supported->next!=NULL){
105              freeIOType(&tmp[i]->supported->next);
106              free(tmp[i]->supported->next);
107              tmp[i]->supported->next=NULL;
108            }else{
109              if(tmp[i]->supported!=NULL)
110                addToMap(tmp[i]->supported->content,"useMapserver","true");
111              addToMap(tmp[i]->defaults->content,"useMapserver","true");
112            }
113            break;
114          case 2:
115            if(geo==2){
116              tmp[i]->name=zStrdup("wfs_link");
117              tmp[i]->content=createMap("Title",_("WFS link"));
118              addToMap(tmp[i]->content,"Abstract",_("The WFS link"));
119            }else{
120              tmp[i]->name=zStrdup("wcs_link");
121              tmp[i]->content=createMap("Title",_("WCS link"));
122              addToMap(tmp[i]->content,"Abstract",_("The WCS link"));
123            }
124            if(tmp[i]->supported!=NULL && tmp[i]->supported->next!=NULL &&
125               tmp[i]->supported->next->content!=NULL){
126              freeIOType(&tmp[i]->supported);
127              free(tmp[i]->supported);
128              tmp[i]->supported=NULL;
129              tmp[i]->supported=createIoType();
130              iotype* cnext=cur->supported->next;
131              tmp[i]->supported->content=createMap(cnext->content->name,cnext->content->value);
132              addMapToMap(&tmp[i]->supported->content,cnext->content->next);
133              addToMap(tmp[i]->supported->content,"useMapserver","true");
134            }else
135              addToMap(tmp[i]->defaults->content,"useMapserver","true");
136            break;
137          }
138        }
139        addToElements(&cur->child,tmp[0]);
140        addToElements(&cur->child,tmp[1]);
141        addToElements(&cur->child,tmp[2]);
142        free(cur->format);
143        cur->format=NULL;
144        if(cur->defaults!=NULL){
145          freeIOType(&cur->defaults);
146          cur->defaults=NULL;
147        }
148        if(cur->supported!=NULL){
149          freeIOType(&cur->supported);
150          cur->supported=NULL;
151        }
152        freeElements(&tmp[2]);
153        free(tmp[2]);
154        freeElements(&tmp[1]);
155        free(tmp[1]);
156        freeElements(&tmp[0]);
157        free(tmp[0]);
158        //addToMap(cur->content,"internal","true");
159      }
160    }
161    cur=cur->next;
162  }
163  //dumpElements((*s)->outputs);
164}
165
166/**
167 * Load and run a HPC Application corresponding to the service.
168 *
169 * @param main_conf the conf maps containing the main.cfg settings
170 * @param request the map containing the HTTP request
171 * @param s the service structure
172 * @param real_inputs the maps containing the inputs
173 * @param real_outputs the maps containing the outputs
174 */
175int zoo_hpc_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
176  maps* m=*main_conf;
177  maps* inputs=*real_inputs;
178  maps* outputs=*real_outputs;
179  map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd");
180  char *ntmp=tmp0->value;
181  map* tmp=NULL;
182  int res=-1;
183  char *serviceType;
184  map* mServiceType=getMap(s->content,"serviceType");
185  if(mServiceType!=NULL)
186    serviceType=mServiceType->value;
187  else
188    serviceType="HPC";
189  map* targetPathMap=getMapFromMaps(*main_conf,serviceType,"storagePath");
190  map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
191  map* uuid=getMapFromMaps(*main_conf,"lenv","usid");
192  pthread_t threads_pool[50];
193  // Force the HPC services to be called asynchronously
194  map* isAsync=getMapFromMaps(*main_conf,"lenv","async");
195  if(isAsync==NULL){
196    errorException(*main_conf,_("The synchronous mode is not supported by this type of service"),"NoSuchMode",s->name);
197    return -1;
198  }
199
200  maps* input=*real_inputs;
201  char **parameters=NULL;
202  int parameters_cnt=0;
203  while(input!=NULL && input->content!=NULL){
204    if(getMaps(*real_outputs,input->name)==NULL){
205      parameters_cnt+=1;
206      if(parameters_cnt==1)
207        parameters=(char**)malloc(parameters_cnt*sizeof(char*));
208      else
209        parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
210      if(getMap(input->content,"mimeType")!=NULL){
211        // Input is ComplexData
212        if(getMap(input->content,"cache_file")==NULL){
213          // Input data has been passed by value
214          // TODO: publish input through MapServer / use output publication
215          dumpMapsValuesToFiles(main_conf,&input);
216          addToMap(input->content,"toPublish","true");
217        }
218        if(getMap(input->content,"cache_file")!=NULL){
219          map* length=getMap(input->content,"length");
220          if(length==NULL){
221            addToMap(input->content,"length","1");
222            length=getMap(input->content,"length");
223          }
224          int len=atoi(length->value);
225          int i=0;
226          for(i=0;i<len;i++){
227            map* tmp=getMapArray(input->content,"cache_file",i);
228            char* targetName=strrchr(tmp->value,'/');
229            char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
230            sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
231            setMapArray(input->content,"targetPath",i,targetPath);
232            setMapArray(input->content,"localPath",i,tmp->value);
233            addToUploadQueue(main_conf,input);
234            if(i==0){
235              parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
236              sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
237            }else{
238              char *tmpStr=zStrdup(parameters[parameters_cnt-1]);
239              parameters[parameters_cnt-1]=(char*)realloc(parameters[parameters_cnt-1],(strlen(tmpStr)+strlen(targetPath)+2)*sizeof(char));
240              sprintf(parameters[parameters_cnt-1],"%s %s",tmpStr,targetPath);
241              free(tmpStr);
242            }
243            free(targetPath);
244          }
245        }else{
246          // ???
247          fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
248          fflush(stderr);
249        }
250      }else{
251        // LitteralData and BboxData
252        if(getMap(input->content,"dataType")!=NULL){
253          // For LitteralData, simply pass the value
254          map* val=getMap(input->content,"value");
255          parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(val->value)+3)*sizeof(char));
256          sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,val->value);
257        }
258      }
259    }
260    input=input->next;
261  }
262
263  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
264  invokeCallback(m,inputs,NULL,2,0);
265  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
266
267  // Upload data on HPC
268  runUpload(main_conf);
269 
270  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
271  invokeCallback(m,inputs,NULL,2,1);
272  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
273
274  // Add the filename to generate for every output to the parameters
275  input=*real_outputs;
276  // TODO: fix appendOutputParameters
277  //appendOutputParameters(input,parameters,&parameters_cnt,s,uuid,targetPathMap);
278  while(input!=NULL){
279    // TODO: parse all outputs including inner outputs if required.
280    if(input->child==NULL){
281      parameters_cnt+=1;
282      if(parameters_cnt==1)
283        parameters=(char**)malloc(parameters_cnt*sizeof(char*));
284      else
285        parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
286      // Name every files that should be produced by the service execution
287      map* mime=getMap(input->content,"mimeType");
288      char* targetName;
289      if(mime!=NULL){
290        bool hasExt=false;
291        map* fileExt=getFileExtensionMap(mime->value,&hasExt);
292        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
293        sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
294        freeMap(&fileExt);
295        free(fileExt);
296      }else{
297        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
298        sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
299      }
300      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
301      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
302      free(targetName);
303      setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
304      // We should verify if any optional tag for output is required
305      // (i.e. -out output.tiff *int8*), meaning that we should search
306      // for a corresponding inputs name.
307      map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
308      if(inValue!=NULL){
309        parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
310        sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
311      }else{
312        parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
313        sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
314      }
315      free(targetPath);
316    }// In other case it means we need to return the cache_file as generated_file
317    else{
318      parameters_cnt+=1;
319      if(parameters_cnt==1)
320        parameters=(char**)malloc(parameters_cnt*sizeof(char*));
321      else
322        parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
323      // Name every files that should be produced by the service execution
324      map* mime=getMap(input->child->content,"mimeType");
325      char* targetName;
326      if(mime!=NULL){
327        bool hasExt=false;
328        map* fileExt=getFileExtensionMap(mime->value,&hasExt);
329        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
330        sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
331        freeMap(&fileExt);
332        free(fileExt);
333      }else{
334        targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
335        sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
336      }
337      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
338      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
339      free(targetName);
340      addToMap(input->content,"generated_file",targetPath);
341      // We should verify if any optional tag for output is required
342      // (i.e. -out output.tiff *int8*), meaning that we should search
343      // for a corresponding inputs name.
344      map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
345      if(inValue!=NULL){
346        parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
347        sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
348      }else{
349        parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
350        sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
351      }
352      free(targetPath);
353    }
354    input=input->next;
355  }
356 
357  // Produce the SBATCH File locally
358  char *scriptPath=(char*)malloc((strlen(s->name)+strlen(tmpPath->value)+strlen(uuid->value)+10)*sizeof(char));
359  sprintf(scriptPath,"%s/zoo_%s_%s.sh",tmpPath->value,s->name,uuid->value);
360  setMapInMaps(*main_conf,"lenv","local_script",scriptPath);
361  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
362  fflush(stderr);
363  invokeCallback(m,inputs,NULL,3,0);
364  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
365  fflush(stderr);
366  FILE* scriptFile=fopen(scriptPath,"w+");
367  map* headerMap=getMapFromMaps(*main_conf,serviceType,"header");
368  if(headerMap!=NULL){
369    // Use the header file if defined in the HPC section of the main.cfg file
370    struct stat f_status;
371    int s=stat(headerMap->value, &f_status);
372    if(s==0){
373      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
374      FILE* f=fopen(headerMap->value,"rb");
375      fread(fcontent,f_status.st_size,1,f);
376      int fsize=f_status.st_size;
377      fcontent[fsize]=0;
378      fclose(f);
379      fprintf(scriptFile,"%s\n### --- ZOO-Service HEADER end --- ###\n\n",fcontent);
380      free(fcontent);
381    }else
382      fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER (no header found) *** ###\n\n");
383  }else
384    fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER *** ###\n\n");
385  maps* hpc_opts=getMaps(*main_conf,"sbatch_options");
386  if(hpc_opts!=NULL){
387    map* hpc_opts_content=hpc_opts->content;
388    while(hpc_opts_content!=NULL){
389      fprintf(scriptFile,"#SBATCH --%s=%s\n",hpc_opts_content->name,hpc_opts_content->value);
390      hpc_opts_content=hpc_opts_content->next;
391    }
392  }
393  fprintf(scriptFile,"#SBATCH --job-name=ZOO-Project_%s_%s\n\n",uuid->value,s->name);
394  map* mods=getMap(s->content,"hpcModules");
395  if(mods!=NULL)
396    fprintf(scriptFile,"#SBATCH --export=MODULES=%s\n",mods->value);
397
398  map* bodyMap=getMapFromMaps(*main_conf,serviceType,"body");
399  if(bodyMap!=NULL){
400    // Use the header file if defined in the HPC section of the main.cfg file
401    struct stat f_status;
402    int s=stat(bodyMap->value, &f_status);
403    if(s==0){
404      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
405      FILE* f=fopen(bodyMap->value,"rb");
406      fread(fcontent,f_status.st_size,1,f);
407      int fsize=f_status.st_size;
408      fcontent[fsize]=0;
409      fclose(f);
410      fprintf(scriptFile,"%s\n### --- ZOO-Service BODY end --- ###\n\n",fcontent);
411      free(fcontent);
412    }else
413      fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service BODY (no body found) *** ###\n\n");
414  }else
415    fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service BODY *** ###\n\n");
416
417 
418  map* sp=getMap(s->content,"serviceProvider");
419 
420  // Require to produce the command line to be executed
421  fprintf(scriptFile,"\n\necho \"Job started at: $(date)\"\n");
422  fprintf(scriptFile,"echo \"Running service: [%s]\"\n",sp->value);
423  fprintf(scriptFile,"%s ",sp->value);
424  for(int i=0;i<parameters_cnt;i++){
425    fprintf(scriptFile," %s",parameters[i]);
426  }
427  for(int i=parameters_cnt-1;i>=0;i--){
428    free(parameters[i]);
429  }
430  free(parameters);
431  fprintf(scriptFile,"\n");
432  fprintf(scriptFile,"echo \"Job finished at: $(date)\"\n");
433  fflush(scriptFile);
434  fclose(scriptFile);
435 
436  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
437  invokeCallback(m,inputs,NULL,3,1);
438  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
439
440  // Upload the SBATCH File to the remote host
441  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
442  invokeCallback(m,inputs,NULL,4,0);
443  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
444  targetPathMap=getMapFromMaps(*main_conf,serviceType,"executePath");
445  if(targetPathMap==NULL){
446    setMapInMaps(*main_conf,"lenv","message",_("There is no executePath defined in you HPC section!"));
447    return SERVICE_FAILED;
448  }
449  char* targetName=strrchr(scriptPath,'/');
450  char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
451  sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
452  setMapInMaps(*main_conf,"lenv","remote_script",targetPath);
453  SSHCON *test=ssh_connect(*main_conf);
454  ssh_copy(*main_conf,scriptPath,targetPath,ssh_get_cnt(*main_conf));
455 
456  // Execute the SBATCH script remotely
457  map* subStr=getMapFromMaps(*main_conf,"HPC","subStr");
458  char *command=(char*)malloc((strlen(targetPath)+strlen(targetPathMap->value)+strlen(subStr->value)+strlen(uuid->value)+137)*sizeof(char));
459  sprintf(command,"sbatch %s 2> %s/error_%s.log | sed \"s:%s::g\"",targetPath,targetPathMap->value,uuid->value,subStr->value);
460  if(ssh_exec(*main_conf,command,ssh_get_cnt(m))==0){
461    // The sbatch command has failed!
462    // Download the error log file from the HPC server
463    char tmpS[1024];
464    free(command);
465    command=(char*)malloc((strlen(targetPathMap->value)+strlen(uuid->value)+11)*sizeof(char));
466    sprintf(command,"%s/error_%s.log",targetPathMap->value,uuid->value);
467    targetName=strrchr(command,'/');
468    free(targetPath);
469    targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(targetName)+2)*sizeof(char));
470    sprintf(targetPath,"%s/%s",tmpPath->value,targetName);
471    if(ssh_fetch(*main_conf,targetPath,command,ssh_get_cnt(m))==0){
472      struct stat f_status;
473      int ts=stat(targetPath, &f_status);
474      if(ts==0) {
475        char* fcontent = NULL;
476        fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
477        FILE* f=fopen(targetPath,"rb");
478        fread(fcontent,f_status.st_size,1,f);
479        int fsize=f_status.st_size;
480        fcontent[fsize]=0;
481        fclose(f);
482        setMapInMaps(*main_conf,"lenv","message",fcontent);
483        free(fcontent);
484      }else
485        setMapInMaps(*main_conf,"lenv","message",_("No message provided"));
486    }else
487      setMapInMaps(*main_conf,"lenv","message",_("Unable to fetch the remote error log file"));
488    tmpPath=getMapFromMaps(*main_conf,"lenv","message");
489    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
490    fflush(stderr);
491    invokeCallback(m,NULL,NULL,7,1);
492    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
493    fflush(stderr);
494    sprintf(tmpS, "Cannot execute the HPC ZOO-Service %s: %s", s->name, tmpPath->value);
495    errorException(m,tmpS,"NoApplicableCode",NULL);
496    free(command);
497    free(targetPath);
498    ssh_close(*main_conf);
499    sleep(120);
500    return -1;
501  }
502  free(targetPath);
503  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
504  fflush(stderr);
505  invokeCallback(m,NULL,NULL,4,1);
506  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
507  fflush(stderr);
508  free(command);
509
510  struct sockaddr_un addr;
511  memset(&addr, 0, sizeof(addr));
512  addr.sun_family = AF_UNIX;
513  int rc, cl, fd = socket(AF_UNIX, SOCK_STREAM, 0);
514  char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+20));
515  sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,uuid->value);
516  strncpy(addr.sun_path, sname, sizeof(addr.sun_path)-1);
517 
518  if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
519    perror("bind error");
520    sleep(120);
521    return -1;
522  }
523  if (listen(fd, 5) == -1) {
524    setMapInMaps(*main_conf,"lenv","message",_("Listen error"));
525    return -1;
526  }
527  if ( (cl = accept(fd, NULL, NULL)) == -1) {
528    setMapInMaps(*main_conf,"lenv","message",_("Accept error"));
529    return -1;
530  }else{
531    int hasPassed=-1;
532    char buf[11];
533    memset(&buf,0,11);
534    while ( (rc=read(cl,buf,10)) ) {     
535      if(rc==0){
536        sleep(1);
537        setMapInMaps(*main_conf,"lenv","message",_("Read closed"));
538        invokeCallback(m,NULL,NULL,7,1);
539        return -1;
540      }else{
541        if(rc<0){
542          setMapInMaps(*main_conf,"lenv","message",_("Read error"));
543          invokeCallback(m,NULL,NULL,7,1);
544          return -1;
545        }
546      }
547      hasPassed=1;
548      res=atoi(buf);
549      unlink(sname);
550      //free(sname);
551
552      if(res==3){
553        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
554        fflush(stderr);
555        invokeCallback(m,NULL,outputs,5,0);
556        fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
557        fflush(stderr);
558        input=*real_outputs;
559        while(input!=NULL){
560          if(input->child==NULL){
561            map* generatedFile=getMap(input->content,"generated_file");
562            if(generatedFile!=NULL){
563              char* filename=strrchr(generatedFile->value,'/');
564              char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
565              sprintf(targetPath,"%s/%s",tmpPath->value,filename);
566              test=ssh_connect(*main_conf);
567              if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
568                setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
569                free(targetPath);
570              }else{
571                char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
572                sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
573                setMapInMaps(*main_conf,"lenv","message",tmpStr);
574                free(tmpStr);
575                return SERVICE_FAILED;
576              }
577            }       
578          }else{
579            fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
580            fflush(stderr);
581            map* generatedFile=getMap(input->content,"generated_file");
582            if(generatedFile!=NULL){
583              char* filename=strrchr(generatedFile->value,'/');
584              char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
585              sprintf(targetPath,"%s/%s",tmpPath->value,filename);
586              test=ssh_connect(*main_conf);
587              if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
588                maps* tmp=getMaps(*real_outputs,input->name);
589                freeMap(&tmp->content);
590                free(tmp->content);
591                tmp->content=NULL;
592                maps* output=getMaps(*real_outputs,input->name);
593                setMapInMaps(output->child,"download_link","generated_file",targetPath);
594                setMapInMaps(output->child,"download_link","useMapserver","false");
595                setMapInMaps(output->child,"wms_link","generated_file",targetPath);
596                setMapInMaps(output->child,"wms_link","useMapserver","true");
597                setMapInMaps(output->child,"wms_link","msOgc","WMS");
598                setMapInMaps(output->child,"wms_link","requestedMimeType","image/png");
599                setMapInMaps(output->child,"wcs_link","generated_file",targetPath);
600                setMapInMaps(output->child,"wcs_link","useMapserver","true");
601              }
602              free(targetPath);
603            }
604          }
605          input=input->next;
606        }
607      }
608      //free(buf);
609    }
610    if(hasPassed<0){
611      perror("Failed to read");
612      setMapInMaps(*main_conf,"lenv","message",_("Unable to parse the value returned by remote execution"));
613      sleep(120);
614      return SERVICE_FAILED;
615    }
616  }
617  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
618  fflush(stderr);
619  ssh_close(*main_conf);
620  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
621  fflush(stderr);
622  return res;
623}
624
625   
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