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 |
---|
31 | extern "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 | #define SERVICE_ACCEPTED 0 |
---|
43 | #define SERVICE_STARTED 1 |
---|
44 | #define SERVICE_PAUSED 2 |
---|
45 | #define SERVICE_SUCCEEDED 3 |
---|
46 | #define SERVICE_FAILED 4 |
---|
47 | #define ELEMENTS_SIZE (sizeof(char*)+(((2*sizeof(char*))+sizeof(maps*))*2)+sizeof(char*)+(((2*sizeof(char*))+sizeof(iotype*))*2)+sizeof(elements*)) |
---|
48 | #define MAP_SIZE (2*sizeof(char*))+sizeof(map*) |
---|
49 | #define MAPS_SIZE (2*sizeof(char*))+sizeof(map*)+sizeof(maps*) |
---|
50 | |
---|
51 | |
---|
52 | static char* mtoupper(char* str){ |
---|
53 | char* tmp=strdup(str); |
---|
54 | if(tmp){ |
---|
55 | int cnt=strlen(tmp); |
---|
56 | int i; |
---|
57 | for(i=0;i<cnt;i++){ |
---|
58 | tmp[i]=toupper(str[i]); |
---|
59 | tmp[i+1]=0; |
---|
60 | } |
---|
61 | } |
---|
62 | else |
---|
63 | tmp[0]=0; |
---|
64 | return tmp; |
---|
65 | } |
---|
66 | |
---|
67 | typedef struct maps{ |
---|
68 | char* name; |
---|
69 | struct map* content; |
---|
70 | struct maps* next; |
---|
71 | } maps; |
---|
72 | |
---|
73 | typedef struct map{ |
---|
74 | char* name; |
---|
75 | char* value; |
---|
76 | struct map* next; |
---|
77 | } map; |
---|
78 | |
---|
79 | #ifdef WIN32 |
---|
80 | #define NULLMAP ((map*) 0) |
---|
81 | #else |
---|
82 | #define NULLMAP NULL |
---|
83 | #endif |
---|
84 | |
---|
85 | static void* _dumpMap(map* t){ |
---|
86 | if(t!=NULL){ |
---|
87 | fprintf(stderr,"[%s] => [%s] \n",t->name,t->value); |
---|
88 | fflush(stderr); |
---|
89 | }else{ |
---|
90 | fprintf(stderr,"NULL\n"); |
---|
91 | fflush(stderr); |
---|
92 | } |
---|
93 | return NULL; |
---|
94 | } |
---|
95 | |
---|
96 | static void* dumpMap(map* t){ |
---|
97 | map* tmp=t; |
---|
98 | while(tmp!=NULL){ |
---|
99 | _dumpMap(tmp); |
---|
100 | tmp=tmp->next; |
---|
101 | } |
---|
102 | return NULL; |
---|
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(char* name,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,char *key){ |
---|
133 | map* tmp=m; |
---|
134 | while(tmp!=NULL){ |
---|
135 | if(strcmp(mtoupper(tmp->name),mtoupper(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,char *key){ |
---|
146 | maps* tmp=m; |
---|
147 | while(tmp!=NULL){ |
---|
148 | if(strcmp(mtoupper(tmp->name),mtoupper(key))==0) |
---|
149 | return tmp; |
---|
150 | tmp=tmp->next; |
---|
151 | } |
---|
152 | return NULL; |
---|
153 | } |
---|
154 | |
---|
155 | static map* getMap(map* m,char *key){ |
---|
156 | map* tmp=m; |
---|
157 | while(tmp!=NULL){ |
---|
158 | if(strcmp(mtoupper(tmp->name),mtoupper(key))==0) |
---|
159 | return tmp; |
---|
160 | tmp=tmp->next; |
---|
161 | } |
---|
162 | return NULL; |
---|
163 | } |
---|
164 | |
---|
165 | static map* getMapFromMaps(maps* m,char* key,char* subkey){ |
---|
166 | maps* _tmpm=getMaps(m,key); |
---|
167 | if(_tmpm!=NULL){ |
---|
168 | map* tmpm=getMap(_tmpm->content,mtoupper(subkey)); |
---|
169 | return tmpm; |
---|
170 | } |
---|
171 | else return NULL; |
---|
172 | } |
---|
173 | |
---|
174 | static void* freeMap(map** mo){ |
---|
175 | map* _cursor=*mo; |
---|
176 | if(_cursor!=NULL){ |
---|
177 | free(_cursor->name); |
---|
178 | free(_cursor->value); |
---|
179 | if(_cursor->next!=NULL){ |
---|
180 | freeMap(&_cursor->next); |
---|
181 | free(_cursor->next); |
---|
182 | } |
---|
183 | } |
---|
184 | return NULL; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | typedef struct iotype{ |
---|
189 | struct map* content; |
---|
190 | struct iotype* next; |
---|
191 | } iotype; |
---|
192 | |
---|
193 | typedef struct elements{ |
---|
194 | char* name; |
---|
195 | struct map* content; |
---|
196 | struct map* metadata; |
---|
197 | char* format; |
---|
198 | struct iotype* defaults; |
---|
199 | struct iotype* supported; |
---|
200 | struct elements* next; |
---|
201 | } elements; |
---|
202 | |
---|
203 | typedef struct service{ |
---|
204 | char* name; |
---|
205 | struct map* content; |
---|
206 | struct map* metadata; |
---|
207 | struct elements* inputs; |
---|
208 | struct elements* outputs; |
---|
209 | } service; |
---|
210 | |
---|
211 | typedef struct services{ |
---|
212 | struct service* content; |
---|
213 | struct services* next; |
---|
214 | } services; |
---|
215 | |
---|
216 | static bool hasElement(elements* e,char* key){ |
---|
217 | elements* tmp=e; |
---|
218 | while(tmp!=NULL){ |
---|
219 | mtoupper(key); |
---|
220 | mtoupper(tmp->name); |
---|
221 | if(strcmp(key,tmp->name)==0) |
---|
222 | return true; |
---|
223 | tmp=tmp->next; |
---|
224 | } |
---|
225 | return false; |
---|
226 | } |
---|
227 | |
---|
228 | static elements* getElements(elements* m,char *key){ |
---|
229 | elements* tmp=m; |
---|
230 | while(tmp!=NULL){ |
---|
231 | mtoupper(tmp->name); |
---|
232 | mtoupper(key); |
---|
233 | if(strcmp(tmp->name,key)==0) |
---|
234 | return tmp; |
---|
235 | tmp=tmp->next; |
---|
236 | } |
---|
237 | return NULL; |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | static void* freeIOType(iotype** i){ |
---|
242 | iotype* _cursor=*i; |
---|
243 | if(_cursor!=NULL){ |
---|
244 | freeMap(&_cursor->content); |
---|
245 | free(_cursor->content); |
---|
246 | freeIOType(&_cursor->next); |
---|
247 | free(_cursor->next); |
---|
248 | } |
---|
249 | return NULL; |
---|
250 | } |
---|
251 | |
---|
252 | static void freeElements(elements** e){ |
---|
253 | elements* tmp=*e; |
---|
254 | if(tmp!=NULL){ |
---|
255 | #ifdef DEBUG |
---|
256 | fprintf(stderr,"FREE 1"); |
---|
257 | #endif |
---|
258 | free(tmp->name); |
---|
259 | freeMap(&tmp->content); |
---|
260 | #ifdef DEBUG |
---|
261 | fprintf(stderr,"FREE 2"); |
---|
262 | #endif |
---|
263 | free(tmp->content); |
---|
264 | freeMap(&tmp->metadata); |
---|
265 | #ifdef DEBUG |
---|
266 | fprintf(stderr,"FREE 3"); |
---|
267 | #endif |
---|
268 | free(tmp->metadata); |
---|
269 | #ifdef DEBUG |
---|
270 | fprintf(stderr,"FREE 4"); |
---|
271 | #endif |
---|
272 | free(tmp->format); |
---|
273 | freeIOType(&tmp->defaults); |
---|
274 | #ifdef DEBUG |
---|
275 | fprintf(stderr,"FREE 5"); |
---|
276 | #endif |
---|
277 | if(tmp->defaults!=NULL) |
---|
278 | free(tmp->defaults); |
---|
279 | freeIOType(&tmp->supported); |
---|
280 | #ifdef DEBUG |
---|
281 | fprintf(stderr,"FREE 6"); |
---|
282 | #endif |
---|
283 | if(tmp->supported!=NULL) |
---|
284 | free(tmp->supported); |
---|
285 | tmp=tmp->next; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | static void* freeService(service** s){ |
---|
290 | service* tmp=*s; |
---|
291 | if(tmp!=NULL){ |
---|
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 | freeElements(&tmp->inputs); |
---|
299 | if(tmp->inputs!=NULL) |
---|
300 | free(tmp->inputs); |
---|
301 | freeElements(&tmp->outputs); |
---|
302 | if(tmp->outputs!=NULL) |
---|
303 | free(tmp->outputs); |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | static void* addMapToMap(map** mo,map* mi){ |
---|
308 | map* tmp=mi; |
---|
309 | map* _cursor=*mo; |
---|
310 | while(tmp!=NULL){ |
---|
311 | if(_cursor==NULL){ |
---|
312 | *mo=(map*)malloc(MAP_SIZE); |
---|
313 | *mo=createMap(tmp->name,tmp->value); |
---|
314 | _cursor=*mo; |
---|
315 | _cursor->next=NULL; |
---|
316 | } |
---|
317 | else{ |
---|
318 | while(_cursor->next!=NULL) |
---|
319 | _cursor=_cursor->next; |
---|
320 | _cursor->next=createMap(tmp->name,tmp->value); |
---|
321 | } |
---|
322 | tmp=tmp->next; |
---|
323 | } |
---|
324 | return NULL; |
---|
325 | } |
---|
326 | |
---|
327 | static void* addMapsToMaps(maps** mo,maps* mi){ |
---|
328 | maps* tmp=mi; |
---|
329 | maps* _cursor=*mo; |
---|
330 | while(tmp!=NULL){ |
---|
331 | if(_cursor==NULL){ |
---|
332 | *mo=(maps*)malloc(MAP_SIZE); |
---|
333 | *mo=mi; |
---|
334 | _cursor=*mo; |
---|
335 | } |
---|
336 | else{ |
---|
337 | while(_cursor->next!=NULL) |
---|
338 | _cursor=_cursor->next; |
---|
339 | _cursor->next=tmp; |
---|
340 | } |
---|
341 | tmp=tmp->next; |
---|
342 | } |
---|
343 | return NULL; |
---|
344 | } |
---|
345 | |
---|
346 | static void* addMapToIotype(iotype** io,map* mi){ |
---|
347 | iotype* tmp=*io; |
---|
348 | while(tmp!=NULL){ |
---|
349 | fprintf(stderr,">> CURRENT MAP"); |
---|
350 | dumpMap(tmp->content); |
---|
351 | tmp=tmp->next; |
---|
352 | } |
---|
353 | tmp=(iotype*)malloc(ELEMENTS_SIZE); |
---|
354 | tmp->content=NULL; |
---|
355 | tmp->next=NULL; |
---|
356 | addMapToMap(&tmp->content,mi); |
---|
357 | return NULL; |
---|
358 | } |
---|
359 | |
---|
360 | static void* addToMap(map* m,char* n,char* v){ |
---|
361 | if(hasKey(m,n)==false){ |
---|
362 | map* _cursor=m; |
---|
363 | while(_cursor->next!=NULL) |
---|
364 | _cursor=_cursor->next; |
---|
365 | _cursor->next=createMap(n,v); |
---|
366 | } |
---|
367 | else{ |
---|
368 | map *tmp=getMap(m,n); |
---|
369 | tmp->value=strdup(v); |
---|
370 | } |
---|
371 | return NULL; |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | static void* dumpElements(elements* e){ |
---|
376 | elements* tmp=e; |
---|
377 | while(tmp!=NULL){ |
---|
378 | fprintf(stderr,"ELEMENT [%s]\n",tmp->name); |
---|
379 | fprintf(stderr," > CONTENT [%s]\n",tmp->name); |
---|
380 | dumpMap(tmp->content); |
---|
381 | fprintf(stderr," > METADATA [%s]\n",tmp->name); |
---|
382 | dumpMap(tmp->metadata); |
---|
383 | fprintf(stderr," > FORMAT [%s]\n",tmp->format); |
---|
384 | iotype* tmpio=tmp->defaults; |
---|
385 | int ioc=0; |
---|
386 | while(tmpio!=NULL){ |
---|
387 | fprintf(stderr," > DEFAULTS [%s] (%i)\n",tmp->name,ioc); |
---|
388 | dumpMap(tmpio->content); |
---|
389 | tmpio=tmpio->next; |
---|
390 | ioc++; |
---|
391 | } |
---|
392 | tmpio=tmp->supported; |
---|
393 | ioc=0; |
---|
394 | while(tmpio!=NULL){ |
---|
395 | fprintf(stderr," > SUPPORTED [%s] (%i)\n",tmp->name,ioc); |
---|
396 | dumpMap(tmpio->content); |
---|
397 | tmpio=tmpio->next; |
---|
398 | ioc++; |
---|
399 | } |
---|
400 | fprintf(stderr,"------------------\n"); |
---|
401 | tmp=tmp->next; |
---|
402 | } |
---|
403 | return NULL; |
---|
404 | } |
---|
405 | |
---|
406 | |
---|
407 | static elements* dupElements(elements* e){ |
---|
408 | if(e!=NULL){ |
---|
409 | #ifdef DEBUG |
---|
410 | dumpElements(e); |
---|
411 | #endif |
---|
412 | elements* tmp=(elements*)malloc(ELEMENTS_SIZE); |
---|
413 | tmp->name=strdup(e->name); |
---|
414 | tmp->content=NULL; |
---|
415 | addMapToMap(&tmp->content,e->content); |
---|
416 | tmp->metadata=NULL; |
---|
417 | addMapToMap(&tmp->metadata,e->metadata); |
---|
418 | tmp->format=strdup(e->format); |
---|
419 | if(e->defaults!=NULL){ |
---|
420 | tmp->defaults=(iotype*)malloc(MAP_SIZE); |
---|
421 | tmp->defaults->content=NULL; |
---|
422 | tmp->defaults->next=NULL; |
---|
423 | addMapToMap(&tmp->defaults->content,e->defaults->content); |
---|
424 | while(e->defaults->next!=NULL){ |
---|
425 | tmp->defaults->next=(iotype*)malloc(MAP_SIZE); |
---|
426 | addMapToMap(&tmp->defaults->next->content,e->defaults->content); |
---|
427 | } |
---|
428 | } |
---|
429 | if(e->supported!=NULL){ |
---|
430 | tmp->supported=(iotype*)malloc(MAP_SIZE); |
---|
431 | tmp->supported->content=NULL; |
---|
432 | tmp->supported->next=NULL; |
---|
433 | addMapToMap(&tmp->supported->content,e->supported->content); |
---|
434 | iotype *tmp2=e->supported->next; |
---|
435 | while(tmp2!=NULL){ |
---|
436 | tmp->supported->next=(iotype*)malloc(MAP_SIZE); |
---|
437 | addMapToMap(&tmp->supported->next->content,tmp2->content); |
---|
438 | tmp2=tmp2->next; |
---|
439 | } |
---|
440 | } |
---|
441 | tmp->next=NULL; |
---|
442 | return tmp; |
---|
443 | } |
---|
444 | return NULL; |
---|
445 | } |
---|
446 | |
---|
447 | static void* addToElements(elements* m,elements* e){ |
---|
448 | elements* _cursor=m; |
---|
449 | elements* tmp=e; |
---|
450 | if(_cursor==NULL){ |
---|
451 | m=dupElements(tmp); |
---|
452 | return NULL; |
---|
453 | } |
---|
454 | while(_cursor->next!=NULL) |
---|
455 | _cursor=_cursor->next; |
---|
456 | _cursor->next=dupElements(tmp); |
---|
457 | return NULL; |
---|
458 | } |
---|
459 | |
---|
460 | static void* dumpService(service* s){ |
---|
461 | fprintf(stderr,"++++++++++++++++++\nSERVICE [%s]\n++++++++++++++++++\n",s->name); |
---|
462 | if(s->content!=NULL){ |
---|
463 | fprintf(stderr,"CONTENT MAP\n"); |
---|
464 | dumpMap(s->content); |
---|
465 | fprintf(stderr,"CONTENT METADATA\n"); |
---|
466 | dumpMap(s->metadata); |
---|
467 | } |
---|
468 | if(s->inputs!=NULL){ |
---|
469 | fprintf(stderr,"INPUT ELEMENTS [%s]\n------------------\n",s->name); |
---|
470 | dumpElements(s->inputs); |
---|
471 | } |
---|
472 | if(s->outputs!=NULL){ |
---|
473 | fprintf(stderr,"OUTPUT ELEMENTS [%s]\n------------------\n",s->name); |
---|
474 | dumpElements(s->outputs); |
---|
475 | } |
---|
476 | fprintf(stderr,"++++++++++++++++++\n"); |
---|
477 | return NULL; |
---|
478 | } |
---|
479 | |
---|
480 | static void* mapsToCharXXX(maps* m,char*** c){ |
---|
481 | maps* tm=m; |
---|
482 | int i=0; |
---|
483 | int j=0; |
---|
484 | char tmp[10][30][1024]; |
---|
485 | memset(tmp,0,1024*10*10); |
---|
486 | while(tm!=NULL){ |
---|
487 | if(i>=10) |
---|
488 | break; |
---|
489 | strcpy(tmp[i][j],"name"); |
---|
490 | j++; |
---|
491 | strcpy(tmp[i][j],tm->name); |
---|
492 | j++; |
---|
493 | map* tc=tm->content; |
---|
494 | while(tc!=NULL){ |
---|
495 | if(j>=30) |
---|
496 | break; |
---|
497 | strcpy(tmp[i][j],tc->name); |
---|
498 | j++; |
---|
499 | strcpy(tmp[i][j],tc->value); |
---|
500 | j++; |
---|
501 | tc=tc->next; |
---|
502 | } |
---|
503 | tm=tm->next; |
---|
504 | j=0; |
---|
505 | i++; |
---|
506 | } |
---|
507 | memcpy(c,tmp,10*10*1024); |
---|
508 | return NULL; |
---|
509 | } |
---|
510 | |
---|
511 | static void* charxxxToMaps(char*** c,maps**m){ |
---|
512 | maps* trorf=*m; |
---|
513 | int i,j; |
---|
514 | char tmp[10][30][1024]; |
---|
515 | memcpy(tmp,c,10*30*1024); |
---|
516 | for(i=0;i<10;i++){ |
---|
517 | if(strlen(tmp[i][1])==0) |
---|
518 | break; |
---|
519 | trorf->name=tmp[i][1]; |
---|
520 | trorf->content=NULL; |
---|
521 | trorf->next=NULL; |
---|
522 | for(j=2;j<29;j+=2){ |
---|
523 | if(strlen(tmp[i][j+1])==0) |
---|
524 | break; |
---|
525 | if(trorf->content==NULL) |
---|
526 | trorf->content=createMap(tmp[i][j],tmp[i][j+1]); |
---|
527 | else |
---|
528 | addMapToMap(&trorf->content,createMap(tmp[i][j],tmp[i][j+1])); |
---|
529 | } |
---|
530 | trorf=trorf->next; |
---|
531 | } |
---|
532 | m=&trorf; |
---|
533 | return NULL; |
---|
534 | } |
---|
535 | |
---|
536 | #ifdef __cplusplus |
---|
537 | } |
---|
538 | #endif |
---|
539 | |
---|
540 | #endif |
---|