Changeset 507 for trunk/zoo-project


Ignore:
Timestamp:
Oct 8, 2014, 11:38:12 AM (9 years ago)
Author:
djay
Message:

Add concurrency gesture for background services. Fix tickets #102 and #103.

Location:
trunk/zoo-project/zoo-kernel
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/zoo-project/zoo-kernel/service.h

    r490 r507  
    4040#define zOpen _open
    4141#define zWrite _write
     42#define zSleep Sleep
    4243#else
    4344#define zStrdup strdup
     
    4546#define zOpen open
    4647#define zWrite write
     48#define zSleep sleep
    4749#endif
    4850
     
    6870#define false -1
    6971#endif
    70 #else
    71   //#include <stdbool.h>
    7272#endif
    7373
  • trunk/zoo-project/zoo-kernel/service_internal.c

    r505 r507  
    8383}
    8484
     85char* _getStatus(maps* conf,int pid){
     86  char lid[1024];
     87  sprintf(lid,"%d",pid);
     88  setMapInMaps(conf,"lenv","lid",lid);
     89  semid lockid=getShmLockId(conf,1);
     90  if(
     91#ifdef WIN32
     92     lockid==NULL
     93#else
     94     lockid<0
     95#endif
     96     ){
     97    char tmp[3];
     98    sprintf(tmp,"%d",ZOO_LOCK_CREATE_FAILED);
     99    return tmp;
     100  }
     101  if(lockShm(lockid)<0){
     102    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
     103    fflush(stderr);
     104    char tmp[3];
     105    sprintf(tmp,"%d",ZOO_LOCK_ACQUIRE_FAILED);
     106    return tmp;
     107  }
     108  char *tmp=getStatus(pid);
     109  unlockShm(lockid);
     110  if(tmp==NULL || strncmp(tmp,"-1",2)==0){
     111    removeShmLock(conf,1);
     112  }
     113  return tmp;
     114}
     115
    85116#ifdef WIN32
    86117
     
    92123
    93124#define SHMEMSIZE 4096
     125
     126char* getKeyValue(maps* conf){
     127  map *tmpMap=getMapFromMaps(conf,"lenv","lid");
     128  if(tmpMap==NULL)
     129    tmpMap=getMapFromMaps(conf,"lenv","sid");
     130  char* key="-1";
     131  if(tmpMap!=NULL){
     132    key=(char*)malloc((strlen(tmpMap->value)+9)*sizeof(char));
     133    sprintf(key,"zoo_sem_%s",tmpMap->value);
     134  }
     135  return key;
     136}
     137
     138
     139semid getShmLockId(maps* conf, int nsems){
     140    semid sem_id;
     141    char* key=getKeyValue(conf);
     142   
     143    sem_id = CreateSemaphore( NULL, nsems, nsems+1, key);
     144    if(sem_id==NULL){
     145      if(strncmp(key,"-1",2)!=0)
     146        free(key);
     147#ifdef DEBUG
     148      fprintf(stderr,"Semaphore failed to create ! %s\n",GetLastError());
     149#endif
     150      return NULL;
     151    }
     152#ifdef DEBUG
     153    fprintf(stderr,"%s Accessed !\n",key);
     154#endif
     155    if(strncmp(key,"-1",2)!=0)
     156      free(key);
     157    return sem_id;
     158}
     159
     160int removeShmLock(maps* conf, int nsems){
     161  semid sem_id=getShmLockId(conf,1);
     162  if (CloseHandle(sem_id) == 0) {
     163    fprintf(stderr,"Unable to remove semaphore %s",GetLastError());
     164    return -1;
     165  }
     166#ifdef DEBUG
     167  fprintf(stderr,"%d Removed !\n",sem_id);
     168#endif
     169  return 0;
     170}
     171
     172int lockShm(semid id){
     173  DWORD dwWaitResult=WaitForSingleObject(id,INFINITE);
     174  switch (dwWaitResult){
     175    case WAIT_OBJECT_0:
     176      return 0;
     177      break;
     178    case WAIT_TIMEOUT:
     179      return -1;
     180      break;
     181    default:
     182      return -2;
     183      break;
     184  }
     185  return 0;
     186}
     187
     188int unlockShm(semid id){
     189  if(!ReleaseSemaphore(id,1,NULL)){
     190    return -1;
     191  }
     192  return 0;
     193}
    94194
    95195static LPVOID lpvMemG = NULL;      // pointer to shared memory
     
    103203  map *tmpMap1;
    104204  map *tmpMap=getMapFromMaps(conf,"lenv","sid");
     205  semid lockid=getShmLockId(conf,1);
     206  if(lockid==NULL){
     207#ifdef DEBUG
     208    fprintf(stderr,"Unable to create semaphore on line %d!! \n",__LINE__);
     209#endif
     210    return ZOO_LOCK_CREATE_FAILED;
     211  }
     212  if(lockShm(lockid)<0){
     213#ifdef DEBUG
     214    fprintf(stderr,"Unable to create semaphore on line %d!! \n",__LINE__);
     215#endif
     216    return ZOO_LOCK_ACQUIRE_FAILED;
     217  }
     218 
    105219  if(hMapObjectG==NULL)
    106220    hMapObjectG = CreateFileMapping(
     
    112226                                    TEXT(tmpMap->value));   // name of map object
    113227  if (hMapObjectG == NULL){
    114     fprintf(stderr,"Unable to create share memory segment %s !! \n",tmpMap->value);
     228#ifdef DEBUG
     229    fprintf(stderr,"Unable to create shared memory segment %d !! \n",GetLastError());
     230#endif
    115231    return -2;
    116232  }
     
    124240                            0);             // default: map entire file
    125241  if (lpvMemG == NULL){
     242#ifdef DEBUG
    126243    fprintf(stderr,"Unable to create or access the shared memory segment %s !! \n",tmpMap->value);
     244#endif
    127245    return -1;
    128246  }
     
    139257  *lpszTmp++ = '\0';
    140258  free(final_string);
     259  unlockShm(lockid);
    141260  return 0;
    142261}
     
    149268  HANDLE hMapObject = NULL;
    150269  BOOL fIgnore,fInit;
    151   char tmp[100];
    152   sprintf(tmp,"%i",pid);
     270  char tmp[1024];
     271  sprintf(tmp,"%d",pid);
    153272  if(hMapObject==NULL)
    154273    hMapObject = CreateFileMapping(
     
    159278                                   4096,                   // size: low 32-bits
    160279                                   TEXT(tmp));   // name of map object
    161   if (hMapObject == NULL)
    162     return FALSE;
     280  if (hMapObject == NULL){
     281#ifdef DEBUG
     282    fprintf(stderr,"ERROR on line %d\n",__LINE__);
     283#endif
     284    return "-1";
     285  }
    163286  if((GetLastError() != ERROR_ALREADY_EXISTS)){
     287#ifdef DEBUG
     288    fprintf(stderr,"ERROR on line %d\n",__LINE__);
     289    fprintf(stderr,"READING STRING S %s\n",GetLastError());
     290#endif
    164291    fIgnore = UnmapViewOfFile(lpvMem);
    165292    fIgnore = CloseHandle(hMapObject);
     
    174301                           0,              // low offset:   beginning
    175302                           0);             // default: map entire file
    176   if (lpvMem == NULL)
     303  if (lpvMem == NULL){
     304#ifdef DEBUG
     305    fprintf(stderr,"READING STRING S %d\n",__LINE__);
     306    fprintf(stderr,"READING STRING S %s\n",GetLastError());
     307#endif
    177308    return "-1";
     309  }
    178310  lpszTmp = (LPWSTR) lpvMem;
    179311  while (*lpszTmp){
     
    183315    i++;
    184316  }
    185 #ifdef DEBUG
    186   fprintf(stderr,"READING STRING S %s\n",lpszBuf);
    187 #endif
    188317  return (char*)lpszBuf;
    189318}
     
    194323  fIgnore = CloseHandle(hMapObjectG);
    195324}
     325
    196326#else
     327
     328#define MAX_RETRIES 10
     329
     330int getKeyValue(maps* conf){
     331  map *tmpMap=getMapFromMaps(conf,"lenv","lid");
     332  if(tmpMap==NULL)
     333    tmpMap=getMapFromMaps(conf,"lenv","sid");
     334  int key=-1;
     335  if(tmpMap!=NULL)
     336    key=atoi(tmpMap->value);
     337  return key;
     338}
     339
     340int getShmLockId(maps* conf, int nsems){
     341    int i;
     342    union semun arg;
     343    struct semid_ds buf;
     344    struct sembuf sb;
     345    semid sem_id;
     346    int key=getKeyValue(conf);
     347   
     348    sem_id = semget(key, nsems, IPC_CREAT | IPC_EXCL | 0666);
     349
     350    if (sem_id >= 0) { /* we got it first */
     351        sb.sem_op = 1;
     352        sb.sem_flg = 0;
     353        arg.val=1;
     354        for(sb.sem_num = 0; sb.sem_num < nsems; sb.sem_num++) {
     355            /* do a semop() to "free" the semaphores. */
     356            /* this sets the sem_otime field, as needed below. */
     357            if (semop(sem_id, &sb, 1) == -1) {
     358                int e = errno;
     359                semctl(sem_id, 0, IPC_RMID); /* clean up */
     360                errno = e;
     361                return -1; /* error, check errno */
     362            }
     363        }
     364    } else if (errno == EEXIST) { /* someone else got it first */
     365        int ready = 0;
     366
     367        sem_id = semget(key, nsems, 0); /* get the id */
     368        if (sem_id < 0) return sem_id; /* error, check errno */
     369
     370        /* wait for other process to initialize the semaphore: */
     371        arg.buf = &buf;
     372        for(i = 0; i < MAX_RETRIES && !ready; i++) {
     373            semctl(sem_id, nsems-1, IPC_STAT, arg);
     374            if (arg.buf->sem_otime != 0) {
     375#ifdef DEBUG
     376              fprintf(stderr,"Semaphore acquired ...\n");
     377#endif
     378              ready = 1;
     379            } else {
     380#ifdef DEBUG
     381              fprintf(stderr,"Retry to access the semaphore later ...\n");
     382#endif
     383              sleep(1);
     384            }
     385        }
     386        errno = NULL;
     387        if (!ready) {
     388#ifdef DEBUG
     389          fprintf(stderr,"Unable to access the semaphore ...\n");
     390#endif
     391          errno = ETIME;
     392          return -1;
     393        }
     394    } else {
     395        return sem_id; /* error, check errno */
     396    }
     397#ifdef DEBUG
     398    fprintf(stderr,"%d Created !\n",sem_id);
     399#endif
     400    return sem_id;
     401}
     402
     403int removeShmLock(maps* conf, int nsems){
     404  union semun arg;
     405  int sem_id=getShmLockId(conf,nsems);
     406  if (semctl(sem_id, 0, IPC_RMID, arg) == -1) {
     407    perror("semctl");
     408    return -1;
     409  }
     410  return 0;
     411}
     412
     413int lockShm(int id){
     414  struct sembuf sb;
     415  int i;
     416  sb.sem_num = 0;
     417  sb.sem_op = -1;  /* set to allocate resource */
     418  sb.sem_flg = SEM_UNDO;
     419  if (semop(id, &sb, 1) == -1){
     420    perror("semop");
     421    return -1;
     422  }
     423  return 0;
     424}
     425
     426int unlockShm(int id){
     427  struct sembuf sb;
     428  sb.sem_num = 0;
     429  sb.sem_op = 1;  /* free resource */
     430  sb.sem_flg = SEM_UNDO;
     431  if (semop(id, &sb, 1) == -1) {
     432    perror("semop");
     433    return -1;
     434  }
     435  return 0;
     436}
    197437
    198438void unhandleStatus(maps *conf){
     
    223463int _updateStatus(maps *conf){
    224464  int shmid;
    225   key_t key;
    226465  char *shm,*s,*s1;
    227466  map *tmpMap=NULL;
    228   tmpMap=getMapFromMaps(conf,"lenv","sid");
    229   if(tmpMap!=NULL){
    230     key=atoi(tmpMap->value);
     467  key_t key=getKeyValue(conf);
     468  if(key!=-1){
     469    semid lockid=getShmLockId(conf,1);
     470    if(lockid<0)
     471      return ZOO_LOCK_CREATE_FAILED;
     472    if(lockShm(lockid)<0){
     473      return ZOO_LOCK_ACQUIRE_FAILED;
     474    }
    231475    if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
    232476#ifdef DEBUG
    233477      fprintf(stderr,"shmget failed to create new Shared memory segment\n");
    234478#endif
     479      unlockShm(lockid);
    235480      return -2;
    236481    }else{
     
    239484        fprintf(stderr,"shmat failed to update value\n");
    240485#endif
     486        unlockShm(lockid);
    241487        return -1;
    242488      }
     
    255501        *s1=NULL;
    256502        shmdt((void *)shm);
     503        unlockShm(lockid);
    257504      }
    258505    }
     
    11121359              if(nci0==0){
    11131360                nc7 = xmlNewNode(ns_ows, BAD_CAST "MinimumValue");
    1114                 int nci=1;
    1115                 for(nci=1;nci<strlen(pToken);nci++){
    1116                   tmpStr[nci-1]=pToken[nci];
    1117                 }
    1118                 }else{
     1361                strncpy( tmpStr, pToken+1, strlen(pToken)-1 );
     1362                tmpStr[strlen(pToken)-1] = '\0';
     1363              }else{
    11191364                nc7 = xmlNewNode(ns_ows, BAD_CAST "MaximumValue");
    1120                 int nci=0;
    1121                 for(nci=0;nci<strlen(pToken)-1;nci++){
    1122                   tmpStr[nci]=pToken[nci];
    1123                 }
     1365                const char* bkt;
     1366                if ( ( bkt = strchr(pToken, '[') ) != NULL || ( bkt = strchr(pToken, ']') ) != NULL ){
     1367                    strncpy( tmpStr, pToken, bkt - pToken );
     1368                    tmpStr[bkt - pToken] = '\0';
     1369                  }
    11241370              }
    11251371              xmlAddChild(nc7,xmlNewText(BAD_CAST tmpStr));
     
    11791425            xmlAddChild(nc6,nc8);
    11801426            _tmp0=e->supported;
    1181             if(getMap(_tmp0->content,"range")!=NULL ||
    1182                getMap(_tmp0->content,"rangeMin")!=NULL ||
    1183                getMap(_tmp0->content,"rangeMax")!=NULL ||
    1184                getMap(_tmp0->content,"rangeClosure")!=NULL ){
     1427            if(_tmp0!=NULL &&
     1428               (getMap(_tmp0->content,"range")!=NULL ||
     1429                getMap(_tmp0->content,"rangeMin")!=NULL ||
     1430                getMap(_tmp0->content,"rangeMax")!=NULL ||
     1431                getMap(_tmp0->content,"rangeClosure")!=NULL )){
    11851432              tmp1=_tmp0->content;
    11861433              goto doRange;
     
    16501897  xmlDocSetRootElement(doc, nr);
    16511898
    1652   if(hasStoredExecuteResponse==true){
    1653     /* We need to write the ExecuteResponse Document somewhere */
    1654     FILE* output=fopen(stored_path,"w");
    1655     if(output==NULL){
    1656       /* If the file cannot be created return an ExceptionReport */
    1657       char tmpMsg[1024];
    1658       sprintf(tmpMsg,_("Unable to create the file : \"%s\" for storing the ExecuteResponse."),stored_path);
    1659       map * errormap = createMap("text",tmpMsg);
    1660       addToMap(errormap,"code", "InternalError");
    1661       printExceptionReportResponse(m,errormap);
    1662       freeMap(&errormap);
    1663       free(errormap);
    1664       xmlFreeDoc(doc);
    1665       xmlCleanupParser();
    1666       zooXmlCleanupNs();
     1899  if(hasStoredExecuteResponse==true && status!=SERVICE_STARTED){
     1900    semid lid=getShmLockId(m,1);
     1901    if(lid<0)
    16671902      return;
    1668     }
    1669     xmlChar *xmlbuff;
    1670     int buffersize;
    1671     xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "UTF-8", 1);
    1672     fwrite(xmlbuff,1,xmlStrlen(xmlbuff)*sizeof(char),output);
    1673     xmlFree(xmlbuff);
    1674     fclose(output);
     1903    else{
     1904#ifdef DEBUG
     1905      fprintf(stderr,"LOCK %s %d !\n",__FILE__,__LINE__);
     1906#endif
     1907      lockShm(lid);
     1908      /* We need to write the ExecuteResponse Document somewhere */
     1909      FILE* output=fopen(stored_path,"w");
     1910      if(output==NULL){
     1911        /* If the file cannot be created return an ExceptionReport */
     1912        char tmpMsg[1024];
     1913        sprintf(tmpMsg,_("Unable to create the file : \"%s\" for storing the ExecuteResponse."),stored_path);
     1914        map * errormap = createMap("text",tmpMsg);
     1915        addToMap(errormap,"code", "InternalError");
     1916        printExceptionReportResponse(m,errormap);
     1917        freeMap(&errormap);
     1918        free(errormap);
     1919        xmlFreeDoc(doc);
     1920        xmlCleanupParser();
     1921        zooXmlCleanupNs();
     1922        unlockShm(lid);
     1923        return;
     1924      }
     1925      xmlChar *xmlbuff;
     1926      int buffersize;
     1927      xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "UTF-8", 1);
     1928      fwrite(xmlbuff,1,xmlStrlen(xmlbuff)*sizeof(char),output);
     1929      xmlFree(xmlbuff);
     1930      fclose(output);
     1931#ifdef DEBUG
     1932      fprintf(stderr,"UNLOCK %s %d !\n",__FILE__,__LINE__);
     1933#endif
     1934      unlockShm(lid);
     1935      map* test1=getMap(request,"status");
     1936      if(test1==NULL || strcasecmp(test1->value,"true")!=0){
     1937        removeShmLock(m,1);
     1938      }
     1939    }
    16751940  }
    16761941  printDocument(m,doc,pid);
  • trunk/zoo-project/zoo-kernel/service_internal.h

    r501 r507  
    3636#define _ss(String) dgettext ("zoo-services",String)
    3737
     38#define ZOO_LOCK_CREATE_FAILED -4
     39#define ZOO_LOCK_ACQUIRE_FAILED -5
     40#define ZOO_LOCK_RELEASE_FAILED -6
     41
    3842#include <sys/stat.h>
    3943#include <sys/types.h>
     
    4246#include <sys/ipc.h>
    4347#include <sys/shm.h>
     48#include <sys/sem.h>
    4449#else
    4550#include <direct.h>
     
    99104  void unhandleStatus(maps*);
    100105  int _updateStatus(maps*);
     106  char* _getStatus(maps*,int);
    101107  char* getStatus(int);
     108  int removeShmLock(maps*, int);
     109#ifndef WIN32
     110#define semid int
     111#else
     112#define semid HANDLE
     113#endif
     114  semid getShmLockId(maps*,int);
     115  int lockShm(semid);
     116  int unlockShm(semid);
    102117
    103118#ifdef USE_JS
  • trunk/zoo-project/zoo-kernel/ulinet.h

    r492 r507  
    2727
    2828#include "fcgi_stdio.h"
    29 #include "service.h"
    3029#include <stdlib.h>
    3130#include <fcntl.h>
     
    4443#include "jsapi.h"
    4544#endif
     45#ifndef bool
     46#define bool int
     47#endif
     48#ifndef true
     49#define true 1
     50#define false -1
     51#endif
    4652
    47 #define MAX_REQ 100
     53#define MAX_REQ 50
    4854
    4955#ifdef _ULINET
  • trunk/zoo-project/zoo-kernel/zoo_service_loader.c

    r504 r507  
    26292629       * process answer to http client.
    26302630       */
     2631#ifndef WIN32
     2632      zSleep(1);
     2633#endif
    26312634      r_inputs=getMapFromMaps(m,"main","tmpPath");
    26322635      map* r_inputs1=getMap(s1->content,"ServiceProvider");
     
    26412644#endif
    26422645      freopen(flog, "w+", stderr);
     2646      semid lid=getShmLockId(m,1);
     2647      fflush(stderr);
     2648      if(lid<0){
     2649        fprintf(stderr,"ERROR %s %d\n",__FILE__,__LINE__);
     2650        fflush(stderr);
     2651        return -1;
     2652      }
     2653      else{
     2654        if(lockShm(lid)<0){
     2655          fprintf(stderr,"ERROR %s %d\n",__FILE__,__LINE__);
     2656          fflush(stderr);
     2657          return -1;
     2658        }
     2659        fflush(stderr);
     2660      }
    26432661      f0=freopen(fbkp , "w+", stdout);
     2662      rewind(stdout);
    26442663#ifndef WIN32
    26452664      fclose(stdin);
     
    26542673      printProcessResponse(m,request_inputs,cpid,s1,r_inputs1->value,SERVICE_STARTED,
    26552674                           request_input_real_format,request_output_real_format);
    2656 #ifndef WIN32
    26572675      fflush(stdout);
    2658       rewind(stdout);
    2659 #else
    2660 #endif
     2676      unlockShm(lid);
     2677      fflush(stderr);
    26612678      fbkp1=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+1024)*sizeof(char));
    26622679      sprintf(fbkp1,"%s/%s_final_%d.xml",r_inputs->value,r_inputs1->value,cpid);
     
    26972714    fclose(stdout);
    26982715    fclose(stderr);
    2699     unhandleStatus(m);
    27002716    /**
    27012717     * Dump back the final file fbkp1 to fbkp
     
    27042720    fclose(f1);
    27052721    FILE* f2=fopen(fbkp1,"rb");
     2722    semid lid=getShmLockId(m,1);
     2723    if(lid<0)
     2724      return -1;
     2725    lockShm(lid);
    27062726    FILE* f3=fopen(fbkp,"wb+");
    27072727    free(fbkp);
     
    27142734    fclose(f2);
    27152735    fclose(f3);
     2736    unlockShm(lid);
    27162737    unlink(fbkp1);
    27172738    free(fbkp1);
    27182739    free(tmps1);
     2740    unhandleStatus(m);
    27192741  }
    27202742
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