LEFT | RIGHT |
(no file at all) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2011 Yufei Cheng |
| 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: Yufei Cheng <yfcheng@ittc.ku.edu> |
| 19 * |
| 20 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director |
| 21 * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets |
| 22 * Information and Telecommunication Technology Center (ITTC) |
| 23 * and Department of Electrical Engineering and Computer Science |
| 24 * The University of Kansas Lawrence, KS USA. |
| 25 * |
| 26 * Work supported in part by NSF FIND (Future Internet Design) Program |
| 27 * under grant CNS-0626918 (Postmodern Internet Architecture), |
| 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. |
| 30 */ |
| 31 |
| 32 #include "http-controller.h" |
| 33 #include "http-distribution.h" |
| 34 #include "http-client.h" |
| 35 #include "http-server.h" |
| 36 |
| 37 #include "ns3/socket.h" |
| 38 #include "ns3/tcp-socket-factory.h" |
| 39 #include "ns3/tcp-socket.h" |
| 40 #include "ns3/uinteger.h" |
| 41 #include "ns3/node.h" |
| 42 #include "ns3/nstime.h" |
| 43 #include "ns3/simulator.h" |
| 44 #include "ns3/log.h" |
| 45 #include <deque> |
| 46 |
| 47 using namespace std; |
| 48 |
| 49 NS_LOG_COMPONENT_DEFINE ("HttpController"); |
| 50 |
| 51 namespace ns3 { |
| 52 namespace http { |
| 53 |
| 54 NS_OBJECT_ENSURE_REGISTERED (HttpController); |
| 55 |
| 56 TypeId |
| 57 HttpController::GetTypeId (void) |
| 58 { |
| 59 static TypeId tid = TypeId ("ns3::http::HttpController") |
| 60 .SetParent<Object> () |
| 61 .AddConstructor<HttpController> () |
| 62 .AddAttribute ("SegmentSize", "The largest segment size", |
| 63 UintegerValue (1460), |
| 64 MakeUintegerAccessor (&HttpController::m_segmentSize), |
| 65 MakeUintegerChecker<uint32_t> ()) |
| 66 ; |
| 67 return tid; |
| 68 } |
| 69 |
| 70 // \brief Constructor |
| 71 HttpController::HttpController () |
| 72 { |
| 73 } |
| 74 |
| 75 // \brief Destructor |
| 76 HttpController::~HttpController () |
| 77 { |
| 78 } |
| 79 |
| 80 void |
| 81 HttpController::SetServerContainer (const AduContainer & container) |
| 82 { |
| 83 NS_LOG_FUNCTION_NOARGS (); |
| 84 m_serverContainer = container; |
| 85 } |
| 86 |
| 87 void |
| 88 HttpController::SetClientContainer (const AduContainer & container) |
| 89 { |
| 90 NS_LOG_FUNCTION_NOARGS (); |
| 91 m_clientContainer = container; |
| 92 } |
| 93 |
| 94 AduContainer & |
| 95 HttpController::GetServerContainer () |
| 96 { |
| 97 return m_serverContainer; |
| 98 } |
| 99 |
| 100 AduContainer & |
| 101 HttpController::GetClientContainer () |
| 102 { |
| 103 return m_clientContainer; |
| 104 } |
| 105 |
| 106 void |
| 107 HttpController::SetHttpVersion (bool pipelining, bool persistent) |
| 108 { |
| 109 NS_LOG_FUNCTION (this << pipelining << persistent); |
| 110 m_persistent = persistent; |
| 111 if (m_persistent) |
| 112 { |
| 113 m_pipelining = pipelining; |
| 114 } |
| 115 else |
| 116 { |
| 117 m_pipelining = false; |
| 118 } |
| 119 } |
| 120 |
| 121 void |
| 122 HttpController::Cleanup () |
| 123 { |
| 124 NS_LOG_FUNCTION (this); |
| 125 m_serverContainer.Clear (); |
| 126 m_clientContainer.Clear (); |
| 127 } |
| 128 |
| 129 void |
| 130 HttpController::SetClientApplication (Ptr<Node> node, uint32_t id) |
| 131 { |
| 132 NS_LOG_FUNCTION (this); |
| 133 m_clientNode = node; |
| 134 m_clientId = id; |
| 135 } |
| 136 |
| 137 void |
| 138 HttpController::SetServerApplication (Ptr<Node> node, uint32_t id) |
| 139 { |
| 140 NS_LOG_FUNCTION (this); |
| 141 m_serverNode = node; |
| 142 m_serverId = id; |
| 143 } |
| 144 |
| 145 void |
| 146 HttpController::SetClientSocket (Ptr<Socket> socket) |
| 147 { |
| 148 NS_LOG_FUNCTION (this); |
| 149 m_clientSocket = socket; |
| 150 } |
| 151 |
| 152 void |
| 153 HttpController::StartNewClientSocket () |
| 154 { |
| 155 NS_LOG_FUNCTION (this); |
| 156 |
| 157 Ptr<HttpClient> httpClient = DynamicCast<HttpClient> |
| 158 (m_clientNode->GetApplication (m_clientId)); |
| 159 |
| 160 httpClient->StartNewSocket (); |
| 161 } |
| 162 |
| 163 void |
| 164 HttpController::StartFirstWebSession () |
| 165 { |
| 166 Ptr<HttpClient> httpClient = DynamicCast<HttpClient> |
| 167 (m_clientNode->GetApplication (m_clientId)); |
| 168 |
| 169 httpClient->StartFirstWebSession (); |
| 170 } |
| 171 |
| 172 void |
| 173 HttpController::ScheduleNextClientSend (Ptr<Socket> socket) |
| 174 { |
| 175 NS_LOG_FUNCTION (this << socket); |
| 176 |
| 177 // It's expected that this won't be called if m_serverContainer is |
| 178 // empty, since the last ADU should have size==0, serve as the FIN message |
| 179 NS_ASSERT (!m_clientContainer.objects.empty ()); |
| 180 |
| 181 if (m_pipelining) |
| 182 { |
| 183 // pipelining |
| 184 m_delayTime = m_clientContainer.objects.front ().requestObjectGapTime; |
| 185 |
| 186 if (m_delayTime > 0) |
| 187 { |
| 188 if (m_clientContainer.clientSentTime < Seconds (0)) |
| 189 { |
| 190 NS_FATAL_ERROR ("client_send_wait: haven't sent anything yet. Adu
container corrupted or wrong?"); |
| 191 } |
| 192 else |
| 193 { |
| 194 // (m_lastSendTime + adu.requestGapTime) is the earliest |
| 195 // time at which the data shall be sent. |
| 196 double sendTime = m_clientContainer.clientSentTime + m_delayTime; |
| 197 NS_LOG_DEBUG ("The send time " << sendTime << " Now time " << Simu
lator::Now ().GetSeconds ()); |
| 198 if (sendTime >= Simulator::Now ().GetSeconds ()) |
| 199 { |
| 200 NS_LOG_LOGIC ("client_send_wait: Scheduling ClientSend at " <<
sendTime << "s"); |
| 201 Simulator::Schedule (Seconds (sendTime - Simulator::Now ().Get
Seconds ()), |
| 202 &HttpController::ClientSend, this, socket
); |
| 203 } |
| 204 else |
| 205 { |
| 206 NS_LOG_LOGIC ("client_send_wait: Scheduling ClientSend immedia
tely (was overdue by " << Simulator::Now ().GetSeconds () - sendTime << "s"); |
| 207 Simulator::ScheduleNow (&HttpController::ClientSend, this, soc
ket); |
| 208 } |
| 209 } |
| 210 } |
| 211 else |
| 212 { |
| 213 NS_LOG_LOGIC ("neither client_recv_wait nor client_send_wait: calling
ClientSend"); |
| 214 Simulator::ScheduleNow (&HttpController::ClientSend, this, socket); |
| 215 } |
| 216 } |
| 217 else |
| 218 { |
| 219 // Non-pipelining |
| 220 NS_LOG_LOGIC ("This is the non-pipelining option, need to determine if it
is persistent connection or not"); |
| 221 |
| 222 /// For the non-persistent option to work, we need to first have the pipel
ining as false |
| 223 if (!m_persistent) |
| 224 { |
| 225 NS_LOG_DEBUG ("Start a new connection for next client sent"); |
| 226 StartNewClientSocket (); |
| 227 } |
| 228 else |
| 229 { |
| 230 NS_LOG_DEBUG ("This is the persistent connection without pipelining"); |
| 231 Simulator::ScheduleNow (&HttpController::ClientSend, this, socket); |
| 232 } |
| 233 } |
| 234 } |
| 235 |
| 236 void |
| 237 HttpController::ScheduleNextServerSend (Ptr<Socket> socket) |
| 238 { |
| 239 NS_LOG_FUNCTION (this << socket); |
| 240 // It's expected that this won't be called if m_serverContainer is |
| 241 // empty, since the last ADU should have size==0, serve as the FIN message |
| 242 NS_ASSERT (!m_serverContainer.objects.empty ()); |
| 243 |
| 244 m_delayTime = m_serverContainer.objects.front ().serverDelayTime; |
| 245 NS_LOG_DEBUG ("The server delay time " << m_delayTime); |
| 246 if (m_delayTime > 0) |
| 247 { |
| 248 // time at which the data may be sent. |
| 249 double sendTime = m_serverContainer.serverReceiveTime + m_delayTime; |
| 250 NS_LOG_DEBUG ("The sende time here " << sendTime << " The now " << Simulat
or::Now ().GetSeconds ()); |
| 251 if (sendTime >= Simulator::Now ().GetSeconds ()) |
| 252 { |
| 253 NS_LOG_LOGIC ("server_send_wait: Scheduling DoSend at " << sendTime <<
"s"); |
| 254 Simulator::Schedule (Seconds (sendTime - Simulator::Now ().GetSeconds
()), |
| 255 &HttpController::ServerSend, this, socket); |
| 256 } |
| 257 else |
| 258 { |
| 259 // past due because that we are doing pipelining |
| 260 NS_LOG_LOGIC ("server_send_wait: Scheduling ServerSend immediately (wa
s overdue by " << Simulator::Now ().GetSeconds () - sendTime << "s"); |
| 261 Simulator::ScheduleNow (&HttpController::ServerSend, this, socket); |
| 262 } |
| 263 } |
| 264 else |
| 265 { |
| 266 NS_LOG_LOGIC ("neither server_recv_wait nor server_send_wait: calling Serv
erSend"); |
| 267 // When both the server_recv_wait and server_recv_wait times are zero, it |
| 268 // means to send those bytes immediately. |
| 269 Simulator::ScheduleNow (&HttpController::ServerSend, this, socket); |
| 270 } |
| 271 } |
| 272 |
| 273 void |
| 274 HttpController::FirstObjectClientSend (Ptr<Socket> socket, double delay) |
| 275 { |
| 276 NS_LOG_FUNCTION (this << socket << delay); |
| 277 NS_LOG_DEBUG ("The second now " << Simulator::Now ().GetSeconds ()); |
| 278 if (m_persistent) |
| 279 { |
| 280 Simulator::Schedule (Seconds (delay - Simulator::Now ().GetSeconds ()), &H
ttpController::ClientSend, this, socket); |
| 281 } |
| 282 else |
| 283 { |
| 284 Simulator::Schedule (Seconds (delay - Simulator::Now ().GetSeconds ()), &H
ttpController::StartNewClientSocket, this); |
| 285 } |
| 286 } |
| 287 |
| 288 void |
| 289 HttpController::CallClientSend () |
| 290 { |
| 291 NS_LOG_FUNCTION (this); |
| 292 ClientSend (m_clientSocket); |
| 293 } |
| 294 |
| 295 void |
| 296 HttpController::ClientSend (Ptr<Socket> socket) |
| 297 { |
| 298 NS_LOG_FUNCTION (this << socket); |
| 299 |
| 300 if (CheckNextRequest (m_clientContainer)) |
| 301 { |
| 302 ADU adu = FindRemoveNextRequest (m_clientContainer); |
| 303 |
| 304 // When we call Client Send function, the first ADU should be request |
| 305 NS_ASSERT (adu.messageType == ADU::REQUEST); |
| 306 |
| 307 // Stuff the whole ADU into the send buffer, one packet at a time. |
| 308 uint32_t bytesToSend = adu.size; |
| 309 uint32_t bytesQueued = 0; |
| 310 while (bytesToSend) |
| 311 { |
| 312 int bytesAccepted; |
| 313 if (bytesToSend > m_segmentSize) |
| 314 { |
| 315 bytesAccepted = socket->Send (Create<Packet> (m_segmentSize)); |
| 316 } |
| 317 else |
| 318 { |
| 319 bytesAccepted = socket->Send (Create<Packet> (bytesToSend)); |
| 320 } |
| 321 if (bytesAccepted > 0) |
| 322 { |
| 323 bytesToSend -= bytesAccepted; |
| 324 bytesQueued += bytesAccepted; |
| 325 } |
| 326 else if (socket->GetErrno () == TcpSocket::ERROR_MSGSIZE) |
| 327 { |
| 328 // This part might have better way |
| 329 NS_FATAL_ERROR ("Send buffer filled while sending ADU bytes alread
y queued). Increase TcpSocket's SndBufSize."); |
| 330 } |
| 331 else if (socket->GetErrno () == TcpSocket::ERROR_NOTCONN) |
| 332 { |
| 333 // This means that the socket was already closed by us for sending
, somehow. |
| 334 NS_FATAL_ERROR ("ERROR_NOTCONN while sending - this is a bug"); |
| 335 } |
| 336 else |
| 337 { |
| 338 NS_FATAL_ERROR ("Error sending " << bytesToSend << " bytes of data
(" << bytesQueued << " bytes already queued). TcpSocket errno == " << socket->G
etErrno ()); |
| 339 } |
| 340 } |
| 341 /// This last send time record the time to send out the previous ADU, this
is the case for both client and server send |
| 342 m_clientContainer.clientSentTime = Simulator::Now ().GetSeconds (); |
| 343 NS_LOG_DEBUG ("The clientContaienr send time " << m_clientContainer.client
SentTime); |
| 344 |
| 345 if (m_pipelining) |
| 346 { |
| 347 NS_LOG_LOGIC ("This is the pipelining request option"); |
| 348 Simulator::ScheduleNow (&HttpController::ScheduleNextClientSend, this,
socket); |
| 349 } |
| 350 /// When it is not pipelining, we wait for the previous response ADU to ar
rive before sending next request |
| 351 } |
| 352 else |
| 353 { |
| 354 NS_LOG_LOGIC ("No request to work on now, waiting for the next web page"); |
| 355 } |
| 356 } |
| 357 |
| 358 void |
| 359 HttpController::ServerSend (Ptr<Socket> socket) |
| 360 { |
| 361 NS_LOG_FUNCTION (this << socket); |
| 362 NS_ASSERT (!m_serverContainer.objects.empty ()); |
| 363 |
| 364 ADU adu = m_serverContainer.objects.front (); |
| 365 // When we call Server Send function, the first ADU should be response or the
terminator |
| 366 NS_ASSERT (adu.messageType == ADU::RESPONSE); |
| 367 // remove the ADU we are about to send |
| 368 m_serverContainer.objects.pop_front (); |
| 369 |
| 370 // Stuff the whole ADU into the send buffer, one packet at a time. |
| 371 uint32_t bytesToSend = adu.size; |
| 372 NS_LOG_DEBUG ("The byte to send size " << bytesToSend); |
| 373 uint32_t bytesQueued = 0; |
| 374 while (bytesToSend) |
| 375 { |
| 376 int bytesAccepted; |
| 377 if (bytesToSend > m_segmentSize) |
| 378 { |
| 379 bytesAccepted = socket->Send (Create<Packet> (m_segmentSize)); |
| 380 } |
| 381 else |
| 382 { |
| 383 bytesAccepted = socket->Send (Create<Packet> (bytesToSend)); |
| 384 } |
| 385 if (bytesAccepted > 0) |
| 386 { |
| 387 bytesToSend -= bytesAccepted; |
| 388 bytesQueued += bytesAccepted; |
| 389 } |
| 390 else if (socket->GetErrno () == TcpSocket::ERROR_MSGSIZE) |
| 391 { |
| 392 // This part might have better way |
| 393 NS_FATAL_ERROR ("Send buffer filled while sending ADU bytes already q
ueued). Increase TcpSocket's SndBufSize."); |
| 394 } |
| 395 else if (socket->GetErrno () == TcpSocket::ERROR_NOTCONN) |
| 396 { |
| 397 // This means that the socket was already closed by us for sending, so
mehow. |
| 398 NS_FATAL_ERROR ("ERROR_NOTCONN while sending - this is a bug"); |
| 399 } |
| 400 else |
| 401 { |
| 402 NS_FATAL_ERROR ("Error sending " << bytesToSend << " bytes of data ("
<< bytesQueued << " bytes already queued). TcpSocket errno == " << socket->GetEr
rno ()); |
| 403 } |
| 404 } |
| 405 } |
| 406 |
| 407 ADU |
| 408 HttpController::FindRemoveNextRequest (AduContainer & container) |
| 409 { |
| 410 NS_LOG_FUNCTION (this); |
| 411 |
| 412 for (deque<ADU>::iterator i = container.objects.begin (); i != container.objec
ts.end (); ++i) |
| 413 { |
| 414 if (i->messageType == ADU::REQUEST) |
| 415 { |
| 416 ADU adu = *i; |
| 417 i = container.objects.erase (i); |
| 418 return adu; |
| 419 } |
| 420 } |
| 421 NS_FATAL_ERROR ("Not finding the next request, error"); |
| 422 ADU adu; |
| 423 return adu; |
| 424 } |
| 425 |
| 426 bool |
| 427 HttpController::CheckNextRequest (AduContainer & container) |
| 428 { |
| 429 NS_LOG_FUNCTION (this); |
| 430 |
| 431 for (deque<ADU>::iterator i = container.objects.begin (); i != container.objec
ts.end (); ++i) |
| 432 { |
| 433 if (i->messageType == ADU::REQUEST) |
| 434 { |
| 435 return true; |
| 436 } |
| 437 } |
| 438 return false; |
| 439 } |
| 440 |
| 441 void |
| 442 AduContainer::Clear () |
| 443 { |
| 444 NS_LOG_FUNCTION (this); |
| 445 startTime = 0; |
| 446 serverReceiveTime = 0; |
| 447 clientSentTime = 0; |
| 448 requestPageGaps.clear (); |
| 449 objects.clear (); |
| 450 pages.clear (); |
| 451 remainingPageSize = 0; |
| 452 totalPageSize.clear (); |
| 453 totalCurrentPage = 0; |
| 454 } |
| 455 |
| 456 } // namespace http |
| 457 } // namespace ns3 |
LEFT | RIGHT |