Blame view

src/Parameters/ServicesServer.hh 11.8 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * ServicesServer.h
 *
 *  Created on: Nov 12, 2012
 *      Author: g.schneller
 */

#ifndef SERVICESSERVER_H_
#define SERVICESSERVER_H_


#include <map>
#include <string>
#include <typeinfo>
#include <list>
fbe3c2bb   Benjamin Renard   First commit
16
17
18
19
20
#include <boost/any.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/functional/factory.hpp>
#include <boost/shared_ptr.hpp>
137db606   Hacene SI HADJ MOHAND   working
21
#include <vector>
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
22
#include <string>
b29746d5   Hacene SI HADJ MOHAND   us ok
23
#include "log4cxx/logger.h"
fbe3c2bb   Benjamin Renard   First commit
24
25
26
27
28
29
30

#include "dsgpatt_Singleton.hh"
#include "Demangle.hh"

#include "AMDA_exception.hh"

namespace AMDA {
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
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
    namespace Parameters {

        //use class
        class ServicesServer;
        class ParamGet;
        class Process;
        class ParamOutput;
        class Parameter;
        class StatisticProcess;

        typedef boost::shared_ptr<ServicesServer> ServicesServerSPtr;

        // ParamGet factory definition
        typedef boost::function < ParamGet*(Parameter &) > ParamGetFactory;
        typedef std::map<std::string, ParamGetFactory> ParamGetFactories;

        // Process factory definition
        typedef boost::function < Process*(Parameter &) > ProcessFactory;
        typedef std::map<std::string, ProcessFactory> ProcessFactories;

        // ParamOutput factory definition
        typedef boost::function < ParamOutput*() > ParamOutputFactory;
        typedef std::map<std::string, ParamOutputFactory> ParamOutputFactories;

        // StatisticProcess factory definition
        typedef boost::function < StatisticProcess*(Parameter &, const int&) > StatisticProcessFactory;
        typedef std::map<std::string, StatisticProcessFactory> StatisticProcessFactories;

137db606   Hacene SI HADJ MOHAND   working
59
60
61
        // map to link  process with pluging
        typedef std::map<std::string, std::map<std::string, std::string>> AMDA_ProcessPluginMaps;

bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
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
        class FileConfigurator;
        typedef boost::shared_ptr<FileConfigurator> ParameterConfiguratorSPtr;

        /**
         * @class ServicesServer
         * @brief To call to register needed Services or to notify implemented Services
         * @details This Singleton class is frequently called into the registerPlugin method to notify one or more Services implementation as Process, ParamGet and ParamOutput.
         */
        class ServicesServer : public ::Singleton<ServicesServer> {
        public:
            // Design pattern
            friend class Singleton<ServicesServer>;
            typedef std::list<boost::any> ServiceList;

            struct exception : virtual AMDA_exception {
            };

            /**
             *  @brief Add a Service implementation
             */
            template<typename T>
            void addService(T v) {
                _serviceList.push_back(v);
            }

            /**
             *  @brief Add a Service implementation
             */
            template<typename T>
            void declareService() {
                typedef typename std::map<std::string, T> TServiceImpList;
                for (ServiceList::const_reverse_iterator rit = _serviceList.rbegin(); rit != _serviceList.rend(); ++rit) {
                    if ((*rit).type() == typeid (TServiceImpList)) {
                        std::stringstream oss;
                        oss << "Service already declared: " << Helpers::Demangle(typeid (T).name());
                        BOOST_THROW_EXCEPTION(exception() << AMDA::ex_msg(oss.str()));
                    }
                }
                _serviceList.push_back(TServiceImpList());
            }

            /**
             *  @brief Add a Service implementation
             */
            template<typename T>
            void addServiceImpl(std::string name, T t) {
                typedef typename std::map<std::string, T> TServiceImpList;
                ServiceList::iterator it = _serviceList.begin();
                for (; it != _serviceList.end(); ++it) {
                    if ((*it).type() == typeid (TServiceImpList)) {
                        TServiceImpList& list = boost::any_cast<TServiceImpList&>(*it);
                        list.insert(make_pair(name, t));
                        break;
                    }
                }
                if (it == _serviceList.end()) {
                    _serviceList.push_back(TServiceImpList());
                    ServiceList::iterator it = _serviceList.end();
                    TServiceImpList& list = boost::any_cast<TServiceImpList&>(*(--it));
                    list.insert(make_pair(name, t));
                }
            }

            /**
             * @brief Search a Service implementation by this type.
             * @return Service or NULL if not found.
             */
            template<typename T>
            T getService() const {
                for (ServiceList::const_reverse_iterator rit = _serviceList.rbegin(); rit != _serviceList.rend(); ++rit) {
                    if ((*rit).type() == typeid (T)) {
                        return boost::any_cast<T>(*rit);
                    }
                }
                return NULL;
            }

