[26] | 1 | /** |
---|
| 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
| 4 | * Copyright 2008-2009 GeoLabs SARL. All rights reserved. |
---|
| 5 | * |
---|
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 7 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 8 | * in the Software without restriction, including without limitation the rights |
---|
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 10 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 11 | * furnished to do so, subject to the following conditions: |
---|
| 12 | * |
---|
| 13 | * The above copyright notice and this permission notice shall be included in |
---|
| 14 | * all copies or substantial portions of the Software. |
---|
| 15 | * |
---|
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 22 | * THE SOFTWARE. |
---|
| 23 | */ |
---|
| 24 | #include "service.h" |
---|
| 25 | |
---|
| 26 | extern "C" { |
---|
| 27 | #include <libxml/tree.h> |
---|
| 28 | #include <libxml/parser.h> |
---|
| 29 | #include <libxml/xpath.h> |
---|
| 30 | #include <libxml/xpathInternals.h> |
---|
| 31 | |
---|
| 32 | #include <libxslt/xslt.h> |
---|
| 33 | #include <libxslt/xsltInternals.h> |
---|
| 34 | #include <libxslt/transform.h> |
---|
| 35 | #include <libxslt/xsltutils.h> |
---|
| 36 | |
---|
| 37 | #include <dirent.h> |
---|
| 38 | #include "service_internal.h" |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * GetStatus ZOO Service : |
---|
| 42 | * This service is used in the ZOO-Project to get information about Services |
---|
| 43 | * running as background tasks. The service will first get the XML document |
---|
| 44 | * cached by the ZOO-Kernel before calling effectively the Service, then |
---|
| 45 | * will access the shared memory space created by the Kernel to extract the |
---|
| 46 | * current status of the running Service. Using a simple XSL file it will |
---|
| 47 | * finally produce the final ExecuteResponse including the updated |
---|
| 48 | * percentCompleted attribute of the ProcessStarted node of the cached |
---|
| 49 | * document if any (so if the Service is currently running) else it will |
---|
| 50 | * return the final ExecuteResponse stored on the Server file system. |
---|
| 51 | */ |
---|
| 52 | #ifdef WIN32 |
---|
| 53 | __declspec(dllexport) |
---|
| 54 | #endif |
---|
| 55 | int GetStatus(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
| 56 | const char *params[2 + 1]; |
---|
| 57 | int xmlLoadExtDtdDefaultValue; |
---|
| 58 | map* tmpMap=NULL,*tmpMmap=NULL, *tmpTmap=NULL; |
---|
| 59 | tmpMap=getMapFromMaps(inputs,"sid","value"); |
---|
| 60 | tmpTmap=getMapFromMaps(conf,"main","tmpPath"); |
---|
| 61 | tmpMmap=getMapFromMaps(conf,"main","dataPath"); |
---|
| 62 | xmlInitParser(); |
---|
| 63 | struct dirent *dp; |
---|
| 64 | DIR *dirp = opendir(tmpTmap->value); |
---|
| 65 | char fileName[1024],xslFileName[1024]; |
---|
[32] | 66 | int hasFile=-1; |
---|
[26] | 67 | if(dirp!=NULL){ |
---|
| 68 | char tmp[128]; |
---|
| 69 | sprintf(tmp,"_%s.xml",tmpMap->value); |
---|
| 70 | while ((dp = readdir(dirp)) != NULL) |
---|
[32] | 71 | if(strstr(dp->d_name,tmp)!=0){ |
---|
[26] | 72 | sprintf(fileName,"%s/%s",tmpTmap->value,dp->d_name); |
---|
[32] | 73 | hasFile=1; |
---|
| 74 | } |
---|
| 75 | }else{ |
---|
| 76 | char tmp[1024]; |
---|
| 77 | snprintf(tmp,1024,"GetStatus was unable to use the tmpPath value set in main.cfg file as directory %s.",tmpTmap->value); |
---|
| 78 | setMapInMaps(conf,"lenv","message",tmp); |
---|
[26] | 79 | return SERVICE_FAILED; |
---|
| 80 | } |
---|
[32] | 81 | if(hasFile<0){ |
---|
| 82 | char tmp[1024]; |
---|
| 83 | snprintf(tmp,1024,"GetStatus was unable to find any cache file for Service ID %s.",tmpMap->value); |
---|
| 84 | setMapInMaps(conf,"lenv","message",tmp); |
---|
| 85 | return SERVICE_FAILED; |
---|
| 86 | } |
---|
[26] | 87 | sprintf(xslFileName,"%s/updateStatus.xsl",tmpMmap->value); |
---|
| 88 | xmlSubstituteEntitiesDefault(1); |
---|
| 89 | xmlLoadExtDtdDefaultValue = 0; |
---|
| 90 | xsltStylesheetPtr cur = NULL; |
---|
| 91 | xmlDocPtr doc, res; |
---|
| 92 | cur = xsltParseStylesheetFile(BAD_CAST xslFileName); |
---|
| 93 | doc = xmlParseFile(fileName); |
---|
| 94 | if(cur!=NULL && doc!=NULL){ |
---|
| 95 | params[0]="value"; |
---|
| 96 | params[1]=getStatus(atoi(tmpMap->value)); |
---|
| 97 | params[2]=NULL; |
---|
| 98 | res = xsltApplyStylesheet(cur, doc, params); |
---|
| 99 | xmlChar *xmlbuff; |
---|
| 100 | int buffersize; |
---|
| 101 | xmlDocDumpFormatMemory(res, &xmlbuff, &buffersize, 1); |
---|
| 102 | setMapInMaps(outputs,"Result","value",(char*)xmlbuff); |
---|
| 103 | setMapInMaps(outputs,"Result","mimeType","text/xml"); |
---|
| 104 | setMapInMaps(outputs,"Result","encoding","UTF-8"); |
---|
| 105 | xmlFree(xmlbuff); |
---|
| 106 | } |
---|
[32] | 107 | else{ |
---|
[26] | 108 | char tmp[1024]; |
---|
[32] | 109 | sprintf(tmp,"ZOO GetStatus Service was unable to parse the cache xml file available for the Service ID %s.",tmpMap->value); |
---|
[26] | 110 | setMapInMaps(conf,"lenv","message",tmp); |
---|
| 111 | return SERVICE_FAILED; |
---|
| 112 | } |
---|
| 113 | return SERVICE_SUCCEEDED; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * longProcess ZOO Service : |
---|
| 119 | * Simple Service which just loop over 100 times then return a welcome message |
---|
| 120 | * string, at each step the service will sleep for one second. |
---|
| 121 | */ |
---|
| 122 | #ifdef WIN32 |
---|
| 123 | __declspec(dllexport) |
---|
| 124 | #endif |
---|
| 125 | int longProcess(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
| 126 | int i=0; |
---|
| 127 | while(i<100){ |
---|
| 128 | char tmp[4]; |
---|
| 129 | sprintf(tmp,"%i",i); |
---|
| 130 | map* tmpMap=NULL; |
---|
| 131 | tmpMap=getMapFromMaps(conf,"lenv","sid"); |
---|
| 132 | if(tmpMap!=NULL) |
---|
| 133 | fprintf(stderr,"Status %s %s\n",tmpMap->value,tmp); |
---|
| 134 | setMapInMaps(conf,"lenv","status",tmp); |
---|
| 135 | updateStatus(conf); |
---|
| 136 | sleep(1); |
---|
| 137 | i+=5; |
---|
| 138 | } |
---|
| 139 | setMapInMaps(outputs,"Result","value","\"Running long process successfully\""); |
---|
| 140 | return SERVICE_SUCCEEDED; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | } |
---|