A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
three-gpp-http-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Magister Solutions
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Lauri Sormunen <lauri.sormunen@magister.fi>
7 */
8
9#include "ns3/applications-module.h"
10#include "ns3/core-module.h"
11#include "ns3/internet-module.h"
12#include "ns3/network-module.h"
13#include "ns3/point-to-point-module.h"
14
15using namespace ns3;
16
17NS_LOG_COMPONENT_DEFINE("ThreeGppHttpExample");
18
19void
21{
22 NS_LOG_INFO("Client has established a connection to the server.");
23}
24
25void
27{
28 NS_LOG_INFO("Server generated a main object of " << size << " bytes.");
29}
30
31void
33{
34 NS_LOG_INFO("Server generated an embedded object of " << size << " bytes.");
35}
36
37void
39{
40 NS_LOG_INFO("Server sent a packet of " << packet->GetSize() << " bytes.");
41}
42
43void
44ClientRx(Ptr<const Packet> packet, const Address& address)
45{
46 NS_LOG_INFO("Client received a packet of " << packet->GetSize() << " bytes from " << address);
47}
48
49void
51{
52 Ptr<Packet> p = packet->Copy();
53 ThreeGppHttpHeader header;
54 p->RemoveHeader(header);
55 if (header.GetContentLength() == p->GetSize() &&
57 {
58 NS_LOG_INFO("Client has successfully received a main object of " << p->GetSize()
59 << " bytes.");
60 }
61 else
62 {
63 NS_LOG_INFO("Client failed to parse a main object. ");
64 }
65}
66
67void
69{
70 Ptr<Packet> p = packet->Copy();
71 ThreeGppHttpHeader header;
72 p->RemoveHeader(header);
73 if (header.GetContentLength() == p->GetSize() &&
75 {
76 NS_LOG_INFO("Client has successfully received an embedded object of " << p->GetSize()
77 << " bytes.");
78 }
79 else
80 {
81 NS_LOG_INFO("Client failed to parse an embedded object. ");
82 }
83}
84
85void
87 const Time& time,
88 uint32_t numObjects,
89 uint32_t numBytes)
90{
91 NS_LOG_INFO("Client " << client << " has received a page that took " << time.As(Time::MS)
92 << " ms to load with " << numObjects << " objects and " << numBytes
93 << " bytes.");
94}
95
96int
97main(int argc, char* argv[])
98{
99 double simTimeSec = 300;
100 CommandLine cmd(__FILE__);
101 cmd.AddValue("SimulationTime", "Length of simulation in seconds.", simTimeSec);
102 cmd.Parse(argc, argv);
103
106 // LogComponentEnableAll (LOG_PREFIX_FUNC);
107 // LogComponentEnable ("ThreeGppHttpClient", LOG_INFO);
108 /// LogComponentEnable ("ThreeGppHttpServer", LOG_INFO);
109 LogComponentEnable("ThreeGppHttpExample", LOG_INFO);
110
111 // Setup two nodes
113 nodes.Create(2);
114
116 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
117 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
118
120 devices = pointToPoint.Install(nodes);
121
123 stack.Install(nodes);
124
126 address.SetBase("10.1.1.0", "255.255.255.0");
127
128 Ipv4InterfaceContainer interfaces = address.Assign(devices);
129
130 Ipv4Address serverAddress = interfaces.GetAddress(1);
131
132 // Create HTTP server helper
133 ThreeGppHttpServerHelper serverHelper(serverAddress);
134
135 // Install HTTP server
136 ApplicationContainer serverApps = serverHelper.Install(nodes.Get(1));
137 Ptr<ThreeGppHttpServer> httpServer = serverApps.Get(0)->GetObject<ThreeGppHttpServer>();
138
139 // Example of connecting to the trace sources
140 httpServer->TraceConnectWithoutContext("ConnectionEstablished",
142 httpServer->TraceConnectWithoutContext("MainObject", MakeCallback(&MainObjectGenerated));
143 httpServer->TraceConnectWithoutContext("EmbeddedObject",
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 // Create HTTP client helper
155 ThreeGppHttpClientHelper clientHelper(serverAddress);
156
157 // Install HTTP client
158 ApplicationContainer clientApps = clientHelper.Install(nodes.Get(0));
159 Ptr<ThreeGppHttpClient> httpClient = clientApps.Get(0)->GetObject<ThreeGppHttpClient>();
160
161 // Example of connecting to the trace sources
162 httpClient->TraceConnectWithoutContext("RxMainObject", MakeCallback(&ClientMainObjectReceived));
163 httpClient->TraceConnectWithoutContext("RxEmbeddedObject",
165 httpClient->TraceConnectWithoutContext("Rx", MakeCallback(&ClientRx));
166 httpClient->TraceConnectWithoutContext("RxPage", MakeCallback(&ClientPageReceived));
167
168 // Stop browsing after 30 minutes
169 clientApps.Stop(Seconds(simTimeSec));
170
173 return 0;
174}
a polymophic address class
Definition address.h:90
holds a vector of ns3::Application pointers.
Parse command-line arguments.
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.
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.
Build a set of PointToPointNetDevice objects.
AttributeValue implementation for Pointer.
Ptr< T > Get() const
Definition pointer.h:223
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold variables of type string.
Definition string.h:45
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:94
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
@ MS
millisecond
Definition nstime.h:106
@ NS
nanosecond
Definition nstime.h:108
static void SetResolution(Unit resolution)
Definition time.cc:202
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
NodeContainer nodes
address
Definition first.py:36
serverApps
Definition first.py:43
pointToPoint
Definition first.py:27
clientApps
Definition first.py:53
devices
Definition first.py:31
stack
Definition first.py:33
interfaces
Definition first.py:39
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:291
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:684
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:108
@ LOG_INFO
Something happened to change state.
Definition log.h:92
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:309
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)