A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
simple-alternate-routing.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License version 2 as
5
* published by the Free Software Foundation;
6
*
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
11
*
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
*
16
*/
17
18
//
19
// Network topology
20
//
21
// n0
22
// \ 5 Mb/s, 2ms
23
// \ 1.5Mb/s, 10ms
24
// n2 ------------------------n3
25
// / /
26
// / 5 Mb/s, 2ms /
27
// n1--------------------------
28
// 1.5 Mb/s, 100ms
29
//
30
// this is a modification of simple-global-routing to allow for
31
// a single hop but higher-cost path between n1 and n3
32
//
33
// - Tracing of queues and packet receptions to file "simple-rerouting.tr"
34
35
#include <iostream>
36
#include <fstream>
37
#include <string>
38
#include <cassert>
39
40
#include "ns3/core-module.h"
41
#include "ns3/internet-module.h"
42
#include "ns3/point-to-point-module.h"
43
#include "ns3/network-module.h"
44
#include "ns3/applications-module.h"
45
#include "ns3/ipv4-global-routing-helper.h"
46
47
using namespace
ns3;
48
49
NS_LOG_COMPONENT_DEFINE
(
"SimpleAlternateRoutingExample"
);
50
51
int
52
main
(
int
argc,
char
*argv[])
53
{
54
// Users may find it convenient to turn on explicit debugging
55
// for selected modules; the below lines suggest how to do this
56
#if 0
57
LogComponentEnable
(
"GlobalRoutingHelper"
,
LOG_LOGIC
);
58
LogComponentEnable
(
"GlobalRouter"
,
LOG_LOGIC
);
59
#endif
60
61
62
Config::SetDefault
(
"ns3::OnOffApplication::PacketSize"
,
UintegerValue
(210));
63
Config::SetDefault
(
"ns3::OnOffApplication::DataRate"
,
StringValue
(
"300b/s"
));
64
65
// The below metric, if set to 3 or higher, will cause packets between
66
// n1 and n3 to take the 2-hop route through n2
67
68
CommandLine
cmd;
69
//
70
// Additionally, we plumb this metric into the default value / command
71
// line argument system as well, for exemplary purposes. This means
72
// that it can be resettable at the command-line to the program,
73
// rather than recompiling
74
// e.g. waf --run "simple-alternate-routing --AlternateCost=5"
75
uint16_t sampleMetric = 1;
76
cmd.
AddValue
(
"AlternateCost"
,
77
"This metric is used in the example script between n3 and n1 "
,
78
sampleMetric);
79
80
// Allow the user to override any of the defaults and the above
81
// DefaultValue::Bind ()s at run-time, via command-line arguments
82
cmd.
Parse
(argc, argv);
83
84
// Here, we will explicitly create four nodes. In more sophisticated
85
// topologies, we could configure a node factory.
86
NS_LOG_INFO
(
"Create nodes."
);
87
NodeContainer
c;
88
c.
Create
(4);
89
NodeContainer
n0n2
=
NodeContainer
(c.
Get
(0), c.
Get
(2));
90
NodeContainer
n1n2
=
NodeContainer
(c.
Get
(1), c.
Get
(2));
91
NodeContainer
n3n2 =
NodeContainer
(c.
Get
(3), c.
Get
(2));
92
NodeContainer
n1n3 =
NodeContainer
(c.
Get
(1), c.
Get
(3));
93
94
// We create the channels first without any IP addressing information
95
NS_LOG_INFO
(
"Create channels."
);
96
PointToPointHelper
p2p;
97
p2p.
SetDeviceAttribute
(
"DataRate"
,
StringValue
(
"5Mbps"
));
98
p2p.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"2ms"
));
99
NetDeviceContainer
d0d2 = p2p.
Install
(n0n2);
100
101
NetDeviceContainer
d1d2 = p2p.
Install
(n1n2);
102
103
p2p.
SetDeviceAttribute
(
"DataRate"
,
StringValue
(
"1500kbps"
));
104
p2p.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"10ms"
));
105
NetDeviceContainer
d3d2 = p2p.
Install
(n3n2);
106
107
p2p.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"100ms"
));
108
NetDeviceContainer
d1d3 = p2p.
Install
(n1n3);
109
110
InternetStackHelper
internet;
111
internet.
Install
(c);
112
113
// Later, we add IP addresses. The middle two octets correspond to
114
// the channel number.
115
NS_LOG_INFO
(
"Assign IP Addresses."
);
116
Ipv4AddressHelper
ipv4;
117
ipv4.
SetBase
(
"10.0.0.0"
,
"255.255.255.0"
);
118
ipv4.
Assign
(d0d2);
119
120
ipv4.
SetBase
(
"10.1.1.0"
,
"255.255.255.0"
);
121
Ipv4InterfaceContainer
i1i2
= ipv4.
Assign
(d1d2);
122
123
ipv4.
SetBase
(
"10.2.2.0"
,
"255.255.255.0"
);
124
ipv4.
Assign
(d3d2);
125
126
ipv4.
SetBase
(
"10.3.3.0"
,
"255.255.255.0"
);
127
Ipv4InterfaceContainer
i1i3 = ipv4.
Assign
(d1d3);
128
129
i1i3.
SetMetric
(0, sampleMetric);
130
i1i3.
SetMetric
(1, sampleMetric);
131
132
// Create router nodes, initialize routing database and set up the routing
133
// tables in the nodes.
134
Ipv4GlobalRoutingHelper::PopulateRoutingTables
();
135
136
// Create the OnOff application to send UDP datagrams
137
NS_LOG_INFO
(
"Create Application."
);
138
uint16_t
port
= 9;
// Discard port (RFC 863)
139
140
// Create a flow from n3 to n1, starting at time 1.1 seconds
141
OnOffHelper
onoff (
"ns3::UdpSocketFactory"
,
142
Address
(
InetSocketAddress
(i1i2.
GetAddress
(0),
port
)));
143
onoff.
SetConstantRate
(
DataRate
(
"300b/s"
));
144
145
ApplicationContainer
apps = onoff.Install (c.
Get
(3));
146
apps.
Start
(Seconds (1.1));
147
apps.
Stop
(Seconds (10.0));
148
149
// Create a packet sink to receive these packets
150
PacketSinkHelper
sink (
"ns3::UdpSocketFactory"
,
151
Address
(
InetSocketAddress
(
Ipv4Address::GetAny
(), port)));
152
apps = sink.
Install
(c.
Get
(1));
153
apps.
Start
(Seconds (1.1));
154
apps.
Stop
(Seconds (10.0));
155
156
AsciiTraceHelper
ascii;
157
p2p.
EnableAsciiAll
(ascii.
CreateFileStream
(
"simple-alternate-routing.tr"
));
158
p2p.
EnablePcapAll
(
"simple-alternate-routing"
);
159
160
NS_LOG_INFO
(
"Run Simulation."
);
161
Simulator::Run
();
162
Simulator::Destroy
();
163
NS_LOG_INFO
(
"Done."
);
164
165
return
0;
166
}
examples
routing
simple-alternate-routing.cc
Generated on Fri Aug 30 2013 01:42:44 for ns-3 by
1.8.1.2