[845] | 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 | * Invoke the callback in case there is a [callback] section containing a url parameter |
---|
| 39 | * |
---|
| 40 | * @param m the maps containing the main configuration file definitions |
---|
| 41 | * @param inputs the inputs defined in the request (can be null if not yet initialized) |
---|
| 42 | * @param inputs the outputs provided in the request (can be null if not yet initialized) |
---|
| 43 | * @param step the step number, steps are defined as: |
---|
| 44 | * 0: Analyze creation |
---|
| 45 | * 1: Fetching Data Inputs |
---|
| 46 | * 2: Uploading data inputs to cluster |
---|
| 47 | * 3: Creating Job Script |
---|
| 48 | * 4: Submitting Job to Cluster |
---|
| 49 | * 5: Downloading processed output from cluster |
---|
| 50 | * 6: Finalize |
---|
| 51 | * 7: Dismiss or Error |
---|
| 52 | * @param state 0 in case the step starts, 1 when it ends |
---|
| 53 | * @return bool true in case of success, false in other cases |
---|
| 54 | */ |
---|
| 55 | bool invokeCallback(maps* m,maps* inputs,maps* outputs,int step,int state){ |
---|
| 56 | map* url=getMapFromMaps(m,"callback","url"); |
---|
| 57 | if(url==NULL) |
---|
| 58 | return false; |
---|
| 59 | |
---|
| 60 | maps* lenv=getMaps(m,"lenv"); |
---|
| 61 | |
---|
| 62 | json_object *res=json_object_new_object(); |
---|
| 63 | |
---|
| 64 | map* sid=getMapFromMaps(m,"lenv","usid"); |
---|
| 65 | if(sid!=NULL){ |
---|
| 66 | json_object *jsStr=json_object_new_string(sid->value); |
---|
| 67 | json_object_object_add(res,"jobid",jsStr); |
---|
| 68 | } |
---|
| 69 | switch(step){ |
---|
| 70 | case 0: { |
---|
| 71 | // Create a new analyze |
---|
| 72 | maps* lenv=getMaps(m,"lenv"); |
---|
| 73 | sid=getMapFromMaps(m,"lenv","xrequest"); |
---|
| 74 | if(sid!=NULL){ |
---|
| 75 | json_object *jsStr=json_object_new_string(sid->value); |
---|
| 76 | json_object_object_add(res,"request_execute_content",jsStr); |
---|
| 77 | } |
---|
| 78 | sid=getMapFromMaps(m,"lenv","identifier"); |
---|
| 79 | if(sid!=NULL){ |
---|
| 80 | json_object *jsStr=json_object_new_string(sid->value); |
---|
| 81 | json_object_object_add(res,"process_identifier",jsStr); |
---|
| 82 | } |
---|
| 83 | break; |
---|
| 84 | } |
---|
| 85 | case 1: { |
---|
| 86 | // Fetching data inputs |
---|
| 87 | maps* curs=inputs; |
---|
| 88 | |
---|
| 89 | char *keys[4][2]={ |
---|
| 90 | { |
---|
| 91 | "href", |
---|
| 92 | "ref" |
---|
| 93 | }, |
---|
| 94 | { |
---|
| 95 | "cache_file", |
---|
| 96 | "cachefile" |
---|
| 97 | }, |
---|
| 98 | { |
---|
| 99 | "fmimeType", |
---|
| 100 | "mimetype" |
---|
| 101 | }, |
---|
| 102 | { |
---|
| 103 | "size", |
---|
| 104 | "size" |
---|
| 105 | } |
---|
| 106 | }; |
---|
| 107 | json_object *res1=json_object_new_object(); |
---|
| 108 | while(curs!=NULL){ |
---|
| 109 | map* tmpMap=getMap(curs->content,"cache_file"); |
---|
| 110 | if(tmpMap!=NULL){ |
---|
| 111 | addToMap(curs->content,"generated_file",tmpMap->value); |
---|
| 112 | tmpMap=getMap(curs->content,"fmimeType"); |
---|
| 113 | if(tmpMap!=NULL){ |
---|
| 114 | addToMap(curs->content,"mimeType",tmpMap->value); |
---|
| 115 | } |
---|
| 116 | setReferenceUrl(m,curs); |
---|
| 117 | //outputMapfile(m,curs); |
---|
| 118 | dumpMaps(curs); |
---|
| 119 | } |
---|
| 120 | json_object *res2=json_object_new_object(); |
---|
| 121 | int i=0; |
---|
| 122 | int hasRef=-1; |
---|
| 123 | for(;i<4;i++){ |
---|
| 124 | sid=getMap(curs->content,keys[i][0]); |
---|
| 125 | if(sid!=NULL){ |
---|
| 126 | json_object *jsStr=json_object_new_string(sid->value); |
---|
| 127 | json_object_object_add(res2,keys[i][1],jsStr); |
---|
| 128 | if(i==0) |
---|
| 129 | hasRef=1; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | if(hasRef<0) |
---|
| 133 | json_object_put(res2); |
---|
| 134 | else |
---|
| 135 | json_object_object_add(res1,curs->name,res2); |
---|
| 136 | curs=curs->next; |
---|
| 137 | } |
---|
| 138 | json_object_object_add(res,"inputs",res1); |
---|
| 139 | json_object* in=mapsToJson(inputs); |
---|
| 140 | if(in!=NULL){ |
---|
| 141 | //json_object_object_add(res,"inputs",in); |
---|
| 142 | json_object_put(in); |
---|
| 143 | } |
---|
| 144 | break; |
---|
| 145 | } |
---|
| 146 | case 2: { |
---|
| 147 | // Uploading data input to cluster |
---|
| 148 | maps* in=getMaps(m,"uploadQueue"); |
---|
| 149 | if(in!=NULL){ |
---|
| 150 | maps* curs=in; |
---|
| 151 | map* length=getMapFromMaps(in,"uploadQueue","length"); |
---|
| 152 | if(length!=NULL){ |
---|
| 153 | json_object *res1=json_object_new_object(); |
---|
| 154 | json_object *res2=json_object_new_object(); |
---|
| 155 | int limit=atoi(length->value); |
---|
| 156 | int i=0; |
---|
| 157 | maps* uploadQueue=getMaps(in,"uploadQueue"); |
---|
| 158 | map* tmp=uploadQueue->content; |
---|
| 159 | for(;i<limit;i++){ |
---|
| 160 | map* tmp0=getMapArray(tmp,"input",i); |
---|
| 161 | map* tmp1=getMapArray(tmp,"localPath",i); |
---|
| 162 | map* tmp2=getMapArray(tmp,"targetPath",i); |
---|
| 163 | if(tmp0!=NULL && tmp1!=NULL && tmp2!=NULL){ |
---|
| 164 | json_object *jsStr=json_object_new_string(tmp1->value); |
---|
| 165 | json_object_object_add(res2,"local_path",jsStr); |
---|
| 166 | jsStr=json_object_new_string(tmp2->value); |
---|
| 167 | json_object_object_add(res2,"target_path",jsStr); |
---|
| 168 | json_object_object_add(res1,tmp0->value,res2); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | json_object_object_add(res,"inputs",res1); |
---|
| 172 | } |
---|
| 173 | //json_object_object_add(res,"inputs",in); |
---|
| 174 | } |
---|
| 175 | break; |
---|
| 176 | } |
---|
| 177 | case 3: { |
---|
| 178 | // Generating job script |
---|
| 179 | sid=getMapFromMaps(m,"lenv","local_script"); |
---|
| 180 | if(sid!=NULL){ |
---|
| 181 | json_object *jsStr=json_object_new_string(sid->value); |
---|
| 182 | json_object_object_add(res,"script",jsStr); |
---|
| 183 | } |
---|
| 184 | break; |
---|
| 185 | } |
---|
| 186 | case 4: { |
---|
| 187 | // Submitting job to cluster |
---|
| 188 | sid=getMapFromMaps(m,"lenv","remote_script"); |
---|
| 189 | if(sid!=NULL){ |
---|
| 190 | json_object *jsStr=json_object_new_string(sid->value); |
---|
| 191 | json_object_object_add(res,"script",jsStr); |
---|
| 192 | } |
---|
| 193 | break; |
---|
| 194 | } |
---|
| 195 | case 5: { |
---|
| 196 | // Downloading process outputs from cluster |
---|
| 197 | json_object* in=mapsToJson(outputs); |
---|
| 198 | if(in!=NULL){ |
---|
| 199 | //json_object_object_add(res,"outputs",in); |
---|
| 200 | json_object_put(in); |
---|
| 201 | } |
---|
| 202 | break; |
---|
| 203 | } |
---|
| 204 | case 6: { |
---|
| 205 | // Finalize HPC |
---|
| 206 | sid=getMapFromMaps(m,"lenv","local_script"); |
---|
| 207 | if(sid!=NULL){ |
---|
| 208 | json_object *jsStr=json_object_new_string(sid->value); |
---|
| 209 | json_object_object_add(res,"inputs",jsStr); |
---|
| 210 | } |
---|
| 211 | break; |
---|
| 212 | } |
---|
| 213 | case 7: { |
---|
| 214 | // Error or Dismiss |
---|
| 215 | sid=getMapFromMaps(m,"lenv","message"); |
---|
| 216 | if(sid!=NULL){ |
---|
| 217 | json_object *jsStr=json_object_new_string(sid->value); |
---|
| 218 | json_object_object_add(res,"message",jsStr); |
---|
| 219 | } |
---|
| 220 | json_object *jsStr=json_object_new_string("failed"); |
---|
| 221 | json_object_object_add(res,"wps_status",jsStr); |
---|
| 222 | break; |
---|
| 223 | } |
---|
| 224 | others: { |
---|
| 225 | break; |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | fprintf(stderr,"************************* %s %d\n\n",__FILE__,__LINE__); |
---|
| 229 | fflush(stderr); |
---|
| 230 | HINTERNET hInternet,res1; |
---|
| 231 | hInternet=InternetOpen("ZooWPSClient\0", |
---|
| 232 | INTERNET_OPEN_TYPE_PRECONFIG, |
---|
| 233 | NULL,NULL, 0); |
---|
| 234 | if(!CHECK_INET_HANDLE(hInternet)){ |
---|
| 235 | InternetCloseHandle (&hInternet); |
---|
| 236 | return false; |
---|
| 237 | } |
---|
| 238 | fprintf(stderr," * JSON: [%s] \n",json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN)); |
---|
| 239 | fprintf(stderr," * URL: %s%d_%d/ \n\n",url->value,step,state); |
---|
| 240 | fflush(stderr); |
---|
| 241 | char *URL=(char*)malloc((strlen(url->value)+5)*sizeof(char)); |
---|
| 242 | sprintf(URL,"%s%d_%d/",url->value,step,state); |
---|
| 243 | hInternet.waitingRequests[0] = zStrdup(URL); |
---|
| 244 | free(URL); |
---|
| 245 | const char* jsonStr=json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN); |
---|
| 246 | res1 = InternetOpenUrl (&hInternet, |
---|
| 247 | hInternet.waitingRequests[0], |
---|
| 248 | (char*)jsonStr, strlen(jsonStr), |
---|
| 249 | INTERNET_FLAG_NO_CACHE_WRITE, |
---|
| 250 | 0); |
---|
[846] | 251 | AddHeaderEntries(&hInternet,m); |
---|
[845] | 252 | //curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_VERBOSE, 1); |
---|
| 253 | processDownloads(&hInternet); |
---|
| 254 | char *tmp = (char *) malloc ((hInternet.ihandle[0].nDataLen + 1) |
---|
| 255 | * sizeof (char)); |
---|
| 256 | if (tmp == NULL) |
---|
| 257 | { |
---|
| 258 | setMapInMaps(m,"lenv","message",_("Unable to allocate memory")); |
---|
| 259 | setMapInMaps(m,"lenv","code","InternalError"); |
---|
| 260 | return false; |
---|
| 261 | } |
---|
| 262 | size_t bRead; |
---|
| 263 | InternetReadFile (hInternet.ihandle[0], |
---|
| 264 | (LPVOID) tmp, |
---|
| 265 | hInternet. |
---|
| 266 | ihandle[0].nDataLen, |
---|
| 267 | &bRead); |
---|
| 268 | tmp[hInternet.ihandle[0].nDataLen] = 0; |
---|
| 269 | fprintf(stderr,"Result: \n%s\n\n",tmp); |
---|
| 270 | fprintf(stderr,"************************* %s %d\n\n",__FILE__,__LINE__); |
---|
| 271 | fflush(stderr); |
---|
| 272 | free(tmp); |
---|
| 273 | json_object_put(res); |
---|
| 274 | InternetCloseHandle(&hInternet); |
---|
| 275 | return true; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | #ifdef __cplusplus |
---|
| 279 | } |
---|
| 280 | #endif |
---|