A Discrete-Event Network Simulator
API
csma-packet-socket.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 //
17 // Network topology
18 //
19 // n0 n1 n2 n3
20 // | | | |
21 // =====================
22 //
23 // - Two packet socket flows: from n0 to n1 and from n3 to n0
24 // - Default 512 byte packets generated by traffic generator
25 // - Output from the PacketSink trace source will be sent to the
26 // csma-packet-socket-sink.tr file
27 // ASCII trace output will be sent to the csma-packet-socket.tr file
28 
29 #include "ns3/applications-module.h"
30 #include "ns3/core-module.h"
31 #include "ns3/csma-module.h"
32 #include "ns3/network-module.h"
33 
34 #include <cassert>
35 #include <fstream>
36 #include <iostream>
37 #include <string>
38 
39 using namespace ns3;
40 
41 NS_LOG_COMPONENT_DEFINE("CsmaPacketSocketExample");
42 
44 std::ofstream g_os;
45 
53 static void
54 SinkRx(std::string path, Ptr<const Packet> p, const Address& address)
55 {
56  g_os << p->GetSize() << std::endl;
57 }
58 
59 int
60 main(int argc, char* argv[])
61 {
62 #if 0
63  LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO);
64 #endif
65 
66  CommandLine cmd(__FILE__);
67  cmd.Parse(argc, argv);
68 
69  g_os.open("csma-packet-socket-sink.tr", std::ios_base::binary | std::ios_base::out);
70 
71  // Here, we will explicitly create four nodes.
72  NS_LOG_INFO("Create nodes.");
74  nodes.Create(4);
75 
76  PacketSocketHelper packetSocket;
77  packetSocket.Install(nodes);
78 
79  // create the shared medium used by all csma devices.
80  NS_LOG_INFO("Create channels.");
82  CreateObjectWithAttributes<CsmaChannel>("DataRate",
83  DataRateValue(DataRate(5000000)),
84  "Delay",
86 
87  // use a helper function to connect our nodes to the shared channel.
88  NS_LOG_INFO("Build Topology.");
90  csma.SetDeviceAttribute("EncapsulationMode", StringValue("Llc"));
91  NetDeviceContainer devs = csma.Install(nodes, channel);
92 
93  NS_LOG_INFO("Create Applications.");
94  // Create the OnOff application to send raw datagrams
95  PacketSocketAddress socket;
96  socket.SetSingleDevice(devs.Get(0)->GetIfIndex());
97  socket.SetPhysicalAddress(devs.Get(1)->GetAddress());
98  socket.SetProtocol(2);
99  OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
100  onoff.SetConstantRate(DataRate("500kb/s"));
101  ApplicationContainer apps = onoff.Install(nodes.Get(0));
102  apps.Start(Seconds(1.0));
103  apps.Stop(Seconds(10.0));
104 
105  socket.SetSingleDevice(devs.Get(3)->GetIfIndex());
106  socket.SetPhysicalAddress(devs.Get(0)->GetAddress());
107  socket.SetProtocol(3);
108  onoff.SetAttribute("Remote", AddressValue(socket));
109  apps = onoff.Install(nodes.Get(3));
110  apps.Start(Seconds(1.0));
111  apps.Stop(Seconds(10.0));
112 
113  // Install packet sink on node 0 to receive packets from node 1
114  PacketSinkHelper sink = PacketSinkHelper("ns3::PacketSocketFactory", socket);
115  apps = sink.Install(nodes.Get(0));
116  apps.Start(Seconds(0.0));
117  apps.Stop(Seconds(20.0));
118 
119  // While the below trace sink is hooked to all nodes (the wildcard "*")
120  // only one packet sink (on node 0) is actually added above, so
121  // only the receive events on node 0 will be traced
122  Config::Connect("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx", MakeCallback(&SinkRx));
123 
124  // Configure tracing of all enqueue, dequeue, and NetDevice receive events
125  // Trace output will be sent to the csma-packet-socket.tr file
126  NS_LOG_INFO("Configure Tracing.");
127 
128  AsciiTraceHelper ascii;
129  csma.EnableAsciiAll(ascii.CreateFileStream("csma-packet-socket.tr"));
130 
131  NS_LOG_INFO("Run Simulation.");
132  Simulator::Run();
134  NS_LOG_INFO("Done.");
135 
136  g_os.close();
137 
138  return 0;
139 }
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
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.
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
Hold variables of type string.
Definition: string.h:56
std::ofstream g_os
Output stream.
static void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
Rx sink.
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:974
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
NodeContainer nodes
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
csma
Definition: second.py:63
cmd
Definition: second.py:40
channel
Definition: third.py:88
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55