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

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

Correct void functions. Special thanks to Angelos Tzotsos for pointing this out and for his work in packaging ZOO-Project for OpenSuSE.

File size: 14.8 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#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
42
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
48
49#define ELEMENTS_SIZE (sizeof(char*)+(((2*sizeof(char*))+sizeof(maps*))*2)+sizeof(char*)+(((2*sizeof(char*))+sizeof(iotype*))*2)+sizeof(elements*))
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*)
54
55#define SHMSZ     27
56
57  /**
58   * \struct maps
59   * \brief linked list of map pointer
60   *
61   * Small object to store WPS KVP set.
62   */
63  typedef struct maps{
64    char* name;         
65    struct map* content; 
66    struct maps* next;   
67  } maps;
68
69  /**
70   * \struct map
71   * \brief KVP linked list
72   *
73   * Deal with WPS KVP (name,value).
74   */
75  typedef struct map{
76    char* name;       /* The key */
77    char* value;      /* The value */
78    struct map* next; /* Next couple */
79  } map;
80
81#ifdef WIN32
82#define NULLMAP ((map*) 0)
83#else
84#define NULLMAP NULL
85#endif
86
87  static void _dumpMap(map* t){
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
97  static void dumpMap(map* t){
98    map* tmp=t;
99    while(tmp!=NULL){
100      _dumpMap(tmp);
101      tmp=tmp->next;
102    }
103  }
104
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
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
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
130  static map* createMap(const char* name,const char* value){
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   
148  static bool hasKey(map* m,const char *key){
149    map* tmp=m;
150    while(tmp!=NULL){
151      if(strcasecmp(tmp->name,key)==0)
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
161  static maps* getMaps(maps* m,const char *key){
162    maps* tmp=m;
163    while(tmp!=NULL){
164      if(strcasecmp(tmp->name,key)==0){
165        return tmp;
166      }
167      tmp=tmp->next;
168    }
169    return NULL;
170  }
171
172  static map* getMap(map* m,const char *key){
173    map* tmp=m;
174    while(tmp!=NULL){
175      if(strcasecmp(tmp->name,key)==0){
176        return tmp;
177      }
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){
186      map* _ztmpm=getMap(_tmpm->content,subkey);
187      return _ztmpm;
188    }
189    else return NULL;
190  }
191
192  static void freeMap(map** mo){
193    map* _cursor=*mo;
194    if(_cursor!=NULL){
195#ifdef DEBUG
196      fprintf(stderr,"freeMap\n");
197#endif
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
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  }
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){
257      if(strcasecmp(key,tmp->name)==0)
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){
267      if(strcasecmp(tmp->name,key)==0)
268        return tmp;
269      tmp=tmp->next;
270    }
271    return NULL;
272  }
273
274
275  static void freeIOType(iotype** i){
276    iotype* _cursor=*i;
277    if(_cursor!=NULL){
278      if(_cursor->next!=NULL){
279        freeIOType(&_cursor->next);
280        free(_cursor->next);
281      }
282      freeMap(&_cursor->content);
283      free(_cursor->content);
284    }
285  }
286
287  static void freeElements(elements** e){
288    elements* tmp=*e;
289    if(tmp!=NULL){
290      if(tmp->name!=NULL)
291        free(tmp->name);
292      freeMap(&tmp->content);
293      if(tmp->content!=NULL)
294        free(tmp->content);
295      freeMap(&tmp->metadata);
296      if(tmp->metadata!=NULL)
297        free(tmp->metadata);
298      if(tmp->format!=NULL)
299        free(tmp->format);
300      freeIOType(&tmp->defaults);
301      if(tmp->defaults!=NULL)
302        free(tmp->defaults);
303      freeIOType(&tmp->supported);
304      if(tmp->supported!=NULL){
305        free(tmp->supported);
306      }
307      freeElements(&tmp->next);
308      if(tmp->next!=NULL)
309        free(tmp->next);
310    }
311  }
312
313  static void freeService(service** s){
314    service* tmp=*s;
315    if(tmp!=NULL){
316      if(tmp->name!=NULL)
317        free(tmp->name);
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
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){
349    map* tmp=mi;
350    map* _cursor=*mo;
351    if(tmp==NULL){
352      if(_cursor!=NULL){
353        while(_cursor!=NULL)
354          _cursor=_cursor->next;
355        _cursor=NULL;
356      }else
357        *mo=NULL;
358    }
359    while(tmp!=NULL){
360      if(_cursor==NULL){
361        if(*mo==NULL)
362          *mo=createMap(tmp->name,tmp->value);
363        else
364          addToMap(*mo,tmp->name,tmp->value);
365      }
366      else{
367#ifdef DEBUG
368        fprintf(stderr,"_CURSOR\n");
369        dumpMap(_cursor);
370#endif
371        while(_cursor!=NULL)
372          _cursor=_cursor->next;
373        _cursor=createMap(tmp->name,tmp->value);
374        _cursor->next=NULL;
375      }
376      tmp=tmp->next;
377#ifdef DEBUG
378      fprintf(stderr,"MO\n");
379      dumpMap(*mo);
380#endif
381    }
382  }
383
384  static void addMapToIoType(iotype** io,map* mi){
385    iotype* tmp=*io;
386    while(tmp->next!=NULL){
387      tmp=tmp->next;
388    }
389    tmp->next=(iotype*)malloc(IOTYPE_SIZE);
390    tmp->next->content=NULL;
391    addMapToMap(&tmp->next->content,mi);
392    tmp->next->next=NULL;
393  }
394
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  }
408
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
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;
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      }
445      if(mc!=NULL){
446        addMapToMap(&res->content,mc);
447      }
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      }
455      res->next=dupMaps(&_cursor->next);
456    }
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;
474    }
475  }
476
477
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
492  static void dumpElements(elements* e){
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){
523    elements* cursor=e;
524    elements* tmp=NULL;
525    if(cursor!=NULL){
526#ifdef DEBUG
527      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
528      dumpElements(e);
529      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
530#endif
531      tmp=(elements*)malloc(ELEMENTS_SIZE);
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){
539        tmp->defaults=(iotype*)malloc(IOTYPE_SIZE);
540        tmp->defaults->content=NULL;
541        addMapToMap(&tmp->defaults->content,e->defaults->content);
542        tmp->defaults->next=NULL;
543#ifdef DEBUG
544        fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
545        dumpMap(tmp->defaults->content);
546#endif
547      }else
548        tmp->defaults=NULL;
549      if(e->supported!=NULL){
550        tmp->supported=(iotype*)malloc(IOTYPE_SIZE);
551        tmp->supported->content=NULL;
552        addMapToMap(&tmp->supported->content,e->supported->content);
553        tmp->supported->next=NULL;
554        iotype *tmp2=e->supported->next;
555        while(tmp2!=NULL){
556          addMapToIoType(&tmp->supported,tmp2->content);
557#ifdef DEBUG
558          fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
559          dumpMap(tmp->defaults->content);
560#endif
561          tmp2=tmp2->next;
562        }
563      }
564      else
565        tmp->supported=NULL;
566      tmp->next=dupElements(cursor->next);
567    }
568    return tmp;
569  }
570
571  static void addToElements(elements** m,elements* e){
572    elements* tmp=e;
573    if(*m==NULL){
574      *m=dupElements(tmp);
575    }else{
576      addToElements(&(*m)->next,tmp);
577    }
578  }
579
580  static void dumpService(service* s){
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
599  static void mapsToCharXXX(maps* m,char*** c){
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
629  static void charxxxToMaps(char*** c,maps**m){
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
646          addToMap(trorf->content,tmp[i][j],tmp[i][j+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