source: trunk/zoo-kernel/service.h @ 100

Last change on this file since 100 was 92, checked in by djay, 13 years ago

Give ZOO-Services capabilities to return a Cookie to the client and to store persistent informations, using senv maps in main_conf, on the server side. Bug fix in addDefaultValues.

File size: 14.8 KB
RevLine 
[1]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#ifndef ZOO_SERVICE_H
26#define ZOO_SERVICE_H 1
27
28#pragma once
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include <stdlib.h>
35#include <ctype.h>
36#include <stdio.h>
37#include <string.h>
38
39#define bool int
40#define true 1
41#define false -1
[9]42
[1]43#define SERVICE_ACCEPTED 0
44#define SERVICE_STARTED 1
45#define SERVICE_PAUSED 2
46#define SERVICE_SUCCEEDED 3
47#define SERVICE_FAILED 4
[9]48
[1]49#define ELEMENTS_SIZE (sizeof(char*)+(((2*sizeof(char*))+sizeof(maps*))*2)+sizeof(char*)+(((2*sizeof(char*))+sizeof(iotype*))*2)+sizeof(elements*))
[9]50#define MAP_SIZE (2*sizeof(char*))+sizeof(NULL)
51#define IOTYPE_SIZE MAP_SIZE+sizeof(NULL)
52#define MAPS_SIZE (2*sizeof(char*))+sizeof(map*)+MAP_SIZE
53#define SERVICE_SIZE (ELEMENTS_SIZE*2)+(MAP_SIZE*2)+sizeof(char*)
[1]54
[26]55#define SHMSZ     27
[1]56
[9]57  /**
58   * \struct maps
59   * \brief linked list of map pointer
60   *
61   * Small object to store WPS KVP set.
62   */
[1]63  typedef struct maps{
[9]64    char* name;         
65    struct map* content; 
66    struct maps* next;   
[1]67  } maps;
68
[9]69  /**
70   * \struct map
71   * \brief KVP linked list
72   *
73   * Deal with WPS KVP (name,value).
74   */
[1]75  typedef struct map{
[9]76    char* name;       /* The key */
77    char* value;      /* The value */
78    struct map* next; /* Next couple */
[1]79  } map;
80
81#ifdef WIN32
82#define NULLMAP ((map*) 0)
83#else
84#define NULLMAP NULL
85#endif
86
[9]87  static void _dumpMap(map* t){
[1]88    if(t!=NULL){
89      fprintf(stderr,"[%s] => [%s] \n",t->name,t->value);
90      fflush(stderr);
91    }else{
92      fprintf(stderr,"NULL\n");
93      fflush(stderr);
94    }
95  }
96
[9]97  static void dumpMap(map* t){
[1]98    map* tmp=t;
99    while(tmp!=NULL){
100      _dumpMap(tmp);
101      tmp=tmp->next;
102    }
103  }
104
[92]105  static void dumpMapToFile(map* t,FILE* file){
106    map* tmp=t;
107    while(tmp!=NULL){
108      fprintf(file,"%s = %s\n",t->name,t->value);
109      tmp=tmp->next;
110    }
111  }
112
[1]113  static void dumpMaps(maps* m){
114    maps* tmp=m;
115    while(tmp!=NULL){
116      fprintf(stderr,"MAP => [%s] \n",tmp->name);
117      dumpMap(tmp->content);
118      tmp=tmp->next;
119    }
120  }
121
[92]122  static void dumpMapsToFile(maps* m,FILE* file){
123    maps* tmp=m;
124    if(tmp!=NULL){
125      fprintf(file,"[%s]\n",tmp->name);
126      dumpMapToFile(tmp->content,file);
127    }
128  }
129
[9]130  static map* createMap(const char* name,const char* value){
[1]131    map* tmp=(map *)malloc(MAP_SIZE);
132    tmp->name=strdup(name);
133    tmp->value=strdup(value);
134    tmp->next=NULL;
135    return tmp;
136  }
137
138  static int count(map* m){
139    map* tmp=m;
140    int c=0;
141    while(tmp!=NULL){
142      c++;
143      tmp=tmp->next;
144    }
145    return c;
146  }
147   
[9]148  static bool hasKey(map* m,const char *key){
[1]149    map* tmp=m;
150    while(tmp!=NULL){
[57]151      if(strcasecmp(tmp->name,key)==0)
[1]152        return true;
153      tmp=tmp->next;
154    }
155#ifdef DEBUG_MAP
156    fprintf(stderr,"NOT FOUND \n");
157#endif
158    return false;
159  }
160
[9]161  static maps* getMaps(maps* m,const char *key){
[1]162    maps* tmp=m;
163    while(tmp!=NULL){
[57]164      if(strcasecmp(tmp->name,key)==0){
[1]165        return tmp;
[9]166      }
[1]167      tmp=tmp->next;
168    }
169    return NULL;
170  }
171
[9]172  static map* getMap(map* m,const char *key){
[1]173    map* tmp=m;
174    while(tmp!=NULL){
[57]175      if(strcasecmp(tmp->name,key)==0){
[1]176        return tmp;
[9]177      }
[1]178      tmp=tmp->next;
179    }
180    return NULL;
181  }
182
183  static map* getMapFromMaps(maps* m,char* key,char* subkey){
184    maps* _tmpm=getMaps(m,key);
185    if(_tmpm!=NULL){
[9]186      map* _ztmpm=getMap(_tmpm->content,subkey);
187      return _ztmpm;
[1]188    }
189    else return NULL;
190  }
191
[9]192  static void freeMap(map** mo){
[1]193    map* _cursor=*mo;
194    if(_cursor!=NULL){
[9]195#ifdef DEBUG
196      fprintf(stderr,"freeMap\n");
197#endif
[1]198      free(_cursor->name);
199      free(_cursor->value);
200      if(_cursor->next!=NULL){
201        freeMap(&_cursor->next);
202        free(_cursor->next);
203      }
204    }
205  }
206
[9]207  static void freeMaps(maps** mo){
208    maps* _cursor=*mo;
209    fflush(stderr);
210    if(_cursor && _cursor!=NULL){
211#ifdef DEBUG
212      fprintf(stderr,"freeMaps\n");
213#endif
214      free(_cursor->name);
215      if(_cursor->content!=NULL){
216        freeMap(&_cursor->content);
217        free(_cursor->content);
218      }
219      if(_cursor->next!=NULL){
220        freeMaps(&_cursor->next);
221        free(_cursor->next);
222      }
223    }
224  }
[1]225
226  typedef struct iotype{
227    struct map* content;
228    struct iotype* next;
229  } iotype;
230
231  typedef struct elements{
232    char* name;
233    struct map* content;
234    struct map* metadata;
235    char* format;
236    struct iotype* defaults;
237    struct iotype* supported;
238    struct elements* next;
239  } elements;
240
241  typedef struct service{
242    char* name;
243    struct map* content;
244    struct map* metadata;
245    struct elements* inputs;
246    struct elements* outputs; 
247  } service;
248
249  typedef struct services{
250    struct service* content; 
251    struct services* next; 
252  } services;
253
254  static bool hasElement(elements* e,char* key){
255    elements* tmp=e;
256    while(tmp!=NULL){
[57]257      if(strcasecmp(key,tmp->name)==0)
[1]258        return true;
259      tmp=tmp->next;
260    }
261    return false;
262  }
263
264  static elements* getElements(elements* m,char *key){
265    elements* tmp=m;
266    while(tmp!=NULL){
[57]267      if(strcasecmp(tmp->name,key)==0)
[1]268        return tmp;
269      tmp=tmp->next;
270    }
271    return NULL;
272  }
273
274
[9]275  static void freeIOType(iotype** i){
[1]276    iotype* _cursor=*i;
277    if(_cursor!=NULL){
[9]278      if(_cursor->next!=NULL){
279        freeIOType(&_cursor->next);
280        free(_cursor->next);
281      }
[57]282      freeMap(&_cursor->content);
283      free(_cursor->content);
[1]284    }
285  }
286
287  static void freeElements(elements** e){
288    elements* tmp=*e;
289    if(tmp!=NULL){
[57]290      if(tmp->name!=NULL)
291        free(tmp->name);
[1]292      freeMap(&tmp->content);
[57]293      if(tmp->content!=NULL)
294        free(tmp->content);
[1]295      freeMap(&tmp->metadata);
[57]296      if(tmp->metadata!=NULL)
297        free(tmp->metadata);
298      if(tmp->format!=NULL)
299        free(tmp->format);
[1]300      freeIOType(&tmp->defaults);
301      if(tmp->defaults!=NULL)
302        free(tmp->defaults);
303      freeIOType(&tmp->supported);
[57]304      if(tmp->supported!=NULL){
[1]305        free(tmp->supported);
[57]306      }
[9]307      freeElements(&tmp->next);
[60]308      if(tmp->next!=NULL)
309        free(tmp->next);
[1]310    }
311  }
312
[9]313  static void freeService(service** s){
[1]314    service* tmp=*s;
315    if(tmp!=NULL){
[9]316      if(tmp->name!=NULL)
317        free(tmp->name);
[1]318      freeMap(&tmp->content);
319      if(tmp->content!=NULL)
320        free(tmp->content);
321      freeMap(&tmp->metadata);
322      if(tmp->metadata!=NULL)
323        free(tmp->metadata);
324      freeElements(&tmp->inputs);
325      if(tmp->inputs!=NULL)
326        free(tmp->inputs);
327      freeElements(&tmp->outputs);
328      if(tmp->outputs!=NULL)
329        free(tmp->outputs);
330    }
331  }
332
[9]333  static void addToMap(map* m,const char* n,const char* v){
334    if(hasKey(m,n)==false){
335      map* _cursor=m;
336      while(_cursor->next!=NULL)
337        _cursor=_cursor->next;
338      _cursor->next=createMap(n,v);
339    }
340    else{
341      map *tmp=getMap(m,n);
342      if(tmp->value!=NULL)
343        free(tmp->value);
344      tmp->value=strdup(v);
345    }
346  }
347
348  static void addMapToMap(map** mo,map* mi){
[1]349    map* tmp=mi;
350    map* _cursor=*mo;
[9]351    if(tmp==NULL){
352      if(_cursor!=NULL){
353        while(_cursor!=NULL)
[1]354          _cursor=_cursor->next;
[9]355        _cursor=NULL;
356      }else
357        *mo=NULL;
[1]358    }
359    while(tmp!=NULL){
360      if(_cursor==NULL){
[9]361        if(*mo==NULL)
362          *mo=createMap(tmp->name,tmp->value);
363        else
364          addToMap(*mo,tmp->name,tmp->value);
[1]365      }
366      else{
[9]367#ifdef DEBUG
368        fprintf(stderr,"_CURSOR\n");
369        dumpMap(_cursor);
370#endif
371        while(_cursor!=NULL)
[1]372          _cursor=_cursor->next;
[9]373        _cursor=createMap(tmp->name,tmp->value);
374        _cursor->next=NULL;
[1]375      }
376      tmp=tmp->next;
[9]377#ifdef DEBUG
378      fprintf(stderr,"MO\n");
379      dumpMap(*mo);
380#endif
[1]381    }
382  }
383
[9]384  static void addMapToIoType(iotype** io,map* mi){
[1]385    iotype* tmp=*io;
[57]386    while(tmp->next!=NULL){
[1]387      tmp=tmp->next;
388    }
[57]389    tmp->next=(iotype*)malloc(IOTYPE_SIZE);
390    tmp->next->content=NULL;
391    addMapToMap(&tmp->next->content,mi);
392    tmp->next->next=NULL;
[1]393  }
394
[57]395  static bool contains(map* m,map* i){
396    while(i!=NULL){     
397      if(strcasecmp(i->name,"value")!=0 &&
398         strcasecmp(i->name,"xlink:href")!=0){
399        map *tmp;
400        if(hasKey(m,i->name) && (tmp=getMap(m,i->name))!=NULL && 
401           strcasecmp(i->value,tmp->value)!=0)
402          return false;
403      }
404      i=i->next;
405    }
406    return true;
407  }
[9]408
[57]409  static iotype* getIoTypeFromElement(elements* e,char *name, map* values){
410    elements* cursor=e;
411    while(cursor!=NULL){
412      if(strcasecmp(cursor->name,name)==0){
413        if(contains(cursor->defaults->content,values)==true)
414          return cursor->defaults;
415        else{
416          iotype* tmp=cursor->supported;
417          while(tmp!=NULL){
418            if(contains(tmp->content,values)==true)
419              return tmp;           
420            tmp=tmp->next;
421          }
422        }
423      }
424      cursor=cursor->next;
425    }
426    return NULL;
427  }
428
[9]429  static maps* dupMaps(maps** mo){
430    maps* _cursor=*mo;
431    maps* res=NULL;
432    if(_cursor!=NULL){
433      res=(maps*)malloc(MAPS_SIZE);
434      res->name=strdup(_cursor->name);
435      res->content=NULL;
436      res->next=NULL;
437      map* mc=_cursor->content;
[59]438      map* tmp=getMap(mc,"size");
439      char* tmpSized=NULL;
440      if(tmp!=NULL){
441        map* tmpV=getMap(mc,"value");
442        tmpSized=(char*)malloc((atoi(tmp->value)+1)*sizeof(char));
443        memmove(tmpSized,tmpV->value,atoi(tmp->value)*sizeof(char));
444      }
[9]445      if(mc!=NULL){
446        addMapToMap(&res->content,mc);
447      }
[59]448      if(tmp!=NULL){
449        map* tmpV=getMap(res->content,"value");
450        free(tmpV->value);
451        tmpV->value=(char*)malloc(atoi(tmp->value)*sizeof(char));
452        memmove(tmpV->value,tmpSized,atoi(tmp->value)*sizeof(char));
453        free(tmpSized);
454      }
[9]455      res->next=dupMaps(&_cursor->next);
[1]456    }
[9]457    return res;
458  }
459
460  static void addMapsToMaps(maps** mo,maps* mi){
461    maps* tmp=mi;
462    maps* _cursor=*mo;
463    while(tmp!=NULL){
464      if(_cursor==NULL){
465        *mo=dupMaps(&mi);
466        (*mo)->next=NULL;
467      }
468      else{
469        while(_cursor->next!=NULL)
470          _cursor=_cursor->next;
471        _cursor->next=dupMaps(&tmp);
472      }
473      tmp=tmp->next;
[1]474    }
475  }
476
477
[26]478  static void* setMapInMaps(maps* m,char* key,char* subkey,char *value){
479    maps* _tmpm=getMaps(m,key);
480    if(_tmpm!=NULL){
481      map* _ztmpm=getMap(_tmpm->content,subkey);
482      if(_ztmpm!=NULL){
483        free(_ztmpm->value);
484        _ztmpm->value=strdup(value);
485      }else{
486        addToMap(_tmpm->content,subkey,value);
487      }
488    }
489  }
490
491
[9]492  static void dumpElements(elements* e){
[1]493    elements* tmp=e;
494    while(tmp!=NULL){
495      fprintf(stderr,"ELEMENT [%s]\n",tmp->name);
496      fprintf(stderr," > CONTENT [%s]\n",tmp->name);
497      dumpMap(tmp->content);
498      fprintf(stderr," > METADATA [%s]\n",tmp->name);
499      dumpMap(tmp->metadata);
500      fprintf(stderr," > FORMAT [%s]\n",tmp->format);
501      iotype* tmpio=tmp->defaults;
502      int ioc=0;
503      while(tmpio!=NULL){
504        fprintf(stderr," > DEFAULTS [%s] (%i)\n",tmp->name,ioc);
505        dumpMap(tmpio->content);
506        tmpio=tmpio->next;
507        ioc++;
508      }
509      tmpio=tmp->supported;
510      ioc=0;
511      while(tmpio!=NULL){
512        fprintf(stderr," > SUPPORTED [%s] (%i)\n",tmp->name,ioc);
513        dumpMap(tmpio->content);
514        tmpio=tmpio->next;
515        ioc++;
516      }
517      fprintf(stderr,"------------------\n");
518      tmp=tmp->next;
519    }
520  }
521
522  static elements* dupElements(elements* e){
[9]523    elements* cursor=e;
524    elements* tmp=NULL;
525    if(cursor!=NULL){
[1]526#ifdef DEBUG
[9]527      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
[1]528      dumpElements(e);
[9]529      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
[1]530#endif
[9]531      tmp=(elements*)malloc(ELEMENTS_SIZE);
[1]532      tmp->name=strdup(e->name);
533      tmp->content=NULL;
534      addMapToMap(&tmp->content,e->content);
535      tmp->metadata=NULL;
536      addMapToMap(&tmp->metadata,e->metadata);
537      tmp->format=strdup(e->format);
538      if(e->defaults!=NULL){
[9]539        tmp->defaults=(iotype*)malloc(IOTYPE_SIZE);
[1]540        tmp->defaults->content=NULL;
[9]541        addMapToMap(&tmp->defaults->content,e->defaults->content);
[1]542        tmp->defaults->next=NULL;
[9]543#ifdef DEBUG
544        fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
545        dumpMap(tmp->defaults->content);
546#endif
547      }else
548        tmp->defaults=NULL;
[1]549      if(e->supported!=NULL){
[9]550        tmp->supported=(iotype*)malloc(IOTYPE_SIZE);
[1]551        tmp->supported->content=NULL;
[57]552        addMapToMap(&tmp->supported->content,e->supported->content);
[1]553        tmp->supported->next=NULL;
554        iotype *tmp2=e->supported->next;
555        while(tmp2!=NULL){
[57]556          addMapToIoType(&tmp->supported,tmp2->content);
[9]557#ifdef DEBUG
558          fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
559          dumpMap(tmp->defaults->content);
560#endif
[1]561          tmp2=tmp2->next;
562        }
563      }
[9]564      else
565        tmp->supported=NULL;
566      tmp->next=dupElements(cursor->next);
[1]567    }
[9]568    return tmp;
[1]569  }
570
[9]571  static void addToElements(elements** m,elements* e){
[1]572    elements* tmp=e;
[60]573    if(*m==NULL){
[9]574      *m=dupElements(tmp);
575    }else{
[57]576      addToElements(&(*m)->next,tmp);
[1]577    }
578  }
579
[9]580  static void dumpService(service* s){
[1]581    fprintf(stderr,"++++++++++++++++++\nSERVICE [%s]\n++++++++++++++++++\n",s->name);
582    if(s->content!=NULL){
583      fprintf(stderr,"CONTENT MAP\n");
584      dumpMap(s->content);
585      fprintf(stderr,"CONTENT METADATA\n");
586      dumpMap(s->metadata);
587    }
588    if(s->inputs!=NULL){
589      fprintf(stderr,"INPUT ELEMENTS [%s]\n------------------\n",s->name);
590      dumpElements(s->inputs);
591    }
592    if(s->outputs!=NULL){
593      fprintf(stderr,"OUTPUT ELEMENTS [%s]\n------------------\n",s->name);
594      dumpElements(s->outputs);
595    }
596    fprintf(stderr,"++++++++++++++++++\n");
597  }
598
[9]599  static void mapsToCharXXX(maps* m,char*** c){
[1]600    maps* tm=m;
601    int i=0;
602    int j=0;
603    char tmp[10][30][1024];
604    memset(tmp,0,1024*10*10);
605    while(tm!=NULL){
606      if(i>=10)
607        break;
608      strcpy(tmp[i][j],"name");
609      j++;
610      strcpy(tmp[i][j],tm->name);
611      j++;
612      map* tc=tm->content;
613      while(tc!=NULL){
614        if(j>=30)
615          break;
616        strcpy(tmp[i][j],tc->name);
617        j++;
618        strcpy(tmp[i][j],tc->value);
619        j++;
620        tc=tc->next;
621      }
622      tm=tm->next;
623      j=0;
624      i++;
625    }
626    memcpy(c,tmp,10*10*1024);
627  }
628
[9]629  static void charxxxToMaps(char*** c,maps**m){
[1]630    maps* trorf=*m;
631    int i,j;
632    char tmp[10][30][1024];
633    memcpy(tmp,c,10*30*1024);
634    for(i=0;i<10;i++){
635      if(strlen(tmp[i][1])==0)
636        break;
637      trorf->name=tmp[i][1];
638      trorf->content=NULL;
639      trorf->next=NULL;
640      for(j=2;j<29;j+=2){
641        if(strlen(tmp[i][j+1])==0)
642          break;
643        if(trorf->content==NULL)
644          trorf->content=createMap(tmp[i][j],tmp[i][j+1]);
645        else
[9]646          addToMap(trorf->content,tmp[i][j],tmp[i][j+1]);
[1]647      }
648      trorf=trorf->next;
649    }
650    m=&trorf;
651  }
652
653#ifdef __cplusplus
654}
655#endif
656
657#endif
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