ServicesServer.cpp
7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* ServicesServer.cpp
*
* Created on: Nov 12, 2012
* Author: g.schneller
*/
#include "ServicesServer.hh"
#include <libxml/xmlwriter.h>
#include "PluginsParser.hh"
#include "PluginManager.hh"
#include <iostream>
#include <sstream>
#include <fstream>
namespace AMDA {
namespace Parameters {
log4cxx::LoggerPtr ServicesServer::_logger(log4cxx::Logger::getLogger("AMDA-Kernel.ServicesServer"));
ServicesServer::ServicesServer() {
}
ServicesServer::~ServicesServer() {
}
bool ServicesServer::_PLUGINS_DYNAMIC_LOADING = false;
/**
* This function generates plugin xml that contains all process and corresponding plugin paths
<plugins xml:id="plugins">
<requestProcess type="paramGet" name="CONSTANT">
<plugin>./../build/Debug/plugin//libConstantInterface.so</plugin>
</requestProcess>
<requestProcess type="paramGet" name="DDBASE">
<plugin>./../build/Debug/plugin//libDDServerInterface.so</plugin>
</requestProcess>
.........
</plugins>
*/
bool ServicesServer::generatePluginsXml(const char* filePath) {
LIBXML_TEST_VERSION
xmlTextWriterPtr writer = xmlNewTextWriterFilename(filePath, 0);
if (writer == NULL) {
return false;
}
int rc;
rc = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL);
if (rc < 0) {
xmlFreeTextWriter(writer);
return false;
}
rc = xmlTextWriterStartElement(writer, BAD_CAST "plugins");
if (rc < 0) {
xmlFreeTextWriter(writer);
return false;
}
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:id", BAD_CAST "plugins");
if (rc < 0) {
xmlFreeTextWriter(writer);
return false;
}
for (auto plugMap : _processPluginMaps) {
for (auto plug : plugMap.second) {
rc = xmlTextWriterStartElement(writer, BAD_CAST "requestProcess");
if (rc < 0) {
xmlFreeTextWriter(writer);
return false;
}
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "type", BAD_CAST plugMap.first.c_str());
if (rc < 0) {
xmlFreeTextWriter(writer);
return false;
}
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST plug.first.c_str());
if (rc < 0) {
xmlFreeTextWriter(writer);
return false;
}
rc = xmlTextWriterWriteElement(writer, BAD_CAST "plugin", BAD_CAST plug.second.c_str());
if (rc < 0) {
xmlFreeTextWriter(writer);
return false;
}
// process node
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
xmlFreeTextWriter(writer);
return false;
}
}
}
// close plugins node
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
xmlFreeTextWriter(writer);
return false;
}
rc = xmlTextWriterEndDocument(writer);
if (rc < 0) {
xmlFreeTextWriter(writer);
return false;
}
xmlFreeTextWriter(writer);
return true;
}
/**
* Thins function reads the xml plugin file and fills the plugin map
*/
bool ServicesServer::fillPluginsMap(std::string xmlFile, std::string xsdFile) {
try {
AMDA::Parameters::PluginsParser parser(xsdFile.c_str());
_processPluginMaps = parser.parse(xmlFile.c_str());
} catch (...) {
throw;
}
return true;
}
/**
* this function laod plugins by plugin type : paramGet process ........
*/
bool ServicesServer::loadPluginByType(const std::string& type) {
if (_processPluginMaps.count(type) != 1) {
LOG4CXX_WARN(_logger, "Unknown Plugin Type : " << type);
return false;
}
for (auto plugMap : _processPluginMaps[type]) {
loadPlugin(plugMap.first, type);
}
return true;
}
/**
* this function laod plugin. type argument is optional
*/
bool ServicesServer::loadPlugin(const std::string& process, const std::string& type) {
if (type == "") {
for (auto plugMap : _processPluginMaps) {
if (plugMap.second.count(process) == 1) {
unsigned found = plugMap.second[process].find_last_of("/\\");
std::string path = plugMap.second[process].substr(0, found);
std::string file = plugMap.second[process].substr(found + 1);
AMDA::Plugins::PluginManager::getInstance()->loadPlugin(path.c_str(), file.c_str());
return true;
}
}
LOG4CXX_WARN(_logger, "Unknown Plugin : " << process);
return false;
} else {
if (_processPluginMaps.count(type) == 1 && _processPluginMaps[type].count(process) == 1) {
unsigned found = _processPluginMaps[type][process].find_last_of("/\\");
std::string path = _processPluginMaps[type][process].substr(0, found);
std::string file = _processPluginMaps[type][process].substr(found + 1);
AMDA::Plugins::PluginManager::getInstance()->loadPlugin(path.c_str(), file.c_str());
return true;
}
LOG4CXX_WARN(_logger, "Unknown Plugin : " << type << " " << process);
return false;
}
}
/*
* this function laod needed outputs (download , plot ...) xplugin from the request file
*/
bool ServicesServer::loadPluginsFromRequest(const char* requestXML) {
std::string line;
std::ifstream infile(requestXML);
while (std::getline(infile, line)) {
for (std::string output : _outputs) {
if (line.find(output, 0) != std::string::npos) {
_pluginsToLoad.push_back(output.erase(0, 1));
return true;
}
}
}
// if no outputs found dataMining is loaded
_pluginsToLoad.push_back("dataMining");
return true;
}
} /* namespace Parameters */
} /* namespace AMDA */