napa-baselibs / monl / monl.cpp @ 507372bb
History | View | Annotate | Download (7.85 KB)
1 |
/***************************************************************************
|
---|---|
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 |
#define LOG_MODULE "[monl] " |
21 |
#include "napa_log.h" |
22 |
#include "ml.h" |
23 |
#include "mon.h" |
24 |
#include "measure_manager.h" |
25 |
#include <iostream> |
26 |
#include <stdlib.h> |
27 |
#include <string.h> |
28 |
#include <errors.h> |
29 |
#include "mon_event.h" |
30 |
|
31 |
|
32 |
|
33 |
/* Manager class Initialised by monInit()*/
|
34 |
class MeasureManager * man; |
35 |
/* Array of available measurements Initialised by monInit()*/
|
36 |
struct MeasurementPlugin *measure_array;
|
37 |
int measure_array_len;
|
38 |
|
39 |
/* callback wrappers */
|
40 |
/*
|
41 |
* callback for a received packet notification from the messaging layer
|
42 |
* @param *arg An argument for a recv packet information struct
|
43 |
*/
|
44 |
|
45 |
extern "C" { |
46 |
void monPktRxcb(void *arg); |
47 |
void monPktTxcb(void *arg); |
48 |
int monPktHdrSetcb(void *arg,send_pkt_type_and_dst_inf *send_pkt_inf); |
49 |
void monDataRxcb(void *arg); |
50 |
void monDataTxcb(void *arg); |
51 |
int monDataHdrSetcb(void *arg,send_pkt_type_and_dst_inf *send_pkt_inf); |
52 |
void dataRxcb(char *buffer,int buflen, unsigned char msgtype, recv_params *arg); |
53 |
} |
54 |
|
55 |
void monPktRxcb(void *arg){ |
56 |
mon_pkt_inf *pkt_info; |
57 |
pkt_info = (mon_pkt_inf *)arg; |
58 |
debug("received a RX packet notification");
|
59 |
man->cbRxPkt(arg); |
60 |
} |
61 |
|
62 |
void monPktTxcb(void *arg){ |
63 |
mon_pkt_inf *pkt_info; |
64 |
pkt_info = (mon_pkt_inf *)arg; |
65 |
debug("received a TX packet notification");
|
66 |
man->cbTxPkt(arg); |
67 |
} |
68 |
|
69 |
int monPktHdrSetcb(socketID_handle remote_socketID, uint8_t msgtype){
|
70 |
debug("set monitoring module packet header");
|
71 |
return man->cbHdrPkt(remote_socketID, msgtype);
|
72 |
} |
73 |
|
74 |
void monDataRxcb(void *arg){ |
75 |
mon_data_inf *data_info; |
76 |
data_info = (mon_data_inf *)arg; |
77 |
debug("received a RX data notification");
|
78 |
man->cbRxData(arg); |
79 |
} |
80 |
|
81 |
void monDataTxcb(void *arg){ |
82 |
mon_data_inf *data_info; |
83 |
data_info = (mon_data_inf *)arg; |
84 |
debug("received a TX data notification");
|
85 |
man->cbTxData(arg); |
86 |
} |
87 |
|
88 |
int monDataHdrSetcb(socketID_handle remote_socketID, uint8_t msgtype){
|
89 |
debug("set monitoring module data header");
|
90 |
return man->cbHdrData(remote_socketID, msgtype);
|
91 |
} |
92 |
|
93 |
/*
|
94 |
* The peer receives data per callback from the messaging layer.
|
95 |
* @param *buffer A pointer to the buffer
|
96 |
* @param buflen The length of the buffer
|
97 |
* @param msgtype The message type
|
98 |
* @param *arg An argument that receives metadata about the received data
|
99 |
*/
|
100 |
void dataRxcb(char *buffer,int buflen, MsgType msgtype, recv_params *rparams){ |
101 |
debug("Received data from callback: msgtype %d buflen %d",msgtype,buflen);
|
102 |
man->receiveCtrlMsg(rparams->remote_socketID, msgtype, buffer, buflen); |
103 |
} |
104 |
|
105 |
|
106 |
//int monInit(cfg_opt_t *cfg_mon) {
|
107 |
int monInit(void *eb, void *repo_client) { |
108 |
MonMeasureMapId c_measure_map_id; |
109 |
|
110 |
debug("X.moninit\n");
|
111 |
init_mon_event(eb); |
112 |
|
113 |
int i,k,len;
|
114 |
/* Create instance of the monitoring layer */
|
115 |
man = new MeasureManager(repo_client);
|
116 |
|
117 |
|
118 |
/* Fill array of possible measurements */
|
119 |
c_measure_map_id = man->monListMeasure(); |
120 |
len = c_measure_map_id.size(); |
121 |
|
122 |
measure_array_len=0;
|
123 |
measure_array = (struct MeasurementPlugin*) malloc(len * sizeof(struct MeasurementPlugin)); |
124 |
if(measure_array == NULL) |
125 |
return -ENOMEM;
|
126 |
memset(measure_array, 0, len * sizeof(struct MeasurementPlugin)); |
127 |
|
128 |
debug("X.moninit\n");
|
129 |
|
130 |
MonMeasureMapId::iterator it; |
131 |
for(it = c_measure_map_id.begin(), i = 0; it != c_measure_map_id.end(); it++,i++) { |
132 |
measure_array[i].name = const_cast<char *>((it->second->getName()).c_str()); |
133 |
measure_array[i].id = it->second->getId(); |
134 |
measure_array[i].desc = const_cast<char *>((it->second->getDesc()).c_str()); |
135 |
|
136 |
len = (it->second->getDeps()).size(); |
137 |
if (len > 0) { |
138 |
measure_array[i].deps_id = (MeasurementId*) malloc(len * sizeof(MeasurementId));
|
139 |
if(measure_array[i].deps_id == NULL) |
140 |
goto out_of_mem;
|
141 |
|
142 |
for(k = 0; k < len; k++) { |
143 |
measure_array[i].deps_id[k] = |
144 |
c_measure_map_id[(it->second->getDeps()).at(k)]->getId(); |
145 |
} |
146 |
measure_array[i].deps_length = k; |
147 |
} |
148 |
|
149 |
len = (it->second->getParams()).size(); |
150 |
if (len > 0) { |
151 |
measure_array[i].params = (MeasurementParameter *) malloc(len * sizeof(MeasurementParameter));
|
152 |
if(measure_array[i].params == NULL) |
153 |
goto out_of_mem;
|
154 |
|
155 |
|
156 |
for(k = 0; k < len; k++) { |
157 |
measure_array[i].params[k].ph = ((it->second->getParams()).at(k))->ph; |
158 |
measure_array[i].params[k].name = const_cast<char *> |
159 |
(((it->second->getParams()).at(k))->name.c_str()); |
160 |
measure_array[i].params[k].desc = const_cast<char *> |
161 |
(((it->second->getParams()).at(k))->desc.c_str()); |
162 |
measure_array[i].params[k].dval = ((it->second->getParams()).at(k))->dval; |
163 |
} |
164 |
measure_array[i].params_length = k; |
165 |
} |
166 |
|
167 |
measure_array[i].caps = it->second->getCaps(); |
168 |
} |
169 |
|
170 |
debug("X.moninit\n");
|
171 |
measure_array_len = i; |
172 |
|
173 |
/* register callback functions */
|
174 |
/* measurement */
|
175 |
mlRegisterGetRecvPktInf(&monPktRxcb); |
176 |
mlRegisterGetSendPktInf(&monPktTxcb); |
177 |
mlRegisterSetMonitoringHeaderPktCb(&monPktHdrSetcb); |
178 |
mlRegisterGetRecvDataInf(&monDataRxcb); |
179 |
mlRegisterSetMonitoringHeaderDataCb(&monDataHdrSetcb); |
180 |
mlRegisterGetSendDataInf(&monDataTxcb); |
181 |
|
182 |
/* control data */
|
183 |
mlRegisterRecvDataCb(&dataRxcb,MSG_TYPE_MONL); |
184 |
|
185 |
|
186 |
return EOK;
|
187 |
|
188 |
out_of_mem:
|
189 |
for(k=0; k=i; k++) { |
190 |
if(measure_array[k].deps_id != NULL) |
191 |
free(measure_array[k].deps_id); |
192 |
if(measure_array[k].params != NULL) |
193 |
free(measure_array[k].params); |
194 |
} |
195 |
return -ENOMEM;
|
196 |
} |
197 |
|
198 |
const struct MeasurementPlugin *monListMeasure (int *length){ |
199 |
*length = measure_array_len; |
200 |
return measure_array;
|
201 |
} |
202 |
|
203 |
MonHandler monCreateMeasureName (const MeasurementName name, MeasurementCapabilities mc){
|
204 |
return man->monCreateMeasureName(name, mc);
|
205 |
} |
206 |
|
207 |
MonHandler monCreateMeasure (MeasurementId id, MeasurementCapabilities mc){ |
208 |
return man->monCreateMeasureId(id, mc);
|
209 |
} |
210 |
|
211 |
int monDestroyMeasure (MonHandler mh) {
|
212 |
return man->monDestroyMeasure(mh);
|
213 |
} |
214 |
|
215 |
struct MeasurementParameter *monListParametersById (MeasurementId mid, int *length) { |
216 |
int i;
|
217 |
for(i = 0; i < measure_array_len; i++) |
218 |
if(measure_array[i].id == mid) {
|
219 |
*length = measure_array[i].params_length; |
220 |
return measure_array[i].params;
|
221 |
} |
222 |
*length = 0;
|
223 |
return NULL; |
224 |
} |
225 |
|
226 |
struct MeasurementParameter *monListParametersByHandler (MonHandler mh, int *length) { |
227 |
return monListParametersById(man->monMeasureHandlerToId(mh), length);
|
228 |
} |
229 |
|
230 |
int monSetParameter (MonHandler mh, MonParameterType ph, MonParameterValue p) {
|
231 |
return man->monSetParameter(mh,ph,p);
|
232 |
} |
233 |
|
234 |
MonParameterValue monGetParameter (MonHandler mh, MonParameterType ph) { |
235 |
return man->monGetParameter(mh,ph);
|
236 |
} |
237 |
|
238 |
MeasurementStatus monGetStatus(MonHandler mh) { |
239 |
return man->monGetStatus(mh);
|
240 |
} |
241 |
|
242 |
int monActivateMeasure (MonHandler mh, SocketId sid, MsgType mt) {
|
243 |
return man->monActivateMeasure(mh ,sid ,mt);
|
244 |
} |
245 |
|
246 |
int monDeactivateMeasure (MonHandler mh) {
|
247 |
return man->monDeactivateMeasure(mh);
|
248 |
} |
249 |
|
250 |
int monPublishStatisticalType(MonHandler mh, const char *publishing_name, const char *channel, stat_types *st, int length, void * repo_client){ |
251 |
return man->monPublishStatisticalType(mh, publishing_name, channel, st, length, repo_client);
|
252 |
} |
253 |
|
254 |
result monRetrieveResult(MonHandler mh, enum stat_types st){
|
255 |
return man->monRetrieveResult(mh,st);
|
256 |
} |
257 |
|
258 |
int monNewSample(MonHandler mh, result r){
|
259 |
return man->monNewSample(mh,r);
|
260 |
} |
261 |
|
262 |
int monSetPeerName(const char *name){ |
263 |
return man->monSetPeerName(name);
|
264 |
} |