A Discrete-Event Network Simulator
API
 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.");
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 ();
127  NS_LOG_INFO ("Done.");
128 
129  g_os.close ();
130 
131  return 0;
132 }
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:141
hold variables of type string
Definition: string.h:18
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
an address for a packet socket
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits. ...
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
Give ns3::PacketSocket powers to ns3::Node.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
a polymophic address class
Definition: address.h:86
int main(int argc, char *argv[])
tuple nodes
Definition: first.py:25
Class for representing data rates.
Definition: data-rate.h:71
hold objects of type ns3::Time
Definition: nstime.h:1008
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:177
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
void SetPhysicalAddress(const Address address)
Set the destination address.
keep track of a set of node pointers.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:63
hold objects of type ns3::Address
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
std::ofstream g_os
hold objects of type ns3::DataRate
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void SetProtocol(uint16_t protocol)
Set the protocol.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
void Parse(int argc, char *argv[])
Parse the program arguments.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
tuple address
Definition: first.py:37
static void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:318