mathematical form

hello,

I try to develop this form: RTTbarre(k)= lambda*RTTbarre(k-1)+(1-lambda)*RTT(k)
RTTbarre(k-1): is the previous filter calculates RTT(k)
RTT(k)is calculated by:

simtime_t rtt = sendSeqNo - msg->getSeqNo() > PING_HISTORY_SIZE ?
0 : simTime() - sendTimeHistory[msg->getSeqNo() % PING_HISTORY_SIZE];
it's the time to go and back packet.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

#include <iostream>
#include "PingApp.h"
#include <math.h>

#include "IPvXAddressResolver.h"
#include "PingPayload_m.h"
#include "IPv4ControlInfo.h"

#include "IPv6ControlInfo.h"


using std::cout;

Define_Module(PingApp);

simsignal_t PingApp::rttSignal = SIMSIGNAL_NULL;
simsignal_t PingApp::rsig = SIMSIGNAL_NULL;
simsignal_t PingApp::numLostSignal = SIMSIGNAL_NULL;
simsignal_t PingApp::outOfOrderArrivalsSignal = SIMSIGNAL_NULL;
simsignal_t PingApp::pingTxSeqSignal = SIMSIGNAL_NULL;
simsignal_t PingApp::pingRxSeqSignal = SIMSIGNAL_NULL;

void PingApp::initialize(int stage)
{
    cSimpleModule::initialize(stage);

    // because of IPvXAddressResolver, we need to wait until interfaces are registered,
    // address auto-assignment takes place etc.
    if (stage != 3)
        return;

    // read params
    // (defer reading srcAddr/destAddr to when ping starts, maybe
    // addresses will be assigned later by some protocol)
    packetSize = par("packetSize");
    sendIntervalp = & par("sendInterval");
    hopLimit = par("hopLimit");
    count = par("count");
    startTime = par("startTime");
    stopTime = par("stopTime");
    if (stopTime != 0 && stopTime <= startTime)
        error("Invalid startTime/stopTime parameters");
    printPing = (bool)par("printPing");

    // state
    sendSeqNo = expectedReplySeqNo = 0;
    WATCH(sendSeqNo);
    WATCH(expectedReplySeqNo);

    // statistics
    rttStat.setName("pingRTT");
    rttSignal = registerSignal("rtt");
    rsig = registerSignal("rt");
    numLostSignal = registerSignal("numLost");
    outOfOrderArrivalsSignal = registerSignal("outOfOrderArrivals");
    pingTxSeqSignal = registerSignal("pingTxSeq");
    pingRxSeqSignal = registerSignal("pingRxSeq");

    lossCount = outOfOrderArrivalCount = numPongs = 0;
    WATCH(lossCount);


    WATCH(outOfOrderArrivalCount);
    WATCH(numPongs);

    // schedule first ping (use empty destAddr to disable)
    if (par("destAddr").stringValue()[0])
    {
        cMessage *msg = new cMessage("sendPing");
        scheduleAt(startTime, msg);
    }
}
void PingApp::handleMessage(cMessage *msg)
{
    if (msg->isSelfMessage())
    {
        // on first call we need to initialize
        if (sendSeqNo == 0)
        {
            srcAddr = IPvXAddressResolver().resolve(par("srcAddr"));
            destAddr = IPvXAddressResolver().resolve(par("destAddr"));
            ASSERT(!destAddr.isUnspecified());

            EV << "Starting up: dest=" << destAddr << "  src=" << srcAddr << "\n";
        }

        // send a ping
        sendPing();

        // then schedule next one if needed
        scheduleNextPing(msg);
    }
    else
    {
        // process ping response
        processPingResponse(check_and_cast<PingPayload *>(msg));

    }
}

void PingApp::sendPing()
{
    EV << "Sending ping #" << sendSeqNo << "\n";

    char name[32];
    sprintf(name, "ping%ld", sendSeqNo);

    PingPayload *msg = new PingPayload(name);// name =ping1..
    msg->setOriginatorId(getId());
    msg->setSeqNo(sendSeqNo);
    msg->setByteLength(packetSize);
    ev << "taille du packet ="<< packetSize << endl;

    // store the sending time in a circular buffer so we can compute RTT when the packet returns
    sendTimeHistory[sendSeqNo % PING_HISTORY_SIZE] = simTime();

    sendToICMP(msg, destAddr, srcAddr, hopLimit);
    emit(pingTxSeqSignal, sendSeqNo);
    sendSeqNo++;
}

