Statistics
| Branch: | Revision:

napa-baselibs / monl / plugins / rtt_measure.cpp @ d1e0f2ca

History | View | Annotate | Download (3.57 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 "rtt_measure.h"
20
#include <sys/time.h>
21

    
22
RttMeasure::RttMeasure(class MeasurePlugin *m, MeasurementCapabilities mc, class MeasureDispatcher *md): MonMeasure(m,mc,md) {
23
}
24

    
25
RttMeasure::~RttMeasure() {
26
}
27

    
28
void RttMeasure::init() {
29
        last_time_rx = 0.0;
30
        last_time_rem_tx = 0.0;
31
}
32

    
33
void RttMeasure::stop() {
34
        r_tx_list[R_RTT] = r_rx_list[R_RTT] = NAN;
35
        r_tx_list[R_RECEIVE_TIME] = r_rx_list[R_RECEIVE_TIME] = NAN;
36
}
37

    
38
result RttMeasure::RxPkt(result *r,ExecutionList *el) {
39
        char dbg[512];
40
        if(flags & REMOTE) {
41
                last_time_rx = r[R_RECEIVE_TIME];
42
                last_time_rem_tx = r[R_SEND_TIME];
43

    
44
                snprintf(dbg, sizeof(dbg), "RX rem: Ts: %f seq#: %f", r[R_RECEIVE_TIME], r[R_SEQNUM]);
45
                debugOutput(dbg);
46

    
47
                return NAN;
48
        } else {
49
                if(r[R_REPLY_TIME] != 0.0) {
50
                        r[R_RTT] = r[R_RECEIVE_TIME] - r[R_REPLY_TIME];
51
                        snprintf(dbg, sizeof(dbg), "RX: Ts: %f seq#: %f Rtt: %f Replyt: %f", r[R_RECEIVE_TIME], r[R_SEQNUM], r[R_RTT], r[R_REPLY_TIME]);
52
                        debugOutput(dbg);
53

    
54
                        return r[R_RTT];
55
                }
56

    
57
                snprintf(dbg, sizeof(dbg), "RX: Ts: %f seq#: %f Rtt: nan Replyt: %f", r[R_RECEIVE_TIME], r[R_SEQNUM], r[R_RTT], r[R_REPLY_TIME]);
58
                debugOutput(dbg);
59
        }
60
        return NAN;
61
}
62

    
63
result RttMeasure::RxData(result *r,ExecutionList *el) {
64
        return RxPkt(r,el);
65
}
66

    
67
result RttMeasure::TxPkt(result *r,ExecutionList *el) {
68
        char dbg[512];
69
        if(flags & REMOTE) {
70
                if(last_time_rem_tx != 0.0 && last_time_rx != 0.0) {
71
                        r[R_REPLY_TIME] = last_time_rem_tx + r[R_SEND_TIME] - last_time_rx;
72

    
73
                        snprintf(dbg, sizeof(dbg), "TX rem: Ts: %f seq#: %f Last: %f Replyt: %f", r[R_SEND_TIME], r[R_SEQNUM], last_time_rx, r[R_REPLY_TIME]);
74
                        debugOutput(dbg);
75
                }
76
                else
77
                        r[R_REPLY_TIME] = NAN;
78
        } else {
79
                snprintf(dbg, sizeof(dbg), "TX: Ts: %f seq#: %f", r[R_SEND_TIME], r[R_SEQNUM], last_time_rx, r[R_REPLY_TIME]);
80
                debugOutput(dbg);
81
        }
82
        return NAN;
83
}
84

    
85
result RttMeasure::TxData(result *r,ExecutionList *el) {
86
        return TxPkt(r,el);
87
}
88

    
89
void RttMeasure::Run() {
90
        if(flags & REMOTE)
91
                return;
92

    
93
        struct timeval tv;
94
        gettimeofday(&tv,NULL);
95
        sendOobData((char*) (&tv),sizeof(struct timeval));
96

    
97
        //Schedule next
98
        struct timeval next = {1,0};
99
        scheduleNextIn(&next);
100
}
101

    
102
void RttMeasure::receiveOobData(char *buf, int buf_len, result *r) {
103
        if(flags & REMOTE) {
104
                sendOobData(buf, buf_len);
105
        } else {
106
                result start, end;
107
                struct timeval tv_end;
108
                struct timeval *tv_start;
109

    
110
                tv_start = (struct timeval *) buf;
111

    
112
                start = tv_start->tv_sec + tv_start->tv_usec / 1000000.0;
113
                end = r[R_RECEIVE_TIME];
114

    
115
                newSample(end-start);
116
        }
117
}
118

    
119
RttMeasurePlugin::RttMeasurePlugin() {
120
        /* Initialise properties: MANDATORY! */
121
        name = "Round trip time";
122
        desc = "The round trip time between two peers in [s]";
123
        id = RTT;
124
        /* end of mandatory properties */
125
}