A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
adhoc-aloha-ideal-phy.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 CTTC
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Nicola Baldo <nbaldo@cttc.es>
18 */
19
20#include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
21#include <ns3/applications-module.h>
22#include <ns3/core-module.h>
23#include <ns3/friis-spectrum-propagation-loss.h>
24#include <ns3/ism-spectrum-value-helper.h>
25#include <ns3/log.h>
26#include <ns3/mobility-module.h>
27#include <ns3/network-module.h>
28#include <ns3/propagation-delay-model.h>
29#include <ns3/single-model-spectrum-channel.h>
30#include <ns3/spectrum-analyzer.h>
31#include <ns3/spectrum-helper.h>
32#include <ns3/spectrum-model-300kHz-300GHz-log.h>
33#include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
34#include <ns3/waveform-generator.h>
35
36#include <iostream>
37#include <string>
38
39using namespace ns3;
40
41NS_LOG_COMPONENT_DEFINE("TestAdhocOfdmAloha");
42
43/// True for verbose output.
44static bool g_verbose = false;
45
46/**
47 * PHY start TX trace.
48 *
49 * \param context The context.
50 * \param p The packet.
51 */
52void
53PhyTxStartTrace(std::string context, Ptr<const Packet> p)
54{
55 if (g_verbose)
56 {
57 std::cout << context << " PHY TX START p: " << p << std::endl;
58 }
59}
60
61/**
62 * PHY end TX trace.
63 *
64 * \param context The context.
65 * \param p The packet.
66 */
67void
68PhyTxEndTrace(std::string context, Ptr<const Packet> p)
69{
70 if (g_verbose)
71 {
72 std::cout << context << " PHY TX END p: " << p << std::endl;
73 }
74}
75
76/**
77 * PHY start RX trace.
78 *
79 * \param context The context.
80 * \param p The packet.
81 */
82void
83PhyRxStartTrace(std::string context, Ptr<const Packet> p)
84{
85 if (g_verbose)
86 {
87 std::cout << context << " PHY RX START p:" << p << std::endl;
88 }
89}
90
91/**
92 * PHY end OK RX trace.
93 *
94 * \param context The context.
95 * \param p The packet.
96 */
97void
98PhyRxEndOkTrace(std::string context, Ptr<const Packet> p)
99{
100 if (g_verbose)
101 {
102 std::cout << context << " PHY RX END OK p:" << p << std::endl;
103 }
104}
105
106/**
107 * PHY end error RX trace.
108 *
109 * \param context The context.
110 * \param p The packet.
111 */
112void
114{
115 if (g_verbose)
116 {
117 std::cout << context << " PHY RX END ERROR p:" << p << std::endl;
118 }
119}
120
121/**
122 * Receive callback.
123 *
124 * \param socket The receiving socket.
125 */
126void
128{
129 Ptr<Packet> packet;
130 uint64_t bytes = 0;
131 while ((packet = socket->Recv()))
132 {
133 bytes += packet->GetSize();
134 }
135 if (g_verbose)
136 {
137 std::cout << "SOCKET received " << bytes << " bytes" << std::endl;
138 }
139}
140
141/**
142 * Create a socket and prepare it for packet reception.
143 *
144 * \param node The node.
145 * \return a new socket
146 */
149{
150 TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
152 sink->Bind();
153 sink->SetRecvCallback(MakeCallback(&ReceivePacket));
154 return sink;
155}
156
157int
158main(int argc, char** argv)
159{
160 CommandLine cmd(__FILE__);
161 cmd.AddValue("verbose", "Print trace information if true", g_verbose);
162 cmd.Parse(argc, argv);
163
165 c.Create(2);
166
168 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
169 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
170 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
171 mobility.SetPositionAllocator(positionAlloc);
172 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
173
174 mobility.Install(c);
175
177 Ptr<SpectrumChannel> channel = channelHelper.Create();
178
180
181 double txPower = 0.1; // Watts
182 uint32_t channelNumber = 1;
183 Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity(txPower, channelNumber);
184
185 // for the noise, we use the Power Spectral Density of thermal noise
186 // at room temperature. The value of the PSD will be constant over the band of interest.
187 const double k = 1.381e-23; // Boltzmann's constant
188 const double T = 290; // temperature in Kelvin
189 double noisePsdValue = k * T; // watts per hertz
190 Ptr<SpectrumValue> noisePsd = sf.CreateConstant(noisePsdValue);
191
193 deviceHelper.SetChannel(channel);
194 deviceHelper.SetTxPowerSpectralDensity(txPsd);
195 deviceHelper.SetNoisePowerSpectralDensity(noisePsd);
196 deviceHelper.SetPhyAttribute("Rate", DataRateValue(DataRate("1Mbps")));
197 NetDeviceContainer devices = deviceHelper.Install(c);
198
199 PacketSocketHelper packetSocket;
200 packetSocket.Install(c);
201
202 PacketSocketAddress socket;
203 socket.SetSingleDevice(devices.Get(0)->GetIfIndex());
204 socket.SetPhysicalAddress(devices.Get(1)->GetAddress());
205 socket.SetProtocol(1);
206
207 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
208 onoff.SetConstantRate(DataRate("0.5Mbps"));
209 onoff.SetAttribute("PacketSize", UintegerValue(125));
210
211 ApplicationContainer apps = onoff.Install(c.Get(0));
212 apps.Start(Seconds(0.1));
213 apps.Stop(Seconds(0.104));
214
215 Ptr<Socket> recvSink = SetupPacketReceive(c.Get(1));
216
218
219 Config::Connect("/NodeList/*/DeviceList/*/Phy/TxStart", MakeCallback(&PhyTxStartTrace));
220 Config::Connect("/NodeList/*/DeviceList/*/Phy/TxEnd", MakeCallback(&PhyTxEndTrace));
221 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxStart", MakeCallback(&PhyRxStartTrace));
222 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback(&PhyRxEndOkTrace));
223 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndError", MakeCallback(&PhyRxEndErrorTrace));
224
226
228
229 return 0;
230}
void PhyTxEndTrace(std::string context, Ptr< const Packet > p)
PHY end TX trace.
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Create a socket and prepare it for packet reception.
void PhyRxEndErrorTrace(std::string context, Ptr< const Packet > p)
PHY end error RX trace.
void PhyRxStartTrace(std::string context, Ptr< const Packet > p)
PHY start RX trace.
void ReceivePacket(Ptr< Socket > socket)
Receive callback.
static bool g_verbose
True for verbose output.
void PhyRxEndOkTrace(std::string context, Ptr< const Packet > p)
PHY end OK RX trace.
void PhyTxStartTrace(std::string context, Ptr< const Packet > p)
PHY start TX trace.
a polymophic address class
Definition: address.h:101
void SetPhyAttribute(std::string name, const AttributeValue &v)
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
void SetNoisePowerSpectralDensity(Ptr< SpectrumValue > noisePsd)
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper
NetDeviceContainer Install(NodeContainer c) const
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.
Parse command-line arguments.
Definition: command-line.h:232
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
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:37
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.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:72
Setup a SpectrumChannel.
Ptr< SpectrumChannel > Create() const
static SpectrumChannelHelper Default()
Setup a default SpectrumChannel.
Implements Wifi SpectrumValue for the 2.4 GHz ISM band only, with a 5 MHz spectrum resolution.
virtual Ptr< SpectrumValue > CreateConstant(double psd)
Creates a SpectrumValue instance with a constant value for all frequencies.
virtual Ptr< SpectrumValue > CreateTxPowerSpectralDensity(double txPower, uint8_t channel)
Creates a SpectrumValue instance that represents the TX Power Spectral Density of a wifi device corre...
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
Hold an unsigned integer type.
Definition: uinteger.h:45
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:978
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
ns devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:706
ns cmd
Definition: second.py:40
ns channel
Definition: third.py:88
ns mobility
Definition: third.py:105
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55