A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
loopback.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 IITP RAS
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  * Authors: Pavel Boyko <boyko@iitp.ru>
19  */
20 
21 #include "loopback.h"
22 #include "ns3/simulator.h"
23 #include "ns3/mobility-helper.h"
24 #include "ns3/double.h"
25 #include "ns3/uinteger.h"
26 #include "ns3/string.h"
27 #include "ns3/boolean.h"
28 #include "ns3/yans-wifi-helper.h"
29 #include "ns3/internet-stack-helper.h"
30 #include "ns3/ipv4-address-helper.h"
31 #include "ns3/abort.h"
32 #include "ns3/udp-echo-helper.h"
33 #include "ns3/mobility-model.h"
34 #include "ns3/pcap-file.h"
35 #include "ns3/aodv-helper.h"
36 #include "ns3/v4ping.h"
37 #include "ns3/nqos-wifi-mac-helper.h"
38 #include "ns3/config.h"
39 #include "ns3/constant-position-mobility-model.h"
40 #include "ns3/names.h"
41 #include <sstream>
42 
43 namespace ns3
44 {
45 namespace aodv
46 {
47 
48 static uint32_t g_count (0);
49 
50 static void
51 PingRtt (std::string context, Time rtt)
52 {
53  g_count++;
54 }
55 
56 void
58 {
59  NodeContainer nodes;
60  nodes.Create (1);
61  Ptr<MobilityModel> m = CreateObject<ConstantPositionMobilityModel> ();
62  m->SetPosition (Vector (0, 0, 0));
63  nodes.Get (0)->AggregateObject (m);
64  // Setup WiFi
66  wifiMac.SetType ("ns3::AdhocWifiMac");
69  wifiPhy.SetChannel (wifiChannel.Create ());
71  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", StringValue ("2200"));
72  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
73 
74  // Setup TCP/IP & AODV
75  AodvHelper aodv; // Use default parameters here
76  InternetStackHelper internetStack;
77  internetStack.SetRoutingHelper (aodv);
78  internetStack.Install (nodes);
79  Ipv4AddressHelper address;
80  address.SetBase ("10.1.1.0", "255.255.255.0");
81  Ipv4InterfaceContainer interfaces = address.Assign (devices);
82 
83  // Setup ping
84  Ptr<V4Ping> ping = CreateObject<V4Ping> ();
85  ping->SetAttribute ("Remote", Ipv4AddressValue (Ipv4Address::GetLoopback ()));
86  nodes.Get (0)->AddApplication (ping);
87  ping->SetStartTime (Seconds (0));
88  ping->SetStopTime (Seconds (4));
89  Names::Add ("ping", ping);
90  Config::Connect ("/Names/ping/Rtt", MakeCallback (&PingRtt));
91 
92  // Run
94  Simulator::Run ();
96 
97  // Check that 4 packets delivered
98  NS_TEST_ASSERT_MSG_EQ (g_count, 4, "Exactly 4 ping replies must be delivered.");
99 }
100 
101 }
102 }
uint32_t AddApplication(Ptr< Application > application)
Definition: node.cc:148
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: wifi-helper.cc:68
hold variables of type string
Definition: string.h:19
Make it easy to create and manage PHY objects for the yans model.
static YansWifiChannelHelper Default(void)
void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
static void Run(void)
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
static YansWifiPhyHelper Default(void)
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
helps to create WifiNetDevice objects
Definition: wifi-helper.h:92
a 3d vector
Definition: vector.h:31
NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:97
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition: names.cc:615
void SetChannel(Ptr< YansWifiChannel > channel)
holds a vector of ns3::NetDevice pointers
static NqosWifiMacHelper Default(void)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
static void Destroy(void)
Definition: simulator.cc:121
void DoRun()
Run test.
Definition: loopback.cc:57
keep track of a set of node pointers.
void Install(std::string nodeName) const
manage and create wifi channel objects for the yans model.
hold objects of type ns3::Ipv4Address
static Ipv4Address GetLoopback(void)
void SetPosition(const Vector &position)
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
static void Stop(void)
Definition: simulator.cc:164
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Helper class that adds AODV routing to nodes.
Definition: aodv-helper.h:35
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
static WifiHelper Default(void)
Definition: wifi-helper.cc:60