A Discrete-Event Network Simulator
API
three-gpp-http-example.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016 Magister Solutions
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: Lauri Sormunen <lauri.sormunen@magister.fi>
19  */
20 
21 #include "ns3/core-module.h"
22 #include "ns3/network-module.h"
23 #include "ns3/internet-module.h"
24 #include "ns3/point-to-point-module.h"
25 #include "ns3/applications-module.h"
26 
27 using namespace ns3;
28 
29 NS_LOG_COMPONENT_DEFINE ("ThreeGppHttpExample");
30 
31 void
33 {
34  NS_LOG_INFO ("Client has established a connection to the server.");
35 }
36 
37 void
38 MainObjectGenerated (uint32_t size)
39 {
40  NS_LOG_INFO ("Server generated a main object of " << size << " bytes.");
41 }
42 
43 void
44 EmbeddedObjectGenerated (uint32_t size)
45 {
46  NS_LOG_INFO ("Server generated an embedded object of " << size << " bytes.");
47 }
48 
49 void
51 {
52  NS_LOG_INFO ("Server sent a packet of " << packet->GetSize () << " bytes.");
53 }
54 
55 void
57 {
58  NS_LOG_INFO ("Client received a packet of " << packet->GetSize () << " bytes from " << address);
59 }
60 
61 void
63 {
64  Ptr<Packet> p = packet->Copy ();
65  ThreeGppHttpHeader header;
66  p->RemoveHeader (header);
67  if (header.GetContentLength () == p->GetSize ()
69  {
70  NS_LOG_INFO ("Client has successfully received a main object of "
71  << p->GetSize () << " bytes.");
72  }
73  else
74  {
75  NS_LOG_INFO ("Client failed to parse a main object. ");
76  }
77 }
78 
79 void
81 {
82  Ptr<Packet> p = packet->Copy ();
83  ThreeGppHttpHeader header;
84  p->RemoveHeader (header);
85  if (header.GetContentLength () == p->GetSize ()
87  {
88  NS_LOG_INFO ("Client has successfully received an embedded object of "
89  << p->GetSize () << " bytes.");
90  }
91  else
92  {
93  NS_LOG_INFO ("Client failed to parse an embedded object. ");
94  }
95 }
96 
97 int
98 main (int argc, char *argv[])
99 {
100  double simTimeSec = 300;
102  cmd.AddValue ("SimulationTime", "Length of simulation in seconds.", simTimeSec);
103  cmd.Parse (argc, argv);
104 
107  //LogComponentEnableAll (LOG_PREFIX_FUNC);
108  //LogComponentEnable ("ThreeGppHttpClient", LOG_INFO);
110  LogComponentEnable ("ThreeGppHttpExample", LOG_INFO);
111 
112  // Setup two nodes
114  nodes.Create (2);
115 
117  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
118  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
119 
121  devices = pointToPoint.Install (nodes);
122 
124  stack.Install (nodes);
125 
127  address.SetBase ("10.1.1.0", "255.255.255.0");
128 
129  Ipv4InterfaceContainer interfaces = address.Assign (devices);
130 
131  Ipv4Address serverAddress = interfaces.GetAddress (1);
132 
133  // Create HTTP server helper
134  ThreeGppHttpServerHelper serverHelper (serverAddress);
135 
136  // Install HTTP server
137  ApplicationContainer serverApps = serverHelper.Install (nodes.Get (1));
138  Ptr<ThreeGppHttpServer> httpServer = serverApps.Get (0)->GetObject<ThreeGppHttpServer> ();
139 
140  // Example of connecting to the trace sources
141  httpServer->TraceConnectWithoutContext ("ConnectionEstablished",
143  httpServer->TraceConnectWithoutContext ("MainObject", MakeCallback (&MainObjectGenerated));
144  httpServer->TraceConnectWithoutContext ("EmbeddedObject", MakeCallback (&EmbeddedObjectGenerated));
145  httpServer->TraceConnectWithoutContext ("Tx", MakeCallback (&ServerTx));
146 
147  // Setup HTTP variables for the server
148  PointerValue varPtr;
149  httpServer->GetAttribute ("Variables", varPtr);
150  Ptr<ThreeGppHttpVariables> httpVariables = varPtr.Get<ThreeGppHttpVariables> ();
151  httpVariables->SetMainObjectSizeMean (102400); // 100kB
152  httpVariables->SetMainObjectSizeStdDev (40960); // 40kB
153 
154 
155  // Create HTTP client helper
156  ThreeGppHttpClientHelper clientHelper (serverAddress);
157 
158  // Install HTTP client
159  ApplicationContainer clientApps = clientHelper.Install (nodes.Get (0));
160  Ptr<ThreeGppHttpClient> httpClient = clientApps.Get (0)->GetObject<ThreeGppHttpClient> ();
161 
162  // Example of connecting to the trace sources
163  httpClient->TraceConnectWithoutContext ("RxMainObject", MakeCallback (&ClientMainObjectReceived));
164  httpClient->TraceConnectWithoutContext ("RxEmbeddedObject", MakeCallback (&ClientEmbeddedObjectReceived));
165  httpClient->TraceConnectWithoutContext ("Rx", MakeCallback (&ClientRx));
166 
167  // Stop browsing after 30 minutes
168  clientApps.Stop (Seconds (simTimeSec));
169 
170  Simulator::Run ();
172  return 0;
173 }
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
holds a vector of ns3::Application pointers.
nanosecond
Definition: nstime.h:117
Ptr< T > Get(void) const
Definition: pointer.h:194
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Helper to make it easier to instantiate an ThreeGppHttpClient on a set of nodes.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void SetMainObjectSizeStdDev(uint32_t stdDev)
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Hold variables of type string.
Definition: string.h:41
Model application which simulates the traffic of a web server.
NetDeviceContainer Install(NodeContainer c)
Informational messages (e.g., banners).
Definition: log.h:103
void EmbeddedObjectGenerated(uint32_t size)
serverApps
Definition: first.py:45
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Helper to make it easier to instantiate an ThreeGppHttpServer on a set of nodes.
aggregate IP/TCP/UDP functionality to existing Nodes.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
ApplicationContainer Install(NodeContainer c) const
Install an ThreeGppHttpServer on each node of the input container configured with all the attributes ...
Header used by web browsing applications to transmit information about content type, content length and timestamps for delay statistics.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
cmd
Definition: second.py:35
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
void MainObjectGenerated(uint32_t size)
stack
Definition: first.py:34
a polymophic address class
Definition: address.h:90
void ServerConnectionEstablished(Ptr< const ThreeGppHttpServer >, Ptr< Socket >)
void ClientMainObjectReceived(Ptr< const ThreeGppHttpClient >, Ptr< const Packet > packet)
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
nodes
Definition: first.py:25
Model application which simulates the traffic of a web browser.
void ServerTx(Ptr< const Packet > packet)
pointToPoint
Definition: first.py:28
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
ApplicationContainer Install(NodeContainer c) const
Install a ThreeGppHttpClient on each node of the input container configured with all the attributes s...
Parse command-line arguments.
Definition: command-line.h:213
void ClientEmbeddedObjectReceived(Ptr< const ThreeGppHttpClient >, Ptr< const Packet > packet)
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:393
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:293
Prefix all trace prints with simulation time.
Definition: log.h:116
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
address
Definition: first.py:37
Container of various random variables to assist in generating web browsing traffic pattern...
uint32_t GetContentLength() const
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:223
void ClientRx(Ptr< const Packet > packet, const Address &address)
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:578
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1007
ContentType_t GetContentType() const
interfaces
Definition: first.py:41
static void SetResolution(enum Unit resolution)
Definition: time.cc:180
clientApps
Definition: first.py:54
void Parse(int argc, char *argv[])
Parse the program arguments.
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.
devices
Definition: first.py:32
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const