Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.56 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
        tx_every = 10;
24
}
25

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

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

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

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

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

    
48
                return NAN;
49
        } else {
50
                if(r[R_REPLY_TIME] != 0.0) {
51
                        r[R_RTT] = r[R_RECEIVE_TIME] - r[R_REPLY_TIME];
52
                        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]);
53
                        debugOutput(dbg);
54

    
55
                        return every(r[R_RTT]);
56
                }
57

    
58
                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]);
59
                debugOutput(dbg);
60
        }
61
        return NAN;
62
}
63

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

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

    
74
                        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]);
75
                        debugOutput(dbg);
76
                }
77
                else
78
                        r[R_REPLY_TIME] = NAN;
79
        } else {
80
                snprintf(dbg, sizeof(dbg), "TX: Ts: %f seq#: %f", r[R_SEND_TIME], r[R_SEQNUM]);
81
                debugOutput(dbg);
82
        }
83
        return NAN;
84
}
85

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

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

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

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

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

    
111
                tv_start = (struct timeval *) buf;
112

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

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

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