A Discrete-Event Network Simulator
API
three-gpp-http-example.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Magister Solutions
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: Lauri Sormunen <lauri.sormunen@magister.fi>
18  */
19 
20 #include "ns3/applications-module.h"
21 #include "ns3/core-module.h"
22 #include "ns3/internet-module.h"
23 #include "ns3/network-module.h"
24 #include "ns3/point-to-point-module.h"
25 
26 using namespace ns3;
27 
28 NS_LOG_COMPONENT_DEFINE("ThreeGppHttpExample");
29 
30 void
32 {
33  NS_LOG_INFO("Client has established a connection to the server.");
34 }
35 
36 void
37 MainObjectGenerated(uint32_t size)
38 {
39  NS_LOG_INFO("Server generated a main object of " << size << " bytes.");
40 }
41 
42 void
44 {
45  NS_LOG_INFO("Server generated an embedded object of " << size << " bytes.");
46 }
47 
48 void
50 {
51  NS_LOG_INFO("Server sent a packet of " << packet->GetSize() << " bytes.");
52 }
53 
54 void
56 {
57  NS_LOG_INFO("Client received a packet of " << packet->GetSize() << " bytes from " << address);
58 }
59 
60 void
62 {
63  Ptr<Packet> p = packet->Copy();
64  ThreeGppHttpHeader header;
65  p->RemoveHeader(header);
66  if (header.GetContentLength() == p->GetSize() &&
68  {
69  NS_LOG_INFO("Client has successfully received a main object of " << p->GetSize()
70  << " bytes.");
71  }
72  else
73  {
74  NS_LOG_INFO("Client failed to parse a main object. ");
75  }
76 }
77 
78 void
80 {
81  Ptr<Packet> p = packet->Copy();
82  ThreeGppHttpHeader header;
83  p->RemoveHeader(header);
84  if (header.GetContentLength() == p->GetSize() &&
86  {
87  NS_LOG_INFO("Client has successfully received an embedded object of " << p->GetSize()
88  << " bytes.");
89  }
90  else
91  {
92  NS_LOG_INFO("Client failed to parse an embedded object. ");
93  }
94 }
95 
96 void
98  const Time& time,
99  uint32_t numObjects,
100  uint32_t numBytes)
101 {
102  NS_LOG_INFO("Client " << client << " has received a page that took " << time.As(Time::MS)
103  << " ms to load with " << numObjects << " objects and " << numBytes
104  << " bytes.");
105 }
106 
107 int
108 main(int argc, char* argv[])
109 {
110  double simTimeSec = 300;
111  CommandLine cmd(__FILE__);
112  cmd.AddValue("SimulationTime", "Length of simulation in seconds.", simTimeSec);
113  cmd.Parse(argc, argv);
114 
117  // LogComponentEnableAll (LOG_PREFIX_FUNC);
118  // LogComponentEnable ("ThreeGppHttpClient", LOG_INFO);
120  LogComponentEnable("ThreeGppHttpExample", LOG_INFO);
121 
122  // Setup two nodes
124  nodes.Create(2);
125 
127  pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
128  pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
129 
131  devices = pointToPoint.Install(nodes);
132 
134  stack.Install(nodes);
135 
137  address.SetBase("10.1.1.0", "255.255.255.0");
138 
140 
141  Ipv4Address serverAddress = interfaces.GetAddress(1);
142 
143  // Create HTTP server helper
144  ThreeGppHttpServerHelper serverHelper(serverAddress);
145 
146  // Install HTTP server
147  ApplicationContainer serverApps = serverHelper.Install(nodes.Get(1));
148  Ptr<ThreeGppHttpServer> httpServer = serverApps.Get(0)->GetObject<ThreeGppHttpServer>();
149 
150  // Example of connecting to the trace sources
151  httpServer->TraceConnectWithoutContext("ConnectionEstablished",
153  httpServer->TraceConnectWithoutContext("MainObject", MakeCallback(&MainObjectGenerated));
154  httpServer->TraceConnectWithoutContext("EmbeddedObject",
156  httpServer->TraceConnectWithoutContext("Tx", MakeCallback(&ServerTx));
157 
158  // Setup HTTP variables for the server
159  PointerValue varPtr;
160  httpServer->GetAttribute("Variables", varPtr);
161  Ptr<ThreeGppHttpVariables> httpVariables = varPtr.Get<ThreeGppHttpVariables>();
162  httpVariables->SetMainObjectSizeMean(102400); // 100kB
163  httpVariables->SetMainObjectSizeStdDev(40960); // 40kB
164 
165  // Create HTTP client helper
166  ThreeGppHttpClientHelper clientHelper(serverAddress);
167 
168  // Install HTTP client
169  ApplicationContainer clientApps = clientHelper.Install(nodes.Get(0));
170  Ptr<ThreeGppHttpClient> httpClient = clientApps.Get(0)->GetObject<ThreeGppHttpClient>();
171 
172  // Example of connecting to the trace sources
173  httpClient->TraceConnectWithoutContext("RxMainObject", MakeCallback(&ClientMainObjectReceived));
174  httpClient->TraceConnectWithoutContext("RxEmbeddedObject",
176  httpClient->TraceConnectWithoutContext("Rx", MakeCallback(&ClientRx));
177  httpClient->TraceConnectWithoutContext("RxPage", MakeCallback(&ClientPageReceived));
178 
179  // Stop browsing after 30 minutes
180  clientApps.Stop(Seconds(simTimeSec));
181 
182  Simulator::Run();
184  return 0;
185 }
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
Parse command-line arguments.
Definition: command-line.h:232
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
holds a vector of std::pair of Ptr<Ipv4> and interface index.
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.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:294
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
Ptr< Packet > Copy() const
performs a COW copy of the packet.
Definition: packet.cc:131
Build a set of PointToPointNetDevice objects.
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Ptr< T > Get() const
Definition: pointer.h:202
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
Hold variables of type string.
Definition: string.h:56
Helper to make it easier to instantiate an ThreeGppHttpClient on a set of nodes.
Model application which simulates the traffic of a web browser.
Header used by web browsing applications to transmit information about content type,...
@ EMBEDDED_OBJECT
Integer equivalent = 2.
@ MAIN_OBJECT
Integer equivalent = 1.
ContentType_t GetContentType() const
Helper to make it easier to instantiate an ThreeGppHttpServer on a set of nodes.
Model application which simulates the traffic of a web server.
Container of various random variables to assist in generating web browsing traffic pattern.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ MS
millisecond
Definition: nstime.h:117
@ NS
nanosecond
Definition: nstime.h:119
static void SetResolution(Unit resolution)
Definition: time.cc:213
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
NodeContainer nodes
address
Definition: first.py:47
serverApps
Definition: first.py:54
pointToPoint
Definition: first.py:38
clientApps
Definition: first.py:64
devices
Definition: first.py:42
stack
Definition: first.py:44
interfaces
Definition: first.py:50
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:302
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:704
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_INFO
Something happened to change state.
Definition: log.h:103
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:320
cmd
Definition: second.py:40
void MainObjectGenerated(uint32_t size)
void ServerTx(Ptr< const Packet > packet)
void ClientPageReceived(Ptr< const ThreeGppHttpClient > client, const Time &time, uint32_t numObjects, uint32_t numBytes)
void ClientRx(Ptr< const Packet > packet, const Address &address)
void ServerConnectionEstablished(Ptr< const ThreeGppHttpServer >, Ptr< Socket >)
void ClientEmbeddedObjectReceived(Ptr< const ThreeGppHttpClient >, Ptr< const Packet > packet)
void ClientMainObjectReceived(Ptr< const ThreeGppHttpClient >, Ptr< const Packet > packet)
void EmbeddedObjectGenerated(uint32_t size)