            /**
             * @brief Search a Service implementation by this type.
             * @return Service or NULL if not found.
             */
            template<typename T>
9517e2ec   Hacene SI HADJ MOHAND   ok test ok
144
145
146
            T getService(const std::string& pImplName){
                if (_PLUGINS_DYNAMIC_LOADING)
                         loadPlugin(pImplName);
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
                typedef typename std::map<std::string, T> TServiceImpList;
                for (ServiceList::const_reverse_iterator rit = _serviceList.rbegin(); rit != _serviceList.rend(); ++rit) {
                    if ((*rit).type() == typeid (TServiceImpList)) {
                        TServiceImpList list = boost::any_cast<TServiceImpList>(*rit);
                        typename TServiceImpList::iterator it = list.find(pImplName);
                        if (it != list.end()) {
                            return it->second;
                        } else {
                            return NULL;
                        }
                    }
                }
                return NULL;
            }

            ParamOutput* getParamOutput(std::string paramOutputId) {
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
163
                if (_PLUGINS_DYNAMIC_LOADING)
9517e2ec   Hacene SI HADJ MOHAND   ok test ok
164
                    loadPlugin(paramOutputId, "paramOutputs");
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
165
166
167
168
169
170
171
172
                return _paramOutputFactory[paramOutputId]();
            }

            void addParamOutputFactory(std::string paramOutputId, ParamOutputFactory paramOutputFactory) {
                _paramOutputFactory[paramOutputId] = paramOutputFactory;
            }

            StatisticProcess* getStatisticProcess(std::string processId, Parameter &parameter, int index) {
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
173
174
                if (_PLUGINS_DYNAMIC_LOADING)
                    loadPlugin(processId, "statisticProcess");
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
175
176
177
178
179
180
181
182
                return _statisticProcessFactory[processId](parameter, index);
            }

            void addStatisticProcessFactory(std::string processId, StatisticProcessFactory statisticProcessFactory) {
                _statisticProcessFactory[processId] = statisticProcessFactory;
            }

            ParamGet* getParamGet(std::string paramGetId, Parameter &parameter) {
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
183
184
185
186
187
188
189
190
                return _paramGetFactory[paramGetId](parameter);
            }

            void addParamGetFactory(std::string paramGetId, ParamGetFactory paramGetFactory) {
                _paramGetFactory[paramGetId] = paramGetFactory;
            }

            Process* getProcess(std::string processId, Parameter &parameter) {
b29746d5   Hacene SI HADJ MOHAND   us ok
191
                if (_PLUGINS_DYNAMIC_LOADING)
9517e2ec   Hacene SI HADJ MOHAND   ok test ok
192
                    loadPlugin(processId, "process");
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
193
194
195
196
                auto lIt = _processFactory.find(processId);
                return (lIt == _processFactory.end()) ? nullptr : lIt->second(parameter);
            }

65f64dfe   Benjamin Renard   Fix bugs with cor...
197
            void addProcessFactory(std::string processId, ProcessFactory processFactory, bool injectResamplingBeforeProcess = true) {
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
198
                _processFactory[processId] = processFactory;
65f64dfe   Benjamin Renard   Fix bugs with cor...
199
200
201
202
203
                _injectResamplingBeforeProcess[processId] = injectResamplingBeforeProcess;
            }

            bool getInjectResamplingBeforeProcess(std::string processId) {
                auto lIt = _injectResamplingBeforeProcess.find(processId);
65f64dfe   Benjamin Renard   Fix bugs with cor...
204
                return (lIt == _injectResamplingBeforeProcess.end()) ? true : lIt->second;
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
205
206
207
208
209
210
211
212
213
214
215
216
217
218
            }

            // Get methods

            ParameterConfiguratorSPtr& getConfigurator() {
                return _configurator;
            }

            // Set methods

