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-multicast.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
// Lan1
20
// ===========
21
// | | |
22
// n0 n1 n2 n3 n4
23
// | | |
24
// ===========
25
// Lan0
26
//
27
// - Multicast source is at node n0;
28
// - Multicast forwarded by node n2 onto LAN1;
29
// - Nodes n0, n1, n2, n3, and n4 receive the multicast frame.
30
// - Node n4 listens for the data
31
32
#include <iostream>
33
#include <fstream>
34
35
#include "ns3/core-module.h"
36
#include "ns3/network-module.h"
37
#include "ns3/csma-module.h"
38
#include "ns3/applications-module.h"
39
#include "ns3/internet-module.h"
40
41
using namespace
ns3;
42
43
NS_LOG_COMPONENT_DEFINE
(
"CsmaMulticastExample"
);
44
45
int
46
main
(
int
argc,
char
*argv[])
47
{
48
//
49
// Users may find it convenient to turn on explicit debugging
50
// for selected modules; the below lines suggest how to do this
51
//
52
// LogComponentEnable ("CsmaMulticastExample", LOG_LEVEL_INFO);
53
54
//
55
// Set up default values for the simulation.
56
//
57
// Select DIX/Ethernet II-style encapsulation (no LLC/Snap header)
58
Config::SetDefault
(
"ns3::CsmaNetDevice::EncapsulationMode"
,
StringValue
(
"Dix"
));
59
60
// Allow the user to override any of the defaults at
61
// run-time, via command-line arguments
62
CommandLine
cmd;
63
cmd.
Parse
(argc, argv);
64
65
NS_LOG_INFO
(
"Create nodes."
);
66
NodeContainer
c;
67
c.
Create
(5);
68
// We will later want two subcontainers of these nodes, for the two LANs
69
NodeContainer
c0 =
NodeContainer
(c.
Get
(0), c.
Get
(1), c.
Get
(2));
70
NodeContainer
c1 =
NodeContainer
(c.
Get
(2), c.
Get
(3), c.
Get
(4));
71
72
NS_LOG_INFO
(
"Build Topology."
);
73
CsmaHelper
csma;
74
csma.
SetChannelAttribute
(
"DataRate"
,
DataRateValue
(
DataRate
(5000000)));
75
csma.
SetChannelAttribute
(
"Delay"
,
TimeValue
(
MilliSeconds
(2)));
76
77
// We will use these NetDevice containers later, for IP addressing
78
NetDeviceContainer
nd0 = csma.
Install
(c0);
// First LAN
79
NetDeviceContainer
nd1 = csma.
Install
(c1);
// Second LAN
80
81
NS_LOG_INFO
(
"Add IP Stack."
);
82
InternetStackHelper
internet;
83
internet.
Install
(c);
84
85
NS_LOG_INFO
(
"Assign IP Addresses."
);
86
Ipv4AddressHelper
ipv4Addr;
87
ipv4Addr.
SetBase
(
"10.1.1.0"
,
"255.255.255.0"
);
88
ipv4Addr.
Assign
(nd0);
89
ipv4Addr.
SetBase
(
"10.1.2.0"
,
"255.255.255.0"
);
90
ipv4Addr.
Assign
(nd1);
91
92
NS_LOG_INFO
(
"Configure multicasting."
);
93
//
94
// Now we can configure multicasting. As described above, the multicast
95
// source is at node zero, which we assigned the IP address of 10.1.1.1
96
// earlier. We need to define a multicast group to send packets to. This
97
// can be any multicast address from 224.0.0.0 through 239.255.255.255
98
// (avoiding the reserved routing protocol addresses).
99
//
100
101
Ipv4Address
multicastSource (
"10.1.1.1"
);
102
Ipv4Address
multicastGroup (
"225.1.2.4"
);
103
104
// Now, we will set up multicast routing. We need to do three things:
105
// 1) Configure a (static) multicast route on node n2
106
// 2) Set up a default multicast route on the sender n0
107
// 3) Have node n4 join the multicast group
108
// We have a helper that can help us with static multicast
109
Ipv4StaticRoutingHelper
multicast;
110
111
// 1) Configure a (static) multicast route on node n2 (multicastRouter)
112
Ptr<Node>
multicastRouter = c.
Get
(2);
// The node in question
113
Ptr<NetDevice>
inputIf = nd0.
Get
(2);
// The input NetDevice
114
NetDeviceContainer
outputDevices;
// A container of output NetDevices
115
outputDevices.
Add
(nd1.
Get
(0));
// (we only need one NetDevice here)
116
117
multicast.
AddMulticastRoute
(multicastRouter, multicastSource,
118
multicastGroup, inputIf, outputDevices);
119
120
// 2) Set up a default multicast route on the sender n0
121
Ptr<Node>
sender = c.
Get
(0);
122
Ptr<NetDevice>
senderIf = nd0.
Get
(0);
123
multicast.
SetDefaultMulticastRoute
(sender, senderIf);
124
125
//
126
// Create an OnOff application to send UDP datagrams from node zero to the
127
// multicast group (node four will be listening).
128
//
129
NS_LOG_INFO
(
"Create Applications."
);
130
131
uint16_t multicastPort = 9;
// Discard port (RFC 863)
132
133
// Configure a multicast packet generator that generates a packet
134
// every few seconds
135
OnOffHelper
onoff (
"ns3::UdpSocketFactory"
,
136
Address
(
InetSocketAddress
(multicastGroup, multicastPort)));
137
onoff.
SetConstantRate
(
DataRate
(
"255b/s"
));
138
onoff.
SetAttribute
(
"PacketSize"
,
UintegerValue
(128));
139
140
ApplicationContainer
srcC = onoff.
Install
(c0.
Get
(0));
141
142
//
143
// Tell the application when to start and stop.
144
//
145
srcC.
Start
(
Seconds
(1.));
146
srcC.
Stop
(
Seconds
(10.));
147
148
// Create an optional packet sink to receive these packets
149
PacketSinkHelper
sink (
"ns3::UdpSocketFactory"
,
150
InetSocketAddress
(
Ipv4Address::GetAny
(), multicastPort));
151
152
ApplicationContainer
sinkC = sink.
Install
(c1.Get (2));
// Node n4
153
// Start the sink
154
sinkC.
Start
(
Seconds
(1.0));
155
sinkC.
Stop
(
Seconds
(10.0));
156
157
NS_LOG_INFO
(
"Configure Tracing."
);
158
//
159
// Configure tracing of all enqueue, dequeue, and NetDevice receive events.
160
// Ascii trace output will be sent to the file "csma-multicast.tr"
161
//
162
AsciiTraceHelper
ascii;
163
csma.
EnableAsciiAll
(ascii.
CreateFileStream
(
"csma-multicast.tr"
));
164
165
// Also configure some tcpdump traces; each interface will be traced.
166
// The output files will be named:
167
// csma-multicast-<nodeId>-<interfaceId>.pcap
168
// and can be read by the "tcpdump -r" command (use "-tt" option to
169
// display timestamps correctly)
170
csma.
EnablePcapAll
(
"csma-multicast"
,
false
);
171
172
//
173
// Now, do the actual simulation.
174
//
175
NS_LOG_INFO
(
"Run Simulation."
);
176
Simulator::Run
();
177
Simulator::Destroy
();
178
NS_LOG_INFO
(
"Done."
);
179
}
src
csma
examples
csma-multicast.cc
Generated on Tue Oct 9 2012 16:45:36 for ns-3 by
1.8.1.2