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