A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-packet-socket.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5//
6// Network topology
7//
8// n0 n1 n2 n3
9// | | | |
10// =====================
11//
12// - Two packet socket flows: from n0 to n1 and from n3 to n0
13// - Default 512 byte packets generated by traffic generator
14// - Output from the PacketSink trace source will be sent to the
15// csma-packet-socket-sink.tr file
16// ASCII trace output will be sent to the csma-packet-socket.tr file
17
18#include "ns3/applications-module.h"
19#include "ns3/core-module.h"
20#include "ns3/csma-module.h"
21#include "ns3/network-module.h"
22
23#include <cassert>
24#include <fstream>
25#include <iostream>
26#include <string>
27
28using namespace ns3;
29
30NS_LOG_COMPONENT_DEFINE("CsmaPacketSocketExample");
31
32/// Output stream.
33std::ofstream g_os;
34
35/**
36 * Rx sink
37 *
38 * \param path The context.
39 * \param p The packet.
40 * \param address The sender address.
41 */
42static void
43SinkRx(std::string path, Ptr<const Packet> p, const Address& address)
44{
45 g_os << p->GetSize() << std::endl;
46}
47
48int
49main(int argc, char* argv[])
50{
51#if 0
52 LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO);
53#endif
54
55 CommandLine cmd(__FILE__);
56 cmd.Parse(argc, argv);
57
58 g_os.open("csma-packet-socket-sink.tr", std::ios_base::binary | std::ios_base::out);
59
60 // Here, we will explicitly create four nodes.
61 NS_LOG_INFO("Create nodes.");
63 nodes.Create(4);
64
65 PacketSocketHelper packetSocket;
66 packetSocket.Install(nodes);
67
68 // create the shared medium used by all csma devices.
69 NS_LOG_INFO("Create channels.");
72 DataRateValue(DataRate(5000000)),
73 "Delay",
75
76 // use a helper function to connect our nodes to the shared channel.
77 NS_LOG_INFO("Build Topology.");
79 csma.SetDeviceAttribute("EncapsulationMode", StringValue("Llc"));
80 NetDeviceContainer devs = csma.Install(nodes, channel);
81
82 NS_LOG_INFO("Create Applications.");
83 // Create the OnOff application to send raw datagrams
85 socket.SetSingleDevice(devs.Get(0)->GetIfIndex());
86 socket.SetPhysicalAddress(devs.Get(1)->GetAddress());
87 socket.SetProtocol(2);
88 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
89 onoff.SetConstantRate(DataRate("500kb/s"));
90 ApplicationContainer apps = onoff.Install(nodes.Get(0));
91 apps.Start(Seconds(1.0));
92 apps.Stop(Seconds(10.0));
93
94 socket.SetSingleDevice(devs.Get(3)->GetIfIndex());
95 socket.SetPhysicalAddress(devs.Get(0)->GetAddress());
96 socket.SetProtocol(3);
97 onoff.SetAttribute("Remote", AddressValue(socket));
98 apps = onoff.Install(nodes.Get(3));
99 apps.Start(Seconds(1.0));
100 apps.Stop(Seconds(10.0));
101
102 // Install packet sink on node 0 to receive packets from node 1
103 PacketSinkHelper sink = PacketSinkHelper("ns3::PacketSocketFactory", socket);
104 apps = sink.Install(nodes.Get(0));
105 apps.Start(Seconds(0.0));
106 apps.Stop(Seconds(20.0));
107
108 // While the below trace sink is hooked to all nodes (the wildcard "*")
109 // only one packet sink (on node 0) is actually added above, so
110 // only the receive events on node 0 will be traced
111 Config::Connect("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx", MakeCallback(&SinkRx));
112
113 // Configure tracing of all enqueue, dequeue, and NetDevice receive events
114 // Trace output will be sent to the csma-packet-socket.tr file
115 NS_LOG_INFO("Configure Tracing.");
116
117 AsciiTraceHelper ascii;
118 csma.EnableAsciiAll(ascii.CreateFileStream("csma-packet-socket.tr"));
119
120 NS_LOG_INFO("Run Simulation.");
123 NS_LOG_INFO("Done.");
124
125 g_os.close();
126
127 return 0;
128}
a polymophic address class
Definition address.h:90
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.
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.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
Class for representing data rates.
Definition data-rate.h:78
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.
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.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold variables of type string.
Definition string.h:45
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:967
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObjectWithAttributes(Args... args)
Allocate an Object on the heap and initialize with a set of attributes.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
NodeContainer nodes
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:291
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:684
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
channel
Definition third.py:77
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44