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

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

Small fixs in freeElements and runRequest.

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