Statistics
| Branch: | Revision:

napa-baselibs / monl / mon_measure.cpp @ faf560e3

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

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

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

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

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

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

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

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

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

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

    
73
        output_name = name;
74

    
75
        std::stringstream mtss;
76
        mtss << "-MT:" << (int)msg_type;
77
        output_name += mtss.str();
78

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

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

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

    
98
void MonMeasure::debugStop() {
99
        output_file.close();
100
}
101

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

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