Blame view

src/Parameters/ServicesServer.cpp 7.02 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
/*
 * ServicesServer.cpp
 *
 *  Created on: Nov 12, 2012
 *      Author: g.schneller
 */

#include "ServicesServer.hh"
a851633d   Hacene SI HADJ MOHAND   xml generated
9
#include <libxml/xmlwriter.h>
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
10
11
#include "PluginsParser.hh" 
#include "PluginManager.hh"
377eb96d   Hacene SI HADJ MOHAND   ok with xmls
12
13
14
#include <iostream>
#include <sstream>
#include <fstream>
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
15

fbe3c2bb   Benjamin Renard   First commit
16

a93672f8   Hacene SI HADJ MOHAND   charement xml ok
17

fbe3c2bb   Benjamin Renard   First commit
18
namespace AMDA {
a851633d   Hacene SI HADJ MOHAND   xml generated
19
    namespace Parameters {
fbe3c2bb   Benjamin Renard   First commit
20

b29746d5   Hacene SI HADJ MOHAND   us ok
21
22
        log4cxx::LoggerPtr ServicesServer::_logger(log4cxx::Logger::getLogger("AMDA-Kernel.ServicesServer"));

a851633d   Hacene SI HADJ MOHAND   xml generated
23
24
        ServicesServer::ServicesServer() {
        }
fbe3c2bb   Benjamin Renard   First commit
25

a851633d   Hacene SI HADJ MOHAND   xml generated
26
27
        ServicesServer::~ServicesServer() {
        }
fbe3c2bb   Benjamin Renard   First commit
28

fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
29
30
        bool ServicesServer::_PLUGINS_DYNAMIC_LOADING = false;

b29746d5   Hacene SI HADJ MOHAND   us ok
31
32
33
34
35
36
37
38
39
40
41
42
43
        /**
         * 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>
         */

a851633d   Hacene SI HADJ MOHAND   xml generated
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        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;
            }
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
65
66
67
68
69
            rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:id", BAD_CAST "plugins");
            if (rc < 0) {
                xmlFreeTextWriter(writer);
                return false;
            }
a93672f8   Hacene SI HADJ MOHAND   charement xml ok
70
71

            for (auto plugMap : _processPluginMaps) {
a851633d   Hacene SI HADJ MOHAND   xml generated
72
                for (auto plug : plugMap.second) {
7262a05d   Hacene SI HADJ MOHAND   improving xml
73
                    rc = xmlTextWriterStartElement(writer, BAD_CAST "requestProcess");
a851633d   Hacene SI HADJ MOHAND   xml generated
74
75
76
77
                    if (rc < 0) {
                        xmlFreeTextWriter(writer);
                        return false;
                    }
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
78

a93672f8   Hacene SI HADJ MOHAND   charement xml ok
79
80
81
82
83
                    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "type", BAD_CAST plugMap.first.c_str());
                    if (rc < 0) {
                        xmlFreeTextWriter(writer);
                        return false;
                    }
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
84

a851633d   Hacene SI HADJ MOHAND   xml generated
85
86
87
88
89
90
91
92
93
94
                    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;
                    }
a851633d   Hacene SI HADJ MOHAND   xml generated
95

fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
96
97
98
99
100
101
102

                    // process node
                    rc = xmlTextWriterEndElement(writer);
                    if (rc < 0) {
                        xmlFreeTextWriter(writer);
                        return false;
                    }
a851633d   Hacene SI HADJ MOHAND   xml generated
103
                }
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
104

a851633d   Hacene SI HADJ MOHAND   xml generated
105
106
            }

7262a05d   Hacene SI HADJ MOHAND   improving xml
107
            // close plugins node
a851633d   Hacene SI HADJ MOHAND   xml generated
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
            rc = xmlTextWriterEndElement(writer);
            if (rc < 0) {
                xmlFreeTextWriter(writer);
                return false;
            }

            rc = xmlTextWriterEndDocument(writer);
            if (rc < 0) {
                xmlFreeTextWriter(writer);
                return false;
            }

            xmlFreeTextWriter(writer);

            return true;
        }

b29746d5   Hacene SI HADJ MOHAND   us ok
125
126
127
        /**
         * Thins function reads the xml plugin file and fills the plugin map 
         */
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
128
129
130
131
132
133
134
135
136
137
        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;
        }

b29746d5   Hacene SI HADJ MOHAND   us ok
138
139
140
        /**
         * this function laod plugins by plugin type : paramGet process ........
         */
90471645   Hacene SI HADJ MOHAND   working with params
141
142
        bool ServicesServer::loadPluginByType(const std::string& type) {
            if (_processPluginMaps.count(type) != 1) {
b29746d5   Hacene SI HADJ MOHAND   us ok
143
                LOG4CXX_WARN(_logger, "Unknown Plugin Type : " << type);
90471645   Hacene SI HADJ MOHAND   working with params
144
145
146
147
148
149
150
151
                return false;
            }
            for (auto plugMap : _processPluginMaps[type]) {
                loadPlugin(plugMap.first, type);
            }
            return true;
        }

b29746d5   Hacene SI HADJ MOHAND   us ok
152
153
154
        /**
         * this function laod plugin. type argument  is optional 
         */
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
155
156
157
158
159
160
161
162
163
164
165
        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;
                    }
                }
b29746d5   Hacene SI HADJ MOHAND   us ok
166
                LOG4CXX_WARN(_logger, "Unknown Plugin : " << process);
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
167
168
169
170
171
172
173
174
175
                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;
                }
b29746d5   Hacene SI HADJ MOHAND   us ok
176
                LOG4CXX_WARN(_logger, "Unknown Plugin : " << type << " " << process);
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
177
178
179
180
                return false;
            }
        }

b29746d5   Hacene SI HADJ MOHAND   us ok
181
182
183
        /*
         * this function laod needed outputs (download , plot ...) xplugin from the request file
         */
377eb96d   Hacene SI HADJ MOHAND   ok with xmls
184
185
186
187
        bool ServicesServer::loadPluginsFromRequest(const char* requestXML) {
            std::string line;
            std::ifstream infile(requestXML);
            while (std::getline(infile, line)) {
90471645   Hacene SI HADJ MOHAND   working with params
188
189
190
                for (std::string output : _outputs) {
                    if (line.find(output, 0) != std::string::npos) {
                        _pluginsToLoad.push_back(output.erase(0, 1));
90471645   Hacene SI HADJ MOHAND   working with params
191
                        return true;
377eb96d   Hacene SI HADJ MOHAND   ok with xmls
192
193
                    }
                }
9517e2ec   Hacene SI HADJ MOHAND   ok test ok
194
               
377eb96d   Hacene SI HADJ MOHAND   ok with xmls
195
            }
9517e2ec   Hacene SI HADJ MOHAND   ok test ok
196
197
198
            // if no outputs found dataMining is loaded
             _pluginsToLoad.push_back("dataMining");
            return true;
377eb96d   Hacene SI HADJ MOHAND   ok with xmls
199
200
        }

a851633d   Hacene SI HADJ MOHAND   xml generated
201
    } /* namespace Parameters */
fbe3c2bb   Benjamin Renard   First commit
202
} /* namespace AMDA */