AMAZON

Showing posts with label test. Show all posts
Showing posts with label test. Show all posts

Sunday, August 19, 2012

NS3 (Compile and run the simulator) on linux (ubuntu, fedora, redhat)

NS3 (Compile and Run Simulator Program)


On the previous article I wrote how to deploy NS3 on Ubuntu 10.10. Now I would like to share how to build and to run your network simulator program into NS3.

Development Tools

As we know, NS3 support development programming C++ and Python.  There are many C++/Python development tools so you choice a tool you have experiences about development.

Getting Started

We start to create folder test on NS3 build folder
n3
Create file hello-world-ns3.cc and write code below ( this code I copy paste from NS3 sample codes, main-simple.cc)
#include <iostream>
 
#include "ns3/core-module.h"
#include "ns3/helper-module.h"
#include "ns3/node-module.h"
#include "ns3/simulator-module.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 (Create<Packet> (size));
  if (size > 0)
    {
      Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50);
    }
  else
    {
      socket->Close ();
    }
}
 
static void
SocketPrinter (Ptr<Socket> socket)
{
  Ptr<Packet> packet;
  while (packet = socket->Recv ())
    { 
      std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << packet->GetSize () << std::endl;
    }
}
 
static void
PrintTraffic (Ptr<Socket> socket)
{
  socket->SetRecvCallback (MakeCallback (&SocketPrinter));
}
 
void
RunSimulation (void)
{
  NodeContainer c;
  c.Create (1);
 
  InternetStackHelper internet;
  internet.Install (c);
 
 
  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
  Ptr<Socket> sink = Socket::CreateSocket (c.Get (0), tid);
  InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
  sink->Bind (local);
 
  Ptr<Socket> source = Socket::CreateSocket (c.Get (0), tid);
  InetSocketAddress remote = InetSocketAddress (Ipv4Address::GetLoopback (), 80);
  source->Connect (remote);
 
  GenerateTraffic (source, 500);
  PrintTraffic (sink);
 
 
  Simulator::Run ();
 
  Simulator::Destroy ();
}
 
int main (int argc, char *argv[])
{
  RunSimulation ();
 
  return 0;
}
Create wscript file and write this script
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
 
def build(bld):
    env = bld.env_of_name('default')
    if not env['ENABLE_EXAMPLES']:
        return;
    
    obj = bld.create_ns3_program('hello-world-ns3',
                                 ['core', 'simulator'])
    obj.source = 'hello-world-ns3.cc'
 
Create waf file and write this script
exec "`dirname "$0"`"/../waf "$@"

Compile and Run

You can compile and run using ./waf . Make sure you ‘re in test folder and run this script
./waf --run "hello-world-ns3"
Sample of output program you can see picture below
n4
Done. This is a simple program for NS3