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[2 + 1]; |
---|
58 | int xmlLoadExtDtdDefaultValue; |
---|
59 | map* tmpMap=NULL,*tmpMmap=NULL, *tmpTmap=NULL; |
---|
60 | tmpMap=getMapFromMaps(inputs,"sid","value"); |
---|
61 | tmpTmap=getMapFromMaps(conf,"main","tmpPath"); |
---|
62 | tmpMmap=getMapFromMaps(conf,"main","dataPath"); |
---|
63 | xmlInitParser(); |
---|
64 | struct dirent *dp; |
---|
65 | DIR *dirp = opendir(tmpTmap->value); |
---|
66 | char fileName[1024],xslFileName[1024]; |
---|
67 | int hasFile=-1; |
---|
68 | if(dirp!=NULL){ |
---|
69 | char tmp[128]; |
---|
70 | sprintf(tmp,"_%s.xml",tmpMap->value); |
---|
71 | while ((dp = readdir(dirp)) != NULL){ |
---|
72 | #ifdef DEBUG |
---|
73 | fprintf(stderr,"File : %s searched : %s\n",dp->d_name,tmp); |
---|
74 | #endif |
---|
75 | if(strstr(dp->d_name,"final_")==0 && strstr(dp->d_name,tmp)!=0){ |
---|
76 | sprintf(fileName,"%s/%s",tmpTmap->value,dp->d_name); |
---|
77 | hasFile=1; |
---|
78 | break; |
---|
79 | } |
---|
80 | } |
---|
81 | }else{ |
---|
82 | char tmp[1024]; |
---|
83 | snprintf(tmp,1024,_ss("GetStatus was unable to use the tmpPath value set in main.cfg file as directory %s."),tmpTmap->value); |
---|
84 | setMapInMaps(conf,"lenv","message",tmp); |
---|
85 | return SERVICE_FAILED; |
---|
86 | } |
---|
87 | if(hasFile<0){ |
---|
88 | char tmp[1024]; |
---|
89 | snprintf(tmp,1024,_ss("GetStatus was unable to find any cache file for Service ID %s."),tmpMap->value); |
---|
90 | setMapInMaps(conf,"lenv","message",tmp); |
---|
91 | return SERVICE_FAILED; |
---|
92 | } |
---|
93 | sprintf(xslFileName,"%s/updateStatus.xsl",tmpMmap->value); |
---|
94 | xmlSubstituteEntitiesDefault(1); |
---|
95 | xmlLoadExtDtdDefaultValue = 0; |
---|
96 | xsltStylesheetPtr cur = NULL; |
---|
97 | xmlDocPtr doc, res; |
---|
98 | cur = xsltParseStylesheetFile(BAD_CAST xslFileName); |
---|
99 | doc = xmlParseFile(fileName); |
---|
100 | if(cur!=NULL && doc!=NULL){ |
---|
101 | params[0]="value"; |
---|
102 | params[1]=getStatus(atoi(tmpMap->value)); |
---|
103 | params[2]=NULL; |
---|
104 | res = xsltApplyStylesheet(cur, doc, params); |
---|
105 | xmlChar *xmlbuff; |
---|
106 | int buffersize; |
---|
107 | xmlDocDumpFormatMemory(res, &xmlbuff, &buffersize, 1); |
---|
108 | setMapInMaps(outputs,"Result","value",(char*)xmlbuff); |
---|
109 | xmlFree(xmlbuff); |
---|
110 | } |
---|
111 | else{ |
---|
112 | char tmp[1024]; |
---|
113 | sprintf(tmp,_ss("ZOO GetStatus Service was unable to parse the cache xml file available for the Service ID %s."),tmpMap->value); |
---|
114 | setMapInMaps(conf,"lenv","message",tmp); |
---|
115 | return SERVICE_FAILED; |
---|
116 | } |
---|
117 | return SERVICE_SUCCEEDED; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | /** |
---|
122 | * longProcess ZOO Service : |
---|
123 | * Simple Service which just loop over 100 times then return a welcome message |
---|
124 | * string, at each step the service will sleep for one second. |
---|
125 | */ |
---|
126 | #ifdef WIN32 |
---|
127 | __declspec(dllexport) |
---|
128 | #endif |
---|
129 | int longProcess(maps*& conf,maps*& inputs,maps*& outputs){ |
---|
130 | int i=0; |
---|
131 | while(i<100){ |
---|
132 | char tmp[4]; |
---|
133 | sprintf(tmp,"%i",i); |
---|
134 | map* tmpMap=NULL; |
---|
135 | tmpMap=getMapFromMaps(conf,"lenv","sid"); |
---|
136 | if(tmpMap!=NULL) |
---|
137 | fprintf(stderr,"Status %s %s\n",tmpMap->value,tmp); |
---|
138 | setMapInMaps(conf,"lenv","status",tmp); |
---|
139 | updateStatus(conf); |
---|
140 | #ifndef WIN32 |
---|
141 | sleep(1); |
---|
142 | #else |
---|
143 | Sleep(1000); |
---|
144 | #endif |
---|
145 | i+=5; |
---|
146 | } |
---|
147 | setMapInMaps(outputs,"Result","value","\"Running long process successfully\""); |
---|
148 | return SERVICE_SUCCEEDED; |
---|
149 | } |
---|
150 | |
---|
151 | } |
---|