Statistics
| Branch: | Revision:

napa-baselibs / monl / mon_measure.cpp @ e852cb42

History | View | Annotate | Download (3.46 KB)

1
/***************************************************************************
2
 *   Copyright (C) 2009 by Robert Birke
3
 *   robert.birke@polito.it
4
 *   Copyright (C) 2010 by Csaba Kiraly
5
 *   kiraly@disi.unitn.it
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11

12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16

17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
20
 ***********************************************************************/
21
#include <sstream>
22
#include "mon_measure.h"
23
#include "measure_dispatcher.h"
24
#include "measure_manager.h"
25
#include "mon_event.h"
26
#include        "napa_log.h"
27

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

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

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

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

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

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

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

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

    
70
        std::string output_name;
71
        
72
        if(output_file.is_open())
73
                output_file.close();
74

    
75
        output_name = name;
76

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

    
81
        if(flags & REMOTE) {
82
                output_name += "-remote";
83
        } else {
84
                output_name += "-local";
85
        }
86

    
87
        sid = mlGetLocalSocketID(&errorstatus);
88
        if(errorstatus == 0) {
89
                if(mlSocketIDToString(sid, sid_string, sizeof(sid_string)) == 0)
90
                        output_name += "-A:";
91
                        output_name += sid_string;
92
        }
93

    
94
        if(dst_socketid != NULL) {
95
                if(mlSocketIDToString((SocketId) dst_socketid, sid_string, sizeof(sid_string)) == 0) {
96
                        output_name += "-B:";
97
                        output_name += sid_string;
98
                }
99
        }
100

    
101
        output_file.open (output_name.c_str(), std::fstream::out | std::fstream::app);
102
        output_file.setf(std::ios::fixed, std::ios::floatfield);
103
        output_file.precision(6);
104
}
105

    
106
void MonMeasure::debugStop() {
107
        output_file.close();
108
}
109

    
110
void MonMeasure::debugOutput(char *out) {
111
        switch((int)param_values[P_DEBUG_FILE]) {
112
        case        1:
113
                        output_file << out << std::endl;
114
                        break;
115
        case         3:
116
                        output_file << out << std::endl;
117
        case        2:
118
                        debug("%s",out);
119
                        break;
120
        }
121
}
122

    
123
result MonMeasure::every(result &m) {
124
        meas_sum += m;
125
        meas_cnt++;
126
        if(rx_cnt++ % tx_every) {
127
                return NAN;        //don't respond too frequently
128
        } else {
129
                m = meas_sum / meas_cnt;
130
                meas_sum = 0;
131
                meas_cnt = 0;
132
                return m;
133
        }
134
}