Statistics
| Branch: | Revision:

napa-baselibs / monl / monl.cpp @ 5f3adef4

History | View | Annotate | Download (7.76 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        "grapes_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
        init_mon_event(eb);
111
        
112
        int i,k,len;
113
   /* Create instance of the monitoring layer */
114
        man = new MeasureManager(repo_client);
115
        
116

    
117
        /* Fill array of possible measurements */
118
        c_measure_map_id = man->monListMeasure();
119
        len = c_measure_map_id.size();
120

    
121
        measure_array_len=0;
122
        measure_array = (struct MeasurementPlugin*) malloc(len * sizeof(struct MeasurementPlugin));
123
        if(measure_array == NULL)
124
                return -ENOMEM;
125
        memset(measure_array, 0, len * sizeof(struct MeasurementPlugin));
126
        
127
   MonMeasureMapId::iterator it;
128
        for(it = c_measure_map_id.begin(), i = 0; it != c_measure_map_id.end(); it++,i++) {
129
                measure_array[i].name = const_cast<char *>((it->second->getName()).c_str());
130
                measure_array[i].id = it->second->getId();
131
                measure_array[i].desc = const_cast<char *>((it->second->getDesc()).c_str());
132

    
133
                len = (it->second->getDeps()).size();
134
                if (len > 0) {
135
                        measure_array[i].deps_id = (MeasurementId*) malloc(len * sizeof(MeasurementId));
136
                        if(measure_array[i].deps_id == NULL)
137
                                goto out_of_mem;
138

    
139
                        for(k = 0; k < len; k++) {
140
                                measure_array[i].deps_id[k] =
141
                                        c_measure_map_id[(it->second->getDeps()).at(k)]->getId();
142
                        }
143
                        measure_array[i].deps_length = k;
144
                }
145

    
146
                len = (it->second->getParams()).size();
147
                if (len > 0) {
148
                        measure_array[i].params = (MeasurementParameter *) malloc(len * sizeof(MeasurementParameter));
149
                        if(measure_array[i].params == NULL)
150
                                goto out_of_mem;
151

    
152
        
153
                        for(k = 0; k < len; k++) {
154
                                measure_array[i].params[k].ph = ((it->second->getParams()).at(k))->ph;
155
                                measure_array[i].params[k].name = const_cast<char *>
156
                                        (((it->second->getParams()).at(k))->name.c_str());
157
                                measure_array[i].params[k].desc = const_cast<char *>
158
                                        (((it->second->getParams()).at(k))->desc.c_str());
159
                                measure_array[i].params[k].dval = ((it->second->getParams()).at(k))->dval;
160
                        }
161
                        measure_array[i].params_length = k;
162
                }
163

    
164
                measure_array[i].caps = it->second->getCaps();
165
        }
166

    
167
        measure_array_len = i;
168

    
169
        /* register callback functions */
170
        /* measurement */
171
        mlRegisterGetRecvPktInf(&monPktRxcb);
172
        mlRegisterGetSendPktInf(&monPktTxcb);
173
        mlRegisterSetMonitoringHeaderPktCb(&monPktHdrSetcb);
174
        mlRegisterGetRecvDataInf(&monDataRxcb);
175
        mlRegisterSetMonitoringHeaderDataCb(&monDataHdrSetcb);
176
        mlRegisterGetSendDataInf(&monDataTxcb);
177

    
178
        /* control data */
179
        mlRegisterRecvDataCb(&dataRxcb,MSG_TYPE_MONL);
180

    
181

    
182
        return EOK;
183

    
184
out_of_mem:
185
        for(k=0; k=i; k++) {
186
                if(measure_array[k].deps_id != NULL)
187
                        free(measure_array[k].deps_id);
188
                if(measure_array[k].params != NULL)
189
                        free(measure_array[k].params);
190
        }
191
        return -ENOMEM;
192
}
193

    
194
const struct MeasurementPlugin *monListMeasure (int *length){
195
        *length = measure_array_len;
196
        return measure_array;
197
}
198

    
199
MonHandler monCreateMeasureName (const MeasurementName name, MeasurementCapabilities mc){
200
   return man->monCreateMeasureName(name, mc);
201
}
202

    
203
MonHandler monCreateMeasure (MeasurementId id, MeasurementCapabilities mc){
204
   return man->monCreateMeasureId(id, mc);
205
}
206

    
207
int monDestroyMeasure (MonHandler mh) {
208
        return man->monDestroyMeasure(mh);
209
}
210

    
211
struct MeasurementParameter *monListParametersById (MeasurementId mid, int *length) {
212
        int i;
213
        for(i = 0; i < measure_array_len; i++)
214
                if(measure_array[i].id == mid) {
215
                        *length = measure_array[i].params_length;
216
                        return measure_array[i].params;
217
                }
218
        *length = 0;
219
        return NULL;
220
}
221

    
222
struct MeasurementParameter *monListParametersByHandler (MonHandler mh, int *length) {
223
        return monListParametersById(man->monMeasureHandlerToId(mh), length);
224
}
225

    
226
int monSetParameter (MonHandler mh, MonParameterType ph, MonParameterValue p) {
227
        return man->monSetParameter(mh,ph,p);
228
}
229

    
230
MonParameterValue monGetParameter (MonHandler mh, MonParameterType ph) {
231
        return man->monGetParameter(mh,ph);
232
}
233

    
234
MeasurementStatus monGetStatus(MonHandler mh) {
235
        return man->monGetStatus(mh);
236
}
237

    
238
int monActivateMeasure (MonHandler mh, SocketId sid, MsgType mt) {
239
        return man->monActivateMeasure(mh ,sid ,mt);
240
}
241

    
242
int monDeactivateMeasure (MonHandler mh) {
243
        return man->monDeactivateMeasure(mh);
244
}
245

    
246
int monPublishStatisticalType(MonHandler mh, const char *publishing_name, const char *channel, stat_types *st, int length, void * repo_client){
247
        return man->monPublishStatisticalType(mh, publishing_name, channel, st, length, repo_client);
248
}
249

    
250
result monRetrieveResult(MonHandler mh, enum stat_types st){
251
        return man->monRetrieveResult(mh,st);
252
}
253

    
254
int monNewSample(MonHandler mh, result r){
255
        return man->monNewSample(mh,r);
256
}
257

    
258
int monSetPeerName(const char *name){
259
        return man->monSetPeerName(name);
260
}