source: branches/PublicaMundi_David_integration_01-devel/zoo-project/zoo-kernel/zoo_fpm.c @ 741

Last change on this file since 741 was 741, checked in by david, 9 years ago
File size: 12.7 KB
Line 
1/**
2 * Author : David Saggiorato
3 *
4 *  Copyright 2008-2015 GeoLabs SARL. All rights reserved.
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#define MALLOC_CHECK_ 0
26#define MALLOC_CHECK 0
27
28/**
29 * Specific includes
30 */
31#ifndef WIN32
32/*
33#include "fcgio.h"
34#include "fcgi_config.h"
35*/
36//#include <stdio.h>
37#include <unistd.h>
38#include <fcgiapp.h>
39#endif
40#include <sys/wait.h>
41#include <pthread.h>
42#include <sys/types.h>
43#include <unistd.h>
44#include "service_internal.h"
45
46extern "C"
47{
48#include <string.h>
49#include "logger.h"
50#include <stdlib.h>
51#include <glib.h>
52#include <sys/stat.h>
53#include <ctype.h>
54
55}
56#include "service.h"
57#include "response_print.h"
58#include "zoo_zcfg.h"
59#include "zoo_json.h"
60#include "zoo_amqp.h"
61//#include "zoo_sql.h"
62#include "server_internal.h"
63#include "request_parser.h"
64#include "logger.h"
65
66void
67loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs,
68                   maps ** inputs, maps ** ioutputs, int *eres,FCGX_Stream *out, FCGX_Stream *err);
69
70
71using namespace std;
72
73#ifndef TRUE
74#define TRUE 1
75#endif
76#ifndef FALSE
77#define FALSE -1
78#endif
79
80
81int
82main (int argc, char *argv[])
83{
84 int debug_flag = 0;
85 int background_flag = 0;
86 char *file_value = NULL;
87 int index;
88 int c;
89 opterr = 0;
90 while ((c = getopt (argc, argv, "dbhf:")) != -1)
91      switch (c)
92      {
93      case 'd':
94        debug_flag = 1;
95        break;
96      case 'b':
97        background_flag = 1;
98        break;
99      case 'h':
100        fprintf(stderr,"TODO: need to print help\n");
101        fflush(stderr);
102        return 0;
103      case 'f':
104        file_value = optarg;
105        break;
106      case '?':
107        if (optopt == 'f')
108          fprintf (stderr, "Option -%c requires an argument.\n", optopt);
109        else if (isprint (optopt))
110          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
111        else
112          fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
113        return 1;
114      default:
115        abort ();
116      }
117
118  maps *conf;
119  conf = (maps *) malloc (MAPS_SIZE);
120 
121  int ret = conf_read (file_value, conf);
122  if ( ret == 2){
123    //a verifier mais conf_read ne renvoie jamais 0
124    fprintf(stderr,"Erreur lors de la lecture de %s\n",file_value);
125    return 1;
126  }
127
128  char *rootDir;
129  map *m_rootDir = getMapFromMaps (conf, "server", "rootDir");
130  if (m_rootDir == NULL){
131    fprintf(stderr,"Configuration error: rootDir");
132    return 2;
133  }
134  else {
135   rootDir = (char*)malloc((strlen(m_rootDir->value) +1)*sizeof(char*));
136   strncpy(rootDir,m_rootDir->value,strlen(m_rootDir->value));
137   rootDir[strlen(m_rootDir->value)] = '\0';
138   //freeMap(&m_rootDir);
139  }
140
141char *regDir;
142  map *m_regDir = getMapFromMaps (conf, "main", "registry");
143  if (m_regDir == NULL){
144    fprintf(stderr,"Configuration error: regDir");
145    return 2;
146  }
147  else {
148   regDir = (char*)malloc((strlen(m_regDir->value) +1)*sizeof(char*));
149   strncpy(regDir,m_regDir->value,strlen(m_regDir->value));
150   regDir[strlen(m_regDir->value)] = '\0';
151  }
152 
153  int async_worker;
154  map *m_async_worker = getMapFromMaps (conf, "server", "async_worker"); 
155  if (m_async_worker == NULL){
156    fprintf(stderr,"Configuration error: async_worker not found");
157    return 2;
158  }
159  else {
160    async_worker = atoi(m_async_worker->value);
161    //freeMap(&m_async_worker);
162    if (async_worker == 0){
163        fprintf(stderr,"Configuration error: req_worker");
164        return 2;
165    }
166  }
167
168  int max_requests;
169  map *m_max_requests = getMapFromMaps (conf, "server", "max_requests");
170  if (m_max_requests == NULL){
171    fprintf(stderr,"Configuration error: max_requests");
172    return 2;
173  }
174  else {
175    max_requests = atoi(m_max_requests->value);
176    //freeMap(&m_max_requests);
177    if (max_requests == 0){
178        fprintf(stderr,"Configuration error: max_requests");
179        return 2;
180    }
181  }
182
183  int id_user;
184  map *m_user = getMapFromMaps (conf, "server", "uid");
185  if (m_user == NULL){
186    fprintf(stderr,"Configuration error: id_user");
187    return 2;
188  }
189  else {
190    id_user = atoi(m_user->value);
191    //freeMap(&m_user);
192    if (id_user == 0){
193        fprintf(stderr,"Configuration error: id_user");
194        return 2;
195    }
196  }
197
198
199  int id_group;
200  map *m_group = getMapFromMaps (conf, "server", "gid");
201  if (m_group == NULL){
202    fprintf(stderr,"Configuration error: gid");
203    return 2;
204  }
205  else {
206    id_group = atoi(m_group->value);
207    //freeMap(&m_group);
208    if (id_group == 0){
209        fprintf(stderr,"Configuration error: id_group");
210        return 2;
211    }
212  }
213
214
215  char * amqp_host;
216  map * m_amqp_host = getMapFromMaps (conf, "rabbitmq", "host");
217  if (m_amqp_host == NULL){
218    fprintf(stderr,"Configuration error: [rabbitmq] host");
219    return 2;
220  }
221  else {
222    amqp_host = (char *)malloc((strlen(m_amqp_host->value) +1)*sizeof(char*));
223    strncpy(amqp_host,m_amqp_host->value,strlen(m_amqp_host->value));
224    amqp_host[strlen(m_amqp_host->value)] = '\0';
225 }
226
227  int amqp_port;
228  map *m_amqp_port = getMapFromMaps (conf, "rabbitmq", "port");
229  if (m_amqp_port == NULL){
230    fprintf(stderr,"Configuration error: [rabbitmq] port");
231    return 2;
232  }
233  else {
234    amqp_port = atoi(m_amqp_port->value);
235    if (amqp_port == 0){
236        fprintf(stderr,"Configuration error: [rabbitmq] port");
237        return 2;
238    }
239  }
240
241  char * amqp_user;
242  map * m_amqp_user = getMapFromMaps (conf, "rabbitmq", "user");
243  if (m_amqp_user == NULL){
244    fprintf(stderr,"Configuration error: [rabbitmq] user");
245    return 2;
246  }
247  else {
248    amqp_user = (char *)malloc((strlen(m_amqp_user->value) +1)*sizeof(char*));
249    strncpy(amqp_user,m_amqp_user->value,strlen(m_amqp_user->value));
250    amqp_user[strlen(m_amqp_user->value)] = '\0';
251 }
252
253  char * amqp_passwd;
254  map * m_amqp_passwd = getMapFromMaps (conf, "rabbitmq", "passwd");
255  if (m_amqp_passwd == NULL){
256    fprintf(stderr,"Configuration error: [rabbitmq] passwd");
257    return 2;
258  }
259  else {
260    amqp_passwd = (char *)malloc((strlen(m_amqp_passwd->value) +1)*sizeof(char*));
261    strncpy(amqp_passwd,m_amqp_passwd->value,strlen(m_amqp_passwd->value));
262    amqp_passwd[strlen(m_amqp_passwd->value)] = '\0';
263 }
264
265  char * amqp_exchange;
266  map * m_amqp_exchange = getMapFromMaps (conf, "rabbitmq", "exchange");
267  if (m_amqp_exchange == NULL){
268    fprintf(stderr,"Configuration error: [rabbitmq] exchange");
269    return 2;
270  }
271  else {
272    amqp_exchange = (char *)malloc((strlen(m_amqp_exchange->value) +1)*sizeof(char*));
273    strncpy(amqp_exchange,m_amqp_exchange->value,strlen(m_amqp_exchange->value));
274    amqp_exchange[strlen(m_amqp_exchange->value)] = '\0';
275 }
276
277  char * amqp_routingkey;
278  map * m_amqp_routingkey = getMapFromMaps (conf, "rabbitmq", "routingkey");
279  if (m_amqp_routingkey == NULL){
280    fprintf(stderr,"Configuration error: [amqp] routingkey");
281    return 2;
282  }
283  else {
284    amqp_routingkey = (char *)malloc((strlen(m_amqp_routingkey->value) +1)*sizeof(char*));
285    strncpy(amqp_routingkey,m_amqp_routingkey->value,strlen(m_amqp_routingkey->value));
286    amqp_routingkey[strlen(m_amqp_routingkey->value)] = '\0';
287 }
288
289  char * amqp_queue;
290  map * m_amqp_queue = getMapFromMaps (conf, "rabbitmq", "queue");
291  if (m_amqp_queue == NULL){
292    fprintf(stderr,"Configuration error: [rabbitmq] queue");
293    return 2;
294  }
295  else {
296    amqp_queue = (char *)malloc((strlen(m_amqp_queue->value) +1)*sizeof(char*));
297    strncpy(amqp_queue,m_amqp_queue->value,strlen(m_amqp_queue->value));
298    amqp_queue[strlen(m_amqp_queue->value)] = '\0';
299 }
300
301
302  /*
303  ret = setgid(id_group);
304  if (ret != 0){
305    fprintf(stderr,"Change gid error\n");
306    return 3;
307  }
308
309  ret = setuid(id_user);
310  if (ret != 0){
311    fprintf(stderr,"Change uid error\n");
312    return 3;
313  }
314*/
315
316
317  init_services_conf (rootDir,regDir);
318  init_amqp(amqp_host,amqp_port,amqp_user, amqp_passwd, amqp_exchange, amqp_routingkey,amqp_queue);
319
320
321
322
323
324  int fork_status = fork();
325  if (fork_status == 0){
326    //child
327    int master_async= getpid();
328    int i;
329    int count_request = 0;
330    for (i = 0; i< async_worker; i++){
331        fork_status = fork();
332        if (fork_status == 0){
333            fprintf(stderr,"child async %d \n",getpid());
334            fflush(stderr);
335            break;
336        }
337    }
338   
339    json_object *msg_obj;
340    json_object *maps_obj;
341    maps * map_c;
342    json_object *req_format_jobj;
343    maps * request_input_real_format;
344    json_object *req_jobj;
345    map * request_inputs;
346    json_object *outputs_jobj;
347    maps * request_output_real_format;
348
349    char *msg;
350    int c;
351    int eres;
352    char * service_identifier;
353    service * s1 = NULL;
354   
355    HINTERNET hInternet;
356    HINTERNET res;
357    hInternet = InternetOpen (
358                            "ZooWPSClient\0",
359                            INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
360   
361    while(1){
362        /* mode asynchrone */
363        if( master_async != getpid()){
364            /*traitement des requetes de la queue */
365            bind_amqp();
366            init_consumer();
367            while(1){
368               
369                c = consumer_amqp(&msg);
370                if (c == 0)
371                    break;
372                msg_obj = json_tokener_parse(msg);
373               
374                free(msg);
375                maps_obj = json_object_object_get(msg_obj,"maps");
376
377                map_c = jsontomaps(maps_obj);
378
379                req_format_jobj = json_object_object_get(msg_obj,"request_input_real_format");
380                request_input_real_format = jsontomaps(req_format_jobj);
381
382                req_jobj = json_object_object_get(msg_obj,"request_inputs");
383                request_inputs = jsontomap(req_jobj);
384
385                outputs_jobj = json_object_object_get(msg_obj,"request_output_real_format");
386                request_output_real_format = jsontomaps(outputs_jobj);
387               
388                json_object_put(msg_obj);
389
390                /* traitemement du message */
391               
392                /* Recherche des references */
393               
394                map * m_identifier = getMap (request_inputs, "Identifier");
395               
396                service_identifier = zStrdup (m_identifier->value);
397
398                s1 = search_service (service_identifier);
399                dumpService(s1);
400                free(service_identifier);
401               
402                validateRequest(&map_c,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet,NULL);
403               
404               
405                dumpMaps(request_input_real_format);
406
407                loadServiceAndRun(&map_c, s1,request_inputs,&request_input_real_format, &request_output_real_format, &eres,NULL,NULL);
408                /*
409                if (eres == SERVICE_SUCCEEDED) {
410                    outputResponse (s1,request_input_real_format,request_output_real_format,request_inputs, 0, map_c, eres,NULL,NULL);
411                }
412                */
413                   
414                dumpMaps(request_output_real_format);
415                fprintf(stderr,"################################################################\n");
416                dumpMaps(map_c);
417
418                //outputResponse (s1,request_input_real_format,request_output_real_format,request_inputs, 0, map_c, eres,NULL,NULL);
419
420               
421                freeMaps(&map_c);
422                map_c= NULL;
423               
424                freeMaps(&request_input_real_format);
425                request_input_real_format = NULL;
426
427                dumpMap(request_inputs);
428                freeMap(&request_inputs);
429                request_inputs = NULL;
430               
431                //dumpMaps(request_output_real_format);
432                //freeMaps(&request_output_real_format);
433                //request_output_real_format = NULL;
434                consumer_ack_amqp(c);
435               
436
437            }
438            close_amqp();
439           
440        }
441        else {
442            wait(0);
443            fprintf(stderr,"Master async %d\n",getpid());
444            fprintf(stderr,"New async Child\n");
445            fflush(stderr);
446            exit(1);
447            fork();
448        }
449    }
450
451  }
452   while(1);
453  return 0;
454}
455 
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