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-ping.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
//
23
// node n0,n1,n3 pings to node n2
24
// node n0 generates protocol 2 (IGMP) to node n3
25
26
#include <iostream>
27
#include <fstream>
28
#include <string>
29
#include <cassert>
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
(
"CsmaPingExample"
);
40
41
static
void
SinkRx
(
Ptr<const Packet>
p,
const
Address
&ad)
42
{
43
//std::cout << *p << std::endl;
44
}
45
46
static
void
PingRtt
(std::string context,
Time
rtt)
47
{
48
//std::cout << context << " " << rtt << std::endl;
49
}
50
51
int
52
main
(
int
argc,
char
*argv[])
53
{
54
55
CommandLine
cmd;
56
cmd.
Parse
(argc, argv);
57
58
// Here, we will explicitly create four nodes.
59
NS_LOG_INFO
(
"Create nodes."
);
60
NodeContainer
c;
61
c.
Create
(4);
62
63
// connect all our nodes to a shared channel.
64
NS_LOG_INFO
(
"Build Topology."
);
65
CsmaHelper
csma;
66
csma.
SetChannelAttribute
(
"DataRate"
,
DataRateValue
(
DataRate
(5000000)));
67
csma.
SetChannelAttribute
(
"Delay"
,
TimeValue
(
MilliSeconds
(2)));
68
csma.
SetDeviceAttribute
(
"EncapsulationMode"
,
StringValue
(
"Llc"
));
69
NetDeviceContainer
devs = csma.
Install
(c);
70
71
// add an ip stack to all nodes.
72
NS_LOG_INFO
(
"Add ip stack."
);
73
InternetStackHelper
ipStack;
74
ipStack.
Install
(c);
75
76
// assign ip addresses
77
NS_LOG_INFO
(
"Assign ip addresses."
);
78
Ipv4AddressHelper
ip;
79
ip.
SetBase
(
"192.168.1.0"
,
"255.255.255.0"
);
80
Ipv4InterfaceContainer
addresses = ip.
Assign
(devs);
81
82
NS_LOG_INFO
(
"Create Source"
);
83
Config::SetDefault
(
"ns3::Ipv4RawSocketImpl::Protocol"
,
StringValue
(
"2"
));
84
InetSocketAddress
dst =
InetSocketAddress
(addresses.
GetAddress
(3));
85
OnOffHelper
onoff =
OnOffHelper
(
"ns3::Ipv4RawSocketFactory"
, dst);
86
onoff.SetConstantRate (
DataRate
(15000));
87
onoff.SetAttribute (
"PacketSize"
,
UintegerValue
(1200));
88
89
90
ApplicationContainer
apps = onoff.Install (c.
Get
(0));
91
apps.
Start
(
Seconds
(1.0));
92
apps.
Stop
(
Seconds
(10.0));
93
94
NS_LOG_INFO
(
"Create Sink."
);
95
PacketSinkHelper
sink =
PacketSinkHelper
(
"ns3::Ipv4RawSocketFactory"
, dst);
96
apps = sink.
Install
(c.
Get
(3));
97
apps.
Start
(
Seconds
(0.0));
98
apps.
Stop
(
Seconds
(11.0));
99
100
NS_LOG_INFO
(
"Create pinger"
);
101
V4PingHelper
ping =
V4PingHelper
(addresses.
GetAddress
(2));
102
NodeContainer
pingers;
103
pingers.
Add
(c.
Get
(0));
104
pingers.Add (c.
Get
(1));
105
pingers.Add (c.
Get
(3));
106
apps = ping.
Install
(pingers);
107
apps.
Start
(
Seconds
(2.0));
108
apps.
Stop
(
Seconds
(5.0));
109
110
NS_LOG_INFO
(
"Configure Tracing."
);
111
// first, pcap tracing in non-promiscuous mode
112
csma.
EnablePcapAll
(
"csma-ping"
,
false
);
113
114
// then, print what the packet sink receives.
115
Config::ConnectWithoutContext
(
"/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx"
,
116
MakeCallback
(&
SinkRx
));
117
// finally, print the ping rtts.
118
Config::Connect
(
"/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt"
,
119
MakeCallback
(&
PingRtt
));
120
121
Packet::EnablePrinting
();
122
123
124
NS_LOG_INFO
(
"Run Simulation."
);
125
Simulator::Run
();
126
Simulator::Destroy
();
127
NS_LOG_INFO
(
"Done."
);
128
}
src
csma
examples
csma-ping.cc
Generated on Tue Nov 13 2012 10:32:12 for ns-3 by
1.8.1.2