samples/main-simple.cc
author Florian Westphal <fw@strlen.de>
Wed, 03 Sep 2008 23:24:59 +0200
changeset 3595 693faf7f4e9b
parent 3125 d2d8a36cfd23
child 6821 203367ae7433
permissions -rw-r--r--
nsc: Fix build problem if gtk config store is disabled gtk config store pulled in libdl.so for us, so things fail to link of the config store isn't enabled. This makes nsc pull in libdl itself when its enabled.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
249
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
     1
#include <iostream>
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
     2
2730
b79415813a1c port to helper API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
     3
#include "ns3/core-module.h"
b79415813a1c port to helper API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
     4
#include "ns3/helper-module.h"
b79415813a1c port to helper API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
     5
#include "ns3/node-module.h"
b79415813a1c port to helper API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
     6
#include "ns3/simulator-module.h"
247
fb7375bb43d7 make the sample code compile and link.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     7
fb7375bb43d7 make the sample code compile and link.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     8
using namespace ns3;
fb7375bb43d7 make the sample code compile and link.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     9
249
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    10
static void
568
e1660959ecbb use Ptr<> everywhere Object or NsUnknown are used
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 567
diff changeset
    11
GenerateTraffic (Ptr<Socket> socket, uint32_t size)
249
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    12
{
250
82e03c8debf5 make simple example work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 249
diff changeset
    13
  std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, tx bytes=" << size << std::endl;
1866
e7dbcc4df546 do not use Packet objects directly. Use Ptr<Packet> instead
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1308
diff changeset
    14
  socket->Send (Create<Packet> (size));
250
82e03c8debf5 make simple example work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 249
diff changeset
    15
  if (size > 0)
82e03c8debf5 make simple example work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 249
diff changeset
    16
    {
82e03c8debf5 make simple example work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 249
diff changeset
    17
      Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50);
82e03c8debf5 make simple example work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 249
diff changeset
    18
    }
501
612bd30cb669 port sample code to refcounting model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 482
diff changeset
    19
  else
612bd30cb669 port sample code to refcounting model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 482
diff changeset
    20
    {
612bd30cb669 port sample code to refcounting model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 482
diff changeset
    21
      socket->Close ();
612bd30cb669 port sample code to refcounting model
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 482
diff changeset
    22
    }
249
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    23
}
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    24
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    25
static void
3100
70b98532fe5c remove previous socket receive methods
Tom Henderson <tomh@tomh.org>
parents: 2887
diff changeset
    26
SocketPrinter (Ptr<Socket> socket)
249
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    27
{
3100
70b98532fe5c remove previous socket receive methods
Tom Henderson <tomh@tomh.org>
parents: 2887
diff changeset
    28
  Ptr<Packet> packet;
3103
91c6fce46125 overloaded Recv() method suggested by Gustavo
Tom Henderson <tomh@tomh.org>
parents: 3102
diff changeset
    29
  while (packet = socket->Recv ())
3100
70b98532fe5c remove previous socket receive methods
Tom Henderson <tomh@tomh.org>
parents: 2887
diff changeset
    30
    { 
70b98532fe5c remove previous socket receive methods
Tom Henderson <tomh@tomh.org>
parents: 2887
diff changeset
    31
      std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << packet->GetSize () << std::endl;
70b98532fe5c remove previous socket receive methods
Tom Henderson <tomh@tomh.org>
parents: 2887
diff changeset
    32
    }
249
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    33
}
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    34
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    35
static void
568
e1660959ecbb use Ptr<> everywhere Object or NsUnknown are used
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 567
diff changeset
    36
PrintTraffic (Ptr<Socket> socket)
249
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    37
{
3102
a225be367c1d more cleanup
Tom Henderson <tomh@tomh.org>
parents: 3100
diff changeset
    38
  socket->SetRecvCallback (MakeCallback (&SocketPrinter));
249
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    39
}
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    40
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 519
diff changeset
    41
void
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 519
diff changeset
    42
RunSimulation (void)
247
fb7375bb43d7 make the sample code compile and link.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    43
{
2730
b79415813a1c port to helper API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    44
  NodeContainer c;
b79415813a1c port to helper API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    45
  c.Create (1);
b79415813a1c port to helper API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    46
b79415813a1c port to helper API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    47
  InternetStackHelper internet;
2887
9a637e6daee0 Build -> Install
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2730
diff changeset
    48
  internet.Install (c);
2730
b79415813a1c port to helper API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    49
453
ddbb935800d8 remove DatagramSocket, use Socket base class for UdpSocket subclass.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 347
diff changeset
    50
3125
d2d8a36cfd23 s/ns3::Udp/ns3::UdpSocketFactory
Tom Henderson <tomh@tomh.org>
parents: 3116
diff changeset
    51
  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
3116
c33b6d2775b7 Move API for socket factory to a Socket::CreateSocket () factory method
Tom Henderson <tomh@tomh.org>
parents: 3103
diff changeset
    52
  Ptr<Socket> sink = Socket::CreateSocket (c.Get (0), tid);
1171
335886fe4ddd InetAddress -> InetSocketAddress
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1164
diff changeset
    53
  InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
1204
d40723d53e3d InetSocketAddress: replace explicit conversion to implicit conversion
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1171
diff changeset
    54
  sink->Bind (local);
249
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    55
3116
c33b6d2775b7 Move API for socket factory to a Socket::CreateSocket () factory method
Tom Henderson <tomh@tomh.org>
parents: 3103
diff changeset
    56
  Ptr<Socket> source = Socket::CreateSocket (c.Get (0), tid);
1171
335886fe4ddd InetAddress -> InetSocketAddress
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1164
diff changeset
    57
  InetSocketAddress remote = InetSocketAddress (Ipv4Address::GetLoopback (), 80);
1204
d40723d53e3d InetSocketAddress: replace explicit conversion to implicit conversion
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1171
diff changeset
    58
  source->Connect (remote);
249
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    59
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    60
  GenerateTraffic (source, 500);
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    61
  PrintTraffic (sink);
66f64be80982 add some traffic generation to sample code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 247
diff changeset
    62
247
fb7375bb43d7 make the sample code compile and link.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    63
fb7375bb43d7 make the sample code compile and link.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    64
  Simulator::Run ();
fb7375bb43d7 make the sample code compile and link.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    65
fb7375bb43d7 make the sample code compile and link.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    66
  Simulator::Destroy ();
567
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 519
diff changeset
    67
}
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 519
diff changeset
    68
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 519
diff changeset
    69
int main (int argc, char *argv[])
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 519
diff changeset
    70
{
6fb98941c36f remove leaks and rework the Ptr class to work with a new refcount mechanism
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 519
diff changeset
    71
  RunSimulation ();
247
fb7375bb43d7 make the sample code compile and link.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    72
fb7375bb43d7 make the sample code compile and link.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    73
  return 0;
fb7375bb43d7 make the sample code compile and link.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    74
}