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 | |
---|
26 | #include "service.h" |
---|
27 | #include "service_internal.h" |
---|
28 | |
---|
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 | extern "C" { |
---|
41 | |
---|
42 | /** |
---|
43 | * GetStatus ZOO Service : |
---|
44 | * This service is used in the ZOO-Project to get information about Services |
---|
45 | * running as background tasks. The service will first get the XML document |
---|
46 | * cached by the ZOO-Kernel before calling effectively the Service, then |
---|
47 | * will access the shared memory space created by the Kernel to extract the |
---|
48 | * current status of the running Service. Using a simple XSL file it will |
---|
49 | * finally produce the final ExecuteResponse including the updated |
---|
50 | * percentCompleted attribute of the ProcessStarted node of the cached |
---|
51 | * document if any (so if the Service is currently running) else it will |
---|
52 | * return the final ExecuteResponse stored on the Server file system. |
---|
53 | */ |
---|
54 | ZOO_DLL_EXPORT int GetStatus(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
55 | const char *params[4 + 1]; |
---|
56 | int xmlLoadExtDtdDefaultValue; |
---|
57 | map* tmpMap=NULL,*tmpMmap=NULL, *tmpTmap=NULL; |
---|
58 | |
---|
59 | tmpMap=getMapFromMaps(inputs,"sid","value"); |
---|
60 | tmpTmap=getMapFromMaps(conf,"main","tmpPath"); |
---|
61 | tmpMmap=getMapFromMaps(conf,"main","dataPath"); |
---|
62 | if(tmpMmap==NULL) |
---|
63 | tmpMmap=tmpTmap; |
---|
64 | xmlInitParser(); |
---|
65 | int hasFile=-1; |
---|
66 | char xslFileName[1024]; |
---|
67 | char* mem=_getStatusFile(conf,tmpMap->value); |
---|
68 | if(mem==NULL){ |
---|
69 | char tmp[1024]; |
---|
70 | snprintf(tmp,1024,_ss("GetStatus was unable to find any cache file for Service ID %s."),tmpMap->value); |
---|
71 | setMapInMaps(conf,"lenv","message",tmp); |
---|
72 | return SERVICE_FAILED; |
---|
73 | } |
---|
74 | map* tmpMap1=getMapFromMaps(conf,"lenv","semaphore"); |
---|
75 | if(tmpMap1!=NULL && strcasecmp(tmpMap1->value,"Created")==0) |
---|
76 | removeShmLock(conf,1); |
---|
77 | sprintf(xslFileName,"%s/updateStatus.xsl",tmpMmap->value); |
---|
78 | xmlSubstituteEntitiesDefault(1); |
---|
79 | xmlLoadExtDtdDefaultValue = 0; |
---|
80 | xsltStylesheetPtr cur = NULL; |
---|
81 | xmlDocPtr doc, res; |
---|
82 | cur = xsltParseStylesheetFile(BAD_CAST xslFileName); |
---|
83 | doc = xmlParseMemory(mem,strlen(mem)); |
---|
84 | //doc = xmlParseFile(fileName); |
---|
85 | if(cur!=NULL && doc!=NULL){ |
---|
86 | /** |
---|
87 | * Parse Status to extract Status / Message |
---|
88 | */ |
---|
89 | char *tmpStr=_getStatus(conf,tmpMap->value); |
---|
90 | //#ifdef DEBUG |
---|
91 | fprintf(stderr,"DEBUG: %s \n",tmpStr); |
---|
92 | //#endif |
---|
93 | if(tmpStr!=NULL && strncmp(tmpStr,"-1",2)!=0){ |
---|
94 | char *tmpStr1=strdup(tmpStr); |
---|
95 | char *tmpStr0=strdup(strstr(tmpStr,"|")+1); |
---|
96 | free(tmpStr); |
---|
97 | tmpStr1[strlen(tmpStr1)-strlen(tmpStr0)-1]='\0'; |
---|
98 | char *tmpStrFinal=(char*)malloc((strlen(tmpStr0)+11)*sizeof(char)); |
---|
99 | sprintf(tmpStrFinal,"string(\"%s\")",tmpStr0); |
---|
100 | params[0]="value"; |
---|
101 | params[1]=tmpStr1; |
---|
102 | params[2]="message"; |
---|
103 | params[3]=tmpStrFinal; |
---|
104 | params[4]=NULL; |
---|
105 | res = xsltApplyStylesheet(cur, doc, params); |
---|
106 | xmlChar *xmlbuff; |
---|
107 | int buffersize; |
---|
108 | xmlDocDumpFormatMemory(res, &xmlbuff, &buffersize, 1); |
---|
109 | setMapInMaps(outputs,"Result","value",(char*)xmlbuff); |
---|
110 | xmlFree(xmlbuff); |
---|
111 | free(tmpStr1); |
---|
112 | free(tmpStr0); |
---|
113 | free(tmpStrFinal); |
---|
114 | }else{ |
---|
115 | xmlChar *xmlbuff; |
---|
116 | int buffersize; |
---|
117 | xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1); |
---|
118 | setMapInMaps(outputs,"Result","value",(char*)xmlbuff); |
---|
119 | xmlFree(xmlbuff); |
---|
120 | } |
---|
121 | } |
---|
122 | else{ |
---|
123 | char tmp[1024]; |
---|
124 | sprintf(tmp,_ss("ZOO GetStatus Service was unable to parse the cache xml file available for the Service ID %s."),tmpMap->value); |
---|
125 | setMapInMaps(conf,"lenv","message",tmp); |
---|
126 | return SERVICE_FAILED; |
---|
127 | } |
---|
128 | return SERVICE_SUCCEEDED; |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | /** |
---|
133 | * longProcess ZOO Service : |
---|
134 | * Simple Service which just loop over 100 times then return a welcome message |
---|
135 | * string, at each step the service will sleep for one second. |
---|
136 | */ |
---|
137 | #ifdef WIN32 |
---|
138 | __declspec(dllexport) |
---|
139 | #endif |
---|
140 | int longProcess(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
141 | int i=0; |
---|
142 | while(i<100){ |
---|
143 | char message[10]; |
---|
144 | sprintf(message,"Step %d",i); |
---|
145 | fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__); |
---|
146 | fflush(stderr); |
---|
147 | updateStatus(conf,i,message); |
---|
148 | #ifndef WIN32 |
---|
149 | sleep(1); |
---|
150 | #else |
---|
151 | Sleep(1000); |
---|
152 | #endif |
---|
153 | i+=5; |
---|
154 | } |
---|
155 | setOutputValue(outputs,"Result",(char*)"\"Long process run successfully\"",-1); |
---|
156 | return SERVICE_SUCCEEDED; |
---|
157 | } |
---|
158 | |
---|
159 | } |
---|