source: trunk/zoo-project/zoo-kernel/service.h @ 518

Last change on this file since 518 was 514, checked in by djay, 9 years ago

Use unique identifier as reference for asynchronous service execution.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr
File size: 25.0 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2012 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 WIN32
31#ifndef USE_MS
32#define strncasecmp _strnicmp
33#define strcasecmp _stricmp
34#endif
35#ifndef snprintf
36#define snprintf sprintf_s
37#endif
38#define zStrdup _strdup
39#define zMkdir _mkdir
40#define zOpen _open
41#define zWrite _write
42#define zSleep Sleep
43#include <sys/timeb.h>
44struct ztimeval {
45  long tv_sec; /* seconds */
46  long tv_usec; /* and microseconds */
47};
48void zGettimeofday(struct mstimeval* tp, void* tzp)
49{
50  struct _timeb theTime;
51  _ftime(&theTime);
52  tp->tv_sec = theTime.time;
53  tp->tv_usec = theTime.millitm * 1000;
54}
55#else
56#define zStrdup strdup
57#define zMkdir mkdir
58#define zOpen open
59#define zWrite write
60#define zSleep sleep
61#define zGettimeofday gettimeofday
62#define ztimeval timeval
63#endif
64
65#ifdef __cplusplus
66extern "C" {
67#endif
68
69#ifdef WIN32
70#ifdef USE_MS
71#include <mapserver.h>
72#endif
73#endif
74#include <stdlib.h>
75#include <ctype.h>
76#include <stdio.h>
77#include <string.h>
78#ifndef WIN32
79#ifndef bool
80#define bool int
81#endif
82#ifndef true
83#define true 1
84#define false -1
85#endif
86#endif
87
88#define SERVICE_ACCEPTED 0
89#define SERVICE_STARTED 1
90#define SERVICE_PAUSED 2
91#define SERVICE_SUCCEEDED 3
92#define SERVICE_FAILED 4
93
94#define ELEMENTS_SIZE (sizeof(char*)+(((2*sizeof(char*))+sizeof(maps*))*2)+sizeof(char*)+(((2*sizeof(char*))+sizeof(iotype*))*2)+sizeof(elements*))
95#define MAP_SIZE (2*sizeof(char*))+sizeof(NULL)
96#define IOTYPE_SIZE MAP_SIZE+sizeof(NULL)
97#define MAPS_SIZE (2*sizeof(char*))+sizeof(map*)+MAP_SIZE
98#define SERVICE_SIZE (ELEMENTS_SIZE*2)+(MAP_SIZE*2)+sizeof(char*)
99
100#define SHMSZ     27
101
102#include "version.h"
103
104#ifdef DEBUG_STACK
105  void debugStack(const char* file,const int line){
106    int stack;
107    fprintf(stderr,"stack %p (%s: %d) \n",&stack,file,line);
108  }
109#endif
110
111  /**
112   * \struct map
113   * \brief KVP linked list
114   *
115   * Deal with WPS KVP (name,value).
116   * A map is defined as:
117   *  - name : a key,
118   *  - value: a value,
119   *  - next : a pointer to the next map if any.
120   */
121  typedef struct map{
122    char* name;
123    char* value;
124    struct map* next;
125  } map;
126
127#ifdef WIN32
128#define NULLMAP ((map*) 0)
129#else
130#define NULLMAP NULL
131#endif
132
133  /**
134   * \struct maps
135   * \brief linked list of map pointer
136   *
137   * Small object to store WPS KVP set.
138   * Maps is defined as:
139   *  - a name,
140   *  - a content map,
141   *  - a pointer to the next maps if any.
142   */
143  typedef struct maps{
144    char* name;         
145    struct map* content; 
146    struct maps* next;   
147  } maps;
148
149  /**
150   * \brief Dump a map on stderr
151   */
152  static void _dumpMap(map* t){
153    if(t!=NULL){
154      fprintf(stderr,"%s: %s\n",t->name,t->value);
155      fflush(stderr);
156        }else{
157      fprintf(stderr,"NULL\n");
158      fflush(stderr);
159    }
160  }
161
162  static void dumpMap(map* t){
163    map* tmp=t;
164    while(tmp!=NULL){
165      _dumpMap(tmp);
166      tmp=tmp->next;
167    }
168  }
169
170  static void dumpMapToFile(map* t,FILE* file){
171    map* tmp=t;
172    while(tmp!=NULL){
173#ifdef DEBUG
174      fprintf(stderr,"%s = %s\n",tmp->name,tmp->value);
175#endif
176      fprintf(file,"%s = %s\n",tmp->name,tmp->value);
177      tmp=tmp->next;
178    }
179  }
180
181  static void dumpMaps(maps* m){
182    maps* tmp=m;
183    while(tmp!=NULL){
184      fprintf(stderr,"MAP => [%s] \n",tmp->name);
185      dumpMap(tmp->content);
186      tmp=tmp->next;
187    }
188  }
189
190  static void dumpMapsToFile(maps* m,char* file_path){
191    FILE* file=fopen(file_path,"w");
192    maps* tmp=m;
193    if(tmp!=NULL){
194      fprintf(file,"[%s]\n",tmp->name);
195      dumpMapToFile(tmp->content,file);
196      fflush(file);
197    }
198    fclose(file);
199  }
200
201  static map* createMap(const char* name,const char* value){
202    map* tmp=(map *)malloc(MAP_SIZE);
203    tmp->name=zStrdup(name);
204    tmp->value=zStrdup(value);
205    tmp->next=NULL;
206    return tmp;
207  }
208
209  static int count(map* m){
210    map* tmp=m;
211    int c=0;
212    while(tmp!=NULL){
213      c++;
214      tmp=tmp->next;
215    }
216    return c;
217  }
218   
219  static bool hasKey(map* m,const char *key){
220    map* tmp=m;
221    while(tmp!=NULL){
222      if(strcasecmp(tmp->name,key)==0)
223        return true;
224      tmp=tmp->next;
225    }
226#ifdef DEBUG_MAP
227    fprintf(stderr,"NOT FOUND \n");
228#endif
229    return false;
230  }
231
232  static maps* getMaps(maps* m,const char *key){
233    maps* tmp=m;
234    while(tmp!=NULL){
235      if(strcasecmp(tmp->name,key)==0){
236        return tmp;
237      }
238      tmp=tmp->next;
239    }
240    return NULL;
241  }
242
243  static map* getMap(map* m,const char *key){
244    map* tmp=m;
245    while(tmp!=NULL){
246      if(strcasecmp(tmp->name,key)==0){
247        return tmp;
248      }
249      tmp=tmp->next;
250    }
251    return NULL;
252  }
253
254
255  static map* getLastMap(map* m){
256    map* tmp=m;
257    while(tmp!=NULL){
258      if(tmp->next==NULL){
259        return tmp;
260      }
261      tmp=tmp->next;
262    }
263    return NULL;
264  }
265
266  static map* getMapFromMaps(maps* m,const char* key,const char* subkey){
267    maps* _tmpm=getMaps(m,key);
268    if(_tmpm!=NULL){
269      map* _ztmpm=getMap(_tmpm->content,subkey);
270      return _ztmpm;
271    }
272    else return NULL;
273  }
274
275
276  static void freeMap(map** mo){
277    map* _cursor=*mo;
278    if(_cursor!=NULL){
279#ifdef DEBUG
280      fprintf(stderr,"freeMap\n");
281#endif
282      free(_cursor->name);
283      free(_cursor->value);
284      if(_cursor->next!=NULL){
285        freeMap(&_cursor->next);
286        free(_cursor->next);
287      }
288    }
289  }
290
291  static void freeMaps(maps** mo){
292    maps* _cursor=*mo;
293    if(_cursor && _cursor!=NULL){
294#ifdef DEBUG
295      fprintf(stderr,"freeMaps\n");
296#endif
297      free(_cursor->name);
298      if(_cursor->content!=NULL){
299        freeMap(&_cursor->content);
300        free(_cursor->content);
301      }
302      if(_cursor->next!=NULL){
303        freeMaps(&_cursor->next);
304        free(_cursor->next);
305      }
306    }
307  }
308
309  /**
310   * \brief Not named linked list
311   *
312   * Used to store informations about formats, such as mimeType, encoding ...
313   *
314   * An iotype is defined as :
315   *  - a content map,
316   *  - a pointer to the next iotype if any.
317   */
318  typedef struct iotype{
319    struct map* content;
320    struct iotype* next;
321  } iotype;
322
323  /**
324   * \brief Metadata information about input or output.
325   *
326   * The elements are used to store metadata informations defined in the ZCFG.
327   *
328   * An elements is defined as :
329   *  - a name,
330   *  - a content map,
331   *  - a metadata map,
332   *  - a format (possible values are LiteralData, ComplexData or
333   * BoundingBoxData),
334   *  - a default iotype,
335   *  - a pointer to the next elements id any.
336   */
337  typedef struct elements{
338    char* name;
339    struct map* content;
340    struct map* metadata;
341    char* format;
342    struct iotype* defaults;
343    struct iotype* supported;
344    struct elements* next;
345  } elements;
346
347  typedef struct service{
348    char* name;
349    struct map* content;
350    struct map* metadata;
351    struct elements* inputs;
352    struct elements* outputs; 
353  } service;
354
355  typedef struct services{
356    struct service* content; 
357    struct services* next; 
358  } services;
359
360  static bool hasElement(elements* e,const char* key){
361    elements* tmp=e;
362    while(tmp!=NULL){
363      if(strcasecmp(key,tmp->name)==0)
364        return true;
365      tmp=tmp->next;
366    }
367    return false;
368  }
369
370  static elements* getElements(elements* m,char *key){
371    elements* tmp=m;
372    while(tmp!=NULL){
373      if(strcasecmp(tmp->name,key)==0)
374        return tmp;
375      tmp=tmp->next;
376    }
377    return NULL;
378  }
379
380
381  static void freeIOType(iotype** i){
382    iotype* _cursor=*i;
383    if(_cursor!=NULL){
384      if(_cursor->next!=NULL){
385        freeIOType(&_cursor->next);
386        free(_cursor->next);
387      }
388      freeMap(&_cursor->content);
389      free(_cursor->content);
390    }
391  }
392
393  static void freeElements(elements** e){
394    elements* tmp=*e;
395    if(tmp!=NULL){
396      if(tmp->name!=NULL)
397        free(tmp->name);
398      freeMap(&tmp->content);
399      if(tmp->content!=NULL)
400        free(tmp->content);
401      freeMap(&tmp->metadata);
402      if(tmp->metadata!=NULL)
403        free(tmp->metadata);
404      if(tmp->format!=NULL)
405        free(tmp->format);
406      freeIOType(&tmp->defaults);
407      if(tmp->defaults!=NULL)
408        free(tmp->defaults);
409      freeIOType(&tmp->supported);
410      if(tmp->supported!=NULL){
411        free(tmp->supported);
412      }
413      freeElements(&tmp->next);
414      if(tmp->next!=NULL)
415        free(tmp->next);
416    }
417  }
418
419  static void freeService(service** s){
420    service* tmp=*s;
421    if(tmp!=NULL){
422      if(tmp->name!=NULL)
423        free(tmp->name);
424      freeMap(&tmp->content);
425      if(tmp->content!=NULL)
426        free(tmp->content);
427      freeMap(&tmp->metadata);
428      if(tmp->metadata!=NULL)
429        free(tmp->metadata);
430      freeElements(&tmp->inputs);
431      if(tmp->inputs!=NULL)
432        free(tmp->inputs);
433      freeElements(&tmp->outputs);
434      if(tmp->outputs!=NULL)
435        free(tmp->outputs);
436    }
437  }
438
439  static void addToMap(map* m,const char* n,const char* v){
440    if(hasKey(m,n)==false){
441      map* _cursor=m;
442      while(_cursor->next!=NULL){
443        _cursor=_cursor->next;
444      }
445      _cursor->next=createMap(n,v);
446    }
447    else{
448      map *tmp=getMap(m,n);
449      if(tmp->value!=NULL)
450        free(tmp->value);
451      tmp->value=zStrdup(v);
452    }
453  }
454
455  static void addToMapWithSize(map* m,const char* n,const char* v,int size){
456    if(hasKey(m,n)==false){
457      map* _cursor=m;
458      if(_cursor!=NULL){
459        addToMap(m,n,"");
460      }else{
461        m=createMap(n,"");
462      }
463    }
464    map *tmp=getMap(m,n);
465    if(tmp->value!=NULL)
466      free(tmp->value);
467    tmp->value=(char*)malloc((size+1)*sizeof(char));
468    memmove(tmp->value,v,size*sizeof(char));
469    tmp->value[size]=0;
470    char sin[128];
471    sprintf(sin,"%ld",size);
472    addToMap(m,"size",sin);
473  }
474
475  static void addMapToMap(map** mo,map* mi){
476    map* tmp=mi;
477    map* _cursor=*mo;
478    while(tmp!=NULL){
479      if(_cursor==NULL){
480        *mo=createMap(tmp->name,tmp->value);
481        (*mo)->next=NULL;
482      }
483      else{
484#ifdef DEBUG
485        fprintf(stderr,"_CURSOR\n");
486        dumpMap(_cursor);
487#endif
488        while(_cursor->next!=NULL)
489          _cursor=_cursor->next;
490        map* tmp1=getMap(*mo,tmp->name);
491        if(tmp1==NULL){
492          _cursor->next=createMap(tmp->name,tmp->value);
493        }
494        else{
495          addToMap(*mo,tmp->name,tmp->value);
496        }
497      }
498      _cursor=*mo;
499      tmp=tmp->next;
500#ifdef DEBUG
501      fprintf(stderr,"MO\n");
502      dumpMap(*mo);
503#endif
504    }
505  }
506
507  static void addMapToIoType(iotype** io,map* mi){
508    iotype* tmp=*io;
509    while(tmp->next!=NULL){
510      tmp=tmp->next;
511    }
512    tmp->next=(iotype*)malloc(IOTYPE_SIZE);
513    tmp->next->content=NULL;
514    addMapToMap(&tmp->next->content,mi);
515    tmp->next->next=NULL;
516  }
517
518  static map* getMapOrFill(map** m,const char *key,const char* value){
519    map* tmp=*m;
520    map* tmpMap=getMap(tmp,key);
521    if(tmpMap==NULL){
522      if(tmp!=NULL){
523        addToMap((*m),key,value);
524      }
525      else
526        (*m)=createMap(key,value);
527      tmpMap=getMap(*m,key);
528    }
529    return tmpMap;
530  }
531
532  static bool contains(map* m,map* i){
533    while(i!=NULL){     
534      if(strcasecmp(i->name,"value")!=0 &&
535         strcasecmp(i->name,"xlink:href")!=0 &&
536         strcasecmp(i->name,"useMapServer")!=0 &&
537         strcasecmp(i->name,"asReference")!=0){
538        map *tmp;
539        if(hasKey(m,i->name) && (tmp=getMap(m,i->name))!=NULL && 
540           strcasecmp(i->value,tmp->value)!=0)
541          return false;
542      }
543      i=i->next;
544    }
545    return true;
546  }
547
548  static iotype* getIoTypeFromElement(elements* e,char *name, map* values){
549    elements* cursor=e;
550    while(cursor!=NULL){
551      if(strcasecmp(cursor->name,name)==0){
552        if(contains(cursor->defaults->content,values)==true)
553          return cursor->defaults;
554        else{
555          iotype* tmp=cursor->supported;
556          while(tmp!=NULL){
557            if(contains(tmp->content,values)==true)
558              return tmp;           
559            tmp=tmp->next;
560          }
561        }
562      }
563      cursor=cursor->next;
564    }
565    return NULL;
566  }
567
568  static maps* dupMaps(maps** mo){
569    maps* _cursor=*mo;
570    maps* res=NULL;
571    if(_cursor!=NULL){
572      res=(maps*)malloc(MAPS_SIZE);
573      res->name=zStrdup(_cursor->name);
574      res->content=NULL;
575      res->next=NULL;
576      map* mc=_cursor->content;
577      map* tmp=getMap(mc,"size");
578      char* tmpSized=NULL;
579      if(tmp!=NULL){
580        map* tmpV=getMap(mc,"value");
581        tmpSized=(char*)malloc((atoi(tmp->value)+1)*sizeof(char));
582        memmove(tmpSized,tmpV->value,atoi(tmp->value)*sizeof(char));
583      }
584      if(mc!=NULL){
585        addMapToMap(&res->content,mc);
586      }
587      if(tmp!=NULL){
588        map* tmpV=getMap(res->content,"value");
589        free(tmpV->value);
590        tmpV->value=(char*)malloc((atoi(tmp->value)+1)*sizeof(char));
591        memmove(tmpV->value,tmpSized,atoi(tmp->value)*sizeof(char));
592        tmpV->value[atoi(tmp->value)]=0;
593        free(tmpSized);
594      }
595      res->next=dupMaps(&_cursor->next);
596    }
597    return res;
598  }
599
600  static void addMapsToMaps(maps** mo,maps* mi){
601    maps* tmp=mi;
602    maps* _cursor=*mo;
603    while(tmp!=NULL){
604      if(_cursor==NULL){
605        *mo=dupMaps(&mi);
606      }
607      else{
608        while(_cursor->next!=NULL)
609          _cursor=_cursor->next;
610        maps* tmp1=getMaps(*mo,tmp->name);
611        if(tmp1==NULL)
612          _cursor->next=dupMaps(&tmp);
613        else
614          addMapToMap(&tmp1->content,tmp->content);
615        _cursor=*mo;
616      }
617      tmp=tmp->next;
618    }
619  }
620
621  static map* getMapArray(map* m,const char* key,int index){
622    char tmp[1024];
623    if(index>0)
624      sprintf(tmp,"%s_%d",key,index);
625    else
626      sprintf(tmp,"%s",key);
627#ifdef DEBUG
628    fprintf(stderr,"** KEY %s\n",tmp);
629#endif
630    map* tmpMap=getMap(m,tmp);
631#ifdef DEBUG
632    if(tmpMap!=NULL)
633      dumpMap(tmpMap);
634#endif
635    return tmpMap;
636  }
637
638
639  static void setMapArray(map* m,char* key,int index,char* value){
640    char tmp[1024];
641    if(index>0)
642      sprintf(tmp,"%s_%d",key,index);
643    else
644      sprintf(tmp,"%s",key);
645    map* tmpSize=getMapArray(m,(char*)"size",index);
646    if(tmpSize!=NULL && strncasecmp(key,"value",5)==0){
647#ifdef DEBUG
648      fprintf(stderr,"%s\n",tmpSize->value);
649#endif
650      map* ptr=getMapOrFill(&m,tmp,(char *)"");
651      free(ptr->value);
652      ptr->value=(char*)malloc((atoi(tmpSize->value)+1)*sizeof(char));
653      memcpy(ptr->value,value,atoi(tmpSize->value)); 
654    }
655    else
656      addToMap(m,tmp,value);
657  }
658
659  static map* getMapType(map* mt){
660    map* tmap=getMap(mt,(char *)"mimeType");
661    if(tmap==NULL){
662      tmap=getMap(mt,"dataType");
663      if(tmap==NULL){
664        tmap=getMap(mt,"CRS");
665      }
666    }
667#ifdef DEBUG
668        dumpMap(tmap);
669#endif
670    return tmap;
671  }
672
673  static int addMapsArrayToMaps(maps** mo,maps* mi,char* typ){
674    maps* tmp=mi;   
675    maps* _cursor=getMaps(*mo,tmp->name);
676
677    if(_cursor==NULL)
678      return -1;
679
680    map* tmpLength=getMap(_cursor->content,"length");
681    char tmpLen[10];
682    int len=1;
683    if(tmpLength!=NULL){
684      len=atoi(tmpLength->value);
685    }
686
687    char *tmpV[8]={
688      (char*)"size",
689      (char*)"value",
690      (char*)"uom",
691      (char*)"Reference",
692      (char*)"xlink:href",
693      typ,
694      (char*)"schema",
695      (char*)"encoding"
696    };
697    sprintf(tmpLen,"%d",len+1);
698    addToMap(_cursor->content,"length",tmpLen);
699    int i=0;
700    for(i=0;i<8;i++){
701      map* tmpVI=getMap(tmp->content,tmpV[i]);
702      if(tmpVI!=NULL){
703#ifdef DEBUG
704        fprintf(stderr,"%s = %s\n",tmpV[i],tmpVI->value);
705#endif
706        if(i<5)
707          setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
708        else
709          if(strncasecmp(tmpV[5],"mimeType",8)==0)
710            setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
711      }
712    }
713   
714    addToMap(_cursor->content,"isArray","true");
715    return 0;
716  }
717
718  static void setMapInMaps(maps* m,const char* key,const char* subkey,const char *value){
719    maps* _tmpm=getMaps(m,key);
720    if(_tmpm!=NULL){
721      map* _ztmpm=getMap(_tmpm->content,subkey);
722      if(_ztmpm!=NULL){
723        if(_ztmpm->value!=NULL)
724          free(_ztmpm->value);
725        _ztmpm->value=zStrdup(value);
726      }else{
727        maps *tmp=(maps*)malloc(MAPS_SIZE);
728        tmp->name=zStrdup(key);
729        tmp->content=createMap(subkey,value);
730        tmp->next=NULL;
731        addMapsToMaps(&_tmpm,tmp);
732        freeMaps(&tmp);
733        free(tmp);
734      }
735    }else{
736      maps *tmp=(maps*)malloc(MAPS_SIZE);
737      tmp->name=zStrdup(key);
738      tmp->content=createMap(subkey,value);
739      tmp->next=NULL;
740      addMapsToMaps(&m,tmp);
741      freeMaps(&tmp);
742      free(tmp);
743    }
744  }
745
746
747  static void dumpElements(elements* e){
748    elements* tmp=e;
749    while(tmp!=NULL){
750      fprintf(stderr,"ELEMENT [%s]\n",tmp->name);
751      fprintf(stderr," > CONTENT [%s]\n",tmp->name);
752      dumpMap(tmp->content);
753      fprintf(stderr," > METADATA [%s]\n",tmp->name);
754      dumpMap(tmp->metadata);
755      fprintf(stderr," > FORMAT [%s]\n",tmp->format);
756      iotype* tmpio=tmp->defaults;
757      int ioc=0;
758      while(tmpio!=NULL){
759        fprintf(stderr," > DEFAULTS [%s] (%i)\n",tmp->name,ioc);
760        dumpMap(tmpio->content);
761        tmpio=tmpio->next;
762        ioc++;
763      }
764      tmpio=tmp->supported;
765      ioc=0;
766      while(tmpio!=NULL){
767        fprintf(stderr," > SUPPORTED [%s] (%i)\n",tmp->name,ioc);
768        dumpMap(tmpio->content);
769        tmpio=tmpio->next;
770        ioc++;
771      }
772      fprintf(stderr,"------------------\n");
773      tmp=tmp->next;
774    }
775  }
776
777  static void dumpElementsAsYAML(elements* e){
778    elements* tmp=e;
779    int i;
780    while(tmp!=NULL){
781      for(i=0;i<2;i++)
782        fprintf(stderr," ");
783      fprintf(stderr,"%s:\n",tmp->name);
784      map* mcurs=tmp->content;
785      while(mcurs!=NULL){
786        for(i=0;i<4;i++)
787          fprintf(stderr," ");
788        _dumpMap(mcurs);
789        mcurs=mcurs->next;
790      }
791      mcurs=tmp->metadata;
792      if(mcurs!=NULL){
793        for(i=0;i<4;i++)
794          fprintf(stderr," ");
795        fprintf(stderr,"MetaData:\n");
796        while(mcurs!=NULL){
797          for(i=0;i<6;i++)
798            fprintf(stderr," ");
799          _dumpMap(mcurs);
800          mcurs=mcurs->next;
801        }
802      }
803      for(i=0;i<4;i++)
804        fprintf(stderr," ");
805      fprintf(stderr,"%s:\n",tmp->format);
806      iotype* tmpio=tmp->defaults;
807      int ioc=0;
808      while(tmpio!=NULL){
809        for(i=0;i<6;i++)
810          fprintf(stderr," ");
811        fprintf(stderr,"default:\n");
812        mcurs=tmpio->content;
813        while(mcurs!=NULL){
814          for(i=0;i<8;i++)
815            fprintf(stderr," ");
816          if(strcasecmp(mcurs->name,"range")==0){
817            fprintf(stderr,"range: \"%s\"\n",mcurs->value);
818          }else
819            _dumpMap(mcurs);
820          mcurs=mcurs->next;
821        }
822        tmpio=tmpio->next;
823        ioc++;
824      }
825      tmpio=tmp->supported;
826      ioc=0;
827      while(tmpio!=NULL){
828        for(i=0;i<6;i++)
829          fprintf(stderr," ");
830        fprintf(stderr,"supported:\n");
831        mcurs=tmpio->content;
832        while(mcurs!=NULL){
833          for(i=0;i<8;i++)
834            fprintf(stderr," ");
835          if(strcasecmp(mcurs->name,"range")==0){
836            fprintf(stderr,"range: \"%s\"\n",mcurs->value);
837          }else
838            _dumpMap(mcurs);
839          mcurs=mcurs->next;
840        }
841        tmpio=tmpio->next;
842        ioc++;
843      }
844      tmp=tmp->next;
845    }
846  }
847
848
849  static elements* dupElements(elements* e){
850    elements* cursor=e;
851    elements* tmp=NULL;
852    if(cursor!=NULL){
853#ifdef DEBUG
854      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
855      dumpElements(e);
856      fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
857#endif
858      tmp=(elements*)malloc(ELEMENTS_SIZE);
859      tmp->name=zStrdup(e->name);
860      tmp->content=NULL;
861      addMapToMap(&tmp->content,e->content);
862      tmp->metadata=NULL;
863      addMapToMap(&tmp->metadata,e->metadata);
864      tmp->format=zStrdup(e->format);
865      if(e->defaults!=NULL){
866        tmp->defaults=(iotype*)malloc(IOTYPE_SIZE);
867        tmp->defaults->content=NULL;
868        addMapToMap(&tmp->defaults->content,e->defaults->content);
869        tmp->defaults->next=NULL;
870#ifdef DEBUG
871        fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
872        dumpMap(tmp->defaults->content);
873#endif
874      }else
875        tmp->defaults=NULL;
876      if(e->supported!=NULL){
877        tmp->supported=(iotype*)malloc(IOTYPE_SIZE);
878        tmp->supported->content=NULL;
879        addMapToMap(&tmp->supported->content,e->supported->content);
880        tmp->supported->next=NULL;
881        iotype *tmp2=e->supported->next;
882        while(tmp2!=NULL){
883          addMapToIoType(&tmp->supported,tmp2->content);
884#ifdef DEBUG
885          fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
886          dumpMap(tmp->defaults->content);
887#endif
888          tmp2=tmp2->next;
889        }
890      }
891      else
892        tmp->supported=NULL;
893      tmp->next=dupElements(cursor->next);
894    }
895    return tmp;
896  }
897
898  static void addToElements(elements** m,elements* e){
899    elements* tmp=e;
900    if(*m==NULL){
901      *m=dupElements(tmp);
902    }else{
903      addToElements(&(*m)->next,tmp);
904    }
905  }
906
907  static void dumpService(service* s){
908    fprintf(stderr,"++++++++++++++++++\nSERVICE [%s]\n++++++++++++++++++\n",s->name);
909    if(s->content!=NULL){
910      fprintf(stderr,"CONTENT MAP\n");
911      dumpMap(s->content);
912      fprintf(stderr,"CONTENT METADATA\n");
913      dumpMap(s->metadata);
914    }
915    if(s->inputs!=NULL){
916      fprintf(stderr,"INPUT ELEMENTS [%s]\n------------------\n",s->name);
917      dumpElements(s->inputs);
918    }
919    if(s->outputs!=NULL){
920      fprintf(stderr,"OUTPUT ELEMENTS [%s]\n------------------\n",s->name);
921      dumpElements(s->outputs);
922    }
923    fprintf(stderr,"++++++++++++++++++\n");
924  }
925
926  static void dumpServiceAsYAML(service* s){
927    int i;
928    fprintf(stderr,"# %s\n\n",s->name);
929    if(s->content!=NULL){
930      map* mcurs=s->content;
931      dumpMap(mcurs);
932      mcurs=s->metadata;
933      if(mcurs!=NULL){
934        fprintf(stderr,"MetaData:\n");
935        while(mcurs!=NULL){
936          for(i=0;i<2;i++)
937            fprintf(stderr," ");
938          _dumpMap(mcurs);
939          mcurs=mcurs->next;
940        }
941      }
942    }
943    if(s->inputs!=NULL){
944      fprintf(stderr,"\ninputs:\n");
945      dumpElementsAsYAML(s->inputs);
946    }
947    if(s->outputs!=NULL){
948      fprintf(stderr,"\noutputs:\n");
949      dumpElementsAsYAML(s->outputs);
950    }
951  }
952
953  static void mapsToCharXXX(maps* m,char*** c){
954    maps* tm=m;
955    int i=0;
956    int j=0;
957    char tmp[10][30][1024];
958    memset(tmp,0,1024*10*10);
959    while(tm!=NULL){
960      if(i>=10)
961        break;
962      strcpy(tmp[i][j],"name");
963      j++;
964      strcpy(tmp[i][j],tm->name);
965      j++;
966      map* tc=tm->content;
967      while(tc!=NULL){
968        if(j>=30)
969          break;
970        strcpy(tmp[i][j],tc->name);
971        j++;
972        strcpy(tmp[i][j],tc->value);
973        j++;
974        tc=tc->next;
975      }
976      tm=tm->next;
977      j=0;
978      i++;
979    }
980    memcpy(c,tmp,10*10*1024);
981  }
982
983  static void charxxxToMaps(char*** c,maps**m){
984    maps* trorf=*m;
985    int i,j;
986    char tmp[10][30][1024];
987    memcpy(tmp,c,10*30*1024);
988    for(i=0;i<10;i++){
989      if(strlen(tmp[i][1])==0)
990        break;
991      trorf->name=tmp[i][1];
992      trorf->content=NULL;
993      trorf->next=NULL;
994      for(j=2;j<29;j+=2){
995        if(strlen(tmp[i][j+1])==0)
996          break;
997        if(trorf->content==NULL)
998          trorf->content=createMap(tmp[i][j],tmp[i][j+1]);
999        else
1000          addToMap(trorf->content,tmp[i][j],tmp[i][j+1]);
1001      }
1002      trorf=trorf->next;
1003    }
1004    m=&trorf;
1005  }
1006
1007#ifdef WIN32
1008  extern char *url_encode(char *);
1009
1010  static char* getMapsAsKVP(maps* m,int length,int type){
1011    char *dataInputsKVP=(char*) malloc(length*sizeof(char));
1012    char *dataInputsKVPi=NULL;
1013    maps* curs=m;
1014    int i=0;
1015    while(curs!=NULL){
1016      map *inRequest=getMap(curs->content,"inRequest");
1017      map *hasLength=getMap(curs->content,"length");
1018      if((inRequest!=NULL && strncasecmp(inRequest->value,"true",4)==0) ||
1019         inRequest==NULL){
1020        if(i==0)
1021          if(type==0){
1022            sprintf(dataInputsKVP,"%s=",curs->name);
1023            if(hasLength!=NULL){
1024              dataInputsKVPi=(char*)malloc((strlen(curs->name)+2)*sizeof(char));
1025              sprintf(dataInputsKVPi,"%s=",curs->name);
1026            }
1027          }
1028          else
1029            sprintf(dataInputsKVP,"%s",curs->name);
1030        else{
1031          char *temp=zStrdup(dataInputsKVP);
1032          if(type==0)
1033            sprintf(dataInputsKVP,"%s;%s=",temp,curs->name);
1034          else
1035            sprintf(dataInputsKVP,"%s;%s",temp,curs->name);
1036        }
1037        map* icurs=curs->content;
1038        if(type==0){
1039          char *temp=zStrdup(dataInputsKVP);
1040          if(getMap(curs->content,"xlink:href")!=NULL)
1041            sprintf(dataInputsKVP,"%sReference",temp);
1042          else{
1043            if(hasLength!=NULL){
1044              int j;
1045              for(j=0;j<atoi(hasLength->value);j++){
1046                map* tmp0=getMapArray(curs->content,"value",j);
1047                if(j==0)
1048                  free(temp);
1049                temp=zStrdup(dataInputsKVP);
1050                if(j==0)
1051                  sprintf(dataInputsKVP,"%s%s",temp,tmp0->value);
1052                else
1053                  sprintf(dataInputsKVP,"%s;%s%s",temp,dataInputsKVPi,tmp0->value);
1054              }
1055            }
1056            else
1057              sprintf(dataInputsKVP,"%s%s",temp,icurs->value);
1058          }
1059          free(temp);
1060        }
1061        while(icurs!=NULL){
1062          if(strncasecmp(icurs->name,"value",5)!=0 &&
1063             strncasecmp(icurs->name,"mimeType_",9)!=0 &&
1064             strncasecmp(icurs->name,"dataType_",9)!=0 &&
1065             strncasecmp(icurs->name,"size",4)!=0 &&
1066             strncasecmp(icurs->name,"length",4)!=0 &&
1067             strncasecmp(icurs->name,"isArray",7)!=0 &&
1068             strcasecmp(icurs->name,"Reference")!=0 &&
1069             strcasecmp(icurs->name,"minOccurs")!=0 &&
1070             strcasecmp(icurs->name,"maxOccurs")!=0 &&
1071             strncasecmp(icurs->name,"fmimeType",9)!=0 &&
1072             strcasecmp(icurs->name,"inRequest")!=0){
1073            char *itemp=zStrdup(dataInputsKVP);
1074            if(strcasecmp(icurs->name,"xlink:href")!=0)
1075              sprintf(dataInputsKVP,"%s@%s=%s",itemp,icurs->name,icurs->value);
1076            else
1077              sprintf(dataInputsKVP,"%s@%s=%s",itemp,icurs->name,url_encode(icurs->value));
1078            free(itemp);
1079          }
1080          icurs=icurs->next;
1081        }
1082      }
1083      curs=curs->next;
1084      i++;
1085    }
1086    return dataInputsKVP;
1087  }
1088#endif
1089
1090#ifdef __cplusplus
1091}
1092#endif
1093
1094#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