A Discrete-Event Network Simulator
API
adhoc-aloha-ideal-phy.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 CTTC
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 
22 
23 #include <iostream>
24 
25 #include <ns3/core-module.h>
26 #include <ns3/network-module.h>
27 #include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
28 #include <ns3/spectrum-model-300kHz-300GHz-log.h>
29 #include <ns3/wifi-spectrum-value-helper.h>
30 #include <ns3/single-model-spectrum-channel.h>
31 #include <ns3/waveform-generator.h>
32 #include <ns3/spectrum-analyzer.h>
33 #include <ns3/log.h>
34 #include <string>
35 #include <ns3/friis-spectrum-propagation-loss.h>
36 #include <ns3/propagation-delay-model.h>
37 #include <ns3/mobility-module.h>
38 #include <ns3/spectrum-helper.h>
39 #include <ns3/applications-module.h>
40 #include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
41 
42 using namespace ns3;
43 
44 NS_LOG_COMPONENT_DEFINE ("TestAdhocOfdmAloha");
45 
46 static bool g_verbose = false;
47 
48 void
49 PhyTxStartTrace (std::string context, Ptr<const Packet> p)
50 {
51  if (g_verbose)
52  {
53  std::cout << context << " PHY TX START p: " << p << std::endl;
54  }
55 }
56 
57 
58 void
59 PhyTxEndTrace (std::string context, Ptr<const Packet> p)
60 {
61  if (g_verbose)
62  {
63  std::cout << context << " PHY TX END p: " << p << std::endl;
64  }
65 }
66 
67 void
68 PhyRxStartTrace (std::string context, Ptr<const Packet> p)
69 {
70  if (g_verbose)
71  {
72  std::cout << context << " PHY RX START p:" << p << std::endl;
73  }
74 }
75 
76 void
77 PhyRxEndOkTrace (std::string context, Ptr<const Packet> p)
78 {
79  if (g_verbose)
80  {
81  std::cout << context << " PHY RX END OK p:" << p << std::endl;
82  }
83 }
84 
85 void
86 PhyRxEndErrorTrace (std::string context, Ptr<const Packet> p)
87 {
88  if (g_verbose)
89  {
90  std::cout << context << " PHY RX END ERROR p:" << p << std::endl;
91  }
92 }
93 
94 
95 void
97 {
98  Ptr<Packet> packet;
99  uint64_t bytes = 0;
100  while ((packet = socket->Recv ()))
101  {
102  bytes += packet->GetSize ();
103  }
104  if (g_verbose)
105  {
106  std::cout << "SOCKET received " << bytes << " bytes" << std::endl;
107  }
108 }
109 
112 {
113  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
114  Ptr<Socket> sink = Socket::CreateSocket (node, tid);
115  sink->Bind ();
116  sink->SetRecvCallback (MakeCallback (&ReceivePacket));
117  return sink;
118 }
119 
120 int main (int argc, char** argv)
121 {
122  CommandLine cmd (__FILE__);
123  cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
124  cmd.Parse (argc, argv);
125 
126  NodeContainer c;
127  c.Create (2);
128 
130  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
131  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
132  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
133  mobility.SetPositionAllocator (positionAlloc);
134  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
135 
136  mobility.Install (c);
137 
138 
140  Ptr<SpectrumChannel> channel = channelHelper.Create ();
141 
143 
144  double txPower = 0.1; // Watts
145  uint32_t channelNumber = 1;
146  Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity (txPower, channelNumber);
147 
148  // for the noise, we use the Power Spectral Density of thermal noise
149  // at room temperature. The value of the PSD will be constant over the band of interest.
150  const double k = 1.381e-23; //Boltzmann's constant
151  const double T = 290; // temperature in Kelvin
152  double noisePsdValue = k * T; // watts per hertz
153  Ptr<SpectrumValue> noisePsd = sf.CreateConstant (noisePsdValue);
154 
155  AdhocAlohaNoackIdealPhyHelper deviceHelper;
156  deviceHelper.SetChannel (channel);
157  deviceHelper.SetTxPowerSpectralDensity (txPsd);
158  deviceHelper.SetNoisePowerSpectralDensity (noisePsd);
159  deviceHelper.SetPhyAttribute ("Rate", DataRateValue (DataRate ("1Mbps")));
160  NetDeviceContainer devices = deviceHelper.Install (c);
161 
162  PacketSocketHelper packetSocket;
163  packetSocket.Install (c);
164 
165  PacketSocketAddress socket;
166  socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
167  socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
168  socket.SetProtocol (1);
169 
170  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
171  onoff.SetConstantRate (DataRate ("0.5Mbps"));
172  onoff.SetAttribute ("PacketSize", UintegerValue (125));
173 
174  ApplicationContainer apps = onoff.Install (c.Get (0));
175  apps.Start (Seconds (0.1));
176  apps.Stop (Seconds (0.104));
177 
178  Ptr<Socket> recvSink = SetupPacketReceive (c.Get (1));
179 
180  Simulator::Stop (Seconds (10.0));
181 
182  Config::Connect ("/NodeList/*/DeviceList/*/Phy/TxStart", MakeCallback (&PhyTxStartTrace));
183  Config::Connect ("/NodeList/*/DeviceList/*/Phy/TxEnd", MakeCallback (&PhyTxEndTrace));
184  Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxStart", MakeCallback (&PhyRxStartTrace));
185  Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback (&PhyRxEndOkTrace));
186  Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndError", MakeCallback (&PhyRxEndErrorTrace));
187 
188 
189  Simulator::Run ();
190 
192 
193  return 0;
194 }
void PhyTxEndTrace(std::string context, Ptr< const Packet > p)
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
void PhyRxEndErrorTrace(std::string context, Ptr< const Packet > p)
void PhyRxStartTrace(std::string context, Ptr< const Packet > p)
void ReceivePacket(Ptr< Socket > socket)
static bool g_verbose
void PhyRxEndOkTrace(std::string context, Ptr< const Packet > p)
void PhyTxStartTrace(std::string context, Ptr< const Packet > p)
a polymophic address class
Definition: address.h:91
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)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
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:229
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
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:43
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
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 Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
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:71
Setup a SpectrumChannel.
static SpectrumChannelHelper Default()
Setup a default SpectrumChannel.
Ptr< SpectrumChannel > Create(void) const
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:829
Hold an unsigned integer type.
Definition: uinteger.h:44
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...
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
devices
Definition: first.py:39
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35
channel
Definition: third.py:92
mobility
Definition: third.py:108
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56