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