LEFT | RIGHT |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2011 Yufei Cheng | 3 * Copyright (c) 2011 Yufei Cheng |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 11 matching lines...) Expand all Loading... |
22 * Information and Telecommunication Technology Center (ITTC) | 22 * Information and Telecommunication Technology Center (ITTC) |
23 * and Department of Electrical Engineering and Computer Science | 23 * and Department of Electrical Engineering and Computer Science |
24 * The University of Kansas Lawrence, KS USA. | 24 * The University of Kansas Lawrence, KS USA. |
25 * | 25 * |
26 * Work supported in part by NSF FIND (Future Internet Design) Program | 26 * Work supported in part by NSF FIND (Future Internet Design) Program |
27 * under grant CNS-0626918 (Postmodern Internet Architecture), | 27 * under grant CNS-0626918 (Postmodern Internet Architecture), |
28 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimenta
tion on GENI), | 28 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimenta
tion on GENI), |
29 * US Department of Defense (DoD), and ITTC at The University of Kansas. | 29 * US Department of Defense (DoD), and ITTC at The University of Kansas. |
30 */ | 30 */ |
31 | 31 |
32 #define NS_LOG_APPEND_CONTEXT \ | 32 #include <limits> |
33 if (m_node) { std::clog << Simulator::Now ().GetSeconds () << " [node " << m_n
ode->GetId () << "] "; } | 33 #include <deque> |
34 | 34 |
35 #include <ostream> | 35 #include "ns3/socket.h" |
36 #include "ns3/address.h" | 36 #include "ns3/tcp-socket-factory.h" |
| 37 #include "ns3/uinteger.h" |
37 #include "ns3/log.h" | 38 #include "ns3/log.h" |
| 39 #include "ns3/simulator.h" |
38 #include "ns3/inet-socket-address.h" | 40 #include "ns3/inet-socket-address.h" |
39 #include "ns3/node.h" | |
40 #include "ns3/socket.h" | |
41 #include "ns3/simulator.h" | |
42 #include "ns3/socket-factory.h" | |
43 #include "ns3/packet.h" | 41 #include "ns3/packet.h" |
44 #include "ns3/trace-source-accessor.h" | 42 #include "ns3/tcp-socket.h" |
45 #include "ns3/tcp-socket-factory.h" | |
46 #include "ns3/udp-socket-factory.h" | |
47 #include "ns3/fatal-error.h" | |
48 #include "ns3/rng-stream.h" | |
49 #include "ns3/nstime.h" | |
50 #include "ns3/data-rate.h" | |
51 #include "ns3/random-variable.h" | |
52 #include "ns3/uinteger.h" | |
53 #include "ns3/pointer.h" | 43 #include "ns3/pointer.h" |
54 | 44 #include "ns3/object.h" |
55 #include "http-seq-header.h" | 45 |
56 #include "http-server.h" | 46 #include "http-server.h" |
57 #include "http-client.h" | 47 #include "http-controller.h" |
58 #include "http-runtime-variable.h" | 48 |
| 49 #include "ns3/tcp-socket.h" |
59 | 50 |
60 NS_LOG_COMPONENT_DEFINE ("HttpServer"); | 51 NS_LOG_COMPONENT_DEFINE ("HttpServer"); |
61 | 52 |
62 using namespace std; | 53 namespace ns3 { |
63 | |
64 namespace ns3 { | |
65 namespace http { | 54 namespace http { |
66 | 55 |
67 NS_OBJECT_ENSURE_REGISTERED (HttpServer); | 56 NS_OBJECT_ENSURE_REGISTERED (HttpServer); |
68 | 57 |
69 TypeId | 58 TypeId |
70 HttpServer::GetTypeId (void) | 59 HttpServer::GetTypeId (void) |
71 { | 60 { |
72 static TypeId tid = TypeId ("ns3::http::HttpServer") | 61 static TypeId tid = TypeId ("ns3::http::HttpServer") |
73 .SetParent<Application> () | 62 .SetParent<Application> () |
74 .AddConstructor<HttpServer> () | 63 .AddConstructor<HttpServer> () |
75 .AddAttribute ("Local", "The Address on which to Bind the rx socket.", | 64 .AddAttribute ("HttpController", "The controller", |
76 AddressValue (InetSocketAddress (Ipv4Address::GetAny (), 80))
, | 65 PointerValue (0), |
| 66 MakePointerAccessor (&HttpServer::SetController, |
| 67 &HttpServer::GetController), |
| 68 MakePointerChecker<HttpController> ()) |
| 69 .AddAttribute ("PageTimeout", "Time out value for each one web page", |
| 70 UintegerValue (1), |
| 71 MakeUintegerAccessor (&HttpServer::m_pageTimeout), |
| 72 MakeUintegerChecker<uint32_t> ()) |
| 73 .AddAttribute ("Local", "The local address for server", |
| 74 AddressValue (), |
77 MakeAddressAccessor (&HttpServer::m_local), | 75 MakeAddressAccessor (&HttpServer::m_local), |
78 MakeAddressChecker ()) | 76 MakeAddressChecker ()) |
79 .AddAttribute ("Protocol", "The type id of the protocol to use for the rx so
cket.", | 77 .AddAttribute ("RunNo", "number of runs for simulation", |
| 78 UintegerValue (0), |
| 79 MakeUintegerAccessor (&HttpServer::m_run), |
| 80 MakeUintegerChecker<uint32_t> ()) |
| 81 .AddAttribute ("TransportId", "The type of protocol to use.", |
80 TypeIdValue (TcpSocketFactory::GetTypeId ()), | 82 TypeIdValue (TcpSocketFactory::GetTypeId ()), |
81 MakeTypeIdAccessor (&HttpServer::m_tid), | 83 MakeTypeIdAccessor (&HttpServer::m_tid), |
82 MakeTypeIdChecker ()) | 84 MakeTypeIdChecker ()) |
83 .AddAttribute ("MaxConnections", "Maximum number of connections a server can
handle", | 85 .AddAttribute ("Persistent", "Set if the connection is persistent connection
or not.", |
84 UintegerValue (50), | 86 BooleanValue (true), |
85 MakeUintegerAccessor (&HttpServer::m_maxConnections), | 87 MakeBooleanAccessor (&HttpServer::m_persistent), |
86 MakeUintegerChecker<uint32_t> ()) | 88 MakeBooleanChecker ()) |
87 .AddAttribute ("RuntimeVariable", "The runtime variable", PointerValue (), | |
88 MakePointerAccessor (&HttpServer::SetRuntimeVariable, | |
89 &HttpServer::GetRuntimeVariable), | |
90 MakePointerChecker<RuntimeVariable> ()) | |
91 ; | 89 ; |
92 return tid; | 90 return tid; |
93 } | 91 } |
94 | 92 |
95 HttpServer::HttpServer () | 93 HttpServer::HttpServer () |
96 { | 94 { |
97 NS_LOG_FUNCTION_NOARGS (); | 95 m_firstConnection = true; |
98 m_socket = 0; | |
99 m_reqsizeS = 0; | |
100 m_rspsizeS = 0; | |
101 m_reqs = 0; | |
102 m_lastreq = false; | |
103 m_connections = 0; | |
104 m_seqHeader = 8; | |
105 } | 96 } |
106 | 97 |
107 HttpServer::~HttpServer () | 98 HttpServer::~HttpServer () |
108 { | 99 { |
109 NS_LOG_FUNCTION_NOARGS (); | 100 m_firstConnection = false; |
110 } | 101 } |
111 | 102 |
112 void | 103 void |
113 HttpServer::DoDispose (void) | 104 HttpServer::SetController (Ptr<HttpController> controller) |
114 { | 105 { |
115 NS_LOG_FUNCTION_NOARGS (); | 106 NS_LOG_FUNCTION (this); |
116 m_socket = 0; | 107 m_controller = controller; |
117 m_reqsizeS = 0; | 108 } |
118 m_rspsizeS = 0; | 109 |
119 m_reqs = 0; | 110 Ptr<HttpController> |
120 m_lastreq = false; | 111 HttpServer::GetController () const |
121 m_connections = 0; | 112 { |
122 // chain up | 113 NS_LOG_FUNCTION (this); |
123 Application::DoDispose (); | 114 return m_controller; |
124 } | 115 } |
125 | 116 |
126 void HttpServer::StartApplication () | 117 void HttpServer::StartApplication () |
127 { | 118 { |
128 NS_LOG_FUNCTION (this); | 119 NS_LOG_FUNCTION (this); |
129 // Called at time specified by Start | 120 // Called at time specified by Start |
130 // Create the socket if not already | 121 // Create the socket if not already |
131 if (!m_socket) | 122 if (!m_socket) |
132 { | 123 { |
133 m_socket = Socket::CreateSocket (GetNode (), m_tid); | 124 m_socket = Socket::CreateSocket (GetNode (), m_tid); |
134 m_socket->Bind (m_local); | 125 m_socket->Bind (m_local); |
135 m_socket->Listen (); | 126 m_socket->Listen (); |
136 } | 127 } |
137 NS_ASSERT (m_socket != 0); | 128 NS_ASSERT (m_socket != 0); |
138 m_socket->SetAcceptCallback (MakeCallback (&HttpServer::ConnectionRequested, t
his), | 129 m_socket->SetAcceptCallback (MakeCallback (&HttpServer::ConnectionRequested, t
his), |
139 MakeCallback (&HttpServer::ConnectionAccepted, th
is)); | 130 MakeCallback (&HttpServer::ConnectionAccepted, th
is)); |
140 m_socket->SetRecvCallback (MakeCallback (&HttpServer::HandleRead, this)); | 131 |
141 | 132 /// By this time save the serve application information in the controller |
| 133 SetServerApplication (); |
142 NS_LOG_LOGIC (Simulator::Now () << " Server ready waiting for Client request")
; | 134 NS_LOG_LOGIC (Simulator::Now () << " Server ready waiting for Client request")
; |
143 } | 135 } |
144 | 136 |
145 void HttpServer::StopApplication () | 137 void HttpServer::StopApplication () |
146 { | 138 { |
147 NS_LOG_FUNCTION (this); | 139 NS_LOG_FUNCTION (this); |
148 // Called at time specified by Stop | 140 // Called at time specified by Stop |
149 NS_LOG_DEBUG (Simulator::Now () << " Server: Stop Application"); | 141 NS_LOG_DEBUG (Simulator::Now () << " Server: Stop Application"); |
150 | 142 |
151 if (m_socket) | 143 if (m_socket) |
152 { | 144 { |
153 m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); | 145 m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); |
154 } | 146 } |
155 } | 147 } |
156 | 148 |
157 void HttpServer::SetRuntimeVariable (Ptr<http::RuntimeVariable> rt) | 149 void |
158 { | 150 HttpServer::SetServerApplication () |
159 m_runtimeVariable = rt; | 151 { |
160 } | 152 NS_LOG_FUNCTION (this); |
161 | 153 Ptr<Node> node = GetNode (); |
162 Ptr<http::RuntimeVariable> HttpServer::GetRuntimeVariable () const | 154 for (uint32_t i = 0; i < node->GetNApplications (); ++i) |
163 { | 155 { |
164 return m_runtimeVariable; | 156 if (node->GetApplication (i) == this) |
165 } | 157 { |
166 | 158 m_controller->SetServerApplication (node, i); |
167 bool HttpServer::ConnectionRequested (Ptr<Socket> s, const Address& a) | 159 } |
168 { | 160 } |
169 NS_LOG_FUNCTION (this << s << a); | 161 } |
170 NS_LOG_DEBUG (Simulator::Now () << " Socket = " << s << " " << " Server: Conne
ctionRequested"); | 162 |
| 163 bool |
| 164 HttpServer::ConnectionRequested (Ptr<Socket> socket, const Address& address) |
| 165 { |
| 166 NS_LOG_FUNCTION (this << socket << address); |
| 167 NS_LOG_DEBUG (Simulator::Now () << " Socket = " << socket << " " << " Server:
ConnectionRequested"); |
171 return true; | 168 return true; |
172 } | 169 } |
173 | 170 |
174 void HttpServer::ConnectionAccepted (Ptr<Socket> s, const Address& a) | 171 void |
175 { | 172 HttpServer::ConnectionAccepted (Ptr<Socket> socket, const Address& address) |
176 NS_LOG_FUNCTION (this << s << a); | 173 { |
| 174 NS_LOG_FUNCTION (this << socket << address); |
177 /* | 175 /* |
178 * We have a new socket and need to set the receive callback and respond with
the response | 176 * We have a new socket and need to set the receive callback and respond with
the response |
179 * when the request arrives | 177 * when the request arrives |
180 */ | 178 */ |
181 NS_LOG_DEBUG (s << " " << Simulator::Now () << " Server::Successful socket id
: " << s << " Connection Accepted"); | 179 NS_LOG_DEBUG (socket << " " << Simulator::Now () << " Server::Successful socke
t id : " << socket << " Connection Accepted"); |
182 m_socket = s; | 180 m_socket = socket; |
183 | 181 |
184 //now we've blocked, so register the up-call for later | 182 // Now we've blocked, so register the up-call for later |
185 s->SetRecvCallback (MakeCallback (&HttpServer::HandleRead, this)); | 183 m_socket->SetRecvCallback (MakeCallback (&HttpServer::ServerReceive, this)); |
186 } | 184 |
187 | 185 if (m_firstConnection) |
188 void HttpServer::HandleRead (Ptr<Socket> socket) | 186 { |
| 187 m_firstConnection = false; |
| 188 /// This is the first connection accepted, call the first web session |
| 189 m_controller->StartFirstWebSession (); |
| 190 } |
| 191 else |
| 192 { |
| 193 NS_ASSERT (!m_persistent); |
| 194 /// After the new connection is established, we start the next client send
immediately |
| 195 m_controller->CallClientSend (); |
| 196 } |
| 197 } |
| 198 |
| 199 void |
| 200 HttpServer::ServerReceive (Ptr<Socket> socket) |
189 { | 201 { |
190 NS_LOG_FUNCTION (this << socket); | 202 NS_LOG_FUNCTION (this << socket); |
191 NS_LOG_DEBUG (Simulator::Now () << " " << m_socket << " socket: " << socket <<
" server: Handle Read"); | 203 Ptr<Packet> packet = socket->Recv (); |
192 Ptr<Packet> packet; | 204 /// We should not have a null packet |
193 Address from; | 205 if (packet == NULL) |
194 m_socket = socket; | 206 { |
195 | 207 NS_LOG_WARN ("We should not receive this part"); |
196 while (packet = socket->RecvFrom (from)) | 208 return; |
197 { | 209 } |
198 NS_LOG_DEBUG ("Inside handleRead"); | 210 |
199 if (packet->GetSize () == 0) | 211 AduContainer serverContainer = m_controller->GetServerContainer (); |
200 { //EOF | 212 uint32_t bytesToRecv = packet->GetSize (); |
| 213 while (bytesToRecv) |
| 214 { |
| 215 uint32_t requestSize = FindFirstRequestSize (serverContainer); |
| 216 NS_LOG_DEBUG ("the request size " << requestSize); |
| 217 if (!requestSize) |
| 218 { |
| 219 NS_FATAL_ERROR ("This should not happen"); |
| 220 return; |
| 221 } |
| 222 |
| 223 if (requestSize > bytesToRecv) |
| 224 { |
| 225 // Received less than a whole ADU. Reduce its size by the number of by
tes received. |
| 226 requestSize -= bytesToRecv; |
| 227 UpdateFirstRequestSize (serverContainer, requestSize); |
| 228 NS_LOG_LOGIC ("Received " << bytesToRecv << " bytes of an ADU. " << se
rverContainer.objects.front ().size << " bytes to go."); |
201 break; | 229 break; |
202 } | 230 } |
203 if (InetSocketAddress::IsMatchingType (from)) | 231 else |
204 { | 232 { |
205 InetSocketAddress address = InetSocketAddress::ConvertFrom (from); | 233 // Front ADU size <= bytesToRecv: remove the whole thing and continue
receiving |
206 NS_LOG_LOGIC (Simulator::Now ().GetSeconds () << "Received " << " pack
et PID : " | 234 bytesToRecv -= requestSize; |
207 << packet->GetUid () <<
" " << packet->GetSize () << " bytes from " << address.GetIpv4 () << " [" | 235 // Remove the next request adu |
208 << address << "]---'" <<
" and packet : " << *packet << "'"); | 236 RemoveFirstRequestSize (serverContainer); |
209 | 237 serverContainer.serverReceiveTime = Simulator::Now ().GetSeconds (); |
210 NS_LOG_LOGIC (Simulator::Now () << " received request from the client
, sending response data packet"); | 238 m_controller->SetServerContainer (serverContainer); |
211 | 239 if (serverContainer.objects.size ()) |
212 m_connection.clear (); | |
213 m_connection.push_back (address.GetIpv4 ()); | |
214 m_ipv4 = GetNode ()->GetObject<Ipv4> (); | |
215 m_server = m_ipv4->GetAddress (1, 0).GetLocal (); | |
216 m_connection.push_back (m_server); | |
217 } | |
218 /* | |
219 * Check if there is HTTP header in this segment | |
220 */ | |
221 uint8_t buf[8]; | |
222 packet->CopyData (buf, sizeof(buf)); | |
223 if (buf[4] || buf[5] || buf[6] || buf[7]) | |
224 { | |
225 HttpSeqHeader httpSeq; | |
226 packet->RemoveHeader (httpSeq); | |
227 | |
228 uint8_t page = httpSeq.GetPage (); | |
229 uint8_t object = httpSeq.GetObject (); | |
230 uint16_t session = httpSeq.GetSession (); | |
231 if (m_runtimeVariable->GetCurrentSession (m_connection) > session && !
(m_runtimeVariable->GetCurrentSession (m_connection))) | |
232 { | 240 { |
233 NS_LOG_DEBUG ("This session has already terminated"); | 241 // After receiving the request we need to schedule the web respons
e |
| 242 m_controller->ScheduleNextServerSend (socket); |
| 243 } |
| 244 continue; |
| 245 } |
| 246 } |
| 247 } |
| 248 |
| 249 uint32_t |
| 250 HttpServer::FindFirstRequestSize (AduContainer & serverContainer) |
| 251 { |
| 252 NS_LOG_FUNCTION (this); |
| 253 for (deque<ADU>::iterator i = serverContainer.objects.begin (); i != serverCon
tainer.objects.end (); ++i) |
| 254 { |
| 255 if (i->messageType == ADU::REQUEST) |
| 256 { |
| 257 return i->size; |
| 258 } |
| 259 } |
| 260 return 0; |
| 261 } |
| 262 |
| 263 void |
| 264 HttpServer::UpdateFirstRequestSize (AduContainer & serverContainer, uint32_t req
uestSize) |
| 265 { |
| 266 NS_LOG_FUNCTION (this << requestSize); |
| 267 for (deque<ADU>::iterator i = serverContainer.objects.begin (); i != serverCon
tainer.objects.end (); ++i) |
| 268 { |
| 269 if (i->messageType == ADU::REQUEST) |
| 270 { |
| 271 i->size = requestSize; |
| 272 break; |
| 273 } |
| 274 } |
| 275 m_controller->SetServerContainer (serverContainer); |
| 276 } |
| 277 |
| 278 void |
| 279 HttpServer::RemoveFirstRequestSize (AduContainer & serverContainer) |
| 280 { |
| 281 NS_LOG_FUNCTION (this); |
| 282 for (deque<ADU>::iterator i = serverContainer.objects.begin (); i != serverCon
tainer.objects.end (); ++i) |
| 283 { |
| 284 if (i->messageType == ADU::REQUEST) |
| 285 { |
| 286 i = serverContainer.objects.erase (i); |
| 287 break; |
| 288 } |
| 289 } |
| 290 m_controller->SetServerContainer (serverContainer); |
| 291 } |
| 292 |
| 293 void |
| 294 HttpServer::PrintContainer (const AduContainer & container) |
| 295 { |
| 296 NS_LOG_FUNCTION (this); |
| 297 for (deque< deque <ADU> >::const_iterator i = container.pages.begin (); i != c
ontainer.pages.end (); ++i) |
| 298 { |
| 299 deque<ADU> objects = *i; |
| 300 for (deque<ADU>::const_iterator j = objects.begin (); j != objects.end ();
++j) |
| 301 { |
| 302 NS_LOG_DEBUG ("The adu size " << j->size); |
| 303 if (j->messageType == ADU::REQUEST) |
| 304 { |
| 305 NS_LOG_DEBUG ("This is a request"); |
234 } | 306 } |
235 else | 307 else |
236 { | 308 { |
237 RuntimeVariableEntry rt; | 309 NS_LOG_DEBUG ("This is a response"); |
238 if (m_runtimeVariable->LookupResponseEntry (m_connection, rt, sess
ion, page, object)) | |
239 { | |
240 m_rspsizeS = rt.m_rspSize; | |
241 m_serverDelay = rt.m_serverDelay; | |
242 NS_LOG_DEBUG ("The response size " << m_rspsizeS << " and the
server delay " << m_serverDelay); | |
243 /* | |
244 * Start sending back responses | |
245 */ | |
246 NS_LOG_DEBUG ("Generate the response after delay"); | |
247 /* | |
248 * Generate the response after server delay time | |
249 */ | |
250 NS_LOG_DEBUG ("Schedule the responses"); | |
251 Simulator::Schedule (Seconds (m_serverDelay), &HttpServer::Res
ponseGen, this, | |
252 m_rspsizeS, session, page, object); | |
253 } | |
254 } | 310 } |
255 } | 311 } |
256 else | 312 } |
257 { | 313 } |
258 NS_LOG_DEBUG ("Remaining part of request"); | 314 |
259 } | 315 } // namespace http |
260 } | 316 } // namespace ns3 |
261 } | |
262 | |
263 void HttpServer::NextSession () | |
264 { | |
265 NS_LOG_FUNCTION (this); | |
266 NS_LOG_INFO ("Earlier web session has completed, ignore earlier response file"
); | |
267 } | |
268 | |
269 void HttpServer::ResponseGen (uint32_t rspsizeS, uint16_t session, uint8_t page,
uint8_t object) | |
270 { | |
271 NS_LOG_FUNCTION (this); | |
272 /* | |
273 * Send response back to the client, if it's the last response to send for one
session, attach | |
274 * a EOF message to the packet to notify the client | |
275 */ | |
276 NS_LOG_DEBUG ("The page " << (uint32_t)page << " the session " << (uint32_t)se
ssion << " the object " << (uint32_t)object); | |
277 m_runtimeVariable->ServerResponse (m_connection, m_socket, rspsizeS, session,
page, object); | |
278 } | |
279 | |
280 } // Namespace http | |
281 } // Namespace ns3 | |
LEFT | RIGHT |