Changeset 652


Ignore:
Timestamp:
Jun 11, 2015, 3:07:11 PM (9 years ago)
Author:
djay
Message:

Better concurrency gesture for asynchronous requests, add db backend support for status informations.

Location:
trunk/zoo-project
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/zoo-project/HISTORY.txt

    r634 r652  
    11Version 1.5.0-dev
     2  * Fix concurrency access to status informations
     3  * Use simple file rather than shared memory for storing status informations
     4  * Add support for db backend to store status informations
     5  * Add the lib_zoo_service shared library to be linked against C services
    26  * Add ZOO-API for the PHP language (with documentation)
    37  * Add doxygen comments in source code
  • trunk/zoo-project/zoo-kernel/Makefile.in

    r649 r652  
    9393        g++ -g -O2 ${XML2CFLAGS} ${CFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${PYTHONCFLAGS} ${JAVACFLAGS} ${JSCFLAGS} ${PERLCFLAGS} ${PHPCFLAGS} ${SAGA_ENABLED} ${OTB_ENABLED} ${PYTHON_ENABLED} ${JS_ENABLED} ${PHP_ENABLED} ${PERL_ENABLED} ${JAVA_ENABLED} -c zoo_service_loader.c  -fno-common -DPIC -o zoo_service_loader.o
    9494
    95 libzoo_service.${EXT}: version.h service_internal.o service.o
    96         gcc -shared  ${GDAL_CFLAGS} ${DEFAULT_OPTS} -fpic -o libzoo_service.${EXT} ${CFLAGS}  service_internal.o service.o -lfcgi ${GDAL_LIBS}
     95libzoo_service.${EXT}: version.h service_internal.o service.o sqlapi.o
     96        gcc -shared  ${GDAL_CFLAGS} ${DEFAULT_OPTS} -fpic -o libzoo_service.${EXT} ${CFLAGS}  service_internal.o service.o sqlapi.o -lfcgi ${GDAL_LIBS}
    9797
    98 zoo_loader.cgi: version.h libzoo_service.${EXT} zoo_loader.c zoo_service_loader.o  ulinet.o service.h lex.sr.o service_conf.tab.o service_conf.y ulinet.o main_conf_read.tab.o lex.cr.o request_parser.o sqlapi.o response_print.o server_internal.o caching.o ${MS_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE} ${RUBY_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE}
     98zoo_loader.cgi: version.h libzoo_service.${EXT} zoo_loader.c zoo_service_loader.o  ulinet.o service.h lex.sr.o service_conf.tab.o service_conf.y ulinet.o main_conf_read.tab.o lex.cr.o request_parser.o response_print.o server_internal.o caching.o ${MS_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE} ${RUBY_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE}
    9999        g++ -g -O2 ${JSCFLAGS} ${PHPCFLAGS}  ${PERLCFLAGS} ${RUBYCFLAGS}  ${JAVACFLAGS} ${XML2CFLAGS} ${PYTHONCFLAGS} ${CFLAGS} -c zoo_loader.c  -fno-common -DPIC -o zoo_loader.o
    100         g++  ${JSCFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${PHPCFLAGS} ${PERLCFLAGS} ${JAVACFLAGS} ${PYTHONCFLAGS} ${CFLAGS} zoo_loader.o zoo_service_loader.o ${MS_FILE} ${PYTHON_FILE}  ${PERL_FILE} ${PHP_FILE}  ${JS_FILE} ${JAVA_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} sqlapi.o response_print.o server_internal.o caching.o request_parser.o ulinet.o lex.cr.o lex.sr.o service_conf.tab.o main_conf_read.tab.o -o zoo_loader.cgi -L. ${LDFLAGS}
     100        g++  ${JSCFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${PHPCFLAGS} ${PERLCFLAGS} ${JAVACFLAGS} ${PYTHONCFLAGS} ${CFLAGS} zoo_loader.o zoo_service_loader.o ${MS_FILE} ${PYTHON_FILE}  ${PERL_FILE} ${PHP_FILE}  ${JS_FILE} ${JAVA_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} response_print.o server_internal.o caching.o request_parser.o ulinet.o lex.cr.o lex.sr.o service_conf.tab.o main_conf_read.tab.o -o zoo_loader.cgi -L. ${LDFLAGS}
    101101
    102102zcfg2yaml: zcfg2yaml.c service.h lex.sr.o service_conf.tab.o service_conf.y main_conf_read.tab.o lex.cr.o response_print.o server_internal.o service_internal.o ${MS_FILE} ${YAML_FILE}
  • trunk/zoo-project/zoo-kernel/ZOOMakefile.opts.in

    r651 r652  
    9090SAGA_FILE=@SAGA_FILE@
    9191
    92 CFLAGS=@DEB_DEF@ -fpic ${YAML_CFLAGS} ${MACOS_CFLAGS} ${MS_CFLAGS} -I../../thirds/cgic206 -I. -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
     92CFLAGS=@RELY_ON_DB@ @DEB_DEF@ -fpic ${YAML_CFLAGS} ${MACOS_CFLAGS} ${MS_CFLAGS} -I../../thirds/cgic206 -I. -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
    9393LDFLAGS=-lzoo_service -lcurl -L../../thirds/cgic206 -lcgic ${GDAL_LIBS} ${XML2LDFLAGS} ${PYTHONLDFLAGS} ${PERLLDFLAGS}  ${PHPLDFLAGS} ${JAVALDFLAGS} ${JSLDFLAGS} -lfcgi -lcrypto -luuid ${MS_LDFLAGS} ${MACOS_LD_FLAGS} ${MACOS_LD_NET_FLAGS} ${YAML_LDFLAGS} ${OTBLDFLAGS} ${SAGA_LDFLAGS}
    9494
  • trunk/zoo-project/zoo-kernel/configure.ac

    r640 r652  
    4747
    4848AC_ARG_WITH([cgi-dir],
    49     [AS_HELP_STRING([--with-cgi-dir=PATH], [specify an alternative cgi directory path ( default: /usr/lib/cgi-bin) ])],
     49    [AS_HELP_STRING([--with-cgi-dir=PATH], [Specifies an alternative cgi directory path ( default: /usr/lib/cgi-bin) ])],
    5050    [CGI_DIR="$withval"], [CGI_DIR="/usr/lib/cgi-bin"])
    5151AC_SUBST([CGI_DIR])
    5252
     53AC_ARG_WITH([db-backend],
     54    [AS_HELP_STRING([--with-db-backend], [Relies on a database for storing status messages and response files ])],
     55    [RELY_ON_DB="-DRELY_ON_DB"], [RELY_ON_DB=""])
     56AC_SUBST([RELY_ON_DB])
     57
    5358
    5459# ===========================================================================
     
    5762
    5863AC_ARG_WITH([yaml],
    59         [AS_HELP_STRING([--with-yaml=PATH], [specify an alternative location for the yaml library])],
     64        [AS_HELP_STRING([--with-yaml=PATH], [Specifies an alternative location for the yaml library])],
    6065        [YAMLPATH="$withval"], [YAMLPATH=""])
    6166
     
    9499
    95100AC_ARG_WITH([fastcgi],
    96         [AS_HELP_STRING([--with-fastcgi=PATH], [specify an alternative location for the fastcgi library])],
     101        [AS_HELP_STRING([--with-fastcgi=PATH], [Specifies an alternative location for the fastcgi library])],
    97102        [FCGIPATH="$withval"], [FCGIPATH="/usr"])
    98103
     
    118123
    119124AC_ARG_WITH([xml2config],
    120         [AS_HELP_STRING([--with-xml2config=FILE], [specify an alternative xml2-config file])],
     125        [AS_HELP_STRING([--with-xml2config=FILE], [Specifies an alternative xml2-config file])],
    121126        [XML2CONFIG="$withval"], [XML2CONFIG=""])
    122127
     
    132137        # XML2CONFIG was specified; display a message to the user
    133138        if test "x$XML2CONFIG" = "xyes"; then
    134                 AC_MSG_ERROR([you must specify a parameter to --with-xml2config, e.g. --with-xml2config=/path/to/xml2-config])
     139                AC_MSG_ERROR([you must Specifies a parameter to --with-xml2config, e.g. --with-xml2config=/path/to/xml2-config])
    135140        else
    136141                if test -f $XML2CONFIG; then
     
    167172
    168173AC_ARG_WITH([xsltconfig],
    169         [AS_HELP_STRING([--with-xsltconfig=FILE], [specify an alternative xslt-config file])],
     174        [AS_HELP_STRING([--with-xsltconfig=FILE], [Specifies an alternative xslt-config file])],
    170175        [XSLTCONFIG="$withval"], [XSLTCONFIG=""])
    171176
     
    181186        # XSLTCONFIG was specified; display a message to the user
    182187        if test "x$XSLTCONFIG" = "xyes"; then
    183                 AC_MSG_ERROR([you must specify a parameter to --with-xsltconfig, e.g. --with-xsltconfig=/path/to/xslt-config])
     188                AC_MSG_ERROR([you must Specifies a parameter to --with-xsltconfig, e.g. --with-xsltconfig=/path/to/xslt-config])
    184189        else
    185190                if test -f $XSLTCONFIG; then
     
    209214
    210215AC_ARG_WITH([gdal-config],
    211         [AS_HELP_STRING([--with-gdal-config=FILE], [specify an alternative gdal-config file])],
     216        [AS_HELP_STRING([--with-gdal-config=FILE], [Specifies an alternative gdal-config file])],
    212217        [GDAL_CONFIG="$withval"], [GDAL_CONFIG=""])
    213218if test -z $GDAL_CONFIG;
     
    238243
    239244AC_ARG_WITH([proj],
    240         [AS_HELP_STRING([--with-proj=PATH], [specify an alternative location for PROJ4 setup])],
     245        [AS_HELP_STRING([--with-proj=PATH], [Specifies an alternative location for PROJ4 setup])],
    241246        [PROJPATH="$withval"], [PROJPATH=""])
    242247
     
    259264
    260265AC_ARG_WITH([geosconfig],
    261         [AS_HELP_STRING([--with-geosconfig=FILE], [specify an alternative geos-config file])],
     266        [AS_HELP_STRING([--with-geosconfig=FILE], [Specifies an alternative geos-config file])],
    262267        [GEOSCONFIG="$withval"], [GEOSCONFIG=""])
    263268
     
    273278        # GEOSCONFIG was specified; display a message to the user
    274279        if test "x$GEOSCONFIG" = "xyes"; then
    275                 AC_MSG_WARN([you must specify a parameter to --with-geosconfig, e.g. --with-geosconfig=/path/to/geos-config])
     280                AC_MSG_WARN([you must Specifies a parameter to --with-geosconfig, e.g. --with-geosconfig=/path/to/geos-config])
    276281        else
    277282                if test -f $GEOSCONFIG; then
     
    301306
    302307AC_ARG_WITH([cgal],
    303         [AS_HELP_STRING([--with-cgal=PATH], [specify an alternative location for CGAL setup])],
     308        [AS_HELP_STRING([--with-cgal=PATH], [Specifies an alternative location for CGAL setup])],
    304309        [CGALPATH="$withval"], [CGALPATH="/usr"])
    305310
     
    323328
    324329AC_ARG_WITH([mapserver],
    325        [AS_HELP_STRING([--with-mapserver=PATH], [specify the path for MapServer compiled source tree])],
     330       [AS_HELP_STRING([--with-mapserver=PATH], [Specifies the path for MapServer compiled source tree])],
    326331       [MS_SRC_PATH="$withval"], [MS_SRC_PATH=""])
    327332
     
    362367
    363368AC_ARG_WITH([python],
    364         [AS_HELP_STRING([--with-python=PATH], [To enable python support or specify an alternative directory for python installation,  disabled by default])],
     369        [AS_HELP_STRING([--with-python=PATH], [To enable python support or Specifies an alternative directory for python installation,  disabled by default])],
    365370        [PYTHON_PATH="$withval"; PYTHON_ENABLED="-DUSE_PYTHON"], [PYTHON_ENABLED=""])
    366371
     
    417422
    418423AC_ARG_WITH([js],
    419         [AS_HELP_STRING([--with-js=PATH], [specify --with-js=path-to-js to enable js support, specify --with-js on linux debian like, js support is disabled by default ])],
     424        [AS_HELP_STRING([--with-js=PATH], [Specifies --with-js=path-to-js to enable js support, specify --with-js on linux debian like, js support is disabled by default ])],
    420425        [JSHOME="$withval";JS_ENABLED="-DUSE_JS"], [JS_ENABLED=""])
    421426
     
    666671
    667672AC_ARG_WITH([itk],
    668         [AS_HELP_STRING([--with-itk=PATH], [specify an alternative location for the itk library])],
     673        [AS_HELP_STRING([--with-itk=PATH], [Specifies an alternative location for the itk library])],
    669674        [ITKPATH="$withval"], [ITKPATH=""])
    670675
    671676AC_ARG_WITH([itk-version],
    672         [AS_HELP_STRING([--with-itk-version=VERSION], [specify an alternative version for the itk library])],
     677        [AS_HELP_STRING([--with-itk-version=VERSION], [Specifies an alternative version for the itk library])],
    673678        [ITKVERS="$withval"], [ITKVERS=""])
    674679
    675680AC_ARG_WITH([otb],
    676         [AS_HELP_STRING([--with-otb=PATH], [specify an alternative location for the otb library])],
     681        [AS_HELP_STRING([--with-otb=PATH], [Specifies an alternative location for the otb library])],
    677682        [OTBPATH="$withval"], [OTBPATH=""])
    678683
     
    720725
    721726AC_ARG_WITH([wx-config],
    722         [AS_HELP_STRING([--with-wx-config=PATH], [specify an alternative path for the wx-config tool])],
     727        [AS_HELP_STRING([--with-wx-config=PATH], [Specifies an alternative path for the wx-config tool])],
    723728        [WXCFG="$withval"], [WXCFG=""])
    724729
    725730AC_ARG_WITH([saga],
    726         [AS_HELP_STRING([--with-saga=PATH], [specify an alternative location for the SAGA-GIS library])],
     731        [AS_HELP_STRING([--with-saga=PATH], [Specifies an alternative location for the SAGA-GIS library])],
    727732        [SAGAPATH="$withval"], [SAGAPATH=""])
    728733
  • trunk/zoo-project/zoo-kernel/response_print.c

    r651 r652  
    15151515      int lpid;
    15161516      map* tmpm2=getMapFromMaps(m,"lenv","usid");
    1517       lpid=atoi(tmpm2->value);
    1518       tmpm2=getMap(tmp_maps->content,"tmpUrl");
    1519       if(tmpm1!=NULL && tmpm2!=NULL){
    1520         if( strncasecmp( tmpm2->value, "http://", 7) == 0 ||
    1521             strncasecmp( tmpm2->value, "https://", 8 ) == 0 ){
    1522           sprintf(url,"%s/%s_%i.xml",tmpm2->value,service,lpid);
     1517      map* tmpm3=getMap(tmp_maps->content,"tmpUrl");
     1518      if(tmpm1!=NULL && tmpm3!=NULL){
     1519        if( strncasecmp( tmpm3->value, "http://", 7) == 0 ||
     1520            strncasecmp( tmpm3->value, "https://", 8 ) == 0 ){
     1521          sprintf(url,"%s/%s_%s.xml",tmpm3->value,service,tmpm2->value);
    15231522        }else
    1524           sprintf(url,"%s/%s/%s_%i.xml",tmpm1->value,tmpm2->value,service,lpid);
     1523          sprintf(url,"%s/%s_%s.xml",tmpm1->value,service,tmpm2->value);
    15251524      }
    15261525    }
     
    15291528    }
    15301529    int lpid;
    1531     tmpm1=getMapFromMaps(m,"lenv","usid");
    1532     lpid=atoi(tmpm1->value);
     1530    map* tmpm2=getMapFromMaps(m,"lenv","usid");
    15331531    tmpm1=getMapFromMaps(m,"main","TmpPath");
    1534     sprintf(stored_path,"%s/%s_%i.xml",tmpm1->value,service,lpid);
     1532    sprintf(stored_path,"%s/%s_%s.xml",tmpm1->value,service,tmpm2->value);
    15351533  }
    15361534
     
    16831681  }
    16841682
    1685   if(hasStoredExecuteResponse==true && status!=SERVICE_STARTED){
    1686     semid lid=getShmLockId(m,1);
     1683  if(hasStoredExecuteResponse==true && status!=SERVICE_STARTED && status!=SERVICE_ACCEPTED){
     1684#ifndef RELY_ON_DB
     1685    semid lid=acquireLock(m);//,1);
    16871686    if(lid<0){
    16881687      /* If the lock failed */
     
    16941693    }
    16951694    else{
    1696 #ifdef DEBUG
    1697       fprintf(stderr,"LOCK %s %d !\n",__FILE__,__LINE__);
    16981695#endif
    1699       lockShm(lid);
    17001696      /* We need to write the ExecuteResponse Document somewhere */
    17011697      FILE* output=fopen(stored_path,"w");
     
    17091705        xmlCleanupParser();
    17101706        zooXmlCleanupNs();
     1707#ifndef RELY_ON_DB
    17111708        unlockShm(lid);
     1709#endif
    17121710        return;
    17131711      }
     
    17181716      xmlFree(xmlbuff);
    17191717      fclose(output);
     1718#ifndef RELY_ON_DB
    17201719#ifdef DEBUG
    17211720      fprintf(stderr,"UNLOCK %s %d !\n",__FILE__,__LINE__);
    17221721#endif
    17231722      unlockShm(lid);
    1724       map* test1=getMap(request,"status");
    1725       if(test1==NULL || strcasecmp(test1->value,"true")!=0){
    1726         removeShmLock(m,1);
    1727       }
    1728     }
     1723      map* v=getMapFromMaps(m,"lenv","sid");
     1724      // Remove the lock when running as a normal task
     1725      if(getpid()==atoi(v->value)){
     1726        removeShmLock (m, 1);
     1727      }
     1728    }
     1729#endif
    17291730  }
    17301731  printDocument(m,doc,pid);
     
    23162317#endif
    23172318    maps* tmpI=request_outputs;
    2318 
     2319    map* usid=getMapFromMaps(m,"lenv","usid");
     2320    int itn=0;
    23192321    while(tmpI!=NULL){
    23202322#ifdef USE_MS
     
    23542356          }
    23552357
    2356           if(gfile==NULL){
     2358          if(gfile==NULL) {
    23572359            map *ext=getMap(tmpI->content,"extension");
    23582360            char *file_path;
     
    23692371            }
    23702372               
    2371             file_name=(char*)malloc((strlen(s->name)+strlen(file_ext)+strlen(tmpI->name)+1024)*sizeof(char));
    2372             int cpid0=cpid+time(NULL);     
    2373             sprintf(file_name,"%s_%s_%i.%s",s->name,tmpI->name,cpid0,file_ext);
     2373            file_name=(char*)malloc((strlen(s->name)+strlen(usid->value)+strlen(file_ext)+strlen(tmpI->name)+45)*sizeof(char));
     2374            sprintf(file_name,"%s_%s_%s_%d.%s",s->name,tmpI->name,usid->value,itn,file_ext);
     2375            itn++;
    23742376            file_path=(char*)malloc((strlen(tmp1->value)+strlen(file_name)+2)*sizeof(char));
    23752377            sprintf(file_path,"%s/%s",tmp1->value,file_name);
    23762378   
    2377                 FILE *ofile=fopen(file_path,"wb");
     2379            FILE *ofile=fopen(file_path,"wb");
    23782380            if(ofile==NULL){
    23792381              char tmpMsg[1024];
  • trunk/zoo-project/zoo-kernel/server_internal.c

    r649 r652  
    2929#include <dlfcn.h>
    3030#endif
     31#include <uuid/uuid.h>
    3132
    3233int getVersionId(const char* version){
     
    3738  }
    3839  return 0;
     40}
     41
     42/**
     43 * Generate a UUID.
     44 * ref: https://www.ietf.org/rfc/rfc4122.txt / 4.2
     45 *
     46 * @return a new char* containing the UUID, make sure to free the returned
     47 *  ressource once used.
     48 */
     49char *get_uuid(){
     50  char *res=(char*)malloc(37*sizeof(char));
     51  uuid_t uuid;
     52  uuid_generate_time(uuid);
     53  char * rest;
     54  uuid_unparse(uuid,rest);
     55  sprintf(res,"%s", rest);
     56  return res;
    3957}
    4058
     
    325343void dumpMapsValuesToFiles(maps** main_conf,maps** in){
    326344  map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
    327   map* tmpSid=getMapFromMaps(*main_conf,"lenv","sid");
     345  map* tmpSid=getMapFromMaps(*main_conf,"lenv","usid");
    328346  maps* inputs=*in;
    329347  int length=0;
  • trunk/zoo-project/zoo-kernel/server_internal.h

    r649 r652  
    3939#include <libxml/parser.h>
    4040#include <libxml/xpath.h>
    41  
     41
     42  char *get_uuid(); 
    4243  char *base64(const char*,int);
    4344  char *base64d(const char*,int,int*);
  • trunk/zoo-project/zoo-kernel/service.c

    r645 r652  
    9393  FILE* file=fopen(file_path,"w");
    9494  maps* tmp=m;
    95   if(tmp!=NULL){
     95  while(tmp!=NULL){
    9696    fprintf(file,"[%s]\n",tmp->name);
    97       dumpMapToFile(tmp->content,file);
    98       fflush(file);
    99     }
    100     fclose(file);
    101   }
     97    dumpMapToFile(tmp->content,file);
     98    fflush(file);
     99    tmp=tmp->next;
     100  }
     101  fclose(file);
     102}
    102103 
    103104/**
  • trunk/zoo-project/zoo-kernel/service_internal.c

    r651 r652  
    3939
    4040#define ERROR_MSG_MAX_LENGTH 1024
    41 
    42 /**
    43  * Get the ongoing status of a running service
     41#ifndef RELY_ON_DB
     42#include <dirent.h>
     43
     44/**
     45 * Read the sid file attached of a service if any
    4446 *
    4547 * @param conf the maps containing the setting of the main.cfg file
    4648 * @param pid the service identifier (usid key from the [lenv] section)
    47  * @return the reported status char* (MESSAGE|POURCENTAGE)
    48  */
    49 char* _getStatus(maps* conf,int pid){
    50   char lid[1024];
    51   sprintf(lid,"%d",pid);
    52   setMapInMaps(conf,"lenv","lid",lid);
    53   semid lockid=getShmLockId(conf,1);
     49 * @return the reported status char* (temporary/final result)
     50 */
     51char* getStatusId(maps* conf,char* pid){
     52  map* r_inputs = getMapFromMaps (conf, "main", "tmpPath");
     53  char* fbkpid =
     54    (char *)
     55    malloc ((strlen (r_inputs->value) + strlen (pid) + 7) * sizeof (char));
     56  sprintf (fbkpid, "%s/%s.sid", r_inputs->value, pid);
     57  FILE* f0 = fopen (fbkpid, "r");
     58  if(f0!=NULL){
     59    fseek (f0, 0, SEEK_END);
     60    long flen = ftell (f0);
     61    fseek (f0, 0, SEEK_SET);
     62    char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
     63    fread(tmps1,flen,1,f0);
     64    tmps1[flen]=0;
     65    fclose(f0);
     66    return tmps1;
     67  }else
     68    return NULL;
     69}
     70
     71/**
     72 * Acquire the global lock
     73 *
     74 * @param conf the maps containing the setting of the main.cfg file
     75 * @return a semid
     76 */
     77semid acquireLock(maps* conf){
     78  semid lockid;
     79  int itn=0;
     80 toRetry1:
     81  lockid=getShmLockId(conf,1);
    5482  if(
    5583#ifdef WIN32
     
    5987#endif
    6088     ){
    61         char* tmp = (char*) malloc(3*sizeof(char));
    62     sprintf(tmp,"%d",ZOO_LOCK_CREATE_FAILED);
    63     return tmp;
     89#ifdef WIN32
     90    return NULL
     91#else
     92    return -1;
     93#endif
    6494  }
    6595  if(lockShm(lockid)<0){
    66     fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
    67     fflush(stderr);   
    68         char* tmp = (char*) malloc(3*sizeof(char));
    69     sprintf(tmp,"%d",ZOO_LOCK_ACQUIRE_FAILED);
    70     return tmp;
    71   }
    72   char *tmp=getStatus(pid);
    73   unlockShm(lockid);
    74   if(tmp==NULL || strncmp(tmp,"-1",2)==0){
     96    if(itn<ZOO_LOCK_MAX_RETRY){
     97      itn++;
     98      goto toRetry1;
     99    }else
     100#ifdef WIN32
     101    return NULL
     102#else
     103    return -1;
     104#endif
     105  }else
     106    return lockid;
     107}
     108
     109/**
     110 * Read the cache file of a running service
     111 *
     112 * @param conf the maps containing the setting of the main.cfg file
     113 * @param pid the service identifier (usid key from the [lenv] section)
     114 * @return the reported status char* (temporary/final result)
     115 */
     116char* _getStatusFile(maps* conf,char* pid){
     117  map* tmpTmap = getMapFromMaps (conf, "main", "tmpPath");
     118
     119  struct dirent *dp;
     120  DIR *dirp = opendir(tmpTmap->value);
     121  char fileName[1024];
     122  int hasFile=-1;
     123  if(dirp!=NULL){
     124    char tmp[128];
     125    sprintf(tmp,"_%s.xml",pid);
     126    while ((dp = readdir(dirp)) != NULL){
     127#ifdef DEBUG
     128      fprintf(stderr,"File : %s searched : %s\n",dp->d_name,tmp);
     129#endif
     130      if(strstr(dp->d_name,"final_")==0 && strstr(dp->d_name,tmp)!=0){
     131        sprintf(fileName,"%s/%s",tmpTmap->value,dp->d_name);
     132        hasFile=1;
     133        break;
     134      }
     135    }
     136  }
     137  if(hasFile>0){
     138    semid lockid;
     139    char* stat=getStatusId(conf,pid);
     140    if(stat!=NULL){
     141      setMapInMaps(conf,"lenv","lid",stat);
     142      lockid=acquireLock(conf);
     143      if(lockid<0)
     144        return NULL;
     145    }
     146
     147    FILE* f0 = fopen (fileName, "r");
     148    if(f0!=NULL){
     149      fseek (f0, 0, SEEK_END);
     150      long flen = ftell (f0);
     151      fseek (f0, 0, SEEK_SET);
     152      char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
     153      fread(tmps1,flen,1,f0);
     154      tmps1[flen]=0;
     155      fclose(f0);
     156      if(stat!=NULL){
     157        unlockShm(lockid);
     158        free(stat);
     159      }
     160
     161      return tmps1;
     162    }
     163    else{
     164      if(stat!=NULL){
     165        unlockShm(lockid);
     166        free(stat);
     167      }
     168      return NULL;
     169    }
     170  }
     171  else
     172    return NULL;
     173}
     174
     175/**
     176 * Get the ongoing status of a running service
     177 *
     178 * @param conf the maps containing the setting of the main.cfg file
     179 * @param pid the service identifier (usid key from the [lenv] section)
     180 * @return the reported status char* (MESSAGE|POURCENTAGE)
     181 */
     182char* _getStatus(maps* conf,char* lid){
     183  map* r_inputs = getMapFromMaps (conf, "main", "tmpPath");
     184  char* fbkpid =
     185    (char *)
     186    malloc ((strlen (r_inputs->value) + strlen (lid) + 9) * sizeof (char));
     187  sprintf (fbkpid, "%s/%s.status", r_inputs->value, lid);
     188  FILE* f0 = fopen (fbkpid, "r");
     189  if(f0!=NULL){
     190    char* stat=getStatusId(conf,lid);
     191    if(stat!=NULL){
     192      setMapInMaps(conf,"lenv","lid",stat);
     193      semid lockid=acquireLock(conf);
     194      if(lockid<0)
     195        return NULL;
     196    }
     197    fseek (f0, 0, SEEK_END);
     198    long flen = ftell (f0);
     199    if(flen>0){
     200      fseek (f0, 0, SEEK_SET);
     201      char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
     202      fread(tmps1,flen,1,f0);
     203      tmps1[flen]=0;
     204      fclose(f0);
     205      free(fbkpid);
     206      if(stat!=NULL){
     207        removeShmLock(conf,1);
     208        free(stat);
     209      }
     210      return tmps1;
     211    }
     212    fclose(f0);
     213    free(fbkpid);
     214    if(stat!=NULL){
     215      removeShmLock(conf,1);
     216      free(stat);
     217    }
     218    return NULL;
     219  }else{
     220    free(fbkpid);
     221    char* stat=getStatusId(conf,lid);
     222    setMapInMaps(conf,"lenv","lid",stat);
    75223    removeShmLock(conf,1);
    76   }
    77   return tmp;
    78 }
     224    return NULL;
     225  }
     226}
     227
     228/**
     229 * Stop handling status repport.
     230 *
     231 * @param conf the map containing the setting of the main.cfg file
     232 */
     233void unhandleStatus(maps *conf){
     234  map* r_inputs = getMapFromMaps (conf, "main", "tmpPath");
     235  map* usid = getMapFromMaps (conf, "lenv", "usid");
     236  char* fbkpid =
     237    (char *) malloc ((strlen (r_inputs->value) + strlen (usid->value) + 9)
     238                     * sizeof (char));
     239  sprintf (fbkpid, "%s/%s.status", r_inputs->value, usid->value);
     240  unlink(fbkpid);
     241  free(fbkpid);
     242}
     243
     244/**
     245 * Update the current status of the running service.
     246 *
     247 * @see acquireLock, lockShm
     248 * @param conf the map containing the setting of the main.cfg file
     249 * @return 0 on success, -2 if shmget failed, -1 if shmat failed
     250 */
     251int _updateStatus(maps *conf){
     252  map* r_inputs = getMapFromMaps (conf, "main", "tmpPath");
     253  map* sid = getMapFromMaps (conf, "lenv", "usid");
     254  char* fbkpid =
     255    (char *)
     256    malloc ((strlen (r_inputs->value) + strlen (sid->value) + 9) * sizeof (char));
     257  sprintf (fbkpid, "%s/%s.status", r_inputs->value, sid->value);
     258  map* status=getMapFromMaps(conf,"lenv","status");
     259  map* msg=getMapFromMaps(conf,"lenv","message");
     260  if(status!=NULL && msg!=NULL &&
     261     status->value!=NULL && msg->value!=NULL &&
     262     strlen(status->value)>0 && strlen(msg->value)>1){
     263    semid lockid;
     264    char* stat=getStatusId(conf,sid->value);
     265    if(stat!=NULL){
     266      lockid=acquireLock(conf);
     267      if(lockid<0)
     268        return ZOO_LOCK_ACQUIRE_FAILED;
     269    }
     270    FILE* fstatus=fopen(fbkpid,"w");
     271    if(fstatus!=NULL){
     272      fprintf(fstatus,"%s|%s\n",status->value,msg->value);
     273      fflush(fstatus);
     274      fclose(fstatus);
     275    }
     276    if(stat!=NULL){
     277      unlockShm(lockid);
     278      free(stat);
     279    }
     280  }
     281  return 0;
     282}
     283
     284#endif
    79285
    80286#ifdef WIN32
     
    96302  map *tmpMap=getMapFromMaps(conf,"lenv","lid");
    97303  if(tmpMap==NULL)
    98         tmpMap=getMapFromMaps(conf,"lenv","usid");
     304        tmpMap=getMapFromMaps(conf,"lenv","osid");
    99305
    100306  if(tmpMap!=NULL){
     
    130336  semid sem_id=getShmLockId(conf,1);
    131337  if (CloseHandle(sem_id) == 0) {
     338#ifdef DEBUG
    132339    fprintf(stderr,"Unable to remove semaphore: %s\n", getLastErrorMessage());
     340#endif
    133341    return -1;
    134342  }
     
    165373static HANDLE hMapObjectG = NULL;  // handle to file mapping
    166374
    167 int _updateStatus(maps *conf){
    168   LPWSTR lpszTmp;
    169   BOOL fInit;
    170   char *final_string=NULL;
    171   char *s=NULL;
    172   map *tmpMap1;
    173   map *tmpMap=getMapFromMaps(conf,"lenv","usid");
    174   semid lockid=getShmLockId(conf,1);
    175   if(lockid==NULL){
    176 #ifdef DEBUG
    177     fprintf(stderr,"Unable to create semaphore on line %d!! \n",__LINE__);
    178 #endif
    179     return ZOO_LOCK_CREATE_FAILED;
    180   }
    181   if(lockShm(lockid)<0){
    182 #ifdef DEBUG
    183     fprintf(stderr,"Unable to create semaphore on line %d!! \n",__LINE__);
    184 #endif
    185     return ZOO_LOCK_ACQUIRE_FAILED;
    186   }
    187  
    188   if(hMapObjectG==NULL)
    189     hMapObjectG = CreateFileMapping(
    190                                     INVALID_HANDLE_VALUE,   // use paging file
    191                                     NULL,                   // default security attributes
    192                                     PAGE_READWRITE,         // read/write access
    193                                     0,                      // size: high 32-bits
    194                                     SHMEMSIZE,              // size: low 32-bits
    195                                     TEXT(tmpMap->value));   // name of map object
    196   if (hMapObjectG == NULL){
    197 #ifdef DEBUG
    198     fprintf(stderr,"Unable to create shared memory segment: %s\n", getLastErrorMessage());
    199 #endif
    200     return -2;
    201   }
    202   fInit = (GetLastError() != ERROR_ALREADY_EXISTS);
    203   if(lpvMemG==NULL)
    204     lpvMemG = MapViewOfFile(
    205                             hMapObjectG,     // object to map view of
    206                             FILE_MAP_WRITE, // read/write access
    207                             0,              // high offset:  map from
    208                             0,              // low offset:   beginning
    209                             0);             // default: map entire file
    210   if (lpvMemG == NULL){
    211 #ifdef DEBUG
    212     fprintf(stderr,"Unable to create or access the shared memory segment %s !! \n",tmpMap->value);
    213 #endif
    214     return -1;
    215   }
    216   memset(lpvMemG, '\0', SHMEMSIZE);
    217   tmpMap=getMapFromMaps(conf,"lenv","status");
    218   tmpMap1=NULL;
    219   tmpMap1=getMapFromMaps(conf,"lenv","message");
    220   lpszTmp = (LPWSTR) lpvMemG;
    221   final_string=(char*)malloc((strlen(tmpMap1->value)+strlen(tmpMap->value)+2)*sizeof(char));
    222   sprintf(final_string,"%s|%s",tmpMap->value,tmpMap1->value);
    223   for(s=final_string;*s!='\0';*s++){
    224     *lpszTmp++ = *s;
    225   }
    226   *lpszTmp++ = '\0';
    227   free(final_string);
    228   unlockShm(lockid);
    229   return 0;
    230 }
    231375
    232376char* getStatus(int pid){
     
    287431}
    288432
    289 void unhandleStatus(maps *conf){
    290   BOOL fIgnore;
    291   fIgnore = UnmapViewOfFile(lpvMemG);
    292   fIgnore = CloseHandle(hMapObjectG);
    293 }
    294 
    295433#else
    296434/**
     
    312450
    313451/**
    314  * Set in the pre-allocated key the zoo_sem_[SID] string
    315  * where [SID] is the lid (if any) or usid value from the [lenv] section.
     452 * Set in the pre-allocated key the zoo_sem_[OSID] string
     453 * where [OSID] is the lid (if any) or osid value from the [lenv] section.
    316454 *
    317455 * @param conf the map containing the setting of the main.cfg file
     
    322460  map *tmpMap=getMapFromMaps(conf,"lenv","lid");
    323461  if(tmpMap==NULL)
    324     tmpMap=getMapFromMaps(conf,"lenv","usid");
     462    tmpMap=getMapFromMaps(conf,"lenv","osid");
    325463  int key=-1;
    326464  if(tmpMap!=NULL)
     
    380518              fprintf(stderr,"Retry to access the semaphore later ...\n");
    381519#endif
    382               sleep(1);
     520              zSleep(1);
    383521            }
    384522        }
     
    411549  int sem_id=getShmLockId(conf,nsems);
    412550  if (semctl(sem_id, 0, IPC_RMID, arg) == -1) {
    413     perror("semctl");
     551#ifdef DEBUG
     552    perror("semctl remove");
     553#endif
    414554    return -1;
    415555  }
     556#ifdef DEBUG
     557  fprintf(stderr,"Semaphore removed!\n");
     558#endif
    416559  return 0;
    417560}
     
    429572  sb.sem_flg = SEM_UNDO;
    430573  if (semop(id, &sb, 1) == -1){
    431     perror("semop");
     574#ifdef DEBUG
     575    perror("semop lock");
     576#endif
    432577    return -1;
    433578  }
     
    447592  sb.sem_flg = SEM_UNDO;
    448593  if (semop(id, &sb, 1) == -1) {
    449     perror("semop");
     594#ifdef DEBUG
     595    perror("semop unlock");
     596#endif
    450597    return -1;
    451598  }
     
    454601
    455602/**
    456  * Stop handling status repport.
    457  *
    458  * @param conf the map containing the setting of the main.cfg file
    459  */
    460 void unhandleStatus(maps *conf){
    461   int shmid;
    462   key_t key;
    463   void *shm;
    464   struct shmid_ds shmids;
    465   map *tmpMap=getMapFromMaps(conf,"lenv","usid");
    466   if(tmpMap!=NULL){
    467     key=atoi(tmpMap->value);
    468     if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
    469 #ifdef DEBUG
    470       fprintf(stderr,"shmget failed to update value\n");
    471 #endif
    472     }else{
    473       if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
    474 #ifdef DEBUG
    475         fprintf(stderr,"shmat failed to update value\n");
    476 #endif
    477       }else{
    478         shmdt(shm);
    479         shmctl(shmid,IPC_RMID,&shmids);
    480       }
    481     }
    482   }
    483 }
    484 
    485 /**
    486  * Update the current of the running service.
    487  *
    488  * @see getKeyValue, getShmLockId, lockShm
    489  * @param conf the map containing the setting of the main.cfg file
    490  * @return 0 on success, -2 if shmget failed, -1 if shmat failed
    491  */
    492 int _updateStatus(maps *conf){
    493   int shmid;
    494   char *shm,*s,*s1;
    495   map *tmpMap=NULL;
    496   key_t key=getKeyValue(conf);
    497   if(key!=-1){
    498     semid lockid=getShmLockId(conf,1);
    499     if(lockid<0)
    500       return ZOO_LOCK_CREATE_FAILED;
    501     if(lockShm(lockid)<0){
    502       return ZOO_LOCK_ACQUIRE_FAILED;
    503     }
    504     if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
    505 #ifdef DEBUG
    506       fprintf(stderr,"shmget failed to create new Shared memory segment\n");
    507 #endif
    508       unlockShm(lockid);
    509       return -2;
    510     }else{
    511       if ((shm = (char*) shmat(shmid, NULL, 0)) == (char *) -1) {
    512 #ifdef DEBUG
    513         fprintf(stderr,"shmat failed to update value\n");
    514 #endif
    515         unlockShm(lockid);
    516         return -1;
    517       }
    518       else{
    519         tmpMap=getMapFromMaps(conf,"lenv","status");
    520         s1=shm;
    521         for(s=tmpMap->value;*s!=NULL && *s!=0;s++){
    522           *s1++=*s;
    523         }
    524         *s1++='|';
    525         tmpMap=getMapFromMaps(conf,"lenv","message");
    526         if(tmpMap!=NULL)
    527           for(s=tmpMap->value;*s!=NULL && *s!=0;s++){
    528             *s1++=*s;
    529         }
    530         *s1=NULL;
    531         shmdt((void *)shm);
    532         unlockShm(lockid);
    533       }
    534     }
    535   }
    536   return 0;
    537 }
    538 
    539 /**
    540  * Update the current of the running service.
     603 * Get the current status of the running service.
    541604 *
    542605 * @see getKeyValue, getShmLockId, lockShm
  • trunk/zoo-project/zoo-kernel/service_internal.h

    r649 r652  
    6060 */
    6161#define ZOO_LOCK_RELEASE_FAILED -6
     62/**
     63 * Number of time the ZOO-Kernel will try to acquire lock
     64 */
     65#define ZOO_LOCK_MAX_RETRY 10
    6266
    6367#include <sys/stat.h>
     
    100104  void unhandleStatus(maps*);
    101105  int _updateStatus(maps*);
    102   char* _getStatus(maps*,int);
     106  char* _getStatus(maps*,char*);
     107  char* _getStatusFile(maps*,char*);
    103108  char* getStatus(int);
     109
    104110  int updateStatus( maps*,const int,const char*);
    105111  int removeShmLock(maps*, int);
     
    112118#define semid HANDLE
    113119#endif
     120  semid acquireLock(maps*);
    114121  semid getShmLockId(maps*,int);
    115122  int lockShm(semid);
    116123  int unlockShm(semid);
    117124
    118 #ifdef USE_JS
    119 #endif
    120  
    121125#ifdef __cplusplus
    122126}
  • trunk/zoo-project/zoo-kernel/service_internal_saga.c

    r640 r652  
    102102
    103103maps* SagaWatcher::m_conf;
    104 char* SagaWatcher::message=zStrdup("");
     104char* SagaWatcher::message=zStrdup("No message left");
    105105int SagaWatcher::status=1;
    106106
     
    121121    {
    122122    default:
    123       res = 0;
     123      return 0;
    124124      break;
    125125
    126     case CALLBACK_PROCESS_GET_OKAY:
     126    case CALLBACK_DLG_ERROR:
     127      return 1;
    127128      break;
    128129
     130    case CALLBACK_DLG_PARAMETERS:
    129131    case CALLBACK_PROCESS_SET_OKAY:
     132    case CALLBACK_DATAOBJECT_COLORS_GET:
     133    case CALLBACK_DATAOBJECT_COLORS_SET:
     134    case CALLBACK_DATAOBJECT_PARAMS_GET:
     135    case CALLBACK_DATAOBJECT_PARAMS_SET:
     136    case CALLBACK_DATAOBJECT_UPDATE:
     137    case CALLBACK_DATAOBJECT_SHOW:
     138    case CALLBACK_DLG_CONTINUE:
     139    case CALLBACK_PROCESS_SET_READY:
    130140      break;
    131141
     
    139149      break;
    140150
    141     case CALLBACK_PROCESS_SET_READY:
     151    case CALLBACK_PROCESS_GET_OKAY:
    142152      status=1;
    143153      break;
     
    161171    case CALLBACK_DLG_MESSAGE:
    162172      SetMessage((param2.String + ": " + param1.String).b_str());
    163       break;
    164 
    165     case CALLBACK_DLG_CONTINUE:
    166       break;
    167 
    168     case CALLBACK_DLG_ERROR:
    169173      break;
    170174
     
    174178      else
    175179        res = 0;
    176       break;
    177 
    178     case CALLBACK_DATAOBJECT_UPDATE:
    179       break;
    180 
    181     case CALLBACK_DATAOBJECT_SHOW:
    182       break;
    183 
    184     case CALLBACK_DATAOBJECT_COLORS_GET:
    185       break;
    186 
    187     case CALLBACK_DATAOBJECT_COLORS_SET:
    188       break;
    189 
    190     case CALLBACK_DATAOBJECT_PARAMS_GET:
    191       break;
    192 
    193     case CALLBACK_DATAOBJECT_PARAMS_SET:
    194       break;
    195 
    196     case CALLBACK_DLG_PARAMETERS:
    197180      break;
    198181
  • trunk/zoo-project/zoo-kernel/sqlapi.c

    r651 r652  
    11/*
    22 * Author : David Saggiorato
    3  *
    4  *  Copyright 2008-2009 GeoLabs SARL. All rights reserved.
     3 *          Gérald Fenoy
     4 *  Copyright 2015 GeoLabs SARL. All rights reserved.
    55 *
    66 * Permission is hereby granted, free of charge, to any person obtaining a copy
     
    2323 */
    2424
     25#ifdef RELY_ON_DB
     26#include "ogr_api.h"
     27#include "ogrsf_frmts.h"
     28#include "ogr_p.h"
     29
    2530#include "sqlapi.h"
    2631#include <fcgi_stdio.h>
    27 #include <uuid/uuid.h>
    2832#include <stdlib.h>
    2933
    3034/**
    31  * Generate a UUID.
    32  * ref: https://www.ietf.org/rfc/rfc4122.txt / 4.2
    33  *
    34  * @return a new char* containing the UUID, make sure to free the returned ressource
    35  * once used.
    36  */
    37 char *get_uuid(){
    38   char *res=(char*)malloc(37*sizeof(char));
    39   uuid_t uuid;
    40   uuid_generate_time(uuid);
    41   char * rest;
    42   uuid_unparse(uuid,rest);
    43   sprintf(res,"%s", rest);
     35 * Global GDALDataset pointer
     36 */
     37OGRDataSource *zoo_DS = NULL;
     38
     39/**
     40 * Global OGRLayer pointer pointing to the lastest result set
     41 */
     42OGRLayer *zoo_ResultSet = NULL;
     43
     44/**
     45 * Create a GDAL / OGR string for connecting to the db backend
     46 * (do not support active_schema)
     47 *
     48 * @param conf the maps containing the setting of the main.cfg file
     49 */
     50char* createInitString(maps* conf){
     51  char* res=NULL;
     52  char keywords[5][9]={
     53    "dbname",
     54    "host",
     55    "port",
     56    "user",
     57    "password"
     58  };
     59  int i=0;
     60  maps* cconf=getMaps(conf,"database");
     61  int len=0;
     62  for(i=0;i<5;i++){
     63    map* tmp=getMap(cconf->content,keywords[i]);
     64    if(tmp!=NULL){
     65      if(res==NULL){
     66        res=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+4)*sizeof(char));
     67        sprintf(res,"%s='%s'",keywords[i],tmp->value);
     68        len+=strlen(res);
     69      }else{
     70        char* res1=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
     71        sprintf(res1," %s='%s'",keywords[i],tmp->value);
     72        res=(char*)realloc(res,(len+strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
     73        memcpy(res+len,res1,(strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
     74        len+=strlen(res1);
     75        res[len]=0;
     76        free(res1);
     77      }
     78    }
     79  }
     80  map* tmp=getMap(cconf->content,"type");
     81  if(tmp!=NULL){
     82    char* fres=(char*)malloc((strlen(res)+strlen(tmp->value)+2)*sizeof(char));
     83    sprintf(fres,"%s:%s",tmp->value,res);
     84    free(res);
     85    return fres;
     86  }
    4487  return res;
    4588}
     89
     90/**
     91 * Connect to the db backend.
     92 *
     93 * @param conf the maps containing the setting of the main.cfg file
     94 * @see createInitString
     95 */
     96void init_sql(maps* conf){
     97  char* sqlInitString=createInitString(conf);
     98  OGRSFDriver *poDriver = NULL;
     99  OGRRegisterAll();
     100  zoo_DS = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);
     101  if( zoo_DS == NULL ){
     102#ifdef DEBUG
     103    fprintf(stderr,"sqlInitString: %s FAILED !\n",sqlInitString);
     104    fflush(stderr);
     105#endif
     106    free(sqlInitString);
     107    setMapInMaps(conf,"lenv","message","Failed to connect to the database backend");
     108    return;
     109  }
     110#ifdef DEBUG
     111  fprintf(stderr,"sqlInitString: %s SUCEED !\n",sqlInitString);
     112  fflush(stderr);
     113#endif
     114  free(sqlInitString);
     115}
     116
     117/**
     118 * Close any connection to the db backend.
     119 *
     120 * @param conf the maps containing the setting of the main.cfg file
     121 */
     122void close_sql(maps* conf){
     123  if( zoo_ResultSet != NULL )
     124    zoo_DS->ReleaseResultSet( zoo_ResultSet );
     125  if(zoo_DS!=NULL){
     126    OGRDataSource::DestroyDataSource( zoo_DS );
     127    zoo_DS=NULL;
     128  }
     129}
     130
     131/**
     132 * Call OGRCleanupAll.
     133 *
     134 */
     135void end_sql(){
     136  OGRCleanupAll();
     137}
     138
     139/**
     140 * Execute a SQL query to the SQL Database Backend.
     141 *
     142 * @param conf the maps containing the setting of the main.cfg file
     143 * @param sqlQuery the SQL query to run
     144 * @return -1 in case of failure and 1 if the query succeed.
     145 */
     146int execSql(maps* conf,const char* sqlQuery){
     147  zoo_ResultSet = zoo_DS->ExecuteSQL( sqlQuery, NULL, NULL);
     148  if( zoo_ResultSet != NULL ){
     149    return 1;
     150  }
     151  return -1;
     152}
     153
     154/**
     155 * Clean any memory allocated by executing a request
     156 *
     157 * @param conf the maps containing the setting of the main.cfg file
     158 * @param sqlQuery the SQL query to run
     159 * @return -1 in case of failure and 1 if the query succeed.
     160 */
     161void cleanUpResultSet(const maps* conf){
     162  if( zoo_ResultSet != NULL ){
     163    zoo_DS->ReleaseResultSet( zoo_ResultSet );
     164    zoo_ResultSet=NULL;
     165  }
     166}
     167
     168/**
     169 * Record a file stored during ZOO-Kernel execution
     170 *
     171 * @param conf the maps containing the setting of the main.cfg file
     172 * @param filename the file's name
     173 * @param type the type (Intput,Output,Response)
     174 * @param name the maps containing the setting of the main.cfg file
     175 */
     176void recordStoredFile(maps* conf,const char* filename,const char* type,const char* name){
     177  map *uusid=getMapFromMaps(conf,"lenv","usid");
     178  map *schema=getMapFromMaps(conf,"database","schema");
     179  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(uusid->value)+strlen(filename)+strlen(type)+(name!=NULL?strlen(name):2)+68+1)*sizeof(char));
     180  if(name!=NULL)
     181    sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s','%s');",schema->value,uusid->value,filename,type,name);
     182  else
     183    sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s',NULL);",schema->value,uusid->value,filename,type);
     184  execSql(conf,sqlQuery);
     185  cleanUpResultSet(conf);
     186}
     187
     188/**
     189 * Insert the reference tuple corresponding to the running service
     190 *
     191 * @param conf the maps containing the setting of the main.cfg file
     192 */
     193void recordServiceStatus(maps* conf){
     194  map *sid=getMapFromMaps(conf,"lenv","sid");
     195  map *osid=getMapFromMaps(conf,"lenv","osid");
     196  map *uusid=getMapFromMaps(conf,"lenv","usid");
     197  map *schema=getMapFromMaps(conf,"database","schema");
     198  char *sqlQuery=(char*)malloc((strlen(schema->value)+
     199                                strlen(uusid->value)+
     200                                strlen(osid->value)+
     201                                strlen(sid->value)+56+1)*sizeof(char));
     202  sprintf(sqlQuery,
     203          "INSERT INTO %s.services (uuid,sid,osid)"
     204          "VALUES ('%s','%s','%s');",
     205          schema->value,
     206          uusid->value,sid->value,osid->value);
     207  execSql(conf,sqlQuery);
     208  cleanUpResultSet(conf);
     209}
     210
     211/**
     212 * Store the content of the result file
     213 *
     214 * @param conf the maps containing the setting of the main.cfg file
     215 * @param filename the file's name
     216 */
     217void recordResponse(maps* conf,char* filename){
     218  FILE *file = fopen (filename, "rb");
     219  fseek (file, 0, SEEK_END);
     220  long flen = ftell (file);
     221  fseek (file, 0, SEEK_SET);
     222  char *tmps = (char *) malloc ((flen + 1) * sizeof (char));
     223  fread (tmps, flen, 1, file);
     224  tmps[flen]=0;
     225  fclose(file);
     226  map *sid=getMapFromMaps(conf,"lenv","usid");
     227  map *schema=getMapFromMaps(conf,"database","schema");
     228  char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+51+1)*sizeof(char));
     229  sprintf(sqlQuery,"UPDATE %s.services set response=$$%s$$ where uuid=$$%s$$;",schema->value,tmps,sid->value);
     230  execSql(conf,sqlQuery);
     231  cleanUpResultSet(conf);
     232}
     233
     234/**
     235 * Update the current status of the running service.
     236 *
     237 * @param conf the map containing the setting of the main.cfg file
     238 * @return 0 on success, -2 if shmget failed, -1 if shmat failed
     239 */
     240int _updateStatus(maps* conf){
     241  map *sid=getMapFromMaps(conf,"lenv","usid");
     242  map *p=getMapFromMaps(conf,"lenv","status");
     243  map *msg=getMapFromMaps(conf,"lenv","message");
     244  map *schema=getMapFromMaps(conf,"database","schema");
     245  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(msg->value)+strlen(p->value)+strlen(sid->value)+64+1)*sizeof(char));
     246  sprintf(sqlQuery,"UPDATE %s.services set status=$$%s$$,message=$$%s$$ where uuid=$$%s$$;",schema->value,p->value,msg->value,sid->value);
     247  fflush(stderr);
     248  if( zoo_DS == NULL )
     249    init_sql(conf);
     250  execSql(conf,sqlQuery);
     251  cleanUpResultSet(conf);
     252  return 0;
     253}
     254
     255/**
     256 * Get the ongoing status of a running service
     257 *
     258 * @param conf the maps containing the setting of the main.cfg file
     259 * @param pid the service identifier (usid key from the [lenv] section)
     260 * @return the reported status char* (MESSAGE|POURCENTAGE)
     261 */
     262char* _getStatus(maps* conf,char* pid){
     263  map *schema=getMapFromMaps(conf,"database","schema");
     264  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
     265  sprintf(sqlQuery,"select status||'|'||message from %s.services where uuid=$$%s$$;",schema->value,pid);
     266  if( zoo_DS == NULL )
     267    init_sql(conf);
     268  execSql(conf,sqlQuery);
     269  OGRFeature  *poFeature = NULL;
     270  const char *tmp1;
     271  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
     272    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
     273      if( poFeature->IsFieldSet( iField ) ){
     274        tmp1=strdup(poFeature->GetFieldAsString( iField ));
     275      }
     276      else
     277        tmp1=NULL;
     278    }
     279    OGRFeature::DestroyFeature( poFeature );
     280  }
     281  cleanUpResultSet(conf);
     282  return (char*)tmp1;
     283}
     284
     285/**
     286 * Read the cache file of a running service
     287 *
     288 * @param conf the maps containing the setting of the main.cfg file
     289 * @param pid the service identifier (usid key from the [lenv] section)
     290 * @return the reported status char* (temporary/final result)
     291 */
     292char* _getStatusFile(maps* conf,char* pid){
     293  map *schema=getMapFromMaps(conf,"database","schema");
     294  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
     295  sprintf(sqlQuery,"select response from %s.services where uuid=$$%s$$;",schema->value,pid);
     296  if( zoo_DS == NULL )
     297    init_sql(conf);
     298  execSql(conf,sqlQuery);
     299  OGRFeature  *poFeature = NULL;
     300  const char *tmp1;
     301  int hasRes=-1;
     302  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
     303    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
     304      if( poFeature->IsFieldSet( iField ) ){
     305        tmp1=strdup(poFeature->GetFieldAsString( iField ));
     306        hasRes=1;
     307      }
     308      else
     309        tmp1=NULL;
     310    }
     311    OGRFeature::DestroyFeature( poFeature );
     312  }
     313  if(hasRes<0)
     314    tmp1=NULL;
     315  cleanUpResultSet(conf);
     316  return (char*)tmp1;
     317}
     318
     319/**
     320 * Stop handling status repport.
     321 *
     322 * @param conf the map containing the setting of the main.cfg file
     323 */
     324void unhandleStatus(maps* conf){
     325  map *sid=getMapFromMaps(conf,"lenv","usid");
     326  char *sqlQuery=(char*)malloc((strlen(sid->value)+52+1)*sizeof(char));
     327  sprintf(sqlQuery,"UPDATE services set end_time=now() where uuid=$$%s$$;",sid->value);
     328  execSql(conf,sqlQuery);
     329  cleanUpResultSet(conf);
     330  close_sql(conf);
     331  end_sql();
     332}
     333
     334#endif
  • trunk/zoo-project/zoo-kernel/sqlapi.h

    r644 r652  
    3030#endif
    3131
    32 char *get_uuid();
    33 void init_sql(const char* host,const char *user, const char *passwd, const char * bdd,int port);
    34 int add_job(const char * uuid);
    35 int start_job(const char *uuid);
     32#ifdef RELY_ON_DB
     33#include "service.h"
     34#include "service_internal.h"
     35  void init_sql(maps*);
     36  void close_sql(maps*);
     37  int execSql(maps*,const char*);
     38  void recordStoredFile(maps*,const char*,const char*,const char*);
     39  void recordServiceStatus(maps*);
     40  void recordResponse(maps*,char*,bool);
     41#endif
    3642
    3743#ifdef __cplusplus
  • trunk/zoo-project/zoo-kernel/zoo_service_loader.c

    r640 r652  
    16691669  }
    16701670
     1671
     1672  // Define each env variable in runing environment
    16711673  maps *curs = getMaps (m, "env");
    16721674  if (curs != NULL)
     
    17281730#endif
    17291731
    1730   /**
    1731    * Need to check if we need to fork to load a status enabled
    1732    */
     1732  // Need to check if we need to fork to load a status enabled
    17331733  r_inputs = NULL;
    17341734  map *store = getMap (request_inputs, "storeExecuteResponse");
     
    17871787  _tmpMaps->name = zStrdup ("lenv");
    17881788  char tmpBuff[100];
    1789   semid lid = getShmLockId (NULL, 1);
    1790   lockShm (lid);
    17911789  struct ztimeval tp;
    17921790  if (zGettimeofday (&tp, NULL) == 0)
     
    17941792  else
    17951793    sprintf (tmpBuff, "%i", (cpid + (int) time (NULL)));
    1796   unlockShm (lid);
    1797   removeShmLock (NULL, 1);
    1798   _tmpMaps->content = createMap ("usid", tmpBuff);
     1794  _tmpMaps->content = createMap ("osid", tmpBuff);
    17991795  _tmpMaps->next = NULL;
    18001796  sprintf (tmpBuff, "%i", cpid);
     
    18021798  char* tmpUuid=get_uuid();
    18031799  addToMap (_tmpMaps->content, "uusid", tmpUuid);
     1800  addToMap (_tmpMaps->content, "usid", tmpUuid);
    18041801  free(tmpUuid);
    18051802  addToMap (_tmpMaps->content, "status", "0");
     
    19101907    }
    19111908#endif
    1912   char *fbkp, *fbkp1;
     1909  char *fbkp, *fbkpid, *fbkp1, *flog;
    19131910  FILE *f0, *f1;
    19141911  if (status != NULL)
     
    19771974           * process answer to http client.
    19781975           */
    1979 #ifndef WIN32
    1980           zSleep (1);
    1981 #endif
    1982           r_inputs = getMapFromMaps (m, "lenv", "usid");
     1976          map* usid = getMapFromMaps (m, "lenv", "uusid");
     1977          r_inputs = getMapFromMaps (m, "lenv", "osid");
    19831978          int cpid = atoi (r_inputs->value);
    19841979          r_inputs = getMapFromMaps (m, "main", "tmpPath");
    1985           //map *r_inputs1 = getMap (s1->content, "ServiceProvider");
    1986                   map* r_inputs1 = createMap("ServiceName", s1->name);
    1987 
     1980          map* r_inputs1 = createMap("ServiceName", s1->name);
     1981
     1982          // Create PID file referencing the OS process identifier
     1983          fbkpid =
     1984            (char *)
     1985            malloc ((strlen (r_inputs->value) +
     1986                     strlen (usid->value) + 7) * sizeof (char));
     1987          sprintf (fbkpid, "%s/%s.pid", r_inputs->value, usid->value);
     1988
     1989          f0 = freopen (fbkpid, "w+", stdout);
     1990          fprintf(stdout,"%d",getpid());
     1991          fflush(stdout);
     1992
     1993          // Create SID file referencing the semaphore name
    19881994          fbkp =
    19891995            (char *)
    19901996            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
    1991                      1024) * sizeof (char));
    1992           sprintf (fbkp, "%s/%s_%d.xml", r_inputs->value, r_inputs1->value,
    1993                    cpid);
    1994           char *flog =
     1997                     strlen (usid->value) + 7) * sizeof (char));
     1998          sprintf (fbkp, "%s/%s.sid", r_inputs->value, usid->value);
     1999
     2000          FILE* f2 = fopen (fbkp, "w+");
     2001          map* tmpm=getMapFromMaps (m, "lenv", "osid");
     2002          fprintf(f2,"%s",tmpm->value);
     2003          fflush(f2);
     2004          fclose(f2);
     2005          free(fbkp);
     2006
     2007          fbkp =
    19952008            (char *)
    19962009            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
    1997                      1024) * sizeof (char));
    1998           sprintf (flog, "%s/%s_%d_error.log", r_inputs->value,
    1999                    r_inputs1->value, cpid);
     2010                     strlen (usid->value) + 7) * sizeof (char));
     2011          sprintf (fbkp, "%s/%s_%s.xml", r_inputs->value, r_inputs1->value,
     2012                   usid->value);
     2013          flog =
     2014            (char *)
     2015            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
     2016                     strlen (usid->value) + 13) * sizeof (char));
     2017          sprintf (flog, "%s/%s_%s_error.log", r_inputs->value,
     2018                   r_inputs1->value, usid->value);
    20002019#ifdef DEBUG
    20012020          fprintf (stderr, "RUN IN BACKGROUND MODE \n");
     
    20052024#endif
    20062025          freopen (flog, "w+", stderr);
    2007           semid lid = getShmLockId (m, 1);
    20082026          fflush (stderr);
    2009           if (lid < 0)
    2010             {
    2011               return errorException (m, _("Lock failed"),
    2012                               "InternalError", NULL);
    2013             }
    2014           else
    2015             {
    2016               if (lockShm (lid) < 0)
    2017                 {
    2018                   return errorException (m, _("Lock failed"),
    2019                                          "InternalError", NULL);
    2020                 }
    2021             }
    20222027          f0 = freopen (fbkp, "w+", stdout);
    20232028          rewind (stdout);
     
    20362041                                request_output_real_format);
    20372042          fflush (stdout);
    2038           unlockShm (lid);
     2043#ifdef RELY_ON_DB
     2044          init_sql(m);
     2045          recordServiceStatus(m);
     2046          recordResponse(m,fbkp);
     2047#endif
    20392048          fflush (stderr);
    20402049          fbkp1 =
    20412050            (char *)
    20422051            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
    2043                      1024) * sizeof (char));
    2044           sprintf (fbkp1, "%s/%s_final_%d.xml", r_inputs->value,
    2045                    r_inputs1->value, cpid);
     2052                     strlen (usid->value) + 13) * sizeof (char));
     2053          sprintf (fbkp1, "%s/%s_final_%s.xml", r_inputs->value,
     2054                   r_inputs1->value, usid->value);
    20462055
    20472056          f1 = freopen (fbkp1, "w+", stdout);
    2048           free (flog);
    20492057
    20502058          if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
     
    20622070            free (tmpmaps);
    20632071            fflush (stdout);
    2064             unlockShm (lid);
    20652072            fflush (stderr);
     2073            unhandleStatus (m);
    20662074            return -1;
    20672075          }
     
    20732081      else
    20742082        {
    2075       /**
    2076        * error server don't accept the process need to output a valid
    2077        * error response here !!!
    2078        */
     2083          /**
     2084           * error server don't accept the process need to output a valid
     2085           * error response here !!!
     2086           */
    20792087          eres = -1;
    20802088          errorException (m, _("Unable to run the child process properly"),
     
    21092117      fclose (stdout);
    21102118      fclose (stderr);
    2111     /**
    2112      * Dump back the final file fbkp1 to fbkp
    2113      */
     2119      /**
     2120       * Dump back the final file fbkp1 to fbkp
     2121       */
    21142122      fclose (f0);
    21152123      fclose (f1);
    21162124      FILE *f2 = fopen (fbkp1, "rb");
     2125#ifndef RELY_ON_DB
    21172126      semid lid = getShmLockId (m, 1);
    21182127      if (lid < 0)
    21192128        return -1;
    21202129      lockShm (lid);
     2130#endif
    21212131      FILE *f3 = fopen (fbkp, "wb+");
    21222132      free (fbkp);
     
    21292139      fclose (f2);
    21302140      fclose (f3);
    2131       unlockShm (lid);
     2141      unlink (fbkpid);
     2142#ifndef RELY_ON_DB
     2143      removeShmLock (m, 1);
     2144#else
     2145      recordResponse(m,fbkp1);
     2146#endif
    21322147      unlink (fbkp1);
     2148      unlink (flog);
     2149      unhandleStatus (m);
     2150      free(fbkpid);
     2151      free (flog);
    21332152      free (fbkp1);
    21342153      free (tmps1);
    2135       unhandleStatus (m);
    21362154    }
    21372155
  • trunk/zoo-project/zoo-services/utils/status/service.c

    r643 r652  
    6565      tmpMmap=tmpTmap;
    6666    xmlInitParser();
    67     struct dirent *dp;
    68     DIR *dirp = opendir(tmpTmap->value);
    69     char fileName[1024],xslFileName[1024];
    7067    int hasFile=-1;
    71     if(dirp!=NULL){
    72       char tmp[128];
    73       sprintf(tmp,"_%s.xml",tmpMap->value);
    74       while ((dp = readdir(dirp)) != NULL){
    75 #ifdef DEBUG
    76         fprintf(stderr,"File : %s searched : %s\n",dp->d_name,tmp);
    77 #endif
    78         if(strstr(dp->d_name,"final_")==0 && strstr(dp->d_name,tmp)!=0){
    79           sprintf(fileName,"%s/%s",tmpTmap->value,dp->d_name);
    80           hasFile=1;
    81           break;
    82         }
    83       }
    84     }else{
    85       char tmp[1024];
    86       snprintf(tmp,1024,_ss("GetStatus was unable to use the tmpPath value set in main.cfg file as directory %s."),tmpTmap->value);
    87       setMapInMaps(conf,"lenv","message",tmp);
    88       return SERVICE_FAILED;
    89     }
    90     if(hasFile<0){
     68    char xslFileName[1024];
     69    char* mem=_getStatusFile(conf,tmpMap->value);
     70    if(mem==NULL){
    9171      char tmp[1024];
    9272      snprintf(tmp,1024,_ss("GetStatus was unable to find any cache file for Service ID %s."),tmpMap->value);
     
    10080    xmlDocPtr doc, res;
    10181    cur = xsltParseStylesheetFile(BAD_CAST xslFileName);
    102     doc = xmlParseFile(fileName);
     82    doc = xmlParseMemory(mem,strlen(mem));
     83    //doc = xmlParseFile(fileName);
    10384    if(cur!=NULL && doc!=NULL){
    10485      /**
    10586       * Parse Status to extract Status / Message
    10687       */
    107       char *tmpStr=_getStatus(conf,atoi(tmpMap->value));
     88      char *tmpStr=_getStatus(conf,tmpMap->value);
    10889#ifdef DEBUG
    10990      fprintf(stderr,"DEBUG: %s \n",tmpStr);
Note: See TracChangeset for help on using the changeset viewer.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png