ZOO-Project
service.h
Go to the documentation of this file.
1 /*
2  * Author : GĂ©rald FENOY
3  *
4  * Copyright (c) 2009-2015 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 #define ZOO_DLL_EXPORT __declspec( dllexport )
32 #else
33 #define ZOO_DLL_EXPORT
34 #endif
35 
36 #ifdef WIN32
37 #ifndef USE_MS
38 ZOO_DLL_EXPORT char *strcasestr (char const *,char const *);
39 #endif
40 #endif
41 
42 #ifdef WIN32
43 #define strncasecmp _strnicmp
44 #define strcasecmp _stricmp
45 #define strtok_r strtok_s
46 #if defined(_MSC_VER) && _MSC_VER < 1900
47 #define snprintf _snprintf
48 #endif
49 #if defined(_MSC_VER) && _MSC_VER < 1800
50 #define false 0
51 #define true 1
52 #define bool int
53 #endif
54 #define zStrdup _strdup
55 #define zMkdir _mkdir
56 #define zOpen _open
57 #define zWrite _write
58 #define zSleep Sleep
59 #include <sys/timeb.h>
60 struct ztimeval {
61  long tv_sec; /* seconds */
62  long tv_usec; /* and microseconds */
63 };
64 static int zGettimeofday(struct ztimeval* tp, void* tzp)
65 {
66  struct _timeb theTime;
67  if (tp == 0) {
68  return -1;
69  }
70 
71  _ftime(&theTime);
72  tp->tv_sec = theTime.time;
73  tp->tv_usec = theTime.millitm * 1000;
74 
75  return 0; // The gettimeofday() function shall return 0 on success
76 }
77 
78 #else
79 
82 #define zStrdup strdup
83 
86 #define zMkdir mkdir
87 
90 #define zOpen open
91 
94 #define zWrite write
95 
98 #define zSleep sleep
99 
102 #define zGettimeofday gettimeofday
103 
106 #define ztimeval timeval
107 #endif
108 
109 #ifdef __cplusplus
110 extern "C" {
111 #endif
112 
113 #ifdef WIN32
114 #ifdef USE_MS
115 #include <mapserver.h>
116 #endif
117 #endif
118 #include <stdlib.h>
119 #include <ctype.h>
120 
121 #include <stdio.h>
122 
123 #include <string.h>
124 #ifndef WIN32
125 #include <ctype.h>
126 #include <stdbool.h>
127 #endif
128 
132 #define SERVICE_ACCEPTED 0
133 
136 #define SERVICE_STARTED 1
137 
140 #define SERVICE_PAUSED 2
141 
144 #define SERVICE_SUCCEEDED 3
145 
148 #define SERVICE_FAILED 4
149 
153 #define ELEMENTS_SIZE (sizeof(char*)+(((2*sizeof(char*))+sizeof(maps*))*2)+sizeof(char*)+(((2*sizeof(char*))+sizeof(iotype*))*2)+(2*sizeof(elements*)))
154 
157 //#define MAP_SIZE (2*sizeof(char*))+sizeof(NULL) // knut: size of NULL pointer may be different from regular pointer (platform dependent)
158 #define MAP_SIZE (2*sizeof(char*))+sizeof(map*)
159 
162 //#define IOTYPE_SIZE MAP_SIZE+sizeof(NULL)
163 #define IOTYPE_SIZE sizeof(map*) + sizeof(iotype*)
164 
167 //#define MAPS_SIZE (2*sizeof(char*))+sizeof(map*)+MAP_SIZE
168 #define MAPS_SIZE sizeof(char*)+sizeof(map*)+(2*sizeof(maps*))
169 
172 //#define SERVICE_SIZE (ELEMENTS_SIZE*2)+(MAP_SIZE*2)+sizeof(char*)
173 #define SERVICE_SIZE sizeof(char*) + 2*sizeof(map*) + 2*sizeof(elements*)
174 
177 //#define SERVICES_SIZE SERVICE_SIZE+sizeof(services*)
178 #define SERVICES_SIZE sizeof(service*)+sizeof(services*)
179 
182 //#define REGISTRY_SIZE SERVICES_SIZE+sizeof(char*)
183 #define REGISTRY_SIZE sizeof(char*)+sizeof(services*)+sizeof(registry*)
184 
185 #define SHMSZ 27
186 
187 #include "version.h"
188 
189 #ifdef DEBUG_STACK
190  void debugStack(const char* file,const int line){
191  int stack;
192  fprintf(stderr,"stack %p (%s: %d) \n",&stack,file,line);
193  }
194 #endif
195 
199  typedef struct map{
200  char* name;
201  char* value;
202  struct map* next;
203  } map;
204 
205 #ifdef WIN32
206 #define NULLMAP ((map*) 0)
207 #else
208 #define NULLMAP NULL
209 #endif
210 
216  typedef struct maps{
217  char* name;
218  struct map* content;
219  struct maps* child;
220  struct maps* next;
221  } maps;
222 
228  typedef struct iotype{
229  struct map* content;
230  struct iotype* next;
231  } iotype;
232 
238  typedef struct elements{
239  char* name;
240  struct map* content;
241  struct map* metadata;
242  char* format;
243  struct iotype* defaults;
244  struct iotype* supported;
245  struct elements* child;
246  struct elements* next;
247  } elements;
248 
252  typedef struct service{
253  char* name;
254  struct map* content;
255  struct map* metadata;
256  struct elements* inputs;
257  struct elements* outputs;
258  } service;
259 
263  typedef struct services{
264  struct service* content;
265  struct services* next;
266  } services;
267 
271  typedef struct registry{
272  char *name;
273  struct services* content;
274  struct registry* next;
275  } registry;
276 
277  ZOO_DLL_EXPORT void _dumpMap(map*);
278  ZOO_DLL_EXPORT void dumpMap(map*);
279  ZOO_DLL_EXPORT void dumpMaps(maps* m);
280  ZOO_DLL_EXPORT void dumpMapToFile(map*,FILE*); // (used only internally)
281  ZOO_DLL_EXPORT void dumpMapsToFile(maps*,char*,int);
282  ZOO_DLL_EXPORT map* createMap(const char*,const char*);
283  ZOO_DLL_EXPORT maps* createMaps(const char*);
284  ZOO_DLL_EXPORT int count(map*);
285  ZOO_DLL_EXPORT bool hasKey(map*,const char*);
286  ZOO_DLL_EXPORT maps* getMaps(maps*,const char*);
287  ZOO_DLL_EXPORT map* getMap(map*,const char*);
289  ZOO_DLL_EXPORT map* getMapFromMaps(maps*,const char*,const char*);
290  ZOO_DLL_EXPORT void freeMap(map**);
291  ZOO_DLL_EXPORT void freeMaps(maps** mo);
292 
293 
297  ZOO_DLL_EXPORT bool hasElement(elements*,const char*);
301  ZOO_DLL_EXPORT void setServiceName(service**,char*);
303  ZOO_DLL_EXPORT void addToMap(map*,const char*,const char*);
304  ZOO_DLL_EXPORT void addIntToMap(map*,const char*,const int);
305  ZOO_DLL_EXPORT map* addToMapWithSize(map*,const char*,const char*,int);
308  ZOO_DLL_EXPORT map* getMapOrFill(map**,const char*,const char*);
309  ZOO_DLL_EXPORT bool contains(map*,map*);
311  ZOO_DLL_EXPORT void loadMapBinary(map**,map*,int);
315  ZOO_DLL_EXPORT map* getMapArray(map*,const char*,int);
316  ZOO_DLL_EXPORT void setMapArray(map*,const char*,int,const char*);
319  ZOO_DLL_EXPORT void setMapInMaps(maps*,const char*,const char*,const char*);
331  ZOO_DLL_EXPORT void inheritMap(map**,map*);
335  ZOO_DLL_EXPORT void mapsToCharXXX(maps*,char***);
336  ZOO_DLL_EXPORT void charxxxToMaps(char***,maps**);
337 #if defined(_MSC_VER) && _MSC_VER < 1800
338  // snprintf for Visual Studio compiler;
339  // it is also used by services (e.g., GetStatus), therefore exported to shared library
340  ZOO_DLL_EXPORT int snprintf(char *buffer, size_t n, const char *format, ...);
341 #endif
342 #ifdef __cplusplus
343 }
344 #endif
345 
346 #endif
ZOO_DLL_EXPORT void freeMap(map **)
Free allocated memory of a map.
Definition: service.c:287
ZOO_DLL_EXPORT elements * getElements(elements *, char *)
Access a specific elements named key.
Definition: service.c:354
ZOO_DLL_EXPORT void inheritance(registry *, service **)
Apply inheritance to a service based on a registry.
Definition: service.c:1502
ZOO_DLL_EXPORT void freeMaps(maps **mo)
Free allocated memory of a maps.
Definition: service.c:308
struct registry registry
Profile registry.
ZOO_DLL_EXPORT void dumpMapsToFile(maps *, char *, int)
Dump a maps to a file, see _dumpMapsToFile().
Definition: service.c:137
struct maps maps
linked list of map pointer
char * name
the maps name
Definition: main_conf_read.y:244
ZOO_DLL_EXPORT void freeIOType(iotype **)
Free allocated memory of an iotype.
Definition: service.c:370
char * format
the format: LiteralData or ComplexData or BoundingBoxData
Definition: main_conf_read.y:269
ZOO_DLL_EXPORT void dumpMap(map *)
Dump a map on stderr, see _dumpMap()
Definition: service.c:67
ZOO_DLL_EXPORT bool hasKey(map *, const char *)
Verify if a key exist in a map.
Definition: service.c:197
ZOO_DLL_EXPORT void addMapToIoType(iotype **, map *)
Add a map to iotype.
Definition: service.c:570
ZOO_DLL_EXPORT void setElementsName(elements **, char *)
Set the name of an elements.
Definition: service.c:994
ZOO_DLL_EXPORT map * getMapArray(map *, const char *, int)
Access a specific map array element.
Definition: service.c:791
ZOO_DLL_EXPORT void inheritElements(elements **, elements *)
Apply inheritance to an out elements from a reference in elements.
Definition: service.c:1470
struct service * content
the content service pointer
Definition: main_conf_read.y:291
struct map * content
the content map
Definition: main_conf_read.y:256
#define ztimeval
The crossplatform timeval alias.
Definition: service.h:106
ZOO_DLL_EXPORT void addToElements(elements **, elements *)
Add an elements to another elements.
Definition: service.c:1196
ZOO_DLL_EXPORT void setServiceName(service **, char *)
Set the name of a service.
Definition: service.c:1210
ZOO_DLL_EXPORT map * getMap(map *, const char *)
Access a specific map.
Definition: service.c:235
ZOO_DLL_EXPORT map * getMapType(map *)
Access the map "type".
Definition: service.c:851
struct iotype * next
the pointer to the next iotype if any or NULL
Definition: main_conf_read.y:257
ZOO_DLL_EXPORT map * getMapFromMaps(maps *, const char *, const char *)
Access a specific map from a maps.
Definition: service.c:272
struct elements * inputs
the inputs elements
Definition: main_conf_read.y:283
struct map * metadata
the metadata map
Definition: main_conf_read.y:268
ZOO_DLL_EXPORT void dumpElementsAsYAML(elements *, int)
Dump an elements on stderr using the YAML syntaxe.
Definition: service.c:1050
ZOO_DLL_EXPORT bool contains(map *, map *)
Verify if a map is contained in another map.
Definition: service.c:610
char * value
the value
Definition: service.h:201
ZOO_DLL_EXPORT elements * createEmptyElements()
Create an empty elements.
Definition: service.c:956
snprintf(current_maps->name,(strlen($1)+1),"%s", $1)
struct iotype * supported
the supported iotype
Definition: main_conf_read.y:271
ZOO_DLL_EXPORT void addMapsToMaps(maps **, maps *)
Add a maps at the end of another maps.
Definition: service.c:752
ZOO_DLL_EXPORT void _dumpMap(map *)
Dump a map on stderr.
Definition: service.c:52
struct registry * next
the next registry pointer
Definition: main_conf_read.y:301
ZOO_DLL_EXPORT service * getServiceFromRegistry(registry *, char *, char *)
Access a service in the registry.
Definition: service.c:1404
struct map map
KVP linked list.
ZOO_DLL_EXPORT void dumpMapToFile(map *, FILE *)
Dump a map to a file.
Definition: service.c:81
ZOO_DLL_EXPORT map * createMap(const char *, const char *)
Create a new map.
Definition: service.c:151
struct map * content
the content map
Definition: main_conf_read.y:245
struct elements * outputs
the outputs elements
Definition: main_conf_read.y:284
ZOO_DLL_EXPORT maps * createMaps(const char *)
Create a new maps with the given name.
Definition: service.c:165
ZOO_DLL_EXPORT void addToMap(map *, const char *, const char *)
Add key value pair to an existing map.
Definition: service.c:451
linked list of map pointer
Definition: main_conf_read.y:243
ZOO_DLL_EXPORT void mapsToCharXXX(maps *, char ***)
Convert a maps to a char*** (only used for Fortran support)
Definition: service.c:1537
char * name
the name
Definition: main_conf_read.y:266
struct services services
Services chained list.
struct map * metadata
the metadata map
Definition: main_conf_read.y:282
ZOO_DLL_EXPORT bool hasElement(elements *, const char *)
Verify if an elements contains a name equal to the given key.
Definition: service.c:337
ZOO_DLL_EXPORT void addMapToMap(map **, map *)
Add a map at the end of another map.
Definition: service.c:532
ZOO_DLL_EXPORT bool addServiceToRegistry(registry **, char *, service *)
Add a service to the registry.
Definition: service.c:1321
ZOO_DLL_EXPORT maps * getMaps(maps *, const char *)
Access a specific maps.
Definition: service.c:217
char * name
the name
Definition: main_conf_read.y:299
struct service service
Metadata information about a full Service.
ZOO_DLL_EXPORT void dumpService(service *)
Dump a service on stderr.
Definition: service.c:1224
ZOO_DLL_EXPORT void dumpMaps(maps *m)
Dump a maps on stderr, see dumpMap().
Definition: service.c:94
KVP linked list.
Definition: service.h:199
ZOO_DLL_EXPORT void dumpRegistry(registry *)
Print the registry on stderr.
Definition: service.c:1300
ZOO_DLL_EXPORT void dumpElements(elements *)
Dump an elements on stderr.
Definition: service.c:1011
ZOO_DLL_EXPORT void freeRegistry(registry **)
Free memory allocated for the registry.
Definition: service.c:1378
ZOO_DLL_EXPORT void loadMapBinary(map **, map *, int)
Load binary values from a map (in) and add them to another map (out)
Definition: service.c:671
ZOO_DLL_EXPORT void addIntToMap(map *, const char *, const int)
Add a key and an integer value to an existing map.
Definition: service.c:474
Metadata information about a full Service.
Definition: main_conf_read.y:279
struct services * content
the content services pointer
Definition: main_conf_read.y:300
struct elements elements
Metadata information about input or output.
ZOO_DLL_EXPORT elements * dupElements(elements *)
Duplicate an elements.
Definition: service.c:1133
ZOO_DLL_EXPORT void charxxxToMaps(char ***, maps **)
Convert a char*** to a maps (only used for Fortran support)
Definition: service.c:1573
ZOO_DLL_EXPORT elements * createElements(char *)
Create a named elements.
Definition: service.c:975
ZOO_DLL_EXPORT void dumpServiceAsYAML(service *)
Dump a service on stderr using the YAML syntaxe.
Definition: service.c:1250
char * name
the key
Definition: service.h:200
Profile registry.
Definition: main_conf_read.y:298
Not named linked list.
Definition: main_conf_read.y:255
struct maps * next
the pointer to the next maps if any or NULL
Definition: main_conf_read.y:247
struct maps * child
the child maps
Definition: main_conf_read.y:246
ZOO_DLL_EXPORT int addMapsArrayToMaps(maps **, maps *, char *)
Add a Maps containing a MapArray to a Maps.
Definition: service.c:874
ZOO_DLL_EXPORT map * getMapOrFill(map **, const char *, const char *)
Access a specific map or set its value.
Definition: service.c:589
struct map * next
the pointer to the next map if any or NULL
Definition: service.h:202
ZOO_DLL_EXPORT map * addToMapWithSize(map *, const char *, const char *, int)
Add a key and a binary value to an existing map.
Definition: service.c:501
ZOO_DLL_EXPORT void inheritMap(map **, map *)
Apply inheritance to an out map from a reference in map.
Definition: service.c:1427
struct iotype * defaults
the default iotype
Definition: main_conf_read.y:270
ZOO_DLL_EXPORT void freeService(service **)
Free allocated memory of a service.
Definition: service.c:424
struct elements * next
the pointer to the next element if any (or NULL)
Definition: main_conf_read.y:273
struct iotype iotype
Not named linked list.
#define ZOO_DLL_EXPORT
Definition: service.h:33
ZOO_DLL_EXPORT void setMapArray(map *, const char *, int, const char *)
Add a key value in a MapArray for a specific index.
Definition: service.c:817
#define zGettimeofday
The crossplatform gettimeofday alias.
Definition: service.h:102
Metadata information about input or output.
Definition: main_conf_read.y:265
struct map * content
the content map
Definition: main_conf_read.y:267
ZOO_DLL_EXPORT int count(map *)
Count number of map in a map.
Definition: service.c:180
ZOO_DLL_EXPORT iotype * getIoTypeFromElement(elements *, char *, map *)
Access a specific iotype from an elements.
Definition: service.c:635
ZOO_DLL_EXPORT void setMapInMaps(maps *, const char *, const char *, const char *)
Set a key value pair to a map contained in a Maps.
Definition: service.c:927
char * name
the name
Definition: main_conf_read.y:280
ZOO_DLL_EXPORT void inheritIOType(iotype **, iotype *)
Apply inheritance to an out iotype from a reference in iotype.
Definition: service.c:1447
ZOO_DLL_EXPORT map * getLastMap(map *)
Access the last map.
Definition: service.c:253
ZOO_DLL_EXPORT void loadMapBinaries(map **, map *)
Load binary values from a map (in) and add them to another map (out).
Definition: service.c:705
struct elements * child
the pointer to the children element if any (or NULL)
Definition: main_conf_read.y:272
ZOO_DLL_EXPORT service * dupService(service *)
Duplicate a service.
Definition: service.c:1283
struct map * content
the content map
Definition: main_conf_read.y:281
struct services * next
the pointer to the next services*
Definition: main_conf_read.y:292
ZOO_DLL_EXPORT void freeElements(elements **)
Free allocated memory of an elements.
Definition: service.c:388
Services chained list.
Definition: main_conf_read.y:290
ZOO_DLL_EXPORT maps * dupMaps(maps **)
Duplicate a Maps.
Definition: service.c:726
pair PAIR FOUND n
Definition: service_conf.y:899