Statistics
| Branch: | Revision:

napa-baselibs / monl / monl.cpp @ eddebd3e

History | View | Annotate | Download (7.86 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
    debug(stderr, "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(stderr, "X.moninit\n");
129
    MonMeasureMapId::iterator it;
130
        for(it = c_measure_map_id.begin(), i = 0; it != c_measure_map_id.end(); it++,i++) {
131
                measure_array[i].name = const_cast<char *>((it->second->getName()).c_str());
132
                measure_array[i].id = it->second->getId();
133
                measure_array[i].desc = const_cast<char *>((it->second->getDesc()).c_str());
134

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

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

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

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

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

    
169
    debug(stderr, "X.moninit\n");
170
        measure_array_len = i;
171

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

    
181
        /* control data */
182
        mlRegisterRecvDataCb(&dataRxcb,MSG_TYPE_MONL);
183

    
184

    
185
        return EOK;
186

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

    
197
const struct MeasurementPlugin *monListMeasure (int *length){
198
        *length = measure_array_len;
199
        return measure_array;
200
}
201

    
202
MonHandler monCreateMeasureName (const MeasurementName name, MeasurementCapabilities mc){
203
   return man->monCreateMeasureName(name, mc);
204
}
205

    
206
MonHandler monCreateMeasure (MeasurementId id, MeasurementCapabilities mc){
207
   return man->monCreateMeasureId(id, mc);
208
}
209

    
210
int monDestroyMeasure (MonHandler mh) {
211
        return man->monDestroyMeasure(mh);
212
}
213

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

    
225
struct MeasurementParameter *monListParametersByHandler (MonHandler mh, int *length) {
226
        return monListParametersById(man->monMeasureHandlerToId(mh), length);
227
}
228

    
229
int monSetParameter (MonHandler mh, MonParameterType ph, MonParameterValue p) {
230
        return man->monSetParameter(mh,ph,p);
231
}
232

    
233
MonParameterValue monGetParameter (MonHandler mh, MonParameterType ph) {
234
        return man->monGetParameter(mh,ph);
235
}
236

    
237
MeasurementStatus monGetStatus(MonHandler mh) {
238
        return man->monGetStatus(mh);
239
}
240

    
241
int monActivateMeasure (MonHandler mh, SocketId sid, MsgType mt) {
242
        return man->monActivateMeasure(mh ,sid ,mt);
243
}
244

    
245
int monDeactivateMeasure (MonHandler mh) {
246
        return man->monDeactivateMeasure(mh);
247
}
248

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

    
253
result monRetrieveResult(MonHandler mh, enum stat_types st){
254
        return man->monRetrieveResult(mh,st);
255
}
256

    
257
int monNewSample(MonHandler mh, result r){
258
        return man->monNewSample(mh,r);
259
}
260

    
261
int monSetPeerName(const char *name){
262
        return man->monSetPeerName(name);
263
}