napa-baselibs / monl / measure_manager.h @ 5f3adef4
History | View | Annotate | Download (8.51 KB)
1 | 956892f0 | ArpadBakay | /***************************************************************************
|
---|---|---|---|
2 | * Copyright (C) 2009 by Robert Birke
|
||
3 | * robert.birke@polito.it
|
||
4 | *
|
||
5 | * This library is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU Lesser General Public
|
||
7 | * License as published by the Free Software Foundation; either
|
||
8 | * version 2.1 of the License, or (at your option) any later version.
|
||
9 | |||
10 | * This library is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
13 | * Lesser General Public License for more details.
|
||
14 | |||
15 | * You should have received a copy of the GNU Lesser General Public
|
||
16 | * License along with this library; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
18 | ***********************************************************************/
|
||
19 | |||
20 | #ifndef _MEASURE_MANAGER_H
|
||
21 | #define _MEASURE_MANAGER_H
|
||
22 | |||
23 | #include <string> |
||
24 | #include <map> |
||
25 | #include "mon_measure.h" |
||
26 | #include "measure_plugin.h" |
||
27 | #include "measure_dispatcher.h" |
||
28 | |||
29 | #include "grapes_log.h" |
||
30 | |||
31 | |||
32 | #define MAX_NUM_MEASURES_INSTANTIATED 1000000 |
||
33 | |||
34 | typedef std::map<MeasurementId, class MeasurePlugin *> MonMeasureMapId;
|
||
35 | typedef std::map<std::string, class MeasurePlugin *> MonMeasureMapName;
|
||
36 | typedef std::map<MonHandler,class MonMeasure *> MeasureMap;
|
||
37 | |||
38 | class MeasureManager { |
||
39 | protected:
|
||
40 | /* List of available measures */
|
||
41 | MonMeasureMapId mMonMeasuresId; |
||
42 | MonMeasureMapName mMonMeasuresName; |
||
43 | |||
44 | /* defualts */
|
||
45 | void *default_repo_client;
|
||
46 | char* default_channel;
|
||
47 | char** peer_name;
|
||
48 | |||
49 | class MeasureDispatcher cDispatcher; |
||
50 | |||
51 | /* Currently installed instances */
|
||
52 | MeasureMap mMeasureInstances; |
||
53 | int next_measure_handler;
|
||
54 | |||
55 | /* Used to load a MeasurePlugin. If the Id or name has been already used it returns a negative value, *
|
||
56 | * 0 otherwise. */
|
||
57 | int loadMeasurePlugin(MeasurePlugin *plugin) {
|
||
58 | if(mMonMeasuresId.find(plugin->getId()) == mMonMeasuresId.end() &&
|
||
59 | mMonMeasuresName.find(plugin->getName()) == mMonMeasuresName.end()) |
||
60 | { |
||
61 | mMonMeasuresId[plugin->getId()] = plugin; |
||
62 | mMonMeasuresName[plugin->getName()] = plugin; |
||
63 | return EOK;
|
||
64 | } else
|
||
65 | return -EINVAL;
|
||
66 | }; |
||
67 | |||
68 | int loadMeasurePlugins(void); |
||
69 | |||
70 | /* Inserts new instance. Returns handler */
|
||
71 | MonHandler insertNewInstance(MonMeasure *m) { |
||
72 | int new_mh;
|
||
73 | /* For memory efficiency we use a hash container and a circular buffer */
|
||
74 | /* check if next_measure_handler is not in use (went around once) */
|
||
75 | if(mMeasureInstances.find(next_measure_handler) != mMeasureInstances.end())
|
||
76 | return -ENOMEM;
|
||
77 | new_mh = next_measure_handler; |
||
78 | mMeasureInstances[new_mh] = m; |
||
79 | |||
80 | /* update next_measure_handler */
|
||
81 | next_measure_handler++; |
||
82 | if(next_measure_handler == MAX_NUM_MEASURES_INSTANTIATED)
|
||
83 | next_measure_handler = 0;
|
||
84 | |||
85 | return new_mh;
|
||
86 | } |
||
87 | /* checks handler */
|
||
88 | bool isValidMonHandler(MonHandler mh) {
|
||
89 | if(mMeasureInstances.find(mh) != mMeasureInstances.end())
|
||
90 | return true; |
||
91 | else
|
||
92 | return false; |
||
93 | } |
||
94 | |||
95 | friend class MonMeasure; |
||
96 | friend class MeasureDispatcher; |
||
97 | public:
|
||
98 | /* Constructor */
|
||
99 | MeasureManager() { |
||
100 | default_repo_client = NULL;
|
||
101 | default_channel = NULL;
|
||
102 | next_measure_handler = 0;
|
||
103 | cDispatcher.mm = this; |
||
104 | peer_name = new char*;
|
||
105 | *peer_name = NULL;
|
||
106 | loadMeasurePlugins(); |
||
107 | }; |
||
108 | |||
109 | MeasureManager(void *repo) {
|
||
110 | default_repo_client = repo; |
||
111 | default_channel = NULL;
|
||
112 | next_measure_handler = 0;
|
||
113 | cDispatcher.mm = this; |
||
114 | peer_name = new char*;
|
||
115 | *peer_name = NULL;
|
||
116 | loadMeasurePlugins(); |
||
117 | }; |
||
118 | |||
119 | /* Destructor */
|
||
120 | ~MeasureManager() { |
||
121 | /* Free list of available measures */
|
||
122 | for(MonMeasureMapId::reverse_iterator it = mMonMeasuresId.rbegin();
|
||
123 | it !=mMonMeasuresId.rend(); ++it) |
||
124 | delete it->second; |
||
125 | /* Free instances */
|
||
126 | for(MeasureMap::iterator it = mMeasureInstances.begin();
|
||
127 | it != mMeasureInstances.end(); ++it) |
||
128 | monDestroyMeasure(it->first); |
||
129 | if(default_channel)
|
||
130 | delete[] default_channel; |
||
131 | if(*peer_name != NULL) |
||
132 | delete[] *peer_name; |
||
133 | delete peer_name; |
||
134 | } |
||
135 | |||
136 | /* Returns a map container with all the measures which can be instantiated and used */
|
||
137 | MonMeasureMapId& monListMeasure() { |
||
138 | return mMonMeasuresId;
|
||
139 | }; |
||
140 | |||
141 | /* Creates an instance of a measure specified through the name of the measure
|
||
142 | * returns handler of the new instance to be used subsequently with the functions below */
|
||
143 | int monCreateMeasureName(MeasurementName name, MeasurementCapabilities mc) {
|
||
144 | std::string t(name); |
||
145 | MonMeasure *m; |
||
146 | int ret;
|
||
147 | MonHandler mh; |
||
148 | |||
149 | if(mMonMeasuresName.find(t) == mMonMeasuresName.end())
|
||
150 | return -EINVAL;
|
||
151 | |||
152 | 40a7aea7 | RobertBirke | if(!(mc & REMOTE))
|
153 | mc |= (mMonMeasuresName[t]->getCaps() & TXRXBI); |
||
154 | |||
155 | 956892f0 | ArpadBakay | m = mMonMeasuresName[t]->createMeasure(mc, &cDispatcher); |
156 | |||
157 | ret = m->setFlags(mc); |
||
158 | if(ret != EOK)
|
||
159 | goto error;
|
||
160 | |||
161 | ret = mh = insertNewInstance(m); |
||
162 | if(mh < 0) |
||
163 | goto error;
|
||
164 | |||
165 | m->mh_local = mh; |
||
166 | return mh;
|
||
167 | |||
168 | error:
|
||
169 | delete m; |
||
170 | return ret;
|
||
171 | }; |
||
172 | |||
173 | int monCreateMeasureId(MeasurementId id, MeasurementCapabilities mc) {
|
||
174 | MonMeasure *m; |
||
175 | int ret;
|
||
176 | MonHandler mh; |
||
177 | |||
178 | if(mMonMeasuresId.find(id) == mMonMeasuresId.end())
|
||
179 | return -EINVAL;
|
||
180 | |||
181 | 40a7aea7 | RobertBirke | if(!(mc & REMOTE))
|
182 | mc |= (mMonMeasuresId[id]->getCaps() & TXRXBI); |
||
183 | |||
184 | 956892f0 | ArpadBakay | m = mMonMeasuresId[id]->createMeasure(mc, &cDispatcher); |
185 | |||
186 | ret = m->setFlags(mc); |
||
187 | if(ret != EOK)
|
||
188 | goto error;
|
||
189 | |||
190 | ret = mh = insertNewInstance(m); |
||
191 | if(mh < 0) |
||
192 | goto error;
|
||
193 | |||
194 | m->mh_local = mh; |
||
195 | |||
196 | return mh;
|
||
197 | |||
198 | error:
|
||
199 | delete m; |
||
200 | return ret;
|
||
201 | }; |
||
202 | |||
203 | /* Destroys a specific measure instance */
|
||
204 | int monDestroyMeasure(MonHandler mh) {
|
||
205 | /* find it */
|
||
206 | if(!isValidMonHandler(mh))
|
||
207 | return -EINVAL;
|
||
208 | |||
209 | /* deactivate it */
|
||
210 | cDispatcher.deactivateMeasure(mMeasureInstances[mh]); |
||
211 | |||
212 | delete mMeasureInstances[mh]; |
||
213 | mMeasureInstances.erase(mh); |
||
214 | |||
215 | return EOK;
|
||
216 | }; |
||
217 | |||
218 | MeasurementId monMeasureHandlerToId (MonHandler mh) { |
||
219 | /* find it */
|
||
220 | if(!isValidMonHandler(mh))
|
||
221 | return -EINVAL;
|
||
222 | |||
223 | return mMeasureInstances[mh]->measure_plugin->getId();
|
||
224 | } |
||
225 | |||
226 | std::vector<class MonParameter *>* monListParameters(MonHandler mh) { |
||
227 | if(!isValidMonHandler(mh))
|
||
228 | return NULL; |
||
229 | return &(mMeasureInstances[mh]->listParameters());
|
||
230 | }; |
||
231 | |||
232 | int monSetParameter(MonHandler mh, int pid, MonParameterValue p) { |
||
233 | if(!isValidMonHandler(mh))
|
||
234 | return -EINVAL;
|
||
235 | return mMeasureInstances[mh]->setParameter(pid,p);
|
||
236 | }; |
||
237 | |||
238 | MonParameterValue monGetParameter(MonHandler mh, size_t pid) { |
||
239 | if(!isValidMonHandler(mh))
|
||
240 | return NAN;
|
||
241 | return mMeasureInstances[mh]->getParameter(pid);
|
||
242 | }; |
||
243 | |||
244 | MeasurementStatus monGetStatus(MonHandler mh) { |
||
245 | if(!isValidMonHandler(mh))
|
||
246 | 5f3adef4 | CsabaKiraly | return ERROR;
|
247 | 956892f0 | ArpadBakay | return mMeasureInstances[mh]->getStatus();
|
248 | }; |
||
249 | |||
250 | MeasurementCapabilities monGetMeasurementCaps(MonHandler mh) { |
||
251 | if(!isValidMonHandler(mh))
|
||
252 | return -EINVAL;
|
||
253 | return mMeasureInstances[mh]->getFlags();
|
||
254 | } |
||
255 | |||
256 | int monActivateMeasure(MonHandler mh, SocketId sid, MsgType mt) {
|
||
257 | if(!isValidMonHandler(mh))
|
||
258 | return -EINVAL;
|
||
259 | return cDispatcher.activateMeasure(mMeasureInstances[mh], sid, mt);
|
||
260 | } |
||
261 | |||
262 | int monDeactivateMeasure(MonHandler mh) {
|
||
263 | if(!isValidMonHandler(mh))
|
||
264 | return -EINVAL;
|
||
265 | return cDispatcher.deactivateMeasure(mMeasureInstances[mh]);
|
||
266 | } |
||
267 | |||
268 | size_t getMeasuresCount(void) {
|
||
269 | return mMonMeasuresId.size();
|
||
270 | }; |
||
271 | |||
272 | void cbRxPkt(void *arg) { |
||
273 | cDispatcher.cbRxPkt(arg); |
||
274 | }; |
||
275 | |||
276 | void cbRxData(void *arg) { |
||
277 | cDispatcher.cbRxData(arg); |
||
278 | }; |
||
279 | |||
280 | void cbTxPkt(void *arg) { |
||
281 | cDispatcher.cbTxPkt(arg); |
||
282 | }; |
||
283 | |||
284 | void cbTxData(void *arg) { |
||
285 | cDispatcher.cbTxData(arg); |
||
286 | }; |
||
287 | |||
288 | int cbHdrPkt(SocketId sid, MsgType mt) {
|
||
289 | return cDispatcher.cbHdrPkt(sid, mt);
|
||
290 | }; |
||
291 | |||
292 | int cbHdrData(SocketId sid, MsgType mt) {
|
||
293 | return cDispatcher.cbHdrData(sid, mt);
|
||
294 | }; |
||
295 | |||
296 | void receiveCtrlMsg(SocketId sid, MsgType mt, char *buffer, int buflen){ |
||
297 | cDispatcher.receiveCtrlMsg(sid, mt, buffer, buflen); |
||
298 | }; |
||
299 | |||
300 | int monPublishStatisticalType(MonHandler mh, const char* publishing_name, const char *channel, enum stat_types st[], int length, void *repo_client) { |
||
301 | if(repo_client == NULL) |
||
302 | repo_client = default_repo_client; |
||
303 | |||
304 | if(repo_client == NULL) |
||
305 | return -EFAILED;
|
||
306 | |||
307 | if(channel == NULL) |
||
308 | channel = default_channel; |
||
309 | |||
310 | if(!isValidMonHandler(mh))
|
||
311 | return -EINVAL;
|
||
312 | |||
313 | return mMeasureInstances[mh]->setPublishStatisticalType(publishing_name, channel, st, length, repo_client);
|
||
314 | }; |
||
315 | |||
316 | result monRetrieveResult(MonHandler mh, enum stat_types st) {
|
||
317 | if(!isValidMonHandler(mh))
|
||
318 | return NAN;
|
||
319 | |||
320 | if(mMeasureInstances[mh]->rb == NULL) |
||
321 | return NAN;
|
||
322 | 5f3adef4 | CsabaKiraly | |
323 | 956892f0 | ArpadBakay | return mMeasureInstances[mh]->rb->stats[st];
|
324 | }; |
||
325 | |||
326 | int monNewSample(MonHandler mh, result r) {
|
||
327 | if(!isValidMonHandler(mh))
|
||
328 | return -EINVAL;
|
||
329 | |||
330 | if(mMeasureInstances[mh]->flags != 0) |
||
331 | return -EINVAL;
|
||
332 | |||
333 | return mMeasureInstances[mh]->newSample(r);
|
||
334 | }; |
||
335 | |||
336 | void monParseConfig(void *); |
||
337 | |||
338 | a1a78de4 | CsabaKiraly | int monSetPeerName(const char *pn); |
339 | 956892f0 | ArpadBakay | }; |
340 | |||
341 | #endif /* _MEASURE_MANAGER_H */ |