-
Notifications
You must be signed in to change notification settings - Fork 0
/
TarpPingApp.cc
227 lines (169 loc) · 6.08 KB
/
TarpPingApp.cc
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
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
#include "TarpPingApp.h"
#include <iostream>
#include "inet/common/Protocol.h"
#include "inet/networklayer/contract/L3Socket.h"
#include "inet/networklayer/common/L3AddressResolver.h"
#include "inet/networklayer/common/L3AddressTag_m.h"
#include "inet/common/ProtocolTag_m.h"
#include "inet/common/packet/chunk/ByteCountChunk.h"
#include "inet/networklayer/common/EchoPacket_m.h"
using std::cout;
using inet::INITSTAGE_LOCAL;
using inet::Protocol;
using inet::L3Socket;
using inet::L3AddressResolver;
using inet::L3AddressReq;
using inet::PacketProtocolTag;
using inet::makeShared;
using inet::ByteCountChunk;
using inet::B;
using inet::EchoPacket;
Define_Module(TarpPingApp);
const std::map<const Protocol*, const Protocol*> TarpPingApp::l3Echo( {
{ &Protocol::flooding, &Protocol::echo },
{ &Protocol::tarpf, &Protocol::echo }
});
TarpPingApp::TarpPingApp() {
}
TarpPingApp::~TarpPingApp() {
cancelAndDelete(event);
socketMap.deleteSockets();
}
void TarpPingApp::initialize(int stage) {
// TODO - Generated method body
ApplicationBase::initialize(stage);
if (stage == INITSTAGE_LOCAL) {
// read params
packetSize = par("packetSize");
sendIntervalPar = &par("sendInterval");
sleepDurationPar = &par("sleepDuration");
hopLimit = par("hopLimit");
startTime = par("startTime");
stopTime = par("stopTime");
if (stopTime >= SIMTIME_ZERO && stopTime < startTime)
throw cRuntimeError("Invalid startTime/stopTime parameters");
//state
pid = -1;
// statistics
numSent = 0;
numReceived = 0;
WATCH(numSent);
WATCH(numReceived);
// references
event = new cMessage("event");
}
}
void TarpPingApp::parseDestAddressesPar() {
srcAddr = L3AddressResolver().resolve(par("srcAddr"));
const char *destAddrs = par("destAddr");
if (!strcmp(destAddrs, "*")) {
// destAddresses = getAllAddresses();
} else {
cStringTokenizer tokenizer(destAddrs);
const char *token;
while ((token = tokenizer.nextToken()) != nullptr) {
L3Address addr = L3AddressResolver().resolve(token);
destAddresses.push_back(addr);
}
}
}
void TarpPingApp::handleSelfMessage(cMessage *msg) {
if (msg == event) {
srcAddr = L3AddressResolver().resolve(par("srcAddr"));
parseDestAddressesPar();
if (destAddresses.empty()) {
return;
}
destAddrIdx = 0;
if (destAddrIdx >= (int) destAddresses.size())
return;
destAddr = destAddresses[destAddrIdx];
EV_INFO << "Starting up: dest=" << destAddr << " src=" << srcAddr
<< "seqNo=" << sendSeqNo << endl;
ASSERT(!destAddr.isUnspecified());
const Protocol *networkProtocol = nullptr;
const char *networkProtocolAsString = par("networkProtocol");
networkProtocol = Protocol::getProtocol(networkProtocolAsString);
const Protocol *icmp = l3Echo.at(networkProtocol);
for (auto socket : socketMap.getMap()) {
socket.second->close();
}
currentSocket = nullptr;
currentSocket = new L3Socket(networkProtocol, gate("socketOut"));
currentSocket->bind(icmp, L3Address());
currentSocket->setCallback(this);
// send a message
char name[32];
sprintf(name, "TARP-%ld", sendSeqNo);
Packet *outPacket = new Packet(name);
auto payload = makeShared<ByteCountChunk>(B(packetSize));
const auto& request = makeShared<EchoPacket>();
request->setChunkLength(B(8));
request->setType(inet::ECHO_PROTOCOL_REQUEST);
request->setIdentifier(pid);
request->setSeqNumber(sendSeqNo);
outPacket->insertAtBack(payload);
outPacket->insertAtFront(request);
outPacket->addTag<PacketProtocolTag>()->setProtocol(&Protocol::echo);
auto addressReq = outPacket->addTag<L3AddressReq>();
addressReq->setSrcAddress(srcAddr);
addressReq->setDestAddress(destAddr);
currentSocket->send(outPacket);
sendSeqNo++;
}
}
void TarpPingApp::handleMessage(cMessage *msg) {
// TODO - Generated method body
if (msg->isSelfMessage()) {
handleSelfMessage(msg);
}
}
void TarpPingApp::refreshDisplay() const {
char buf[100];
sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent);
getDisplayString().setTagArg("t", 0, buf);
}
void TarpPingApp::handleMessageWhenUp(cMessage *msg) {
throw cRuntimeError("Debug::handleMessageWhenUp");
if (msg->isSelfMessage()) {
handleSelfMessage(msg);
}
}
void TarpPingApp::handleStartOperation(LifecycleOperation *operation) {
startSendingMessages();
}
void TarpPingApp::startSendingMessages() {
scheduleAt(simTime(), event);
}
void TarpPingApp::handleStopOperation(LifecycleOperation *operation) {
if (socketMap.size() > 0) {
for (auto socket : socketMap.getMap())
socket.second->close();
}
}
void TarpPingApp::handleCrashOperation(LifecycleOperation *operation) {
for (auto socket : socketMap.getMap())
socket.second->destroy();
socketMap.deleteSockets();
}
void TarpPingApp::socketDataArrived(INetworkSocket *socket, Packet *packet) {
}
void TarpPingApp::socketClosed(INetworkSocket *socket) {
if (socket == currentSocket)
currentSocket = nullptr;
delete socketMap.removeSocket(socket);
}