OLD | NEW |
(Empty) | |
| 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 <limits> |
| 33 #include <deque> |
| 34 |
| 35 #include "ns3/socket.h" |
| 36 #include "ns3/tcp-socket-factory.h" |
| 37 #include "ns3/uinteger.h" |
| 38 #include "ns3/log.h" |
| 39 #include "ns3/simulator.h" |
| 40 #include "ns3/inet-socket-address.h" |
| 41 #include "ns3/packet.h" |
| 42 #include "ns3/tcp-socket.h" |
| 43 #include "ns3/pointer.h" |
| 44 #include "ns3/object.h" |
| 45 |
| 46 #include "http-server.h" |
| 47 #include "http-controller.h" |
| 48 |
| 49 #include "ns3/tcp-socket.h" |
| 50 |
| 51 NS_LOG_COMPONENT_DEFINE ("HttpServer"); |
| 52 |
| 53 namespace ns3 { |
| 54 namespace http { |
| 55 |
| 56 NS_OBJECT_ENSURE_REGISTERED (HttpServer); |
| 57 |
| 58 TypeId |
| 59 HttpServer::GetTypeId (void) |
| 60 { |
| 61 static TypeId tid = TypeId ("ns3::http::HttpServer") |
| 62 .SetParent<Application> () |
| 63 .AddConstructor<HttpServer> () |
| 64 .AddAttribute ("HttpController", "The controller", |
| 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 (), |
| 75 MakeAddressAccessor (&HttpServer::m_local), |
| 76 MakeAddressChecker ()) |
| 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.", |
| 82 TypeIdValue (TcpSocketFactory::GetTypeId ()), |
| 83 MakeTypeIdAccessor (&HttpServer::m_tid), |
| 84 MakeTypeIdChecker ()) |
| 85 .AddAttribute ("Persistent", "Set if the connection is persistent connection
or not.", |
| 86 BooleanValue (true), |
| 87 MakeBooleanAccessor (&HttpServer::m_persistent), |
| 88 MakeBooleanChecker ()) |
| 89 ; |
| 90 return tid; |
| 91 } |
| 92 |
| 93 HttpServer::HttpServer () |
| 94 { |
| 95 m_firstConnection = true; |
| 96 } |
| 97 |
| 98 HttpServer::~HttpServer () |
| 99 { |
| 100 m_firstConnection = false; |
| 101 } |
| 102 |
| 103 void |
| 104 HttpServer::SetController (Ptr<HttpController> controller) |
| 105 { |
| 106 NS_LOG_FUNCTION (this); |
| 107 m_controller = controller; |
| 108 } |
| 109 |
| 110 Ptr<HttpController> |
| 111 HttpServer::GetController () const |
| 112 { |
| 113 NS_LOG_FUNCTION (this); |
| 114 return m_controller; |
| 115 } |
| 116 |
| 117 void HttpServer::StartApplication () |
| 118 { |
| 119 NS_LOG_FUNCTION (this); |
| 120 // Called at time specified by Start |
| 121 // Create the socket if not already |
| 122 if (!m_socket) |
| 123 { |
| 124 m_socket = Socket::CreateSocket (GetNode (), m_tid); |
| 125 m_socket->Bind (m_local); |
| 126 m_socket->Listen (); |
| 127 } |
| 128 NS_ASSERT (m_socket != 0); |
| 129 m_socket->SetAcceptCallback (MakeCallback (&HttpServer::ConnectionRequested, t
his), |
| 130 MakeCallback (&HttpServer::ConnectionAccepted, th
is)); |
| 131 |
| 132 /// By this time save the serve application information in the controller |
| 133 SetServerApplication (); |
| 134 NS_LOG_LOGIC (Simulator::Now () << " Server ready waiting for Client request")
; |
| 135 } |
| 136 |
| 137 void HttpServer::StopApplication () |
| 138 { |
| 139 NS_LOG_FUNCTION (this); |
| 140 // Called at time specified by Stop |
| 141 NS_LOG_DEBUG (Simulator::Now () << " Server: Stop Application"); |
| 142 |
| 143 if (m_socket) |
| 144 { |
| 145 m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); |
| 146 } |
| 147 } |
| 148 |
| 149 void |
| 150 HttpServer::SetServerApplication () |
| 151 { |
| 152 NS_LOG_FUNCTION (this); |
| 153 Ptr<Node> node = GetNode (); |
| 154 for (uint32_t i = 0; i < node->GetNApplications (); ++i) |
| 155 { |
| 156 if (node->GetApplication (i) == this) |
| 157 { |
| 158 m_controller->SetServerApplication (node, i); |
| 159 } |
| 160 } |
| 161 } |
| 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"); |
| 168 return true; |
| 169 } |
| 170 |
| 171 void |
| 172 HttpServer::ConnectionAccepted (Ptr<Socket> socket, const Address& address) |
| 173 { |
| 174 NS_LOG_FUNCTION (this << socket << address); |
| 175 /* |
| 176 * We have a new socket and need to set the receive callback and respond with
the response |
| 177 * when the request arrives |
| 178 */ |
| 179 NS_LOG_DEBUG (socket << " " << Simulator::Now () << " Server::Successful socke
t id : " << socket << " Connection Accepted"); |
| 180 m_socket = socket; |
| 181 |
| 182 // Now we've blocked, so register the up-call for later |
| 183 m_socket->SetRecvCallback (MakeCallback (&HttpServer::ServerReceive, this)); |
| 184 |
| 185 if (m_firstConnection) |
| 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) |
| 201 { |
| 202 NS_LOG_FUNCTION (this << socket); |
| 203 Ptr<Packet> packet = socket->Recv (); |
| 204 /// We should not have a null packet |
| 205 if (packet == NULL) |
| 206 { |
| 207 NS_LOG_WARN ("We should not receive this part"); |
| 208 return; |
| 209 } |
| 210 |
| 211 AduContainer serverContainer = m_controller->GetServerContainer (); |
| 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."); |
| 229 break; |
| 230 } |
| 231 else |
| 232 { |
| 233 // Front ADU size <= bytesToRecv: remove the whole thing and continue
receiving |
| 234 bytesToRecv -= requestSize; |
| 235 // Remove the next request adu |
| 236 RemoveFirstRequestSize (serverContainer); |
| 237 serverContainer.serverReceiveTime = Simulator::Now ().GetSeconds (); |
| 238 m_controller->SetServerContainer (serverContainer); |
| 239 if (serverContainer.objects.size ()) |
| 240 { |
| 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"); |
| 306 } |
| 307 else |
| 308 { |
| 309 NS_LOG_DEBUG ("This is a response"); |
| 310 } |
| 311 } |
| 312 } |
| 313 } |
| 314 |
| 315 } // namespace http |
| 316 } // namespace ns3 |
OLD | NEW |