Changeset 368 for trunk/zoo-project


Ignore:
Timestamp:
Oct 4, 2012, 5:04:31 PM (11 years ago)
Author:
djay
Message:

Many thanks to Trevor Clarke for providing patch for Python ZOO-API. This rev. shall fix ticket #74.

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

Legend:

Unmodified
Added
Removed
  • trunk/zoo-project/zoo-kernel/nmake.opt

    r364 r368  
    1 LIBINTL_CPATH=..\..\..\
    2 PYTHON_CPATH=..\..\..\Python-2.7.3
    3 TPATH=..\..\..\tools
    4 #GEODIR=c:/OSGeo4W
    5 #DESTDIR=c:/OSGeo4W
    6 TOOLS=$(TPATH)\bin
    71
    82CC=cl $(CFLAGS)
     
    3731!ENDIF
    3832
    39 CFLAGS=-SPOST_DEBUG $(CFLAGS1) /nologo /MD /W3 /EHsc /O2 /D_CRT_SECURE_NO_WARNINGS /DWIN32 $(CJFLAGS) -I$(ZOOK_DIR) -I./ $(JAVA_CFLAGS) -I$(TROOT)..\..\thirds\dirent-win32 -I$(INTL_DIR) -I$(CURL_DIR)\include -I$(XML2_DIR)\include -I$(GDAL_DIR)\port $(JS_CFLAGS) -I$(GDAL_DIR)\ogr -I$(GDAL_DIR)\gcore -I$(GD_DIR) -I$(ICONV_DIR) -I$(TROOT)..\..\thirds\include -I$(TROOT)..\..\thirds\cgic206 -I$(PYTHON_CPATH)\include -I$(SSL_DIR)/inc32 -I$(FCGI_DIR)\include $(PY_CFLAGS) -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
     33TROOT=$(ZOOK_DIR)
     34TPATH=$(ZOOK_DIR)\..\..\..\tools
     35LIBINTL_CPATH=$(ZOOK_DIR)\..\..\..\
     36PYTHON_CPATH=$(ZOOK_DIR)\..\..\..\Python-2.7.3
    4037
    41 LDFLAGS=$(FCGI_LIBRARY) $(CURL_LIBRARY) $(PY_LDFLAGS) $(XML2_LIBRARY) $(TROOT)../../thirds/cgic206/libcgic.lib $(SSL_DIR)/out32dll/libeay32.lib $(JAVA_LDFLAGS) $(SSL_DIR)/out32dll/ssleay32.lib $(MS_LDFLAGS) $(INTL_DIR)/intl.lib $(XML2_DIR)/win32/bin.msvc/libxml2.lib $(GDAL_DIR)/gdal_i.lib $(CURL_DIR)/lib/libcurl.lib $(JS_LDFLAGS) /machine:i386
     38CFLAGS=$(CFLAGS1) /nologo /MD /W3 /EHsc /O2 /D_CRT_SECURE_NO_WARNINGS /DWIN32 $(CJFLAGS) -I$(ZOOK_DIR) -I./ $(JAVA_CFLAGS) $(MS_CFLAGS) -I$(TROOT)\..\..\thirds\dirent-win32 -I$(INTL_DIR) -I$(CURL_DIR)\include -I$(XML2_DIR)\include -I$(GDAL_DIR)\port $(JS_CFLAGS) -I$(GDAL_DIR)\ogr -I$(GDAL_DIR)\gcore -I$(GD_DIR) -I$(ICONV_DIR) -I$(TROOT)\..\..\thirds\include -I$(TROOT)\..\..\thirds\cgic206 -I$(PYTHON_CPATH)\include -I$(SSL_DIR)/inc32 -I$(FCGI_DIR)\include $(PY_CFLAGS) -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
     39
     40LDFLAGS=$(FCGI_LIBRARY) $(CURL_LIBRARY) $(PY_LDFLAGS) $(XML2_LIBRARY) $(TROOT)/../../thirds/cgic206/libcgic.lib $(SSL_DIR)/out32dll/libeay32.lib $(JAVA_LDFLAGS) $(SSL_DIR)/out32dll/ssleay32.lib $(MS_LDFLAGS) $(INTL_DIR)/intl.lib $(XML2_DIR)/win32/bin.msvc/libxml2.lib $(GDAL_DIR)/gdal_i.lib $(CURL_DIR)/lib/libcurl.lib $(JS_LDFLAGS) /machine:i386
  • trunk/zoo-project/zoo-kernel/service_internal_js.c

    r364 r368  
    120120  }
    121121
     122  jsval tmp=INT_TO_JSVAL(3);
     123  if (!JS_SetProperty(cx, global, "SERVICE_SUCCEEDED", &tmp))
     124    return 1;
     125  tmp=INT_TO_JSVAL(4);
     126  if (!JS_SetProperty(cx, global, "SERVICE_FAILED", &tmp))
     127    return 1;
    122128  if (!JS_DefineFunction(cx, global, "ZOORequest", JSRequest, 4, 0))
    123129    return 1;
     
    638644}
    639645
     646char* JSValToChar(JSContext* context, jsval* arg) {
     647  char *c;
     648  char *tmp;
     649  JSString *jsmsg;
     650  size_t len;
     651  int i;
     652  if(!JSVAL_IS_STRING(*arg)) {
     653    return NULL;
     654  }
     655  jsmsg = JS_ValueToString(context,*arg);
     656  len = JS_GetStringLength(jsmsg);
     657  tmp = JS_EncodeString(context,jsmsg);
     658  c = (char*)malloc((len+1)*sizeof(char));
     659  c[len] = '\0';
     660#ifdef ULINET_DEBUG
     661  fprintf(stderr,"%d \n",len);
     662#endif
     663  for(i = 0;i < len;i++) {
     664    c[i] = tmp[i];
     665    c[i+1] = 0;
     666  }
     667#ifdef ULINET_DEBUG
     668  fprintf(stderr,"%s \n",c);
     669#endif
     670  return c;
     671}
     672
     673HINTERNET setHeader(HINTERNET handle,JSContext *cx,JSObject *header){
     674  jsuint length=0;
     675  jsint i=0;
     676  char *tmp1;
     677#ifdef ULINET_DEBUG
     678  fprintf(stderr,"setHeader\n");
     679#endif
     680  if(JS_IsArrayObject(cx,header)){
     681#ifdef ULINET_DEBUG
     682    fprintf(stderr,"header is an array\n");
     683#endif
     684    JS_GetArrayLength(cx,header,&length);
     685#ifdef ULINET_DEBUG
     686    fprintf(stderr,"header is an array of %d elements\n",length);
     687#endif
     688    handle.header=NULL;
     689    for(i=0;i<length;i++){
     690      jsval tmp;
     691      JS_GetElement(cx,header,i,&tmp);
     692      tmp1=JSValToChar(cx,&tmp);
     693#ifdef ULINET_DEBUG
     694      fprintf(stderr,"Element of array n° %d, value : %s\n",i,tmp1);
     695#endif
     696      handle.header=curl_slist_append(handle.header, tmp1);
     697      free(tmp1);
     698    }
     699  }
     700  else{
     701    fprintf(stderr,"not an array !!!!!!!\n");
     702  }
     703  return handle;
     704}
     705
     706JSBool
     707JSRequest(JSContext *cx, uintN argc, jsval *argv1)
     708{
     709  jsval *argv = JS_ARGV(cx,argv1);
     710  HINTERNET hInternet;
     711  HINTERNET res;
     712  HINTERNET res1;
     713  JSObject *header;
     714  char *url;
     715  char *method;
     716  char* tmpValue;
     717  size_t dwRead;
     718  int i=0;
     719  JS_MaybeGC(cx);
     720  hInternet=InternetOpen("ZooWPSClient\0",
     721                         INTERNET_OPEN_TYPE_PRECONFIG,
     722                         NULL,NULL, 0);
     723  if(!CHECK_INET_HANDLE(hInternet))
     724    return JS_FALSE;
     725  if(argc>=2){
     726    method=JSValToChar(cx,&argv[0]);
     727    url=JSValToChar(cx,&argv[1]);
     728  }
     729  else{
     730    method=strdup("GET");
     731    url=JSValToChar(cx,argv);
     732  }
     733  if(argc==4){
     734    char *body;
     735    body=JSValToChar(cx,&argv[2]);
     736    header=JSVAL_TO_OBJECT(argv[3]);
     737#ifdef ULINET_DEBUG
     738    fprintf(stderr,"URL (%s) \nBODY (%s)\n",url,body);
     739#endif
     740    if(JS_IsArrayObject(cx,header))
     741      res1=setHeader(hInternet,cx,header);
     742#ifdef ULINET_DEBUG
     743    fprintf(stderr,"BODY (%s)\n",body);
     744#endif
     745    res=InternetOpenUrl(res1,url,body,strlen(body),
     746                        INTERNET_FLAG_NO_CACHE_WRITE,0);   
     747    free(body);
     748  }else{
     749    if(argc==3){
     750      char *body=JSValToChar(cx,&argv[2]);
     751      res=InternetOpenUrl(hInternet,url,body,strlen(body),
     752                          INTERNET_FLAG_NO_CACHE_WRITE,0);
     753      free(body);
     754    }
     755    res=InternetOpenUrl(hInternet,url,NULL,0,
     756                        INTERNET_FLAG_NO_CACHE_WRITE,0);
     757  }
     758  tmpValue=(char*)malloc((res.nDataLen+1)*sizeof(char));
     759  InternetReadFile(res,(LPVOID)tmpValue,res.nDataLen,&dwRead);
     760#ifdef ULINET_DEBUG
     761  fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
     762#endif
     763  if(dwRead==0){
     764    JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,"Unable to access the file.",strlen("Unable to access the file."))));
     765    return JS_TRUE;
     766  }
     767
     768#ifdef ULINET_DEBUG
     769  fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
     770#endif
     771  JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpValue,strlen(tmpValue))));
     772  free(url);
     773  if(argc>=2)
     774    free(method);
     775  if(argc==4 && res.header!=NULL){
     776    curl_slist_free_all(res.header);
     777  }
     778  InternetCloseHandle(hInternet);
     779  JS_MaybeGC(cx);
     780  return JS_TRUE;
     781}
  • trunk/zoo-project/zoo-kernel/service_internal_js.h

    r364 r368  
    4444  maps* mapsFromJSObject(JSContext *,jsval);
    4545  map* mapFromJSObject(JSContext *,jsval);
     46  JSBool JSRequest(JSContext*, uintN, jsval*);
    4647
    4748  void reportError(JSContext *cx, const char *message, JSErrorReport *report);
  • trunk/zoo-project/zoo-kernel/service_internal_python.c

    r364 r368  
    2525#include "service_internal_python.h"
    2626
     27static PyObject* ZooError;
     28
     29PyMethodDef zooMethods[] = {
     30  {"update_status", PythonUpdateStatus, METH_VARARGS, "Update status percentage of a running process."},
     31  {NULL, NULL, 0, NULL} /* tempt not the blade, all fear the sentinel */
     32};
     33
     34PyMODINIT_FUNC init_zoo(){
     35  PyObject *tmp,*d;
     36  PyObject* module = Py_InitModule("zoo", zooMethods);
     37  if (module == NULL)
     38    return;
     39 
     40  d = PyModule_GetDict(module);
     41  tmp = PyInt_FromLong(3);
     42  PyDict_SetItemString(d, "SERVICE_SUCCEEDED", tmp);
     43  Py_DECREF(tmp);
     44
     45  tmp = PyInt_FromLong(4);
     46  PyDict_SetItemString(d, "SERVICE_FAILED", tmp);
     47  Py_DECREF(tmp);
     48
     49  ZooError = PyErr_NewException("zoo.error", NULL, NULL);
     50  Py_INCREF(ZooError);
     51  PyModule_AddObject(module, "error", ZooError);
     52}
     53
    2754int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
    2855  maps* m=*main_conf;
     
    80107  PyEval_InitThreads();
    81108  Py_Initialize();
     109  init_zoo();
    82110  mainstate = PyThreadState_Swap(NULL);
    83111  PyEval_ReleaseLock();
     
    388416  return res;
    389417}
     418
     419PyObject*
     420PythonUpdateStatus(PyObject* self, PyObject* args)
     421{
     422  maps* conf;
     423  PyObject* confdict;
     424  int istatus;
     425  char* status;
     426  if (!PyArg_ParseTuple(args, "O!i", &PyDict_Type, &confdict, &istatus)){
     427#ifdef DEBUG
     428    fprintf(stderr,"Incorrect arguments to update status function");
     429#endif
     430    return NULL;
     431  }
     432  if (istatus < 0 || istatus > 100){
     433     PyErr_SetString(ZooError, "Status must be a percentage.");
     434     return NULL;
     435  }else{
     436     char tmpStatus[4];
     437     snprintf(tmpStatus, 4, "%i", istatus);
     438     status = strdup(tmpStatus);
     439  }
     440  /* now update the map */
     441  {
     442    PyObject* lenv = PyMapping_GetItemString(confdict, "lenv");
     443    if (lenv && PyMapping_Check(lenv)){
     444      PyObject* valobj = PyString_FromString(status);
     445      PyMapping_SetItemString(lenv, "status", valobj);
     446      Py_DECREF(valobj);
     447    }
     448    Py_DECREF(lenv);
     449  }
     450  conf = mapsFromPyDict((PyDictObject*)confdict);
     451  if (getMapFromMaps(conf,"lenv","status") != NULL){
     452    fprintf(stderr,"STATUS RETURNED : %s\n",status);
     453    if(status!=NULL){
     454      setMapInMaps(conf,"lenv","status",status);
     455      free(status);
     456    }
     457    else
     458      setMapInMaps(conf,"lenv","status","15");
     459    updateStatus(conf);
     460  }
     461  freeMaps(&conf);
     462  free(conf);
     463  Py_RETURN_NONE;
     464}
  • trunk/zoo-project/zoo-kernel/service_internal_python.h

    r348 r368  
    4545int zoo_python_support(maps**,map*,service*,maps**,maps**);
    4646
     47PyObject* PythonUpdateStatus(PyObject*, PyObject*);
     48
    4749#endif
  • trunk/zoo-project/zoo-kernel/ulinet.c

    r364 r368  
    374374}
    375375
    376 #ifdef USE_JS
    377 #define XP_WIN 1
    378 #include "jsapi.h"
    379 
    380 char* JSValToChar(JSContext* context, jsval* arg) {
    381   char *c;
    382   char *tmp;
    383   JSString *jsmsg;
    384   size_t len;
    385   int i;
    386   if(!JSVAL_IS_STRING(*arg)) {
    387     return NULL;
    388   }
    389   jsmsg = JS_ValueToString(context,*arg);
    390   len = JS_GetStringLength(jsmsg);
    391   tmp = JS_EncodeString(context,jsmsg);
    392   c = (char*)malloc((len+1)*sizeof(char));
    393   c[len] = '\0';
    394 #ifdef ULINET_DEBUG
    395   fprintf(stderr,"%d \n",len);
    396 #endif
    397   for(i = 0;i < len;i++) {
    398     c[i] = tmp[i];
    399     c[i+1] = 0;
    400   }
    401 #ifdef ULINET_DEBUG
    402   fprintf(stderr,"%s \n",c);
    403 #endif
    404   return c;
    405 }
    406 
    407 HINTERNET setHeader(HINTERNET handle,JSContext *cx,JSObject *header){
    408   jsuint length=0;
    409   jsint i=0;
    410   char *tmp1;
    411 #ifdef ULINET_DEBUG
    412   fprintf(stderr,"setHeader\n");
    413 #endif
    414   if(JS_IsArrayObject(cx,header)){
    415 #ifdef ULINET_DEBUG
    416     fprintf(stderr,"header is an array\n");
    417 #endif
    418     JS_GetArrayLength(cx,header,&length);
    419 #ifdef ULINET_DEBUG
    420     fprintf(stderr,"header is an array of %d elements\n",length);
    421 #endif
    422     handle.header=NULL;
    423     for(i=0;i<length;i++){
    424       jsval tmp;
    425       JS_GetElement(cx,header,i,&tmp);
    426       tmp1=JSValToChar(cx,&tmp);
    427 #ifdef ULINET_DEBUG
    428       fprintf(stderr,"Element of array n° %d, value : %s\n",i,tmp1);
    429 #endif
    430       handle.header=curl_slist_append(handle.header, tmp1);
    431       free(tmp1);
    432     }
    433   }
    434   else{
    435     fprintf(stderr,"not an array !!!!!!!\n");
    436   }
    437   return handle;
    438 }
    439 
    440 JSBool
    441 JSRequest(JSContext *cx, uintN argc, jsval *argv1)
    442 {
    443   jsval *argv = JS_ARGV(cx,argv1);
    444   HINTERNET hInternet;
    445   HINTERNET res;
    446   HINTERNET res1;
    447   JSObject *header;
    448   char *url;
    449   char *method;
    450   char* tmpValue;
    451   size_t dwRead;
    452   int i=0;
    453   JS_MaybeGC(cx);
    454   hInternet=InternetOpen((LPCTSTR)"ZooWPSClient\0",
    455                          INTERNET_OPEN_TYPE_PRECONFIG,
    456                          NULL,NULL, 0);
    457   if(!CHECK_INET_HANDLE(hInternet))
    458     return JS_FALSE;
    459   if(argc>=2){
    460     method=JSValToChar(cx,&argv[0]);
    461     url=JSValToChar(cx,&argv[1]);
    462   }
    463   else{
    464     method=strdup("GET");
    465     url=JSValToChar(cx,argv);
    466   }
    467   if(argc==4){
    468     char *body;
    469     body=JSValToChar(cx,&argv[2]);
    470     header=JSVAL_TO_OBJECT(argv[3]);
    471 #ifdef ULINET_DEBUG
    472     fprintf(stderr,"URL (%s) \nBODY (%s)\n",url,body);
    473 #endif
    474     if(JS_IsArrayObject(cx,header))
    475       res1=setHeader(hInternet,cx,header);
    476 #ifdef ULINET_DEBUG
    477     fprintf(stderr,"BODY (%s)\n",body);
    478 #endif
    479     res=InternetOpenUrl(res1,url,body,strlen(body),
    480                         INTERNET_FLAG_NO_CACHE_WRITE,0);   
    481     free(body);
    482   }else{
    483     if(argc==3){
    484       char *body=JSValToChar(cx,&argv[2]);
    485       res=InternetOpenUrl(hInternet,url,body,strlen(body),
    486                           INTERNET_FLAG_NO_CACHE_WRITE,0);
    487       free(body);
    488     }
    489     res=InternetOpenUrl(hInternet,url,NULL,0,
    490                         INTERNET_FLAG_NO_CACHE_WRITE,0);
    491   }
    492   tmpValue=(char*)malloc((res.nDataLen+1)*sizeof(char));
    493   InternetReadFile(res,(LPVOID)tmpValue,res.nDataLen,&dwRead);
    494   fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
    495   if(dwRead==0){
    496     JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,"Unable to access the file.",strlen("Unable to access the file."))));
    497     return JS_TRUE;
    498   }
    499 
    500 #ifdef ULINET_DEBUG
    501   fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
    502 #endif
    503   JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpValue,strlen(tmpValue))));
    504   free(url);
    505   if(argc>=2)
    506     free(method);
    507   if(argc==4 && res.header!=NULL){
    508     curl_slist_free_all(res.header);
    509   }
    510   InternetCloseHandle(hInternet);
    511   JS_MaybeGC(cx);
    512   return JS_TRUE;
    513 }
    514 #endif
  • trunk/zoo-project/zoo-kernel/ulinet.h

    r364 r368  
    141141bool InternetGetCookie(LPCTSTR lpszUrl,LPCTSTR lpszCookieName,LPTSTR lpszCookieData,LPDWORD lpdwSize);
    142142
    143 #ifdef USE_JS
    144 JSBool JSRequest(JSContext*, uintN, jsval*);
    145 #endif
    146 
    147143#ifdef __cplusplus
    148144}
  • trunk/zoo-project/zoo-kernel/zoo_service_loader.c

    r365 r368  
    6666#include "service_internal_perl.h"
    6767#endif
    68 
    69 
    7068
    7169#include <dirent.h>
     
    633631  service* s1;
    634632  int scount=0;
    635 
    636633#ifdef DEBUG
    637634  dumpMap(r_inputs);
     
    11401137#endif
    11411138                {
    1142                   loadRemoteFile(m,tmpmaps->content,hInternet,tmpx2);
     1139                  if(loadRemoteFile(m,tmpmaps->content,hInternet,tmpx2)<0){
     1140                    freeMaps(&m);
     1141                    free(m);
     1142                    free(REQUEST);
     1143                    free(SERVICE_URL);
     1144                    InternetCloseHandle(hInternet);
     1145                    freeService(&s1);
     1146                    free(s1);
     1147                    return 0;
     1148                  }
    11431149                }
    11441150              free(tmpx2);
     
    12931299                  if(!(ltmp!=NULL && strcmp(ltmp->value,"POST")==0)
    12941300                     && CHECK_INET_HANDLE(hInternet)){
    1295                     loadRemoteFile(m,tmpmaps->content,hInternet,(char*)val);
     1301                    if(loadRemoteFile(m,tmpmaps->content,hInternet,(char*)val)){
     1302                      freeMaps(&m);
     1303                      free(m);
     1304                      free(REQUEST);
     1305                      free(SERVICE_URL);
     1306                      InternetCloseHandle(hInternet);
     1307                      freeService(&s1);
     1308                      free(s1);
     1309                      return 0;
     1310                    }
    12961311                  }
    12971312                }
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