            void setConfigurator(ParameterConfiguratorSPtr &configurator) {
                _configurator = configurator;
            }

377eb96d   Hacene SI HADJ MOHAND   ok with xmls
219
            // link Process with the corresponding Plugin
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
220

3caf58ee   Hacene SI HADJ MOHAND   compil mais march...
221
            void linkProcessWithPlugin(std::string process, std::string pluginPath) {
137db606   Hacene SI HADJ MOHAND   working
222
223
                if (pluginPath != "")
                    _processPluginMaps["process"].insert(std::pair<std::string, std::string>(process, pluginPath));
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
224
225
            }

3caf58ee   Hacene SI HADJ MOHAND   compil mais march...
226
            void linkStatisticProcessWithPlugin(std::string process, std::string pluginPath) {
137db606   Hacene SI HADJ MOHAND   working
227
228
                if (pluginPath != "")
                    _processPluginMaps["statisticProcess"].insert(std::pair<std::string, std::string>(process, pluginPath));
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
229
230
            }

3caf58ee   Hacene SI HADJ MOHAND   compil mais march...
231
            void linkParamOutputsWithPlugin(std::string process, std::string pluginPath) {
fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
232
                if (pluginPath != "")
137db606   Hacene SI HADJ MOHAND   working
233
                    _processPluginMaps["paramOutputs"].insert(std::pair<std::string, std::string>(process, pluginPath));
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
234
235
            }

3caf58ee   Hacene SI HADJ MOHAND   compil mais march...
236
            void linkParamGetWithPlugin(std::string process, std::string pluginPath) {
137db606   Hacene SI HADJ MOHAND   working
237
                _processPluginMaps["paramGet"].insert(std::pair<std::string, std::string>(process, pluginPath));
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
238
239
            }

fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
240
241
242
243
244
245
246
247
248
249
250
251
252
            bool generatePluginsXml(const char* filePath);

            bool fillPluginsMap(std::string xmlFile, std::string xsdFile);

            void setPluginsDynamicLoading(bool isPluginsDynamicLoading) {
                _PLUGINS_DYNAMIC_LOADING = isPluginsDynamicLoading;
            }

            bool getPluginsDynamicLoading() {
                return _PLUGINS_DYNAMIC_LOADING;
            }

            bool loadPlugin(const std::string& process, const std::string& type = "");
90471645   Hacene SI HADJ MOHAND   working with params
253
254
255

            bool loadPluginByType(const std::string& type);

377eb96d   Hacene SI HADJ MOHAND   ok with xmls
256
            bool loadPluginsFromRequest(const char* requestXML);
90471645   Hacene SI HADJ MOHAND   working with params
257
258

            std::list<std::string> getPluginsToLoad() {
377eb96d   Hacene SI HADJ MOHAND   ok with xmls
259
260
                return _pluginsToLoad;
            }
137db606   Hacene SI HADJ MOHAND   working
261

bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
262
263
264
265
266
267
268
269
270
271
272
        private:

            ServicesServer();
            virtual ~ServicesServer();

            /**
             * Factory of ParamGet
             * for a string are a sourceParmGet, use
             * @code ParamGet p = _paramGetFactory[souceParamGet]()
             */
            ParamGetFactories _paramGetFactory;
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
273
274
275
276
277
278
279

            /**
             * Factory of Process
             * for a string are a sourceProcess, use
             * @code Process p = _processFactory[souceProcess]()
             */
            ProcessFactories _processFactory;
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
280

65f64dfe   Benjamin Renard   Fix bugs with cor...
281
282
            std::map<std::string, bool> _injectResamplingBeforeProcess;

bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
283
284
285
286
287
288
            /**
             * Factory of ParamOutput
             * for a string are a sourceParmOutput, use
             * @code ParamOutput p = _paramOutputFactory[souceParamGet]()
             */
            ParamOutputFactories _paramOutputFactory;
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
289
290
291
292
293

            /**
             * Factory of StatisticProcess
             */
            StatisticProcessFactories _statisticProcessFactory;
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
294
295
296
297

            ParameterConfiguratorSPtr _configurator;

            /**
137db606   Hacene SI HADJ MOHAND   working
298
299
300
301
302
             * Map of processes and corresponding plugins  
             */
            AMDA_ProcessPluginMaps _processPluginMaps;

            /**
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
303
304
305
306
             * List of services.
             */
            ServiceList _serviceList;

fe04cd0c   Hacene SI HADJ MOHAND   dynamic impliment...
307
            static bool _PLUGINS_DYNAMIC_LOADING;
90471645   Hacene SI HADJ MOHAND   working with params
308

377eb96d   Hacene SI HADJ MOHAND   ok with xmls
309
            std::list<std::string> _pluginsToLoad;
90471645   Hacene SI HADJ MOHAND   working with params
310

9517e2ec   Hacene SI HADJ MOHAND   ok test ok
311
312
            std::list<std::string> _outputs{"<download", "<dataMining", "<plot", "<statistic", "<int:ervalTrue", "<intervalTrue"};

b29746d5   Hacene SI HADJ MOHAND   us ok
313
            static log4cxx::LoggerPtr _logger;
bf54b7d4   Hacene SI HADJ MOHAND   not woring yet
314
315
316
        };

    } /* namespace Parameters */
fbe3c2bb   Benjamin Renard   First commit
317
318
} /* namespace AMDA */
#endif /* SERVICESSERVER_H_ */