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-packet-socket.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
// Network topology
19
//
20
// n0 n1 n2 n3
21
// | | | |
22
// =====================
23
//
24
// - Two packet socket flows: from n0 to n1 and from n3 to n0
25
// - Default 512 byte packets generated by traffic generator
26
// - Output from the PacketSink trace source will be sent to the
27
// csma-packet-socket-sink.tr file
28
// ASCII trace output will be sent to the csma-packet-socket.tr file
29
30
#include <iostream>
31
#include <fstream>
32
#include <string>
33
#include <cassert>
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
40
using namespace
ns3;
41
42
NS_LOG_COMPONENT_DEFINE
(
"CsmaPacketSocketExample"
);
43
44
std::ofstream
g_os
;
45
46
static
void
47
SinkRx
(std::string path,
Ptr<const Packet>
p,
const
Address
&address)
48
{
49
g_os
<< p->
GetSize
() << std::endl;
50
}
51
52
int
53
main
(
int
argc,
char
*argv[])
54
{
55
#if 0
56
LogComponentEnable
(
"CsmaPacketSocketExample"
,
LOG_LEVEL_INFO
);
57
#endif
58
59
CommandLine
cmd;
60
cmd.
Parse
(argc, argv);
61
62
g_os
.open (
"csma-packet-socket-sink.tr"
,std::ios_base::binary | std::ios_base::out);
63
64
// Here, we will explicitly create four nodes.
65
NS_LOG_INFO
(
"Create nodes."
);
66
NodeContainer
nodes;
67
nodes.
Create
(4);
68
69
PacketSocketHelper
packetSocket;
70
packetSocket.
Install
(nodes);
71
72
// create the shared medium used by all csma devices.
73
NS_LOG_INFO
(
"Create channels."
);
74
Ptr<CsmaChannel>
channel = CreateObjectWithAttributes<CsmaChannel> (
75
"DataRate"
,
DataRateValue
(
DataRate
(5000000)),
76
"Delay"
,
TimeValue
(
MilliSeconds
(2)));
77
78
// use a helper function to connect our nodes to the shared channel.
79
NS_LOG_INFO
(
"Build Topology."
);
80
CsmaHelper
csma;
81
csma.
SetDeviceAttribute
(
"EncapsulationMode"
,
StringValue
(
"Llc"
));
82
NetDeviceContainer
devs = csma.
Install
(nodes, channel);
83
84
NS_LOG_INFO
(
"Create Applications."
);
85
// Create the OnOff application to send raw datagrams
86
PacketSocketAddress
socket;
87
socket.
SetSingleDevice
(devs.
Get
(0)->
GetIfIndex
());
88
socket.
SetPhysicalAddress
(devs.
Get
(1)->
GetAddress
());
89
socket.
SetProtocol
(2);
90
OnOffHelper
onoff (
"ns3::PacketSocketFactory"
,
Address
(socket));
91
onoff.
SetConstantRate
(
DataRate
(
"500kb/s"
));
92
ApplicationContainer
apps = onoff.
Install
(nodes.
Get
(0));
93
apps.
Start
(
Seconds
(1.0));
94
apps.
Stop
(
Seconds
(10.0));
95
96
socket.
SetSingleDevice
(devs.
Get
(3)->
GetIfIndex
());
97
socket.
SetPhysicalAddress
(devs.
Get
(0)->
GetAddress
());
98
socket.
SetProtocol
(3);
99
onoff.
SetAttribute
(
"Remote"
,
AddressValue
(socket));
100
apps = onoff.
Install
(nodes.
Get
(3));
101
apps.
Start
(
Seconds
(1.0));
102
apps.
Stop
(
Seconds
(10.0));
103
104
// Install packet sink on node 0 to receive packets from node 1
105
PacketSinkHelper
sink =
PacketSinkHelper
(
"ns3::PacketSocketFactory"
,
106
socket);
107
apps = sink.
Install
(nodes.
Get
(0));
108
apps.
Start
(
Seconds
(0.0));
109
apps.
Stop
(
Seconds
(20.0));
110
111
// While the below trace sink is hooked to all nodes (the wildcard "*")
112
// only one packet sink (on node 0) is actually added above, so
113
// only the receive events on node 0 will be traced
114
Config::Connect
(
"/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx"
,
115
MakeCallback
(&
SinkRx
));
116
117
// Configure tracing of all enqueue, dequeue, and NetDevice receive events
118
// Trace output will be sent to the csma-packet-socket.tr file
119
NS_LOG_INFO
(
"Configure Tracing."
);
120
121
AsciiTraceHelper
ascii;
122
csma.
EnableAsciiAll
(ascii.
CreateFileStream
(
"csma-packet-socket.tr"
));
123
124
NS_LOG_INFO
(
"Run Simulation."
);
125
Simulator::Run
();
126
Simulator::Destroy
();
127
NS_LOG_INFO
(
"Done."
);
128
129
g_os
.close ();
130
131
return
0;
132
}
src
csma
examples
csma-packet-socket.cc
Generated on Tue May 14 2013 11:08:19 for ns-3 by
1.8.1.2