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