-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopo5.cc
385 lines (325 loc) · 11.8 KB
/
topo5.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
//
// Simple example of OLSR routing over some point-to-point links
//
// Network topology
//
// n0
// \ 5 Mb/s, 2ms
// \ 1.5Mb/s, 10ms
// n2 -------------------------n3---------n4
// /
// / 5 Mb/s, 2ms
// n1
//
// - all links are point-to-point links with indicated one-way BW/delay
// - CBR/UDP flows from n0 to n4, and from n3 to n1
// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec.
// (i.e., DataRate of 448,000 bps)
// - DropTail queues
// - Tracing of queues and packet receptions to file "simple-point-to-point-olsr.tr"
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "ns3/topology-read-module.h"
#include <list>
#include <ctime>
#include <sstream>
#include <time.h>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/olsr-helper.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/ipv4-nix-vector-helper.h"
#include "ns3/delay-jitter-estimation.h"
#include "ns3/traffic-control-module.h"
using namespace ns3;
NetDeviceContainer ndc[100];
NodeContainer nc[100];
DelayJitterEstimation delayJitter;
static void CalculateLen()
{
int max = -1;
int tmp;
//int maxaddr=-1;
for (int i = 0; i < 100; i++)
{
tmp = 0;
int size = ndc[i].GetN();
//std::cout<<"test1"<<i<<size<<std::endl;
for (int j = 0; j < size; j++)
{
tmp = tmp + ndc[i].Get(j)->GetObject<PointToPointNetDevice>()->GetQueue()->GetNBytes();
//std::cout<<"test2"<<j<<std::endl;
}
if (tmp > max)
{
max = tmp;
//maxaddr=i;
}
}
//std::cout << Simulator::Now ().GetSeconds ()<<" "<<max<<" "<<maxaddr<<std::endl;
std::cout << max << std::endl;
}
static void
CalculateDelay(Ptr<const Packet> p, const Address &address)
{
static int k = 0;
k++;
delayJitter.RecordRx(p);
Time t = delayJitter.GetLastDelay();
//std::cout <<"Delay: "<< Simulator::Now().GetSeconds() << "\t" << t.GetMilliSeconds() << std::endl;
}
static void
TagTx(Ptr<const Packet> p)
{
delayJitter.PrepareTx(p);
}
NS_LOG_COMPONENT_DEFINE("SJTU_CNPROJECTzz");
std::vector<int> random_pair()
{
std::vector<int> l;
int end = 49;
int begin = 0;
int sum = (rand() % (25) + 1) * 2;
while (l.size() < (unsigned)(sum))
{
l.push_back(rand() % (end - begin + 1) + begin);
//l.unique();//
}
return l;
}
int main(int argc, char *argv[])
{
srand((unsigned)time(NULL));
// Users may find it convenient to turn on explicit debugging
// for selected modules; the below lines suggest how to do this
// #if 0
LogComponentEnable("SJTU_CNPROJECTzz", LOG_LEVEL_ALL);
//LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
//LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
// #endif
// Set up some default values for the simulation. Use the
// Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (210));
// Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("448kb/s"));
//DefaultValue::Bind ("DropTailQueue::m_maxPackets", 30);
// Allow the user to override any of the defaults and the above
// DefaultValue::Bind ()s at run-time, via command-line arguments
std::string format("Inet");
std::string input("topo.txt");
// Set up command line parameters used to control the experiment.
CommandLine cmd;
cmd.AddValue("format", "Format to use for data input [Orbis|Inet|Rocketfuel].",
format);
cmd.AddValue("input", "Name of the input file.",
input);
cmd.Parse(argc, argv);
// ------------------------------------------------------------
// -- Read topology data.
// --------------------------------------------
// Pick a topology reader based in the requested format.
TopologyReaderHelper topoHelp;
topoHelp.SetFileName(input);
topoHelp.SetFileType(format);
Ptr<TopologyReader> inFile = topoHelp.GetTopologyReader();
NodeContainer nodes;
if (inFile != 0)
{
nodes = inFile->Read();
}
if (inFile->LinksSize() == 0)
{
NS_LOG_ERROR("Problems reading the topology file. Failing.");
return -1;
}
NS_LOG_INFO("read the file");
NodeContainer routers;
NodeContainer terminals;
std::cout << nodes.GetN() << ' ' << std::endl;
for (int i = 0; i < 50; i++)
{
terminals.Add(nodes.Get(i));
}
for (int i = 50; i < 80; i++)
{
routers.Add(nodes.Get(i));
}
// ------------------------------------------------------------
// -- Create nodes and network stacks
// --------------------------------------------
NS_LOG_INFO("creating internet stack");
// InternetStackHelper stack_router;
// // // Setup NixVector Routing
// // Ipv4NixVectorHelper nixRouting;
// // stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
// // stack.Install (nodes);
// RipHelper ripRouting;
// // Rule of thumb:
// // Interfaces are added sequentially, starting from 0
// // However, interface 0 is always the loopback...
// std::vector<int> nodes_Interface(100, 0);
// int i = 0;
// for (TopologyReader::ConstLinksIterator iter = inFile->LinksBegin(); i < 50; iter++, i++)
// {
// ripRouting.ExcludeInterface(iter->GetToNode(), ++nodes_Interface[iter->GetToNode()->GetId()]);
// }
// Ipv4ListRoutingHelper listRH;
// listRH.Add(ripRouting, 0);
// // Ipv4StaticRoutingHelper staticRh;
// // listRH.Add (staticRh, 5);
// stack_router.SetIpv6StackInstall(false);
// stack_router.SetRoutingHelper(listRH);
// stack_router.Install(routers);
// InternetStackHelper stack_term;
// stack_term.SetIpv6StackInstall(false);
// stack_term.Install(terminals);
OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRouting;
Ipv4ListRoutingHelper list;
list.Add (staticRouting, 0);
list.Add (olsr, 10);
InternetStackHelper internet;
internet.SetRoutingHelper (list); // has effect on the next Install
internet.Install (nodes);
NS_LOG_INFO("creating ip4 addresses");
Ipv4AddressHelper address;
address.SetBase("10.0.0.0", "255.255.255.0");
int totlinks = inFile->LinksSize();
NS_LOG_INFO("creating node containers");
// NodeContainer* nc = new NodeContainer[totlinks];
TopologyReader::ConstLinksIterator iter;
int i = 0;
for (TopologyReader::ConstLinksIterator iter = inFile->LinksBegin(); iter != inFile->LinksEnd(); iter++, i++)
{
nc[i] = NodeContainer(iter->GetFromNode(), iter->GetToNode());
}
NS_LOG_INFO("creating net device containers");
// NetDeviceContainer* ndc = new NetDeviceContainer[totlinks];
PointToPointHelper p2p;
p2p.SetQueue ("ns3::DropTailQueue", "MaxSize", StringValue ("2MB"));
// error model
Ptr<RateErrorModel> em = CreateObject<RateErrorModel>();
em->SetAttribute("ErrorRate", DoubleValue(0.0001));
for (int i = 0; i < totlinks; i++)
{
// p2p.SetChannelAttribute ("Delay", TimeValue(MilliSeconds(weight[i])));
p2p.SetChannelAttribute("Delay", StringValue("2ms"));
p2p.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
p2p.SetDeviceAttribute("InterframeGap", StringValue("0.333ms"));
ndc[i] = p2p.Install(nc[i]);
ndc[i].Get(1)->SetAttribute("ReceiveErrorModel", PointerValue(em));
ndc[i].Get(0)->SetAttribute("ReceiveErrorModel", PointerValue(em));
}
// it crates little subnets, one for each couple of nodes.
NS_LOG_INFO("creating ipv4 interfaces");
Ipv4InterfaceContainer *ipic = new Ipv4InterfaceContainer[totlinks];
for (int i = 0; i < totlinks; i++)
{
ipic[i] = address.Assign(ndc[i]);
address.NewNetwork();
}
// Ptr<Ipv4StaticRouting> staticRouting;
// for (int i = 0; i < 50; i++)
// {
// staticRouting = Ipv4RoutingHelper::GetRouting<Ipv4StaticRouting>(terminals.Get(i)->GetObject<Ipv4>()->GetRoutingProtocol());
// staticRouting->SetDefaultRoute(ipic[i].GetAddress(1), 1);
// }
NS_LOG_INFO("creating sending");
ApplicationContainer clientContainer;
int port = 1;
UdpEchoClientHelper client(ipic[0].GetAddress(0), port);
client.SetAttribute("MaxPackets", UintegerValue(10000));
client.SetAttribute("Interval", TimeValue(Seconds(0.00375)));
client.SetAttribute("PacketSize", UintegerValue(210));
double current_time;
std::vector<int> host_pair;
int pair_num;
int current_pair;
double total_time = 120.0;
for (current_time = 0.0; current_time < total_time; current_time += 0.1)
{
host_pair = random_pair();
//NS_LOG_INFO ();
pair_num = host_pair.size() / 2;
for (current_pair = 0; current_pair < pair_num; ++current_pair)
{
client.SetAttribute("RemoteAddress", AddressValue(ipic[host_pair[2 * current_pair + 1]].GetAddress(0)));
clientContainer = client.Install(terminals.Get(host_pair[2 * current_pair]));
clientContainer.Start(Seconds(current_time));
clientContainer.Stop(Seconds(current_time + 0.1));
clientContainer.Get(0)->TraceConnectWithoutContext("Tx", MakeCallback(&TagTx));
}
}
std::cout<<"######## "<<clientContainer.GetN()<<std::endl;
// for (i = 0; i < (int)clientContainer.GetN(); i++)
// {
// clientContainer.Get(i)->TraceConnectWithoutContext("Tx", MakeCallback(&TagTx));
// }
PacketSinkHelper sink("ns3::UdpSocketFactory",
InetSocketAddress(Ipv4Address::GetAny(), port));
ApplicationContainer sinkApps = sink.Install(terminals);
sinkApps.Start(Seconds(0.0));
sinkApps.Stop(Seconds(120.0));
for (i = 0; i < 50; i++)
{
sinkApps.Get(i)->TraceConnectWithoutContext("Rx", MakeCallback(&CalculateDelay));
}
/*// Create the OnOff application to send UDP datagrams of size
// 210 bytes at a rate of 448 Kb/s from n0 to n4
NS_LOG_INFO ("Create Applications.");
uint16_t port = 9; // Discard port (RFC 863)
OnOffHelper onoff1 ("ns3::UdpSocketFactory",
InetSocketAddress (i34.GetAddress(1) (1), port));
onoff1.SetConstantRate (DataRate ("448kb/s"));
ApplicationContainer onOffApp1 = onoff1.Install (c.Get (0));
onOffApp1.Start (Seconds (10.0));
onOffApp1.Stop (Seconds (20.0));
// Create a similar flow from n3 to n1, starting at time 1.1 seconds
OnOffHelper onoff2 ("ns3::UdpSocketFactory",
InetSocketAddress (i12.GetAddress (0), port));
onoff2.SetConstantRate (DataRate ("448kb/s"));
ApplicationContainer onOffApp2 = onoff2.Install (c.Get (3));
onOffApp2.Start (Seconds (10.1));
onOffApp2.Stop (Seconds (20.0));
// Create packet sinks to receive these packets
PacketSinkHelper sink ("ns3::UdpSocketFactory",
InetSocketAddress (Ipv4Address::GetAny (), port));
NodeContainer sinks = NodeContainer (c.Get (4), c.Get (1));
ApplicationContainer sinkApps = sink.Install (sinks);
sinkApps.Start (Seconds (0.0));
sinkApps.Stop (Seconds (21.0));
AsciiTraceHelper ascii;
p2p.EnableAsciiAll (ascii.CreateFileStream ("simple-point-to-point-olsr.tr"));
p2p.EnablePcapAll ("simple-point-to-point-olsr");*/
Simulator::Stop(Seconds(30));
NS_LOG_INFO("set avg queue collector");
for (int i = 0; i <= 1200; i++)
{
Simulator::Schedule(MilliSeconds(100 + 100 * i), CalculateLen);
}
NS_LOG_INFO("Run Simulation.");
Simulator::Run();
Simulator::Destroy();
NS_LOG_INFO("Done.");
return 0;
}