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