View | Details | Raw Unified | Return to bug 235
Collapse All | Expand All

(-)1adfc4d446ab (+139 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 *
16
 */
17
18
//
19
// Network topology
20
//
21
//                 5Mb/s, 2ms
22
//       n0 -------------------------n1
23
//
24
//
25
//
26
//
27
// - all links are point-to-point links with indicated one-way BW/delay
28
// - HTTP requests from n1 to n0 via TCP
29
// - DropTail queues 
30
// - Tracing of queues and packet receptions to file "http-traffic.tr"
31
32
#include <iostream>
33
#include <fstream>
34
#include <string>
35
#include <cassert>
36
37
#include "ns3/core-module.h"
38
#include "ns3/simulator-module.h"
39
#include "ns3/node-module.h"
40
#include "ns3/helper-module.h"
41
#include "ns3/http-module.h"
42
43
#include "ns3/global-route-manager.h"
44
45
using namespace ns3;
46
47
NS_LOG_COMPONENT_DEFINE ("SimpleGlobalRoutingExample");
48
49
int 
50
main (int argc, char *argv[])
51
{
52
  // Users may find it convenient to turn on explicit debugging
53
  // for selected modules; the below lines suggest how to do this
54
#if 0 
55
  LogComponentEnable ("SimpleGlobalRoutingExample", LOG_LEVEL_INFO);
56
#endif
57
58
  //
59
  // Make the random number generators generate reproducible results.
60
  //
61
  RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8);
62
63
  // Set up some default values for the simulation.  Use the 
64
65
  //DefaultValue::Bind ("DropTailQueue::m_maxPackets", 30);   
66
67
  // Allow the user to override any of the defaults and the above
68
  // DefaultValue::Bind ()s at run-time, via command-line arguments
69
  CommandLine cmd;
70
  cmd.Parse (argc, argv);
71
72
  // Here, we will explicitly create two nodes.  In more sophisticated
73
  // topologies, we could configure a node factory.
74
  NS_LOG_INFO ("Create nodes.");
75
  NodeContainer c;
76
  c.Create (2);
77
78
  InternetStackHelper internet;
79
  internet.Install (c);
80
81
  // We create the channels first without any IP addressing information
82
  NS_LOG_INFO ("Create channels.");
83
  PointToPointHelper p2p;
84
  p2p.SetDeviceParameter ("DataRate", StringValue ("5Mbps"));
85
  p2p.SetChannelParameter ("Delay", StringValue ("2ms"));
86
  NetDeviceContainer devices = p2p.Install (c);
87
88
  // Later, we add IP addresses.  
89
  NS_LOG_INFO ("Assign IP Addresses.");
90
  Ipv4AddressHelper ipv4;
91
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
92
  Ipv4InterfaceContainer ipInterfaces = ipv4.Assign (devices);
93
94
  //Use "god" BFS static routing
95
  GlobalRouteManager::PopulateRoutingTables ();
96
97
  // ...instead of OLSR
98
  //NS_LOG_INFO ("Enabling OLSR Routing.");
99
  //OlsrHelper olsr;
100
  //olsr.InstallAll ();
101
102
  // Create the HTTP applications
103
104
  NS_LOG_INFO ("Create Applications.");
105
106
  //have to use low level/internal APIs here since I didn't make a helper
107
108
  //add the server to node0
109
  ObjectFactory webServerFactory;
110
  webServerFactory.SetTypeId ("ns3::WebServer");
111
  //these can be used to configure the parameters of the connection
112
  //webServerFactory.Set ("Protocol", TypeIdValue (TcpSocketFactory::GetTypeId ()));
113
  //webServerFactory.Set ("Local", AddressValue (InetSocketAddress (Ipv4Address::GetAny (), 80)));
114
  Ptr<Application> serverApplication = webServerFactory.Create<Application> ();
115
  c.Get(0)->AddApplication(serverApplication);
116
  serverApplication->Start(Seconds (0.0));
117
118
  //add the client to node1
119
  ObjectFactory webClientFactory;
120
  webClientFactory.SetTypeId("ns3::WebClient");
121
  webClientFactory.Set("Remote", AddressValue (InetSocketAddress(ipInterfaces.GetAddress(0), 80)));
122
  Ptr<Application> clientApplication = webClientFactory.Create<Application> ();
123
  c.Get(1)->AddApplication(clientApplication);
124
  clientApplication->Start(Seconds(0.0));
125
126
  std::ofstream ascii;
127
  ascii.open ("simple-http.tr");
128
  PointToPointHelper::EnablePcapAll ("simple-http");
129
  PointToPointHelper::EnableAsciiAll (ascii);
130
131
  Simulator::Stop (Seconds (30));
132
133
  NS_LOG_INFO ("Run Simulation.");
134
  Simulator::Run ();
135
  Simulator::Destroy ();
136
  NS_LOG_INFO ("Done.");
137
138
  return 0;
139
}
(-)a/examples/wscript (+4 lines)
 Lines 46-51   def build(bld): Link Here 
