A Discrete-Event Network Simulator
API
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 
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.");
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 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
Hold variables of type string.
Definition: string.h:41
an address for a packet socket
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1022
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
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:90
channel
Definition: third.py:85
Class for representing data rates.
Definition: data-rate.h:88
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
nodes
Definition: first.py:25
AttributeValue implementation for Time.
Definition: nstime.h:1076
LOG_INFO and above.
Definition: log.h:104
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
csma
Definition: second.py:63
Parse command-line arguments.
Definition: command-line.h:213
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:843
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetPhysicalAddress(const Address address)
Set the destination address.
keep track of a set of node pointers.
address
Definition: first.py:37
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
AttributeValue implementation for Address.
Definition: address.h:278
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
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void SetProtocol(uint16_t protocol)
Set the protocol.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
static void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)