void PingApp::scheduleNextPing(cMessage *timer)
{
    simtime_t nextPing = simTime() + sendIntervalp->doubleValue();

    if ((count == 0 || sendSeqNo < count) && (stopTime == 0 || nextPing < stopTime))
       scheduleAt(nextPing, timer);

    else
        delete timer;
}

void PingApp::sendToICMP(cMessage *msg, const IPvXAddress& destAddr, const IPvXAddress& srcAddr, int hopLimit)
{
    if (!destAddr.isIPv6())
    {
        // send to IPv4
        IPv4ControlInfo *ctrl = new IPv4ControlInfo();
        ctrl->setSrcAddr(srcAddr.get4());
        ctrl->setDestAddr(destAddr.get4());
        ctrl->setTimeToLive(hopLimit);
        msg->setControlInfo(ctrl);
        send(msg, "pingOut");
    }
    else
    {
        // send to IPv6
        IPv6ControlInfo *ctrl = new IPv6ControlInfo();
        ctrl->setSrcAddr(srcAddr.get6());
        ctrl->setDestAddr(destAddr.get6());
        ctrl->setHopLimit(hopLimit);
        msg->setControlInfo(ctrl);
        send(msg, "pingv6Out");
    }
}
void PingApp::processPingResponse(PingPayload *msg)
{

    // get src, hopCount etc from packet, and print them
    IPvXAddress src, dest;
    int msgHopCount = -1;

     ASSERT(msg->getOriginatorId() == getId());  // ICMP module error

    if (dynamic_cast<IPv4ControlInfo *>(msg->getControlInfo()) != NULL)
    {
        IPv4ControlInfo *ctrl = (IPv4ControlInfo *)msg->getControlInfo();
        src = ctrl->getSrcAddr();
        dest = ctrl->getDestAddr();
        msgHopCount = ctrl->getTimeToLive();
    }
    else if (dynamic_cast<IPv6ControlInfo *>(msg->getControlInfo()) != NULL)
    {
        IPv6ControlInfo *ctrl = (IPv6ControlInfo *)msg->getControlInfo();
        src = ctrl->getSrcAddr();
        dest = ctrl->getDestAddr();
        msgHopCount = ctrl->getHopLimit();
    }

    // calculate the RTT time by looking up the send time of the packet
    // if the send time is no longer available (i.e. the packet is very old and the
    // sendTime was overwritten in the circular buffer) then we just return a 0
    // to signal that this value should not be used during the RTT statistics)


          simtime_t rtt = sendSeqNo - msg->getSeqNo() > PING_HISTORY_SIZE ?
                                             0 : simTime() - sendTimeHistory[msg->getSeqNo() % PING_HISTORY_SIZE];

            if (printPing)
    {
        cout << getFullPath() << ": reply of " << std::dec << msg->getByteLength()
             << " bytes from " << src
             << " icmp_seq=" << msg->getSeqNo() << " ttl=" << msgHopCount

             << " (" << msg->getName() << ")" << endl;
    }
    // update statistics
    countPingResponse(msg->getByteLength(), msg->getSeqNo(), rtt);
    delete msg;
}

void PingApp::countPingResponse( int bytes, long seqNo, simtime_t rtt)
{
     EV << "Ping reply #" << seqNo << "arrived, rtt=" << rtt << "\n";
    emit(pingRxSeqSignal, seqNo);

    numPongs++;

    //count only non 0 RTT values as 0s are invalid
    if (rtt > 0)
    {
        rttStat.collect(rtt);
        emit(rttSignal, rtt);
        //emit(rsig, A[i]);
    }

    if (seqNo == expectedReplySeqNo)
    {
        // expected ping reply arrived; expect next sequence number
        expectedReplySeqNo++;
    }
    else if (seqNo > expectedReplySeqNo)
    {
        EV << "Jump in seq numbers, assuming pings since #" << expectedReplySeqNo << " got lost\n";

        // jump in the sequence: count pings in gap as lost for now
        // (if they arrive later, we'll decrement back the loss counter)
        long jump = seqNo - expectedReplySeqNo;
        lossCount += jump;
        emit(numLostSignal, lossCount);

        // expect sequence numbers to continue from here
        expectedReplySeqNo = seqNo+1;
    }
    else // seqNo < expectedReplySeqNo
    {
        // ping reply arrived too late: count as out-of-order arrival (not loss after all)
        EV << "Arrived out of order (too late)\n";
        outOfOrderArrivalCount++;
        lossCount--;
        emit(outOfOrderArrivalsSignal, outOfOrderArrivalCount);
        emit(numLostSignal, lossCount);
    }
}

Last edited on
Topic archived. No new replies allowed.