46
        ['point-to-point', 'internet-stack', 'olsr'])
46
        ['point-to-point', 'internet-stack', 'olsr'])
47
    obj.source = 'simple-point-to-point-olsr.cc'
47
    obj.source = 'simple-point-to-point-olsr.cc'
48
48
49
    obj = bld.create_ns3_program('simple-http',
50
        ['point-to-point', 'internet-stack', 'global-routing'])
51
    obj.source = 'simple-http.cc'
52
49
    obj = bld.create_ns3_program('tcp-large-transfer',
53
    obj = bld.create_ns3_program('tcp-large-transfer',
50
        ['point-to-point', 'internet-stack'])
54
        ['point-to-point', 'internet-stack'])
51
    obj.source = 'tcp-large-transfer.cc'
55
    obj.source = 'tcp-large-transfer.cc'
(-)1adfc4d446ab (+1 lines)
Added Link Here 
1
exec "`dirname "$0"`"/../../../waf "$@"
(-)1adfc4d446ab (+132 lines)
Added Link Here 
1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
//
3
// Copyright (c) 2006 Georgia Tech Research Corporation
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: George F. Riley<riley@ece.gatech.edu>
19
//
20
21
// ns3 - On/Off Data Source Application class
22
// George F. Riley, Georgia Tech, Spring 2007
23
// Adapted from ApplicationOnOff in GTNetS.
24
25
#include "ns3/log.h"
26
#include "ns3/address.h"
27
#include "ns3/node.h"
28
#include "ns3/nstime.h"
29
#include "ns3/data-rate.h"
30
#include "ns3/random-variable.h"
31
#include "ns3/socket.h"
32
#include "ns3/simulator.h"
33
#include "ns3/socket-factory.h"
34
#include "ns3/packet.h"
35
#include "ns3/uinteger.h"
36
#include "ns3/trace-source-accessor.h"
37
#include "ns3/tcp-socket-factory.h"
38
#include "ns3/callback.h"
39
40
#include "web-client.h"
41
42
NS_LOG_COMPONENT_DEFINE ("WebClient");
43
44
using namespace std;
45
46
namespace ns3 {
47
48
NS_OBJECT_ENSURE_REGISTERED (WebClient);
49
50
TypeId
51
WebClient::GetTypeId (void)
52
{
53
  static TypeId tid = TypeId ("ns3::WebClient")
54
    .SetParent<Application> ()
55
    .AddConstructor<WebClient> ()
56
    .AddAttribute ("Remote", "The address of the destination",
57
                   AddressValue (),
58
                   MakeAddressAccessor (&WebClient::m_peer),
59
                   MakeAddressChecker ())
60
    .AddAttribute ("Protocol", "The type of protocol to use.",
61
                   TypeIdValue (TcpSocketFactory::GetTypeId ()),
62
                   MakeTypeIdAccessor (&WebClient::m_tid),
63
                   MakeTypeIdChecker ())
64
    ;
65
  return tid;
66
}
67
68
69
WebClient::WebClient ()
70
{
71
  NS_LOG_FUNCTION_NOARGS ();
72
  m_socket = 0;
73
  m_connected = false;
74
}
75
76
WebClient::~WebClient()
77
{
78
  NS_LOG_FUNCTION_NOARGS ();
79
}
80
81
void
82
WebClient::DoDispose (void)
83
{
84
  NS_LOG_FUNCTION_NOARGS ();
85
86
  m_socket = 0;
87
  // chain up
88
  Application::DoDispose ();
89
}
90
91
// Application Methods
92
void WebClient::StartApplication() // Called at time specified by Start
93
{
94
  NS_LOG_FUNCTION_NOARGS ();
95
96
  // Create the socket if not already
97
  if (!m_socket)
98
    {
99
      m_socket = Socket::CreateSocket (GetNode(), m_tid);
100
      m_socket->Bind ();
101
      m_socket->Connect (m_peer);
102
      Callback<void, Ptr<Socket> > successCB = MakeCallback(&ns3::WebClient::ConnectionSucceeded, this);
103
      Callback<void, Ptr<Socket> > failCB = MakeCallback(&ns3::WebClient::ConnectionFailed, this);
104
      m_socket->SetConnectCallback (successCB,failCB);
105
    }
106
}
107
108
void WebClient::StopApplication ()
109
{
110
}
111
112
void WebClient::ConnectionSucceeded(Ptr<Socket> s)
113
{
114
  NS_LOG_FUNCTION_NOARGS ();
115
  m_connected = true;
116
  NS_ASSERT (s==m_socket);
117
  HttpGet("/favicon.ico");
118
}
119
120
void WebClient::ConnectionFailed(Ptr<Socket> s)
121
{
122
  NS_LOG_FUNCTION_NOARGS ();
123
  cout << "WebClient, Connection Failed" << endl;
124
}
125
126
void WebClient::HttpGet(std::string url)
127
{
128
  std::string request = std::string("GET ").append(url).append(" HTTP/1.1\r\n");
129
  const uint8_t* data = reinterpret_cast<const uint8_t*>(request.data());
130
  m_socket->Send(data, request.length(), 0);
131
}
132
} // Namespace ns3
(-)1adfc4d446ab (+89 lines)
Added Link Here 
1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
//
3
// Copyright (c) 2006 Georgia Tech Research Corporation
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: George F. Riley<riley@ece.gatech.edu>
19
//
20
21
// ns3 - On/Off Data Source Application class
22
// George F. Riley, Georgia Tech, Spring 2007
23
// Adapted from ApplicationOnOff in GTNetS.
24
25
#ifndef __web_client_h__
26
#define __web_client_h__
27
28
#include "ns3/address.h"
29
#include "ns3/application.h"
30
#include "ns3/event-id.h"
31
#include "ns3/ptr.h"
32
#include "ns3/data-rate.h"
33
#include "ns3/random-variable.h"
34
#include "ns3/traced-callback.h"
35
36
#include <string>
37
38
namespace ns3 {
39
40
class Address;
41
class RandomVariable;
42
class Socket;
43
44
class WebClient : public Application 
45
{
46
public:
47
  static TypeId GetTypeId (void);
48
49
  WebClient ();
50
51
  virtual ~WebClient();
52
  
53
  //issue an http get request
54
  void HttpGet(std::string url);
55
56
protected:
57
  virtual void DoDispose (void);
58
private:
59
  // inherited from Application base class.
60
  virtual void StartApplication (void);    // Called at time specified by Start
61
  virtual void StopApplication (void);     // Called at time specified by Stop
62
63
  void Construct (Ptr<Node> n,
64
                  const Address &remote,
65
                  std::string tid,
66
                  const RandomVariable& ontime,
67
                  const RandomVariable& offtime,
68
                  uint32_t size);
69
70
71
  // Event handlers
72
  void StartSending();
73
  void StopSending();
74
  void SendPacket();
75
76
  Ptr<Socket>     m_socket;       // Associated socket
77
  Address         m_peer;         // Peer address
78
  bool            m_connected;    // True if connected
79
  TypeId          m_tid;
80
81
private:
82
  void ConnectionSucceeded(Ptr<Socket>);
83
  void ConnectionFailed(Ptr<Socket>);
84
};
85
86
} // namespace ns3
87
88
#endif
89
(-)1adfc4d446ab (+153 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright 2007 University of Washington
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:  Tom Henderson (tomhend@u.washington.edu)
19
 */
20
#include "ns3/address.h"
21
#include "ns3/log.h"
22
#include "ns3/inet-socket-address.h"
23
#include "ns3/node.h"
24
#include "ns3/socket.h"
25
#include "ns3/simulator.h"
26
#include "ns3/socket-factory.h"
27
#include "ns3/packet.h"
28
#include "ns3/trace-source-accessor.h"
29
#include "ns3/tcp-socket-factory.h"
30
31
#include "web-server.h"
32
33
using namespace std;
34
35
namespace ns3 {
36
37
NS_LOG_COMPONENT_DEFINE ("WebServer");
38
NS_OBJECT_ENSURE_REGISTERED (WebServer);
39
40
TypeId 
41
WebServer::GetTypeId (void)
42
{
43
  static TypeId tid = TypeId ("ns3::WebServer")
44
    .SetParent<Application> ()
45
    .AddConstructor<WebServer> ()
46
    .AddAttribute ("Local", "The Address on which to Bind the rx socket.",
47
                   AddressValue (InetSocketAddress (Ipv4Address::GetAny (), 80)),
48
                   MakeAddressAccessor (&WebServer::m_local),
49
                   MakeAddressChecker ())
50
    .AddAttribute ("Protocol", "The type id of the protocol to use for the rx socket.",
51
                   TypeIdValue (TcpSocketFactory::GetTypeId ()),
52
                   MakeTypeIdAccessor (&WebServer::m_tid),
53
                   MakeTypeIdChecker ())
54
    ;
55
  return tid;
56
}
57
58
WebServer::WebServer ()
59
  : m_stopped(true), m_connections(0), m_maxConnections(1)
60
{
61
  m_socket = 0;
62
}
63
64
WebServer::~WebServer()
65
{}
66
67
void
68
WebServer::DoDispose (void)
69
{
70
  if (m_socket != 0)
71
    {
72
      m_socket->Close ();
73
    }
74
  m_socket = 0;
75
76
  // chain up
77
  Application::DoDispose ();
78
}
79
80
81
// Application Methods
82
void WebServer::StartApplication()    // Called at time specified by Start
83
{
84
  // Create the socket if not already
85
  if (!m_socket)
86
    {
87
      m_socket = Socket::CreateSocket (GetNode(), m_tid);
88
      m_socket->Bind (m_local);
89
      m_socket->Listen (0);
90
    }
91
92
  m_socket->SetRecvCallback (MakeCallback(&WebServer::HandleRead, this));
93
  m_socket->SetAcceptCallback (MakeCallback(&WebServer::ConnectionRequested, this),
94
                               MakeCallback(&WebServer::ConnectionAccepted, this));
95
}
96
97
void WebServer::StopApplication()     // Called at time specified by Stop
98
{
99
  if (m_socket) 
100
    {
101
      m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
102
    }
103
}
104
105
bool WebServer::ConnectionRequested(Ptr<Socket> s, const Address& a)
106
{
107
  NS_LOG_FUNCTION(this << s << a );
108
  return m_connections < m_maxConnections;
109
}
110
111
void WebServer::ConnectionAccepted(Ptr<Socket> s, const Address& a)
112
{ // We have a new socket. We need to set the receive callback and
113
  // respond with the web-page response when the request arrives
114
  NS_LOG_FUNCTION (this << s << a);
115
  m_socket = s;
116
  Ptr<Packet> p;
117
  //read until it "blocks"
118
  while(p = m_socket->Recv())
119
  {
120
    const char* data = reinterpret_cast<const char*>(p->PeekData());
121
    m_data.append(data,p->GetSize());
122
  }
123
  //now we've blocked, so register the upcall for later
124
  s->SetRecvCallback(MakeCallback(&WebServer::HandleRead, this));
125
  //and parse out any HTTP we got
126
  if (m_data.length() > 0)
127
  {
128
    ParseHttp();
129
  }
130
}
131
132
void WebServer::HandleRead (Ptr<Socket> socket)
133
{
134
  Ptr<Packet> p;
135
  //read until it "blocks"
136
  while(p = m_socket->Recv())
137
  {
138
    const char* data = reinterpret_cast<const char*>(p->PeekData());
139
    m_data.append(data,p->GetSize());
140
  }
141
  //and parse out any HTTP we got
142
  if (m_data.length() > 0)
143
  {
144
    ParseHttp();
145
  }
146
}
147
148
void WebServer::ParseHttp()
149
{
150
  std::cout<<m_data<<std::endl;
151
}
152
153
} // Namespace ns3
(-)1adfc4d446ab (+74 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright 2007 University of Washington
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:  Tom Henderson (tomhend@u.washington.edu)
19
 */
20
21
#ifndef __web_server_h__
22
#define __web_server_h__
23
24
#include "ns3/application.h"
25
#include "ns3/event-id.h"
26
#include "ns3/ptr.h"
27
#include "ns3/traced-callback.h"
28
#include "ns3/address.h"
29
30
#include <vector>
31
32
namespace ns3 {
33
34
class Address;
35
class Socket;
36
class Packet;
37
38
class WebServer : public Application 
39
{
40
public:
41
  static TypeId GetTypeId (void);
42
  WebServer ();
43
44
  virtual ~WebServer ();
45
46
protected:
47
  virtual void DoDispose (void);
48
private:
49
  // inherited from Application base class.
50
  virtual void StartApplication (void);    // Called at time specified by Start
51
  virtual void StopApplication (void);     // Called at time specified by Stop
52
53
  void HandleRead (Ptr<Socket> socket);
54
55
  virtual bool ConnectionRequested(Ptr<Socket>, const Address&);
56
  virtual void ConnectionAccepted (Ptr<Socket>, const Address&);
57
58
  //Helpers
59
  void ParseHttp();
60
  
61
  Ptr<Socket>     m_socket;       // Associated socket
62
  Address         m_local;        // Local address to bind to
63
  TypeId          m_tid;          // Protocol TypeId
64
  std::string m_data;
65
  bool        m_stopped;        // Rejects all connection requests if true
66
  uint32_t    m_connections;    // Current active connection count
67
  uint32_t    m_maxConnections; // Maximum simultaneous connections allowed
68
  
69
};
70
71
} // namespace ns3
72
73
#endif
74
(-)1adfc4d446ab (+15 lines)
Added Link Here 
1
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
3
def build(bld):
4
    module = bld.create_ns3_module('http', ['core', 'simulator', 'node'])
5
    module.source = [
6
        'web-client.cc',
7
        'web-server.cc',
8
        ]
9
    headers = bld.create_obj('ns3header')
10
    headers.module = 'http'
11
    headers.source = [
12
        'web-client.h',
13
        'web-server.h',
14
        ]
15
(-)a/src/wscript (+1 lines)
 Lines 19-24   all_modules = ( Link Here 
19
    'internet-stack',
19
    'internet-stack',
20
    'devices/point-to-point',
20
    'devices/point-to-point',
21
    'devices/csma',
21
    'devices/csma',
22
    'applications/http',
22
    'applications/onoff',
23
    'applications/onoff',
23
    'applications/packet-sink',
24
    'applications/packet-sink',
24
    'applications/udp-echo',
25
    'applications/udp-echo',

Return to bug 235