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 | |
---|
25 | #include "service.h" |
---|
26 | #include "service_internal.h" |
---|
27 | |
---|
28 | extern "C" { |
---|
29 | #include <libxml/tree.h> |
---|
30 | #include <libxml/parser.h> |
---|
31 | #include <libxml/xpath.h> |
---|
32 | #include <libxml/xpathInternals.h> |
---|
33 | |
---|
34 | #include <libxslt/xslt.h> |
---|
35 | #include <libxslt/xsltInternals.h> |
---|
36 | #include <libxslt/transform.h> |
---|
37 | #include <libxslt/xsltutils.h> |
---|
38 | |
---|
39 | #include <dirent.h> |
---|
40 | |
---|
41 | /** |
---|
42 | * GetStatus ZOO Service : |
---|
43 | * This service is used in the ZOO-Project to get information about Services |
---|
44 | * running as background tasks. The service will first get the XML document |
---|
45 | * cached by the ZOO-Kernel before calling effectively the Service, then |
---|
46 | * will access the shared memory space created by the Kernel to extract the |
---|
47 | * current status of the running Service. Using a simple XSL file it will |
---|
48 | * finally produce the final ExecuteResponse including the updated |
---|
49 | * percentCompleted attribute of the ProcessStarted node of the cached |
---|
50 | * document if any (so if the Service is currently running) else it will |
---|
51 | * return the final ExecuteResponse stored on the Server file system. |
---|
52 | */ |
---|
53 | #ifdef WIN32 |
---|
54 | __declspec(dllexport) |
---|
55 | #endif |
---|
56 | int GetStatus(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
57 | const char *params[4 + 1]; |
---|
58 | int xmlLoadExtDtdDefaultValue; |
---|
59 | map* tmpMap=NULL,*tmpMmap=NULL, *tmpTmap=NULL; |
---|
60 | |
---|
61 | tmpMap=getMapFromMaps(inputs,"sid","value"); |
---|
62 | tmpTmap=getMapFromMaps(conf,"main","tmpPath"); |
---|
63 | tmpMmap=getMapFromMaps(conf,"main","dataPath"); |
---|
64 | if(tmpMmap==NULL) |
---|
65 | tmpMmap=tmpTmap; |
---|
66 | xmlInitParser(); |
---|
67 | struct dirent *dp; |
---|
68 | DIR *dirp = opendir(tmpTmap->value); |
---|
69 | char fileName[1024],xslFileName[1024]; |
---|
70 | 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){ |
---|
91 | char tmp[1024]; |
---|
92 | snprintf(tmp,1024,_ss("GetStatus was unable to find any cache file for Service ID %s."),tmpMap->value); |
---|
93 | setMapInMaps(conf,"lenv","message",tmp); |
---|
94 | return SERVICE_FAILED; |
---|
95 | } |
---|
96 | sprintf(xslFileName,"%s/updateStatus.xsl",tmpMmap->value); |
---|
97 | xmlSubstituteEntitiesDefault(1); |
---|
98 | xmlLoadExtDtdDefaultValue = 0; |
---|
99 | xsltStylesheetPtr cur = NULL; |
---|
100 | xmlDocPtr doc, res; |
---|
101 | cur = xsltParseStylesheetFile(BAD_CAST xslFileName); |
---|
102 | doc = xmlParseFile(fileName); |
---|
103 | if(cur!=NULL && doc!=NULL){ |
---|
104 | /** |
---|
105 | * Parse Status to extract Status / Message |
---|
106 | */ |
---|
107 | char *tmpStr=_getStatus(conf,atoi(tmpMap->value)); |
---|
108 | #ifdef DEBUG |
---|
109 | fprintf(stderr,"DEBUG: %s \n",tmpStr); |
---|
110 | #endif |
---|
111 | if(tmpStr!=NULL && strncmp(tmpStr,"-1",2)!=0){ |
---|
112 | char *tmpStr1=strdup(tmpStr); |
---|
113 | char *tmpStr0=strdup(strstr(tmpStr,"|")+1); |
---|
114 | free(tmpStr); |
---|
115 | tmpStr1[strlen(tmpStr1)-strlen(tmpStr0)-1]='\0'; |
---|
116 | char *tmpStrFinal=(char*)malloc((strlen(tmpStr0)+11)*sizeof(char)); |
---|
117 | sprintf(tmpStrFinal,"string(\"%s\")",tmpStr0); |
---|
118 | params[0]="value"; |
---|
119 | params[1]=tmpStr1; |
---|
120 | params[2]="message"; |
---|
121 | params[3]=tmpStrFinal; |
---|
122 | params[4]=NULL; |
---|
123 | res = xsltApplyStylesheet(cur, doc, params); |
---|
124 | xmlChar *xmlbuff; |
---|
125 | int buffersize; |
---|
126 | xmlDocDumpFormatMemory(res, &xmlbuff, &buffersize, 1); |
---|
127 | setMapInMaps(outputs,"Result","value",(char*)xmlbuff); |
---|
128 | xmlFree(xmlbuff); |
---|
129 | free(tmpStr1); |
---|
130 | free(tmpStr0); |
---|
131 | free(tmpStrFinal); |
---|
132 | }else{ |
---|
133 | xmlChar *xmlbuff; |
---|
134 | int buffersize; |
---|
135 | xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1); |
---|
136 | setMapInMaps(outputs,"Result","value",(char*)xmlbuff); |
---|
137 | xmlFree(xmlbuff); |
---|
138 | } |
---|
139 | } |
---|
140 | else{ |
---|
141 | char tmp[1024]; |
---|
142 | sprintf(tmp,_ss("ZOO GetStatus Service was unable to parse the cache xml file available for the Service ID %s."),tmpMap->value); |
---|
143 | setMapInMaps(conf,"lenv","message",tmp); |
---|
144 | return SERVICE_FAILED; |
---|
145 | } |
---|
146 | return SERVICE_SUCCEEDED; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | /** |
---|
151 | * longProcess ZOO Service : |
---|
152 | * Simple Service which just loop over 100 times then return a welcome message |
---|
153 | * string, at each step the service will sleep for one second. |
---|
154 | */ |
---|
155 | #ifdef WIN32 |
---|
156 | __declspec(dllexport) |
---|
157 | #endif |
---|
158 | int longProcess(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
159 | int i=0; |
---|
160 | while(i<100){ |
---|
161 | char message[10]; |
---|
162 | sprintf(message,"Step %d",i); |
---|
163 | updateStatus(conf,i,message); |
---|
164 | #ifndef WIN32 |
---|
165 | sleep(1); |
---|
166 | #else |
---|
167 | Sleep(1000); |
---|
168 | #endif |
---|
169 | i+=5; |
---|
170 | } |
---|
171 | setOutputValue(outputs,"Result","\"Long process run successfully\"",-1); |
---|
172 | return SERVICE_SUCCEEDED; |
---|
173 | } |
---|
174 | |
---|
175 | } |
---|