[644] | 1 | /* |
---|
[822] | 2 | * Authors : David Saggiorato |
---|
| 3 | * Gérald Fenoy |
---|
[652] | 4 | * Copyright 2015 GeoLabs SARL. All rights reserved. |
---|
[644] | 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 | |
---|
[652] | 25 | #include "ogr_api.h" |
---|
| 26 | #include "ogrsf_frmts.h" |
---|
| 27 | #include "ogr_p.h" |
---|
[785] | 28 | #include "response_print.h" |
---|
[784] | 29 | #if GDAL_VERSION_MAJOR >= 2 |
---|
| 30 | #include <gdal_priv.h> |
---|
| 31 | #endif |
---|
[652] | 32 | |
---|
[644] | 33 | #include "sqlapi.h" |
---|
| 34 | #include <fcgi_stdio.h> |
---|
| 35 | #include <stdlib.h> |
---|
| 36 | |
---|
| 37 | /** |
---|
[652] | 38 | * Global GDALDataset pointer |
---|
[644] | 39 | */ |
---|
[784] | 40 | #if GDAL_VERSION_MAJOR >=2 |
---|
| 41 | GDALDataset |
---|
| 42 | #else |
---|
| 43 | OGRDataSource |
---|
| 44 | #endif |
---|
[839] | 45 | **zoo_DS = NULL; |
---|
[652] | 46 | |
---|
| 47 | /** |
---|
| 48 | * Global OGRLayer pointer pointing to the lastest result set |
---|
| 49 | */ |
---|
| 50 | OGRLayer *zoo_ResultSet = NULL; |
---|
| 51 | |
---|
| 52 | /** |
---|
[822] | 53 | * Create a GDAL / OGR string for connecting to a db backend defined in the |
---|
| 54 | * key section. |
---|
[652] | 55 | * |
---|
| 56 | * @param conf the maps containing the setting of the main.cfg file |
---|
[822] | 57 | * @param key the name of the section containing the connection setting |
---|
| 58 | * @return the OGR connection string |
---|
[652] | 59 | */ |
---|
[822] | 60 | char* _createInitString(maps* conf,const char* key){ |
---|
[652] | 61 | char* res=NULL; |
---|
[822] | 62 | char keywords[6][14]={ |
---|
[652] | 63 | "dbname", |
---|
| 64 | "host", |
---|
| 65 | "port", |
---|
| 66 | "user", |
---|
[822] | 67 | "password", |
---|
| 68 | "active_schema" |
---|
[652] | 69 | }; |
---|
| 70 | int i=0; |
---|
[822] | 71 | maps* cconf=getMaps(conf,key); |
---|
[854] | 72 | if(cconf==NULL){ |
---|
[933] | 73 | fprintf(stderr,"%s %d\n",__FILE__,__LINE__); |
---|
| 74 | static char err[] = "-1"; |
---|
| 75 | return err; |
---|
[854] | 76 | } |
---|
[652] | 77 | int len=0; |
---|
[822] | 78 | for(i=0;i<6;i++){ |
---|
[652] | 79 | map* tmp=getMap(cconf->content,keywords[i]); |
---|
| 80 | if(tmp!=NULL){ |
---|
| 81 | if(res==NULL){ |
---|
| 82 | res=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+4)*sizeof(char)); |
---|
| 83 | sprintf(res,"%s='%s'",keywords[i],tmp->value); |
---|
| 84 | len+=strlen(res); |
---|
| 85 | }else{ |
---|
| 86 | char* res1=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char)); |
---|
| 87 | sprintf(res1," %s='%s'",keywords[i],tmp->value); |
---|
| 88 | res=(char*)realloc(res,(len+strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char)); |
---|
| 89 | memcpy(res+len,res1,(strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char)); |
---|
| 90 | len+=strlen(res1); |
---|
| 91 | res[len]=0; |
---|
| 92 | free(res1); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | map* tmp=getMap(cconf->content,"type"); |
---|
| 97 | if(tmp!=NULL){ |
---|
| 98 | char* fres=(char*)malloc((strlen(res)+strlen(tmp->value)+2)*sizeof(char)); |
---|
| 99 | sprintf(fres,"%s:%s",tmp->value,res); |
---|
| 100 | free(res); |
---|
| 101 | return fres; |
---|
| 102 | } |
---|
[644] | 103 | return res; |
---|
| 104 | } |
---|
[652] | 105 | |
---|
| 106 | /** |
---|
[822] | 107 | * Create a GDAL / OGR string for connecting to the db backend |
---|
[652] | 108 | * |
---|
| 109 | * @param conf the maps containing the setting of the main.cfg file |
---|
[822] | 110 | * @return the OGR connection string |
---|
| 111 | */ |
---|
| 112 | char* createInitString(maps* conf){ |
---|
| 113 | return _createInitString(conf,"database"); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * Connect to a db backend. |
---|
| 118 | * |
---|
| 119 | * @param conf the maps containing the setting of the main.cfg file |
---|
[652] | 120 | * @see createInitString |
---|
| 121 | */ |
---|
[839] | 122 | int _init_sql(maps* conf,const char* key){ |
---|
[822] | 123 | char* sqlInitString=_createInitString(conf,key); |
---|
[860] | 124 | #ifdef SQL_DEBUG |
---|
[854] | 125 | fprintf(stderr,"Try to connect to: %s %s !\n",key,sqlInitString); |
---|
| 126 | fflush(stderr); |
---|
[860] | 127 | #endif |
---|
[839] | 128 | if(strncmp(sqlInitString,"-1",2)==0) |
---|
| 129 | return -1; |
---|
[652] | 130 | OGRSFDriver *poDriver = NULL; |
---|
| 131 | OGRRegisterAll(); |
---|
[839] | 132 | int zoo_ds_nb=0; |
---|
| 133 | map* dsNb=getMapFromMaps(conf,"lenv","ds_nb"); |
---|
| 134 | if(dsNb==NULL){ |
---|
| 135 | setMapInMaps(conf,"lenv","ds_nb","1"); |
---|
| 136 | }else{ |
---|
| 137 | zoo_ds_nb=atoi(dsNb->value); |
---|
| 138 | char* tmp=(char*)malloc(11*sizeof(char)); |
---|
| 139 | sprintf(tmp,"%d",zoo_ds_nb+1); |
---|
| 140 | setMapInMaps(conf,"lenv","ds_nb",(const char*)tmp); |
---|
| 141 | free(tmp); |
---|
| 142 | } |
---|
| 143 | if(zoo_DS==NULL) |
---|
| 144 | zoo_DS= |
---|
[784] | 145 | #if GDAL_VERSION_MAJOR >= 2 |
---|
[839] | 146 | (GDALDataset**) malloc(sizeof(GDALDataset*)) |
---|
| 147 | #else |
---|
| 148 | (OGRDataSource**) malloc(sizeof(OGRDataSource*)) |
---|
| 149 | #endif |
---|
| 150 | ; |
---|
| 151 | else |
---|
| 152 | zoo_DS= |
---|
| 153 | #if GDAL_VERSION_MAJOR >= 2 |
---|
| 154 | (GDALDataset**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(GDALDataset*)) |
---|
| 155 | #else |
---|
| 156 | (OGRDataSource**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(OGRDataSource*)) |
---|
| 157 | #endif |
---|
| 158 | ; |
---|
| 159 | |
---|
| 160 | #if GDAL_VERSION_MAJOR >= 2 |
---|
| 161 | zoo_DS[zoo_ds_nb] = (GDALDataset*) GDALOpenEx( sqlInitString, |
---|
[784] | 162 | GDAL_OF_UPDATE | GDAL_OF_VECTOR, |
---|
| 163 | NULL, NULL, NULL ); |
---|
| 164 | #else |
---|
[839] | 165 | zoo_DS[zoo_ds_nb] = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver); |
---|
[784] | 166 | #endif |
---|
[839] | 167 | if( zoo_DS[zoo_ds_nb] == NULL ){ |
---|
[860] | 168 | #ifdef SQL_DEBUG |
---|
[652] | 169 | fprintf(stderr,"sqlInitString: %s FAILED !\n",sqlInitString); |
---|
| 170 | fflush(stderr); |
---|
| 171 | #endif |
---|
| 172 | free(sqlInitString); |
---|
[839] | 173 | setMapInMaps(conf,"lenv","dbIssue","1"); |
---|
| 174 | setMapInMaps(conf,"lenv","message",_("Failed to connect to the database backend")); |
---|
| 175 | return -2; |
---|
[652] | 176 | } |
---|
[860] | 177 | #ifdef SQL_DEBUG |
---|
[652] | 178 | fprintf(stderr,"sqlInitString: %s SUCEED !\n",sqlInitString); |
---|
| 179 | fflush(stderr); |
---|
| 180 | #endif |
---|
| 181 | free(sqlInitString); |
---|
[839] | 182 | zoo_ds_nb++; |
---|
| 183 | return zoo_ds_nb; |
---|
[652] | 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
[822] | 187 | * Connect to the db backend. |
---|
| 188 | * |
---|
| 189 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 190 | * @see createInitString |
---|
| 191 | */ |
---|
[839] | 192 | int init_sql(maps* conf){ |
---|
[822] | 193 | return _init_sql(conf,"database"); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /** |
---|
[652] | 197 | * Close any connection to the db backend. |
---|
| 198 | * |
---|
| 199 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 200 | */ |
---|
[839] | 201 | void close_sql(maps* conf,int cid){ |
---|
[854] | 202 | if( zoo_ResultSet != NULL ){ |
---|
[839] | 203 | zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet ); |
---|
[854] | 204 | zoo_ResultSet=NULL; |
---|
| 205 | } |
---|
| 206 | if(zoo_DS!=NULL && zoo_DS[cid]!=NULL){ |
---|
[784] | 207 | #if GDAL_VERSION_MAJOR >= 2 |
---|
[839] | 208 | GDALClose(zoo_DS[cid]); |
---|
[784] | 209 | #else |
---|
[839] | 210 | OGRDataSource::DestroyDataSource( zoo_DS[cid] ); |
---|
[784] | 211 | #endif |
---|
[839] | 212 | zoo_DS[cid]=NULL; |
---|
[652] | 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | /** |
---|
| 217 | * Call OGRCleanupAll. |
---|
| 218 | * |
---|
| 219 | */ |
---|
| 220 | void end_sql(){ |
---|
| 221 | OGRCleanupAll(); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | /** |
---|
[822] | 225 | * Fetch a tuple set by executing a SQL query to the Database Backend. |
---|
| 226 | * |
---|
| 227 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 228 | * @param sqlQuery the SQL query to run |
---|
| 229 | * @return NULL in case of failure or an OGRLayer pointer if the query succeed. |
---|
| 230 | */ |
---|
[839] | 231 | OGRLayer *fetchSql(maps* conf,int cid,const char* sqlQuery){ |
---|
[854] | 232 | if(zoo_DS==NULL || zoo_DS[cid]==NULL) |
---|
[839] | 233 | return NULL; |
---|
[822] | 234 | OGRLayer *res=NULL; |
---|
[860] | 235 | #ifdef SQL_DEBUG |
---|
[839] | 236 | fprintf(stderr,"************************* %s %s %d\n\n",sqlQuery,__FILE__,__LINE__); |
---|
| 237 | fflush(stderr); |
---|
| 238 | #endif |
---|
| 239 | res = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL); |
---|
[822] | 240 | return res; |
---|
| 241 | } |
---|
| 242 | |
---|
[839] | 243 | void cleanFetchSql(maps* conf,int cid,OGRLayer *objects){ |
---|
[822] | 244 | if( objects != NULL ){ |
---|
[839] | 245 | zoo_DS[cid]->ReleaseResultSet( objects ); |
---|
[822] | 246 | objects=NULL; |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
[652] | 251 | * Execute a SQL query to the SQL Database Backend. |
---|
| 252 | * |
---|
| 253 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 254 | * @param sqlQuery the SQL query to run |
---|
| 255 | * @return -1 in case of failure and 1 if the query succeed. |
---|
| 256 | */ |
---|
[839] | 257 | int execSql(maps* conf,int cid,const char* sqlQuery){ |
---|
[822] | 258 | int res=-1; |
---|
[854] | 259 | if(zoo_DS == NULL || zoo_DS[cid]==NULL) |
---|
| 260 | return -1; |
---|
[839] | 261 | zoo_ResultSet = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL); |
---|
[652] | 262 | if( zoo_ResultSet != NULL ){ |
---|
[822] | 263 | res=1; |
---|
[652] | 264 | } |
---|
[822] | 265 | return res; |
---|
[652] | 266 | } |
---|
| 267 | |
---|
| 268 | /** |
---|
| 269 | * Clean any memory allocated by executing a request |
---|
| 270 | * |
---|
| 271 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 272 | * @param sqlQuery the SQL query to run |
---|
| 273 | * @return -1 in case of failure and 1 if the query succeed. |
---|
| 274 | */ |
---|
[839] | 275 | void cleanUpResultSet(const maps* conf,int cid){ |
---|
[652] | 276 | if( zoo_ResultSet != NULL ){ |
---|
[839] | 277 | zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet ); |
---|
[652] | 278 | zoo_ResultSet=NULL; |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
[839] | 282 | #ifdef RELY_ON_DB |
---|
| 283 | int getCurrentId(maps* conf){ |
---|
| 284 | int res=0; |
---|
| 285 | map* dsNb=getMapFromMaps(conf,"lenv","ds_nb"); |
---|
| 286 | if(dsNb!=NULL) |
---|
| 287 | res=atoi(dsNb->value); |
---|
| 288 | return res; |
---|
| 289 | } |
---|
| 290 | |
---|
[652] | 291 | /** |
---|
| 292 | * Record a file stored during ZOO-Kernel execution |
---|
| 293 | * |
---|
| 294 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 295 | * @param filename the file's name |
---|
| 296 | * @param type the type (Intput,Output,Response) |
---|
| 297 | * @param name the maps containing the setting of the main.cfg file |
---|
| 298 | */ |
---|
| 299 | void recordStoredFile(maps* conf,const char* filename,const char* type,const char* name){ |
---|
[839] | 300 | int zoo_ds_nb=getCurrentId(conf); |
---|
[652] | 301 | map *uusid=getMapFromMaps(conf,"lenv","usid"); |
---|
| 302 | map *schema=getMapFromMaps(conf,"database","schema"); |
---|
| 303 | char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(uusid->value)+strlen(filename)+strlen(type)+(name!=NULL?strlen(name):2)+68+1)*sizeof(char)); |
---|
| 304 | if(name!=NULL) |
---|
| 305 | sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s','%s');",schema->value,uusid->value,filename,type,name); |
---|
| 306 | else |
---|
| 307 | sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s',NULL);",schema->value,uusid->value,filename,type); |
---|
[839] | 308 | execSql(conf,zoo_ds_nb-1,sqlQuery); |
---|
[734] | 309 | free(sqlQuery); |
---|
[839] | 310 | cleanUpResultSet(conf,zoo_ds_nb-1); |
---|
[652] | 311 | } |
---|
| 312 | |
---|
| 313 | /** |
---|
| 314 | * Insert the reference tuple corresponding to the running service |
---|
| 315 | * |
---|
| 316 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 317 | */ |
---|
| 318 | void recordServiceStatus(maps* conf){ |
---|
[839] | 319 | int zoo_ds_nb=getCurrentId(conf); |
---|
[652] | 320 | map *sid=getMapFromMaps(conf,"lenv","sid"); |
---|
| 321 | map *osid=getMapFromMaps(conf,"lenv","osid"); |
---|
| 322 | map *uusid=getMapFromMaps(conf,"lenv","usid"); |
---|
| 323 | map *schema=getMapFromMaps(conf,"database","schema"); |
---|
| 324 | char *sqlQuery=(char*)malloc((strlen(schema->value)+ |
---|
| 325 | strlen(uusid->value)+ |
---|
| 326 | strlen(osid->value)+ |
---|
[785] | 327 | strlen(sid->value)+ |
---|
| 328 | strlen(wpsStatus[2])+66+1)*sizeof(char)); |
---|
[652] | 329 | sprintf(sqlQuery, |
---|
[785] | 330 | "INSERT INTO %s.services (uuid,sid,osid,fstate)" |
---|
| 331 | "VALUES ('%s','%s','%s','%s');", |
---|
[652] | 332 | schema->value, |
---|
[785] | 333 | uusid->value, |
---|
| 334 | sid->value, |
---|
| 335 | osid->value, |
---|
| 336 | wpsStatus[2]); |
---|
[839] | 337 | execSql(conf,zoo_ds_nb-1,sqlQuery); |
---|
[734] | 338 | free(sqlQuery); |
---|
[839] | 339 | cleanUpResultSet(conf,zoo_ds_nb-1); |
---|
[652] | 340 | } |
---|
| 341 | |
---|
| 342 | /** |
---|
| 343 | * Store the content of the result file |
---|
| 344 | * |
---|
| 345 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 346 | * @param filename the file's name |
---|
| 347 | */ |
---|
| 348 | void recordResponse(maps* conf,char* filename){ |
---|
[839] | 349 | int zoo_ds_nb=getCurrentId(conf); |
---|
[652] | 350 | FILE *file = fopen (filename, "rb"); |
---|
| 351 | fseek (file, 0, SEEK_END); |
---|
| 352 | long flen = ftell (file); |
---|
| 353 | fseek (file, 0, SEEK_SET); |
---|
| 354 | char *tmps = (char *) malloc ((flen + 1) * sizeof (char)); |
---|
| 355 | fread (tmps, flen, 1, file); |
---|
| 356 | tmps[flen]=0; |
---|
| 357 | fclose(file); |
---|
| 358 | map *sid=getMapFromMaps(conf,"lenv","usid"); |
---|
| 359 | map *schema=getMapFromMaps(conf,"database","schema"); |
---|
[786] | 360 | char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+57+1)*sizeof(char)); |
---|
[654] | 361 | sprintf(sqlQuery,"INSERT INTO %s.responses (content,uuid) VALUES ($$%s$$,$$%s$$);",schema->value,tmps,sid->value); |
---|
[839] | 362 | execSql(conf,zoo_ds_nb-1,sqlQuery); |
---|
[734] | 363 | free(sqlQuery); |
---|
| 364 | free(tmps); |
---|
[839] | 365 | cleanUpResultSet(conf,zoo_ds_nb-1); |
---|
[652] | 366 | } |
---|
| 367 | |
---|
| 368 | /** |
---|
| 369 | * Update the current status of the running service. |
---|
| 370 | * |
---|
| 371 | * @param conf the map containing the setting of the main.cfg file |
---|
| 372 | * @return 0 on success, -2 if shmget failed, -1 if shmat failed |
---|
| 373 | */ |
---|
| 374 | int _updateStatus(maps* conf){ |
---|
[839] | 375 | int zoo_ds_nb=getCurrentId(conf); |
---|
[652] | 376 | map *sid=getMapFromMaps(conf,"lenv","usid"); |
---|
| 377 | map *p=getMapFromMaps(conf,"lenv","status"); |
---|
| 378 | map *msg=getMapFromMaps(conf,"lenv","message"); |
---|
| 379 | map *schema=getMapFromMaps(conf,"database","schema"); |
---|
| 380 | char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(msg->value)+strlen(p->value)+strlen(sid->value)+64+1)*sizeof(char)); |
---|
| 381 | sprintf(sqlQuery,"UPDATE %s.services set status=$$%s$$,message=$$%s$$ where uuid=$$%s$$;",schema->value,p->value,msg->value,sid->value); |
---|
[865] | 382 | if( zoo_DS == NULL || zoo_DS[zoo_ds_nb-1]==NULL ){ |
---|
[652] | 383 | init_sql(conf); |
---|
[839] | 384 | zoo_ds_nb++; |
---|
| 385 | } |
---|
[865] | 386 | execSql(conf,zoo_ds_nb-1,sqlQuery); |
---|
| 387 | cleanUpResultSet(conf,zoo_ds_nb-1); |
---|
[734] | 388 | free(sqlQuery); |
---|
[652] | 389 | return 0; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | /** |
---|
| 393 | * Get the ongoing status of a running service |
---|
| 394 | * |
---|
| 395 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 396 | * @param pid the service identifier (usid key from the [lenv] section) |
---|
| 397 | * @return the reported status char* (MESSAGE|POURCENTAGE) |
---|
| 398 | */ |
---|
| 399 | char* _getStatus(maps* conf,char* pid){ |
---|
[839] | 400 | int zoo_ds_nb=getCurrentId(conf); |
---|
[850] | 401 | int created=-1; |
---|
[652] | 402 | map *schema=getMapFromMaps(conf,"database","schema"); |
---|
| 403 | char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char)); |
---|
| 404 | sprintf(sqlQuery,"select status||'|'||message from %s.services where uuid=$$%s$$;",schema->value,pid); |
---|
[839] | 405 | if( zoo_ds_nb== |
---|
| 406 | #ifdef META_DB |
---|
| 407 | 1 |
---|
| 408 | #else |
---|
| 409 | 0 |
---|
| 410 | #endif |
---|
| 411 | ){ |
---|
[652] | 412 | init_sql(conf); |
---|
[839] | 413 | zoo_ds_nb++; |
---|
[850] | 414 | created=1; |
---|
[839] | 415 | } |
---|
| 416 | execSql(conf,zoo_ds_nb-1,sqlQuery); |
---|
[652] | 417 | OGRFeature *poFeature = NULL; |
---|
| 418 | const char *tmp1; |
---|
| 419 | while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){ |
---|
| 420 | for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){ |
---|
| 421 | if( poFeature->IsFieldSet( iField ) ){ |
---|
[890] | 422 | tmp1=zStrdup(poFeature->GetFieldAsString( iField )); |
---|
[652] | 423 | } |
---|
| 424 | else |
---|
| 425 | tmp1=NULL; |
---|
| 426 | } |
---|
| 427 | OGRFeature::DestroyFeature( poFeature ); |
---|
| 428 | } |
---|
[839] | 429 | cleanUpResultSet(conf,zoo_ds_nb-1); |
---|
[734] | 430 | free(sqlQuery); |
---|
[652] | 431 | return (char*)tmp1; |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | /** |
---|
| 435 | * Read the cache file of a running service |
---|
| 436 | * |
---|
| 437 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 438 | * @param pid the service identifier (usid key from the [lenv] section) |
---|
| 439 | * @return the reported status char* (temporary/final result) |
---|
| 440 | */ |
---|
| 441 | char* _getStatusFile(maps* conf,char* pid){ |
---|
[839] | 442 | int zoo_ds_nb=getCurrentId(conf); |
---|
[652] | 443 | map *schema=getMapFromMaps(conf,"database","schema"); |
---|
[854] | 444 | OGRFeature *poFeature = NULL; |
---|
| 445 | const char *tmp1=NULL; |
---|
| 446 | int hasRes=-1; |
---|
[654] | 447 | char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+82+1)*sizeof(char)); |
---|
| 448 | sprintf(sqlQuery, |
---|
| 449 | "select content from %s.responses where uuid=$$%s$$" |
---|
| 450 | " order by creation_time desc limit 1",schema->value,pid); |
---|
[839] | 451 | if( zoo_ds_nb== |
---|
| 452 | #ifdef META_DB |
---|
| 453 | 1 |
---|
| 454 | #else |
---|
| 455 | 0 |
---|
| 456 | #endif |
---|
| 457 | ){ |
---|
[652] | 458 | init_sql(conf); |
---|
[839] | 459 | zoo_ds_nb++; |
---|
| 460 | } |
---|
| 461 | execSql(conf,zoo_ds_nb-1,sqlQuery); |
---|
| 462 | if(zoo_ResultSet!=NULL){ |
---|
| 463 | while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){ |
---|
| 464 | for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){ |
---|
| 465 | if( poFeature->IsFieldSet( iField ) ){ |
---|
[890] | 466 | tmp1=zStrdup(poFeature->GetFieldAsString( iField )); |
---|
[839] | 467 | hasRes=1; |
---|
| 468 | } |
---|
| 469 | else |
---|
| 470 | tmp1=NULL; |
---|
| 471 | } |
---|
| 472 | OGRFeature::DestroyFeature( poFeature ); |
---|
[652] | 473 | } |
---|
| 474 | } |
---|
| 475 | if(hasRes<0) |
---|
| 476 | tmp1=NULL; |
---|
[854] | 477 | cleanUpResultSet(conf,zoo_ds_nb-1); |
---|
[734] | 478 | free(sqlQuery); |
---|
[652] | 479 | return (char*)tmp1; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | /** |
---|
[654] | 483 | * Delete a service reference from the database. |
---|
| 484 | * |
---|
| 485 | * @param conf the map containing the setting of the main.cfg file |
---|
| 486 | * @param pid the service identifier (usid key from the [lenv] section) |
---|
| 487 | */ |
---|
| 488 | void removeService(maps* conf,char* pid){ |
---|
[839] | 489 | int zoo_ds_nb=getCurrentId(conf); |
---|
[654] | 490 | map *schema=getMapFromMaps(conf,"database","schema"); |
---|
| 491 | char *sqlQuery=(char*) |
---|
| 492 | malloc((strlen(pid)+strlen(schema->value)+38+1) |
---|
| 493 | *sizeof(char)); |
---|
[839] | 494 | if( zoo_ds_nb== |
---|
| 495 | #ifdef META_DB |
---|
| 496 | 1 |
---|
| 497 | #else |
---|
| 498 | 0 |
---|
| 499 | #endif |
---|
| 500 | ){ |
---|
[654] | 501 | init_sql(conf); |
---|
[839] | 502 | zoo_ds_nb++; |
---|
| 503 | } |
---|
[654] | 504 | sprintf(sqlQuery, |
---|
| 505 | "DELETE FROM %s.services where uuid=$$%s$$;", |
---|
| 506 | schema->value,pid); |
---|
[839] | 507 | execSql(conf,zoo_ds_nb-1,sqlQuery); |
---|
| 508 | cleanUpResultSet(conf,zoo_ds_nb-1); |
---|
| 509 | close_sql(conf,zoo_ds_nb-1); |
---|
[734] | 510 | free(sqlQuery); |
---|
[654] | 511 | end_sql(); |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | /** |
---|
[652] | 515 | * Stop handling status repport. |
---|
| 516 | * |
---|
| 517 | * @param conf the map containing the setting of the main.cfg file |
---|
| 518 | */ |
---|
| 519 | void unhandleStatus(maps* conf){ |
---|
[839] | 520 | int zoo_ds_nb=getCurrentId(conf); |
---|
[654] | 521 | map *schema=getMapFromMaps(conf,"database","schema"); |
---|
[652] | 522 | map *sid=getMapFromMaps(conf,"lenv","usid"); |
---|
[654] | 523 | map *fstate=getMapFromMaps(conf,"lenv","fstate"); |
---|
| 524 | char *sqlQuery=(char*)malloc((strlen(sid->value)+ |
---|
| 525 | strlen(schema->value)+ |
---|
| 526 | (fstate!=NULL? |
---|
| 527 | strlen(fstate->value): |
---|
| 528 | 6) |
---|
| 529 | +66+1)*sizeof(char)); |
---|
| 530 | sprintf(sqlQuery, |
---|
| 531 | "UPDATE %s.services set end_time=now(), fstate=$$%s$$" |
---|
| 532 | " where uuid=$$%s$$;", |
---|
| 533 | schema->value,(fstate!=NULL?fstate->value:"Failed"),sid->value); |
---|
[839] | 534 | execSql(conf,zoo_ds_nb-1,sqlQuery); |
---|
| 535 | cleanUpResultSet(conf,zoo_ds_nb-1); |
---|
[851] | 536 | //close_sql(conf,zoo_ds_nb-1); |
---|
[734] | 537 | free(sqlQuery); |
---|
[854] | 538 | //end_sql(); |
---|
[652] | 539 | } |
---|
| 540 | |
---|
[654] | 541 | /** |
---|
| 542 | * Read the sid identifier attached of a service if any |
---|
| 543 | * |
---|
| 544 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 545 | * @param pid the service identifier (usid key from the [lenv] section) |
---|
| 546 | * @return the sid value |
---|
| 547 | */ |
---|
| 548 | char* getStatusId(maps* conf,char* pid){ |
---|
[839] | 549 | int zoo_ds_nb=getCurrentId(conf); |
---|
[654] | 550 | map *schema=getMapFromMaps(conf,"database","schema"); |
---|
| 551 | char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char)); |
---|
| 552 | sprintf(sqlQuery, |
---|
| 553 | "select osid from %s.services where uuid=$$%s$$", |
---|
| 554 | schema->value,pid); |
---|
[839] | 555 | if( zoo_ds_nb==0 ){ |
---|
[654] | 556 | init_sql(conf); |
---|
[839] | 557 | zoo_ds_nb++; |
---|
| 558 | } |
---|
[854] | 559 | if(execSql(conf,zoo_ds_nb-1,sqlQuery)<0) |
---|
| 560 | return NULL; |
---|
[654] | 561 | OGRFeature *poFeature = NULL; |
---|
| 562 | const char *tmp1; |
---|
| 563 | int hasRes=-1; |
---|
| 564 | while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){ |
---|
| 565 | for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){ |
---|
| 566 | if( poFeature->IsFieldSet( iField ) ){ |
---|
| 567 | tmp1=zStrdup(poFeature->GetFieldAsString( iField )); |
---|
| 568 | hasRes=1; |
---|
| 569 | break; |
---|
| 570 | } |
---|
| 571 | } |
---|
| 572 | OGRFeature::DestroyFeature( poFeature ); |
---|
| 573 | } |
---|
| 574 | if(hasRes<0) |
---|
| 575 | tmp1=NULL; |
---|
[734] | 576 | free(sqlQuery); |
---|
[839] | 577 | cleanUpResultSet(conf,zoo_ds_nb-1); |
---|
[654] | 578 | return (char*)tmp1; |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | /** |
---|
| 582 | * Read the Result file (.res). |
---|
| 583 | * |
---|
| 584 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 585 | * @param pid the service identifier (usid key from the [lenv] section) |
---|
| 586 | */ |
---|
| 587 | void readFinalRes(maps* conf,char* pid,map* statusInfo){ |
---|
[839] | 588 | int zoo_ds_nb=getCurrentId(conf); |
---|
[654] | 589 | map *schema=getMapFromMaps(conf,"database","schema"); |
---|
| 590 | char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char)); |
---|
| 591 | sprintf(sqlQuery, |
---|
| 592 | "select fstate from %s.services where uuid=$$%s$$", |
---|
| 593 | schema->value,pid); |
---|
| 594 | if( zoo_DS == NULL ) |
---|
| 595 | init_sql(conf); |
---|
[839] | 596 | execSql(conf,zoo_ds_nb-1,sqlQuery); |
---|
[654] | 597 | OGRFeature *poFeature = NULL; |
---|
| 598 | int hasRes=-1; |
---|
| 599 | while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){ |
---|
| 600 | for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){ |
---|
| 601 | if( poFeature->IsFieldSet( iField ) ){ |
---|
| 602 | addToMap(statusInfo,"Status",poFeature->GetFieldAsString( iField )); |
---|
| 603 | hasRes=1; |
---|
| 604 | break; |
---|
| 605 | } |
---|
| 606 | } |
---|
| 607 | OGRFeature::DestroyFeature( poFeature ); |
---|
| 608 | } |
---|
[851] | 609 | cleanUpResultSet(conf,zoo_ds_nb-1); |
---|
[654] | 610 | if(hasRes<0) |
---|
| 611 | addToMap(statusInfo,"Status","Failed"); |
---|
[734] | 612 | free(sqlQuery); |
---|
[654] | 613 | return; |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | /** |
---|
| 617 | * Check if a service is running. |
---|
| 618 | * |
---|
| 619 | * @param conf the maps containing the setting of the main.cfg file |
---|
| 620 | * @param pid the unique service identifier (usid from the lenv section) |
---|
| 621 | * @return 1 in case the service is still running, 0 otherwise |
---|
| 622 | */ |
---|
| 623 | int isRunning(maps* conf,char* pid){ |
---|
| 624 | int res=0; |
---|
[839] | 625 | int zoo_ds_nb=getCurrentId(conf); |
---|
[654] | 626 | map *schema=getMapFromMaps(conf,"database","schema"); |
---|
| 627 | char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+73+1)*sizeof(char)); |
---|
| 628 | sprintf(sqlQuery,"select count(*) as t from %s.services where uuid=$$%s$$ and end_time is null;",schema->value,pid); |
---|
[839] | 629 | if( zoo_ds_nb == 0 ){ |
---|
[654] | 630 | init_sql(conf); |
---|
[839] | 631 | zoo_ds_nb++; |
---|
| 632 | } |
---|
| 633 | execSql(conf,zoo_ds_nb-1,sqlQuery); |
---|
[654] | 634 | OGRFeature *poFeature = NULL; |
---|
| 635 | const char *tmp1; |
---|
| 636 | while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){ |
---|
| 637 | for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){ |
---|
| 638 | if( poFeature->IsFieldSet( iField ) && |
---|
| 639 | atoi(poFeature->GetFieldAsString( iField ))>0 ){ |
---|
| 640 | res=1; |
---|
| 641 | break; |
---|
| 642 | } |
---|
| 643 | } |
---|
| 644 | OGRFeature::DestroyFeature( poFeature ); |
---|
| 645 | } |
---|
[839] | 646 | cleanUpResultSet(conf,zoo_ds_nb-1); |
---|
[734] | 647 | free(sqlQuery); |
---|
[654] | 648 | return res; |
---|
| 649 | } |
---|
| 650 | |
---|
[652] | 651 | #endif |
---|