source: branches/branch-1.4/zoo-project/zoo-kernel/service_loader.c @ 822

Last change on this file since 822 was 348, checked in by neteler, 13 years ago

set correctly svn propset

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 7.0 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2010 GeoLabs SARL
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/**
26 * Environment variables definitions.
27 * All the following fixed values should be extracted from a configuration main
28 * configuration file for all the potential services.
29 */
30/**
31 * END "Environment variables definitions"
32 */
33#define length(x) (sizeof(x) / sizeof(x[0]))
34
35extern "C" int yylex();
36extern "C" int crlex();
37
38#include <string.h>
39
40#include "service.h"
41#include "service_internal.h"
42#include "service_internal_python.h"
43
44#include <dirent.h>
45#include <signal.h>
46#include <unistd.h>
47#ifndef WIN32
48#include <dlfcn.h>
49#include <libgen.h>
50#endif
51#include <fcntl.h>
52#include <time.h>
53#include <stdarg.h>
54
55void sigint_handler(int sig){
56    printf("Not this time!\n");
57}
58
59int main(int argc, char *argv[])
60{
61  if (argc < 4){
62    printf( "Usage: %s <servicename> <directory>|<definition_file> <REQUEST> [<functionname> <param_1>[...<param_n>]]\n", basename(argv[0]) );
63    return 1;
64  }
65 
66  map* outputs=NULL;
67  /**
68   * Parsing inputs (need a loop over all files in the service path !!)
69   */
70  maps* m;
71  m=(maps*)malloc(MAP_SIZE);
72  conf_read("main.cfg",m);
73  map* tmpm=getMapFromMaps(m,"main","serverAddress");
74  int toto=count(tmpm);
75  //printf(" - %i \n",toto);
76
77  if(tmpm!=NULL)
78    SERVICE_URL=strdup(tmpm->value);
79  else
80    SERVICE_URL=DEFAULT_SERVICE_URL;
81
82  service* s[100];
83  service* s1;
84  int scount=0;
85
86  if(strcmp(argv[3],"GetCapabilities")==0){
87    int i=0;
88    struct dirent *dp;
89    DIR *dirp = opendir(argv[1]);
90    int t;
91    xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
92    xmlNodePtr n = printGetCapabilitiesHeader(doc,argv[2],m);
93   
94    int saved_stdout = dup(fileno(stdout));
95    stdout = freopen("/dev/null" , "w" , stdout);
96    while ((dp = readdir(dirp)) != NULL)
97      if(strstr(dp->d_name,".zcfg")!=0){
98        char toto1[1024];
99        sprintf(toto1,"%s%s",argv[1],dp->d_name);
100        char *toto=toto1;
101        s1=(service*)malloc(sizeof(char*)+(MAP_SIZE*2)+(2*ELEMENTS_SIZE));
102        //s[scount]=(service*)malloc(sizeof(service*));
103        //#ifdef DEBUG
104        fprintf(stderr,"#################\n%s\n#################\n",toto1);
105        //printf("|(1)");
106        //#endif
107        t=getServiceFromFile(toto1,&s1);
108       
109        //printf("|(2)");
110        printGetCapabilitiesForProcess(m,n,s1);
111        /**
112         * Valgrind told us that there is an issue regarding a
113         * "conditional jump or move depends on uninitialised value(s)" for
114         * freeIOType
115         */
116        //freeService(&s1);
117        scount++;
118      }
119    char buf[20];
120    sprintf(buf, "/dev/fd/%d", saved_stdout);
121    stdout = freopen(buf , "w" , stdout);
122
123    printDocument(doc);
124    fflush(stdout);
125    free(m);
126    return 0;
127  }
128  else{
129    s1=(service*)malloc(sizeof(char*)+(MAP_SIZE*2)+(2*ELEMENTS_SIZE));
130    //s[0]=(service*)malloc(sizeof(service*));
131    int t=getServiceFromFile(argv[1],&s1);
132    if(strcmp(argv[3],"DescribeProcess")==0){
133      printDescribeProcessResponse(s1,argv[2]);
134      //dumpMaps(m);
135      //free(s1);
136      return 0;
137    }
138    else
139      if(strcmp(argv[3],"Execute")!=0){
140        fprintf(stderr,"");
141        //free(s);
142        return 0;
143      }
144  }
145  //dumpService(s);
146  s[0]=s1;
147  map* inputs=NULL;
148  elements* c_inputs=s1->inputs;
149  int j;
150  for(j=0;j<argc-5;j++){
151    //dumpElements(c_inputs);
152    if(inputs!=NULL)
153      addToMap(inputs,c_inputs->name,argv[j+5]);
154    else
155      inputs=createMap(c_inputs->name,argv[j+5]);
156    if(c_inputs->next!=NULL || j+1>=argc-5)
157      c_inputs=c_inputs->next;
158    else{
159      map* tmps=createMap("text","ERROR you provided more inputs than requested.");
160      printExceptionReportResponse(m,tmps);
161      //printf("ERROR you provided more inputs than requested.");
162      return -1;
163    }
164#ifdef DEBUG
165    printf("ARGV1 %d %s\n",j,inputs->value);
166#endif
167  }
168
169#ifdef DEBUG
170  dumpMap(inputs);
171#endif
172
173  const struct tm *tm;
174  size_t len;
175  time_t now;
176  char *sDate;
177 
178  now = time ( NULL );
179  tm = localtime ( &now );
180
181  sDate = new char[TIME_SIZE];
182
183  len = strftime ( sDate, TIME_SIZE, "%d-%B-%YT%I:%M:%SZ", tm );
184
185#ifdef DEBUG
186  printf("Trying to load %s\n", argv[2]);
187#endif
188  void* so = dlopen(argv[2], RTLD_LAZY);
189  char *errstr;
190  errstr = dlerror();
191  if( so != NULL ) {
192    typedef int (*execute_t)(map**,map**);
193#ifdef DEBUG
194    printf("Library loaded %s \n",errstr);
195#endif
196    execute_t execute=(execute_t)dlsym(so,argv[4]);
197#ifdef DEBUG
198    errstr = dlerror();
199    printf("Function loaded %s\n",errstr);
200#endif 
201
202    /**
203     * Need to check if we need to fork to load a status enabled
204     */
205    char _toto[10];
206    sprintf(_toto,"input_%i",argc-5);
207    map* toto=getMap(inputs,_toto);
208    if(strcmp(argv[argc-1],"bg")!=0){
209#ifdef DEBUG
210      printf("RUN IN NORMAL MODE \n");
211#endif
212      int res=execute(&inputs,&outputs);
213#ifdef DEBUG
214      printf("RUNNED IN NORMAL MODE \n");
215      dumpMap(inputs);
216      dumpMap(outputs);
217#endif
218      printProcessResponse(m,getpid(),s[0],argv[2],res,inputs,outputs);
219    }
220    else{
221      pid_t   pid;
222      int cpid=getpid();
223      pid = fork ();
224      if (pid > 0) {
225        /**
226         * dady :
227         * set status to SERVICE_ACCEPTED
228         */
229        printProcessResponse(m,pid,s[0],argv[2],SERVICE_ACCEPTED,inputs,outputs);
230      }else if (pid == 0) {
231        /* son */
232        if (signal(SIGINT, sigint_handler) == SIG_ERR) {
233          printf("signal");
234          map* tmps=createMap("text","father received sigint.");
235          printExceptionReportResponse(m,tmps);
236          exit(1);
237        }
238#ifdef DEBUG
239        printf("RUN IN BACKGROUND MODE \n");
240#endif
241        char tmp1[256];
242        sprintf(tmp1,"service/temp/%s_%d.xml",argv[2],getpid());
243        stdout = freopen(tmp1 , "w+" , stdout);
244        /**
245         * set status to SERVICE_STARTED
246         */
247        printProcessResponse(m,getpid(),s[0],argv[2],SERVICE_STARTED,inputs,outputs);
248        fflush(stdout);
249        rewind(stdout);
250        int t=execute(&inputs,&outputs);
251        /**
252         * set status to status code returned by the service function
253         */
254        printProcessResponse(m,getpid(),s[0],argv[2],t,inputs,outputs);
255      } else {
256        /* error */
257      }
258    }
259#ifdef DEBUG
260    errstr = dlerror();
261    printf("Function successfully loaded %s, unloading now.\n",errstr);
262#endif
263    dlclose(so);
264  }
265  else {
266#ifdef DEBUG
267    printf("C Library can't be loaded %s \n",errstr);
268#endif
269    python_support(m,s[0],argc,argv,inputs,outputs);
270  }
271  return 0;
272}
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

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