Statistics
| Branch: | Revision:

napa-baselibs / monl / mon_measure.cpp @ 507372bb

History | View | Annotate | Download (3.24 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
#include "mon_measure.h"
20
#include "measure_dispatcher.h"
21
#include "measure_manager.h"
22
#include "mon_event.h"
23
#include        "napa_log.h"
24

    
25
MonMeasure::MonMeasure(class MeasurePlugin *mp, MeasurementCapabilities mc, class MeasureDispatcher *ptrDisp) {
26
        rx_cnt = 0;
27
        meas_sum = 0;
28
        meas_cnt = 0;
29
        tx_every = 1;
30

    
31
        measure_plugin = mp;
32
        flags = mc;
33
        used_counter = 0;
34
        auto_loaded = false;
35
        mh_remote = -1;
36
        ptrDispatcher = ptrDisp;
37
        dst_socketid_publish = false;
38

    
39
        /* Initialise default values */
40
        int i;
41
        status = STOPPED;
42
        r_rx_list = r_tx_list = NULL;
43
        param_values = new MonParameterValue[mp->params.size()];
44

    
45
        for( i = 0; i < mp->params.size(); i++)
46
                param_values[i] =  mp->params[i]->dval;
47

    
48
        if(!(flags & REMOTE))
49
                rb = new ResultBuffer((int)param_values[P_WINDOW_SIZE], mp->name.c_str(), ptrDispatcher->mm->peer_name, this);
50
        else
51
                rb = NULL;
52
};
53

    
54
int MonMeasure::sendOobData(char* buf, int buf_len) {
55
        return ptrDispatcher->oobDataTx(this, buf, buf_len);
56
}
57

    
58
int MonMeasure::scheduleNextIn(struct timeval *tv) {
59
        return schedule_measure(tv, this);
60
}
61

    
62
void MonMeasure::debugInit(const char *name) {
63
        char sid_string[SOCKETID_STRING_SIZE];
64
        SocketId sid;
65
        int errorstatus;
66

    
67
        std::string output_name;
68
        
69
        if(output_file.is_open())
70
                output_file.close();
71

    
72
        output_name = name;
73

    
74
        output_name += "-MT:" + msg_type;
75

    
76
        sid = mlGetLocalSocketID(&errorstatus);
77
        if(errorstatus == 0) {
78
                if(mlSocketIDToString(sid, sid_string, sizeof(sid_string)) == 0)
79
                        output_name += "-A:";
80
                        output_name += sid_string;
81
        }
82

    
83
        if(dst_socketid != NULL) {
84
                if(mlSocketIDToString((SocketId) dst_socketid, sid_string, sizeof(sid_string)) == 0) {
85
                        output_name += "-B:";
86
                        output_name += sid_string;
87
                }
88
        }
89

    
90
        output_file.open (output_name.c_str(), std::fstream::out | std::fstream::app);
91
        output_file.setf(std::ios::fixed, std::ios::floatfield);
92
        output_file.precision(6);
93
}
94

    
95
void MonMeasure::debugStop() {
96
        output_file.close();
97
}
98

    
99
void MonMeasure::debugOutput(char *out) {
100
        switch((int)param_values[P_DEBUG_FILE]) {
101
        case        1:
102
                        output_file << out << std::endl;
103
                        break;
104
        case         3:
105
                        output_file << out << std::endl;
106
        case        2:
107
                        debug("%s",out);
108
                        break;
109
        }
110
}
111

    
112
result MonMeasure::every(result &m) {
113
        meas_sum += m;
114
        meas_cnt++;
115
        if(rx_cnt++ % tx_every) {
116
                return NAN;        //don't respond too frequently
117
        } else {
118
                m = meas_sum / meas_cnt;
119
                meas_sum = 0;
120
                meas_cnt = 0;
121
                return m;
122
        }
123
}