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 | #ifdef WIN32 |
---|
27 | #include <windows.h> |
---|
28 | #endif |
---|
29 | |
---|
30 | extern "C" { |
---|
31 | #include <libxml/tree.h> |
---|
32 | #include <libxml/parser.h> |
---|
33 | #include <libxml/xpath.h> |
---|
34 | #include <libxml/xpathInternals.h> |
---|
35 | |
---|
36 | #include <libxslt/xslt.h> |
---|
37 | #include <libxslt/xsltInternals.h> |
---|
38 | #include <libxslt/transform.h> |
---|
39 | #include <libxslt/xsltutils.h> |
---|
40 | |
---|
41 | #include <dirent.h> |
---|
42 | #include "service_internal.h" |
---|
43 | |
---|
44 | /** |
---|
45 | * GetStatus ZOO Service : |
---|
46 | * This service is used in the ZOO-Project to get information about Services |
---|
47 | * running as background tasks. The service will first get the XML document |
---|
48 | * cached by the ZOO-Kernel before calling effectively the Service, then |
---|
49 | * will access the shared memory space created by the Kernel to extract the |
---|
50 | * current status of the running Service. Using a simple XSL file it will |
---|
51 | * finally produce the final ExecuteResponse including the updated |
---|
52 | * percentCompleted attribute of the ProcessStarted node of the cached |
---|
53 | * document if any (so if the Service is currently running) else it will |
---|
54 | * return the final ExecuteResponse stored on the Server file system. |
---|
55 | */ |
---|
56 | #ifdef WIN32 |
---|
57 | __declspec(dllexport) |
---|
58 | #endif |
---|
59 | int GetStatus(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
60 | const char *params[2 + 1]; |
---|
61 | int xmlLoadExtDtdDefaultValue; |
---|
62 | map* tmpMap=NULL,*tmpMmap=NULL, *tmpTmap=NULL; |
---|
63 | tmpMap=getMapFromMaps(inputs,"sid","value"); |
---|
64 | tmpTmap=getMapFromMaps(conf,"main","tmpPath"); |
---|
65 | tmpMmap=getMapFromMaps(conf,"main","dataPath"); |
---|
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,tmp)!=0){ |
---|
79 | sprintf(fileName,"%s/%s",tmpTmap->value,dp->d_name); |
---|
80 | hasFile=1; |
---|
81 | } |
---|
82 | } |
---|
83 | }else{ |
---|
84 | char tmp[1024]; |
---|
85 | snprintf(tmp,1024,_ss("GetStatus was unable to use the tmpPath value set in main.cfg file as directory %s."),tmpTmap->value); |
---|
86 | setMapInMaps(conf,"lenv","message",tmp); |
---|
87 | return SERVICE_FAILED; |
---|
88 | } |
---|
89 | if(hasFile<0){ |
---|
90 | char tmp[1024]; |
---|
91 | snprintf(tmp,1024,_ss("GetStatus was unable to find any cache file for Service ID %s."),tmpMap->value); |
---|
92 | setMapInMaps(conf,"lenv","message",tmp); |
---|
93 | return SERVICE_FAILED; |
---|
94 | } |
---|
95 | sprintf(xslFileName,"%s/updateStatus.xsl",tmpMmap->value); |
---|
96 | xmlSubstituteEntitiesDefault(1); |
---|
97 | xmlLoadExtDtdDefaultValue = 0; |
---|
98 | xsltStylesheetPtr cur = NULL; |
---|
99 | xmlDocPtr doc, res; |
---|
100 | cur = xsltParseStylesheetFile(BAD_CAST xslFileName); |
---|
101 | doc = xmlParseFile(fileName); |
---|
102 | if(cur!=NULL && doc!=NULL){ |
---|
103 | params[0]="value"; |
---|
104 | params[1]=getStatus(atoi(tmpMap->value)); |
---|
105 | params[2]=NULL; |
---|
106 | res = xsltApplyStylesheet(cur, doc, params); |
---|
107 | xmlChar *xmlbuff; |
---|
108 | int buffersize; |
---|
109 | xmlDocDumpFormatMemory(res, &xmlbuff, &buffersize, 1); |
---|
110 | setMapInMaps(outputs,"Result","value",(char*)xmlbuff); |
---|
111 | setMapInMaps(outputs,"Result","mimeType","text/xml"); |
---|
112 | setMapInMaps(outputs,"Result","encoding","UTF-8"); |
---|
113 | xmlFree(xmlbuff); |
---|
114 | } |
---|
115 | else{ |
---|
116 | char tmp[1024]; |
---|
117 | sprintf(tmp,_ss("ZOO GetStatus Service was unable to parse the cache xml file available for the Service ID %s."),tmpMap->value); |
---|
118 | setMapInMaps(conf,"lenv","message",tmp); |
---|
119 | return SERVICE_FAILED; |
---|
120 | } |
---|
121 | return SERVICE_SUCCEEDED; |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | /** |
---|
126 | * longProcess ZOO Service : |
---|
127 | * Simple Service which just loop over 100 times then return a welcome message |
---|
128 | * string, at each step the service will sleep for one second. |
---|
129 | */ |
---|
130 | #ifdef WIN32 |
---|
131 | __declspec(dllexport) |
---|
132 | #endif |
---|
133 | int longProcess(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
134 | int i=0; |
---|
135 | while(i<100){ |
---|
136 | char tmp[4]; |
---|
137 | sprintf(tmp,"%i",i); |
---|
138 | map* tmpMap=NULL; |
---|
139 | tmpMap=getMapFromMaps(conf,"lenv","sid"); |
---|
140 | if(tmpMap!=NULL) |
---|
141 | fprintf(stderr,"Status %s %s\n",tmpMap->value,tmp); |
---|
142 | setMapInMaps(conf,"lenv","status",tmp); |
---|
143 | updateStatus(conf); |
---|
144 | #ifndef WIN32 |
---|
145 | sleep(1); |
---|
146 | #else |
---|
147 | Sleep(1000); |
---|
148 | #endif |
---|
149 | i+=5; |
---|
150 | } |
---|
151 | setMapInMaps(outputs,"Result","value","\"Running long process successfully\""); |
---|
152 | return SERVICE_SUCCEEDED; |
---|
153 | } |
---|
154 | |
---|
155 | } |
---|