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
csma-bridge.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
// Network topology
18
//
19
// n0 n1
20
// | |
21
// ----------
22
// | Switch |
23
// ----------
24
// | |
25
// n2 n3
26
//
27
//
28
// - CBR/UDP flows from n0 to n1 and from n3 to n0
29
// - DropTail queues
30
// - Tracing of queues and packet receptions to file "csma-bridge.tr"
31
32
#include <iostream>
33
#include <fstream>
34
35
#include "ns3/core-module.h"
36
#include "ns3/network-module.h"
37
#include "ns3/applications-module.h"
38
#include "ns3/bridge-module.h"
39
#include "ns3/csma-module.h"
40
#include "ns3/internet-module.h"
41
42
using namespace
ns3;
43
44
NS_LOG_COMPONENT_DEFINE
(
"CsmaBridgeExample"
);
45
46
int
47
main
(
int
argc,
char
*argv[])
48
{
49
//
50
// Users may find it convenient to turn on explicit debugging
51
// for selected modules; the below lines suggest how to do this
52
//
53
#if 0
54
LogComponentEnable
(
"CsmaBridgeExample"
,
LOG_LEVEL_INFO
);
55
#endif
56
57
//
58
// Allow the user to override any of the defaults and the above Bind() at
59
// run-time, via command-line arguments
60
//
61
CommandLine
cmd;
62
cmd.
Parse
(argc, argv);
63
64
//
65
// Explicitly create the nodes required by the topology (shown above).
66
//
67
NS_LOG_INFO
(
"Create nodes."
);
68
NodeContainer
terminals;
69
terminals.
Create
(4);
70
71
NodeContainer
csmaSwitch;
72
csmaSwitch.
Create
(1);
73
74
NS_LOG_INFO
(
"Build Topology"
);
75
CsmaHelper
csma;
76
csma.
SetChannelAttribute
(
"DataRate"
,
DataRateValue
(5000000));
77
csma.
SetChannelAttribute
(
"Delay"
,
TimeValue
(
MilliSeconds
(2)));
78
79
// Create the csma links, from each terminal to the switch
80
81
NetDeviceContainer
terminalDevices;
82
NetDeviceContainer
switchDevices;
83
84
for
(
int
i = 0; i < 4; i++)
85
{
86
NetDeviceContainer
link = csma.
Install
(
NodeContainer
(terminals.
Get
(i), csmaSwitch));
87
terminalDevices.
Add
(link.
Get
(0));
88
switchDevices.
Add
(link.
Get
(1));
89
}
90
91
// Create the bridge netdevice, which will do the packet switching
92
Ptr<Node>
switchNode = csmaSwitch.
Get
(0);
93
BridgeHelper
bridge;
94
bridge.
Install
(switchNode, switchDevices);
95
96
// Add internet stack to the terminals
97
InternetStackHelper
internet;
98
internet.
Install
(terminals);
99
100
// We've got the "hardware" in place. Now we need to add IP addresses.
101
//
102
NS_LOG_INFO
(
"Assign IP Addresses."
);
103
Ipv4AddressHelper
ipv4;
104
ipv4.
SetBase
(
"10.1.1.0"
,
"255.255.255.0"
);
105
ipv4.
Assign
(terminalDevices);
106
107
//
108
// Create an OnOff application to send UDP datagrams from node zero to node 1.
109
//
110
NS_LOG_INFO
(
"Create Applications."
);
111
uint16_t
port
= 9;
// Discard port (RFC 863)
112
113
OnOffHelper
onoff (
"ns3::UdpSocketFactory"
,
114
Address
(
InetSocketAddress
(
Ipv4Address
(
"10.1.1.2"
), port)));
115
onoff.
SetConstantRate
(
DataRate
(
"500kb/s"
));
116
117
ApplicationContainer
app = onoff.
Install
(terminals.
Get
(0));
118
// Start the application
119
app.
Start
(
Seconds
(1.0));
120
app.
Stop
(
Seconds
(10.0));
121
122
// Create an optional packet sink to receive these packets
123
PacketSinkHelper
sink (
"ns3::UdpSocketFactory"
,
124
Address
(
InetSocketAddress
(
Ipv4Address::GetAny
(), port)));
125
app = sink.
Install
(terminals.
Get
(1));
126
app.
Start
(
Seconds
(0.0));
127
128
//
129
// Create a similar flow from n3 to n0, starting at time 1.1 seconds
130
//
131
onoff.
SetAttribute
(
"Remote"
,
132
AddressValue
(
InetSocketAddress
(
Ipv4Address
(
"10.1.1.1"
), port)));
133
app = onoff.
Install
(terminals.
Get
(3));
134
app.
Start
(
Seconds
(1.1));
135
app.
Stop
(
Seconds
(10.0));
136
137
app = sink.
Install
(terminals.
Get
(0));
138
app.
Start
(
Seconds
(0.0));
139
140
NS_LOG_INFO
(
"Configure Tracing."
);
141
142
//
143
// Configure tracing of all enqueue, dequeue, and NetDevice receive events.
144
// Trace output will be sent to the file "csma-bridge.tr"
145
//
146
AsciiTraceHelper
ascii;
147
csma.
EnableAsciiAll
(ascii.
CreateFileStream
(
"csma-bridge.tr"
));
148
149
//
150
// Also configure some tcpdump traces; each interface will be traced.
151
// The output files will be named:
152
// csma-bridge-<nodeId>-<interfaceId>.pcap
153
// and can be read by the "tcpdump -r" command (use "-tt" option to
154
// display timestamps correctly)
155
//
156
csma.
EnablePcapAll
(
"csma-bridge"
,
false
);
157
158
//
159
// Now, do the actual simulation.
160
//
161
NS_LOG_INFO
(
"Run Simulation."
);
162
Simulator::Run
();
163
Simulator::Destroy
();
164
NS_LOG_INFO
(
"Done."
);
165
}
src
bridge
examples
csma-bridge.cc
Generated on Tue Oct 9 2012 16:45:33 for ns-3 by
1.8.1.2