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

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

Code cleanup to avoid most of the warning messages at compilation time.

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