samples/main-simple.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri, 11 May 2007 08:59:49 +0200
changeset 571 ac324a1cba74
parent 568 e1660959ecbb
child 605 3a62e5c4de75
permissions -rw-r--r--
rework the refcounting framework to use the MakeNewObject function

#include <iostream>

#include "ns3/internet-node.h"
#include "ns3/simulator.h"
#include "ns3/i-udp.h"
#include "ns3/socket.h"
#include "ns3/nstime.h"

using namespace ns3;

static void
GenerateTraffic (Ptr<Socket> socket, uint32_t size)
{
  std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, tx bytes=" << size << std::endl;
  socket->Send (0, size);
  if (size > 0)
    {
      Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50);
    }
  else
    {
      socket->Close ();
    }
}

static void
SocketPrinter (Ptr<Socket> socket, uint32_t size, const Ipv4Address &from, uint16_t fromPort)
{
  std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << size << std::endl;
}

static void
PrintTraffic (Ptr<Socket> socket)
{
  socket->RecvDummy (MakeCallback (&SocketPrinter));
}

void
RunSimulation (void)
{
  Ptr<InternetNode> a = MakeNewObject<InternetNode> ();

  Ptr<IUdp> udp = a->QueryInterface<IUdp> (IUdp::iid);

  Ptr<Socket> sink = udp->CreateSocket ();
  sink->Bind (80);

  Ptr<Socket> source = udp->CreateSocket ();
  source->Connect (Ipv4Address::GetLoopback (), 80);

  GenerateTraffic (source, 500);
  PrintTraffic (sink);


  Simulator::Run ();

  Simulator::Destroy ();
}

int main (int argc, char *argv[])
{
  RunSimulation ();

  return 0;
}