Directory: | ./ |
---|---|
File: | src/PAbstractControlManager.h |
Date: | 2024-12-09 11:00:39 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 62 | 68 | 91.2% |
Branches: | 42 | 52 | 80.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #ifndef __PABSTRACTCONTROLMANAGER_H__ | ||
8 | #define __PABSTRACTCONTROLMANAGER_H__ | ||
9 | |||
10 | #include "PAbstractConnector.h" | ||
11 | |||
12 | ///@brief Abtract class which defines Control manager with arbitrary backend | ||
13 | template<typename _TConnectorKey, typename _TBackend, typename _TMockBackend> | ||
14 | class PAbstractControlManager{ | ||
15 | public: | ||
16 | PAbstractControlManager(ConnectorMode::ConnectorMode mode = ConnectorMode::NO_MOCK, const typename _TBackend::MgrParam & mgrParam = _TBackend::defaultMgrParam(), const typename _TMockBackend::Param & mockParam = _TMockBackend::defaultParam(), const typename _TMockBackend::MgrParam & mockMgrParam = _TMockBackend::defaultMgrParam()); | ||
17 | virtual ~PAbstractControlManager(); | ||
18 | |||
19 | bool addClientConnector(const _TConnectorKey & name, const std::string & hostName, const typename _TBackend::Param & param); | ||
20 | bool addServerConnector(const _TConnectorKey & name, const std::string & hostName, const typename _TBackend::Param & param); | ||
21 | |||
22 | void removeConnector(const _TConnectorKey & name); | ||
23 | void clearConnector(); | ||
24 | void clear(); | ||
25 | |||
26 | bool registerVar(const _TConnectorKey & name, const PComposeVar & composeVar); | ||
27 | bool registerCall(const _TConnectorKey & name, const PFunctionPrototype & prototype); | ||
28 | |||
29 | ///Register a variable | ||
30 | /** @param name : name of the connector to be used | ||
31 | * @param varName : name of the variable to be used | ||
32 | * @return true on success, false otherwise | ||
33 | */ | ||
34 | template<typename T> | ||
35 | 1 | bool registerVar(const _TConnectorKey & name, const std::string & varName){ | |
36 |
1/1✓ Branch 2 taken 1 times.
|
1 | return registerVar(name, phoenix_getComposeVar<T>(varName)); |
37 | } | ||
38 | |||
39 | ///Register a method to be called | ||
40 | /** @param name : name of the connector to be used | ||
41 | * @param functionName : name of the method to be used | ||
42 | * @param __f : C++ protoype of the function | ||
43 | * @return true on success, false otherwise | ||
44 | */ | ||
45 | template<typename _Callable> | ||
46 | 1 | bool registerCall(const _TConnectorKey & name, const std::string & functionName, _Callable&& __f){ | |
47 |
1/1✓ Branch 2 taken 1 times.
|
1 | return registerCall(name, phoenix_getPrototype(functionName, __f)); |
48 | } | ||
49 | |||
50 | |||
51 | bool pushMsg(const _TConnectorKey & name, const std::string & varName, const typename _TBackend::Message & msg); | ||
52 | |||
53 | ///Push data to a remote server | ||
54 | /** @param name : name of the connector to be used | ||
55 | * @param varName : name of the variable to be pused | ||
56 | * @param value : value to be pused | ||
57 | * @return true on success, false otherwise | ||
58 | */ | ||
59 | template<typename T> | ||
60 | 73 | bool pushData(const _TConnectorKey & name, const std::string & varName, const T & value){ | |
61 | 73 | PAbstractConnector<_TBackend, _TMockBackend> * connector = getConnector(name); | |
62 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 1 times.
|
73 | if(connector != NULL){ |
63 | 72 | return connector->pushData(varName, value); | |
64 | }else{ | ||
65 | 1 | return false; | |
66 | } | ||
67 | } | ||
68 | |||
69 | bool pullMsg(const _TConnectorKey & name, const std::string & varName, typename _TBackend::Message & msg); | ||
70 | |||
71 | ///Pull data from a remote server | ||
72 | /** @param name : name of the connector to be used | ||
73 | * @param varName : name of the variable to be pused | ||
74 | * @param value : value to be pulled | ||
75 | * @return true on success, false otherwise | ||
76 | */ | ||
77 | template<typename T> | ||
78 | 30 | bool pullData(const _TConnectorKey & name, const std::string & varName, T & value){ | |
79 | 30 | PAbstractConnector<_TBackend, _TMockBackend> * connector = getConnector(name); | |
80 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if(connector != NULL){ |
81 | 30 | return connector->pullData(varName, value); | |
82 | }else{ | ||
83 | ✗ | return false; | |
84 | } | ||
85 | } | ||
86 | bool call(const _TConnectorKey & name, PFunctionCall & methodCall); | ||
87 | |||
88 | ///Call a remote method | ||
89 | /** @param name : name of the connector to be used | ||
90 | * @param functionName : name of the method to be called | ||
91 | * @param __args : arguments to call the method | ||
92 | * @return true on success, false otherwise | ||
93 | */ | ||
94 | template<typename... _Args> | ||
95 | 20 | bool call(const _TConnectorKey & name, const std::string & functionName, _Args&&... __args){ | |
96 | 20 | PAbstractConnector<_TBackend, _TMockBackend> * connector = getConnector(name); | |
97 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if(connector != NULL){ |
98 |
1/1✓ Branch 1 taken 20 times.
|
20 | PFunctionCall methodCall; |
99 |
1/1✓ Branch 1 taken 20 times.
|
20 | phoenix_serialiseCallParam(methodCall, functionName, __args...); |
100 |
1/1✓ Branch 1 taken 20 times.
|
20 | return connector->call(methodCall); |
101 | 20 | }else{ | |
102 | ✗ | return false; | |
103 | } | ||
104 | } | ||
105 | |||
106 | void removeInternalVar(const std::string & varName); | ||
107 | void clearAllInternalVar(); | ||
108 | |||
109 | ///Register a local variable | ||
110 | /** @param varName : name of the variable | ||
111 | * @param defaultValue : default value of the variable | ||
112 | * @return true on success, false otherwise | ||
113 | */ | ||
114 | template<typename T> | ||
115 | 2 | bool registerLocalVar(const std::string & varName, const T & defaultValue){ | |
116 |
1/1✓ Branch 2 taken 2 times.
|
2 | PAbstractInternalVar<T, _TBackend, _TMockBackend> * var = new PAbstractInternalVar<T, _TBackend, _TMockBackend>(defaultValue, varName, p_mode, p_mockParam); |
117 | 2 | bool b(true); | |
118 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if(var != NULL){ |
119 | 2 | p_mapInternalVar[varName] = (PGenericInternalVar<_TBackend, _TMockBackend>*)var; | |
120 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if(p_mode != ConnectorMode::MOCK){ //Let's register the local variable in backend |
121 | 2 | b = _TBackend::registerLocalVar(p_manager, var->getComposeVar()); | |
122 | } | ||
123 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if(p_mode != ConnectorMode::NO_MOCK){ //Let's register the local variable in mock backend |
124 | 1 | b = _TMockBackend::registerLocalVar(p_mockManager, var->getComposeVar()); | |
125 | } | ||
126 | } | ||
127 | 2 | return b; | |
128 | } | ||
129 | |||
130 | ///Read the value of a local variable | ||
131 | /** @param varName : name of the variable | ||
132 | * @param[out] value : value of the variable | ||
133 | * @return true on success, false otherwise | ||
134 | */ | ||
135 | template<typename T> | ||
136 | 20 | bool getLocalVar(const std::string & varName, T & value){ | |
137 | //Let's check if the varName does exist | ||
138 |
1/1✓ Branch 1 taken 20 times.
|
20 | typename std::map<std::string, PGenericInternalVar<_TBackend, _TMockBackend>* >::iterator it(p_mapInternalVar.find(varName)); |
139 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
20 | if(it == p_mapInternalVar.end()){ |
140 | //TODO : we should call an error method in the backend to propagate the error properly | ||
141 | ✗ | return false; | |
142 | } | ||
143 | 20 | const PComposeVar & localVar =it->second->getComposeVar(); | |
144 | //Here the variable name is found, so we will check its type | ||
145 |
1/1✓ Branch 1 taken 20 times.
|
20 | PComposeVar inputVar = phoenix_getComposeVar<T>(varName); |
146 |
5/6✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✓ Branch 8 taken 20 times.
✓ Branch 11 taken 20 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 20 times.
|
20 | if(!checkValue("getLocalVar : wrong type", inputVar.getType(), localVar.getType())){ |
147 | ✗ | return false; | |
148 | } | ||
149 | 20 | PAbstractInternalVar<T, _TBackend, _TMockBackend>* var = (PAbstractInternalVar<T, _TBackend, _TMockBackend>*)it->second; | |
150 | 20 | value = var->getValue(); | |
151 | 20 | bool b(true); | |
152 |
1/1✓ Branch 1 taken 20 times.
|
20 | var->saveValueToComposeVar(); |
153 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if(p_mode != ConnectorMode::MOCK){ //Let's register the local variable in backend |
154 |
1/1✓ Branch 2 taken 20 times.
|
20 | b &= _TBackend::getLocalVar(p_manager, var->getComposeVar()); |
155 | } | ||
156 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if(p_mode != ConnectorMode::NO_MOCK){ //Let's register the local variable in backend |
157 |
1/1✓ Branch 2 taken 10 times.
|
10 | b &= _TMockBackend::getLocalVar(p_mockManager, var->getComposeVar()); |
158 | } | ||
159 | 20 | return b; | |
160 | 20 | } | |
161 | |||
162 | ///Set the value of a local variable | ||
163 | /** @param varName : name of the variable | ||
164 | * @param value : value of the variable | ||
165 | * @return true on success, false otherwise | ||
166 | */ | ||
167 | template<typename T> | ||
168 | 20 | bool setLocalVar(const std::string & varName, const T & value){ | |
169 | //Let's check if the varName does exist | ||
170 |
1/1✓ Branch 1 taken 20 times.
|
20 | typename std::map<std::string, PGenericInternalVar<_TBackend, _TMockBackend>* >::iterator it(p_mapInternalVar.find(varName)); |
171 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
20 | if(it == p_mapInternalVar.end()){ |
172 | //TODO : we should call an error method in the backend to propagate the error properly | ||
173 | ✗ | return false; | |
174 | } | ||
175 | 20 | const PComposeVar & localVar =it->second->getComposeVar(); | |
176 | //Here the variable name is found, so we will check its type | ||
177 |
1/1✓ Branch 1 taken 20 times.
|
20 | PComposeVar inputVar = phoenix_getComposeVar<T>(varName); |
178 |
5/6✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✓ Branch 8 taken 20 times.
✓ Branch 11 taken 20 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 20 times.
|
20 | if(!checkValue("setLocalVar : wrong type", inputVar.getType(), localVar.getType())){ |
179 | ✗ | return false; | |
180 | } | ||
181 | 20 | PAbstractInternalVar<T, _TBackend, _TMockBackend>* var = (PAbstractInternalVar<T, _TBackend, _TMockBackend>*)it->second; | |
182 | 20 | var->setValue(value); | |
183 |
1/1✓ Branch 1 taken 20 times.
|
20 | var->saveValueToComposeVar(); |
184 | 20 | bool b(true); | |
185 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if(p_mode != ConnectorMode::MOCK){ //Let's register the local variable in backend |
186 |
1/1✓ Branch 2 taken 20 times.
|
20 | b &= _TBackend::setLocalVar(p_manager, var->getComposeVar()); |
187 | } | ||
188 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if(p_mode != ConnectorMode::NO_MOCK){ //Let's register the local variable in backend |
189 |
1/1✓ Branch 2 taken 10 times.
|
10 | b &= _TMockBackend::setLocalVar(p_mockManager, var->getComposeVar()); |
190 | } | ||
191 | 20 | return b; | |
192 | 20 | } | |
193 | |||
194 | PAbstractConnector<_TBackend, _TMockBackend>* getConnector(const _TConnectorKey & name); | ||
195 | bool isConnectorExist(const _TConnectorKey & name) const; | ||
196 | |||
197 | private: | ||
198 | void initialisationPAbstractControlManager(ConnectorMode::ConnectorMode mode, const typename _TBackend::MgrParam & mgrParam, const typename _TMockBackend::Param & mockParam, const typename _TMockBackend::MgrParam & mockMgrParam); | ||
199 | |||
200 | ///Mode of the Connector (no mock, mock, mock_record) | ||
201 | ConnectorMode::ConnectorMode p_mode; | ||
202 | ///Parameters of the mock backend | ||
203 | typename _TMockBackend::Param p_mockParam; | ||
204 | |||
205 | ///Map of the connector to be used by the manager | ||
206 | std::map<_TConnectorKey, PAbstractConnector<_TBackend, _TMockBackend> *> p_mapConnector; | ||
207 | ///Map of internal variables (will be allocated by PAbstractConnectorManager) | ||
208 | std::map<std::string, PGenericInternalVar<_TBackend, _TMockBackend>* > p_mapInternalVar; | ||
209 | |||
210 | ///Data of the manager | ||
211 | typename _TBackend::Manager p_manager; | ||
212 | ///Data of the mock manager | ||
213 | typename _TMockBackend::Manager p_mockManager; | ||
214 | }; | ||
215 | |||
216 | #include "PAbstractControlManager_impl.h" | ||
217 | |||
218 | |||
219 | #endif | ||
220 | |||
221 |