1 | /* |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright 2017 GeoLabs SARL. All rights reserved. |
---|
5 | * |
---|
6 | * This work was supported by public funds received in the framework of GEOSUD, |
---|
7 | * a project (ANR-10-EQPX-20) of the program "Investissements d'Avenir" managed |
---|
8 | * by the French National Research Agency |
---|
9 | * |
---|
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
11 | * of this software and associated documentation files (the "Software"), to deal |
---|
12 | * in the Software without restriction, including without limitation the rights |
---|
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
14 | * copies of the Software, and to permit persons to whom the Software is |
---|
15 | * furnished to do so, subject to the following conditions: |
---|
16 | * |
---|
17 | * The above copyright notice and this permission notice shall be included in |
---|
18 | * all copies or substantial portions of the Software. |
---|
19 | * |
---|
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
26 | * THE SOFTWARE. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "service_callback.h" |
---|
30 | #include "service_json.h" |
---|
31 | #include "service_internal_ms.h" |
---|
32 | |
---|
33 | #ifdef __cplusplus |
---|
34 | extern "C" { |
---|
35 | #endif |
---|
36 | |
---|
37 | /** |
---|
38 | * Check if a service name is prohibited, meaning that we don't have to invoke |
---|
39 | * the callback for this specific service. |
---|
40 | * |
---|
41 | */ |
---|
42 | bool isProhibited(maps* conf,const char* serviceName){ |
---|
43 | map* plist=getMapFromMaps(conf,"callback","prohibited"); |
---|
44 | if(plist!=NULL){ |
---|
45 | char *tmp=plist->value; |
---|
46 | char *tmpS=strtok(tmp,","); |
---|
47 | while(tmpS!=NULL){ |
---|
48 | if(strcmp(serviceName,tmpS)==0) |
---|
49 | return true; |
---|
50 | tmpS=strtok(NULL,","); |
---|
51 | } |
---|
52 | } |
---|
53 | return false; |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Invoke the callback in case there is a [callback] section containing a url parameter |
---|
58 | * |
---|
59 | * @param m the maps containing the main configuration file definitions |
---|
60 | * @param inputs the inputs defined in the request (can be null if not yet initialized) |
---|
61 | * @param inputs the outputs provided in the request (can be null if not yet initialized) |
---|
62 | * @param step the step number, steps are defined as: |
---|
63 | * 0: Analyze creation |
---|
64 | * 1: Fetching Data Inputs |
---|
65 | * 2: Uploading data inputs to cluster |
---|
66 | * 3: Creating Job Script |
---|
67 | * 4: Submitting Job to Cluster |
---|
68 | * 5: Downloading processed output from cluster |
---|
69 | * 6: Finalize |
---|
70 | * 7: Dismiss or Error |
---|
71 | * @param state 0 in case the step starts, 1 when it ends |
---|
72 | * @return bool true in case of success, false in other cases |
---|
73 | */ |
---|
74 | bool invokeCallback(maps* m,maps* inputs,maps* outputs,int step,int state){ |
---|
75 | map* url=getMapFromMaps(m,"callback","url"); |
---|
76 | if(url==NULL) |
---|
77 | return false; |
---|
78 | |
---|
79 | maps* lenv=getMaps(m,"lenv"); |
---|
80 | map* sname=getMap(lenv->content,"identifier"); |
---|
81 | if(sname!=NULL && isProhibited(m,sname->value)) |
---|
82 | return false; |
---|
83 | |
---|
84 | json_object *res=json_object_new_object(); |
---|
85 | |
---|
86 | map* sid=getMapFromMaps(m,"lenv","usid"); |
---|
87 | if(sid!=NULL){ |
---|
88 | json_object *jsStr=json_object_new_string(sid->value); |
---|
89 | json_object_object_add(res,"jobid",jsStr); |
---|
90 | } |
---|
91 | switch(step){ |
---|
92 | case 0: { |
---|
93 | // Create a new analyze |
---|
94 | maps* lenv=getMaps(m,"lenv"); |
---|
95 | sid=getMapFromMaps(m,"lenv","xrequest"); |
---|
96 | if(sid!=NULL){ |
---|
97 | json_object *jsStr=json_object_new_string(sid->value); |
---|
98 | json_object_object_add(res,"request_execute_content",jsStr); |
---|
99 | } |
---|
100 | sid=getMapFromMaps(m,"lenv","identifier"); |
---|
101 | if(sid!=NULL){ |
---|
102 | json_object *jsStr=json_object_new_string(sid->value); |
---|
103 | json_object_object_add(res,"process_identifier",jsStr); |
---|
104 | } |
---|
105 | break; |
---|
106 | } |
---|
107 | case 1: { |
---|
108 | // Fetching data inputs |
---|
109 | maps* curs=inputs; |
---|
110 | |
---|
111 | char *keys[8][2]={ |
---|
112 | { |
---|
113 | "href", |
---|
114 | "ref" |
---|
115 | }, |
---|
116 | { |
---|
117 | "cache_file", |
---|
118 | "cachefile" |
---|
119 | }, |
---|
120 | { |
---|
121 | "fmimeType", |
---|
122 | "mimetype" |
---|
123 | }, |
---|
124 | { |
---|
125 | "size", |
---|
126 | "size" |
---|
127 | }, |
---|
128 | { |
---|
129 | "ref_wms_link", |
---|
130 | "ref_wms_link" |
---|
131 | }, |
---|
132 | { |
---|
133 | "ref_wcs_link", |
---|
134 | "ref_wcs_link" |
---|
135 | }, |
---|
136 | { |
---|
137 | "ref_wfs_link", |
---|
138 | "ref_wfs_link" |
---|
139 | }, |
---|
140 | { |
---|
141 | "datatype", |
---|
142 | "datatype" |
---|
143 | } |
---|
144 | }; |
---|
145 | json_object *res1=json_object_new_object(); |
---|
146 | while(curs!=NULL){ |
---|
147 | map* tmpMap=getMap(curs->content,"cache_file"); |
---|
148 | sid=getMap(curs->content,"ref_wms_link"); |
---|
149 | if(tmpMap!=NULL && sid==NULL){ |
---|
150 | addToMap(curs->content,"generated_file",tmpMap->value); |
---|
151 | tmpMap=getMap(curs->content,"fmimeType"); |
---|
152 | if(tmpMap!=NULL){ |
---|
153 | addToMap(curs->content,"mimeType",tmpMap->value); |
---|
154 | } |
---|
155 | setReferenceUrl(m,curs); |
---|
156 | //outputMapfile(m,curs); |
---|
157 | dumpMaps(curs); |
---|
158 | } |
---|
159 | json_object *res2=json_object_new_object(); |
---|
160 | int i=0; |
---|
161 | int hasRef=-1; |
---|
162 | for(;i<8;i++){ |
---|
163 | sid=getMap(curs->content,keys[i][0]); |
---|
164 | if(sid!=NULL){ |
---|
165 | json_object *jsStr=json_object_new_string(sid->value); |
---|
166 | json_object_object_add(res2,keys[i][1],jsStr); |
---|
167 | if(i==0) |
---|
168 | hasRef=1; |
---|
169 | } |
---|
170 | } |
---|
171 | if(hasRef<0) |
---|
172 | json_object_put(res2); |
---|
173 | else |
---|
174 | json_object_object_add(res1,curs->name,res2); |
---|
175 | curs=curs->next; |
---|
176 | } |
---|
177 | json_object_object_add(res,"inputs",res1); |
---|
178 | json_object* in=mapsToJson(inputs); |
---|
179 | if(in!=NULL){ |
---|
180 | //json_object_object_add(res,"inputs",in); |
---|
181 | json_object_put(in); |
---|
182 | } |
---|
183 | break; |
---|
184 | } |
---|
185 | case 2: { |
---|
186 | // Uploading data input to cluster |
---|
187 | maps* in=getMaps(m,"uploadQueue"); |
---|
188 | if(in!=NULL){ |
---|
189 | maps* curs=in; |
---|
190 | map* length=getMapFromMaps(in,"uploadQueue","length"); |
---|
191 | if(length!=NULL){ |
---|
192 | json_object *res1=json_object_new_object(); |
---|
193 | json_object *res2=json_object_new_object(); |
---|
194 | int limit=atoi(length->value); |
---|
195 | int i=0; |
---|
196 | maps* uploadQueue=getMaps(in,"uploadQueue"); |
---|
197 | map* tmp=uploadQueue->content; |
---|
198 | for(;i<limit;i++){ |
---|
199 | map* tmp0=getMapArray(tmp,"input",i); |
---|
200 | map* tmp1=getMapArray(tmp,"localPath",i); |
---|
201 | map* tmp2=getMapArray(tmp,"targetPath",i); |
---|
202 | if(tmp0!=NULL && tmp1!=NULL && tmp2!=NULL){ |
---|
203 | json_object *jsStr=json_object_new_string(tmp1->value); |
---|
204 | json_object_object_add(res2,"local_path",jsStr); |
---|
205 | jsStr=json_object_new_string(tmp2->value); |
---|
206 | json_object_object_add(res2,"target_path",jsStr); |
---|
207 | json_object_object_add(res1,tmp0->value,res2); |
---|
208 | } |
---|
209 | } |
---|
210 | json_object_object_add(res,"inputs",res1); |
---|
211 | } |
---|
212 | //json_object_object_add(res,"inputs",in); |
---|
213 | } |
---|
214 | break; |
---|
215 | } |
---|
216 | case 3: { |
---|
217 | // Generating job script |
---|
218 | sid=getMapFromMaps(m,"lenv","local_script"); |
---|
219 | if(sid!=NULL){ |
---|
220 | json_object *jsStr=json_object_new_string(sid->value); |
---|
221 | json_object_object_add(res,"script",jsStr); |
---|
222 | } |
---|
223 | break; |
---|
224 | } |
---|
225 | case 4: { |
---|
226 | // Submitting job to cluster |
---|
227 | sid=getMapFromMaps(m,"lenv","remote_script"); |
---|
228 | if(sid!=NULL){ |
---|
229 | json_object *jsStr=json_object_new_string(sid->value); |
---|
230 | json_object_object_add(res,"script",jsStr); |
---|
231 | } |
---|
232 | break; |
---|
233 | } |
---|
234 | case 5: { |
---|
235 | // Downloading process outputs from cluster |
---|
236 | json_object* in=mapsToJson(outputs); |
---|
237 | if(in!=NULL){ |
---|
238 | //json_object_object_add(res,"outputs",in); |
---|
239 | json_object_put(in); |
---|
240 | } |
---|
241 | break; |
---|
242 | } |
---|
243 | case 6: { |
---|
244 | // Finalize HPC |
---|
245 | sid=getMapFromMaps(m,"lenv","local_script"); |
---|
246 | if(sid!=NULL){ |
---|
247 | json_object *jsStr=json_object_new_string(sid->value); |
---|
248 | json_object_object_add(res,"inputs",jsStr); |
---|
249 | } |
---|
250 | break; |
---|
251 | } |
---|
252 | case 7: { |
---|
253 | // Error or Dismiss |
---|
254 | sid=getMapFromMaps(m,"lenv","message"); |
---|
255 | if(sid!=NULL){ |
---|
256 | json_object *jsStr=json_object_new_string(sid->value); |
---|
257 | json_object_object_add(res,"message",jsStr); |
---|
258 | } |
---|
259 | json_object *jsStr=json_object_new_string("failed"); |
---|
260 | json_object_object_add(res,"wps_status",jsStr); |
---|
261 | break; |
---|
262 | } |
---|
263 | others: { |
---|
264 | break; |
---|
265 | } |
---|
266 | } |
---|
267 | fprintf(stderr,"************************* %s %d\n\n",__FILE__,__LINE__); |
---|
268 | fflush(stderr); |
---|
269 | HINTERNET hInternet,res1; |
---|
270 | hInternet=InternetOpen("ZooWPSClient\0", |
---|
271 | INTERNET_OPEN_TYPE_PRECONFIG, |
---|
272 | NULL,NULL, 0); |
---|
273 | if(!CHECK_INET_HANDLE(hInternet)){ |
---|
274 | InternetCloseHandle (&hInternet); |
---|
275 | return false; |
---|
276 | } |
---|
277 | fprintf(stderr," * JSON: [%s] \n",json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN)); |
---|
278 | fprintf(stderr," * URL: %s%d_%d/ \n\n",url->value,step,state); |
---|
279 | fflush(stderr); |
---|
280 | char *URL=(char*)malloc((strlen(url->value)+5)*sizeof(char)); |
---|
281 | sprintf(URL,"%s%d_%d/",url->value,step,state); |
---|
282 | hInternet.waitingRequests[0] = zStrdup(URL); |
---|
283 | free(URL); |
---|
284 | const char* jsonStr=json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN); |
---|
285 | res1 = InternetOpenUrl (&hInternet, |
---|
286 | hInternet.waitingRequests[0], |
---|
287 | (char*)jsonStr, strlen(jsonStr), |
---|
288 | INTERNET_FLAG_NO_CACHE_WRITE, |
---|
289 | 0); |
---|
290 | AddHeaderEntries(&hInternet,m); |
---|
291 | //curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_VERBOSE, 1); |
---|
292 | processDownloads(&hInternet); |
---|
293 | char *tmp = (char *) malloc ((hInternet.ihandle[0].nDataLen + 1) |
---|
294 | * sizeof (char)); |
---|
295 | if (tmp == NULL) |
---|
296 | { |
---|
297 | setMapInMaps(m,"lenv","message",_("Unable to allocate memory")); |
---|
298 | setMapInMaps(m,"lenv","code","InternalError"); |
---|
299 | return false; |
---|
300 | } |
---|
301 | size_t bRead; |
---|
302 | InternetReadFile (hInternet.ihandle[0], |
---|
303 | (LPVOID) tmp, |
---|
304 | hInternet. |
---|
305 | ihandle[0].nDataLen, |
---|
306 | &bRead); |
---|
307 | tmp[hInternet.ihandle[0].nDataLen] = 0; |
---|
308 | fprintf(stderr,"Result: \n%s\n\n",tmp); |
---|
309 | fprintf(stderr,"************************* %s %d\n\n",__FILE__,__LINE__); |
---|
310 | fflush(stderr); |
---|
311 | free(tmp); |
---|
312 | json_object_put(res); |
---|
313 | InternetCloseHandle(&hInternet); |
---|
314 | return true; |
---|
315 | } |
---|
316 | |
---|
317 | #ifdef __cplusplus |
---|
318 | } |
---|
319 | #endif |
---|