AMAZON

Showing posts with label ns3. Show all posts
Showing posts with label ns3. 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

Thursday, July 12, 2012

How To Install ns3 on linux in ubuntu, fedora, linux?


we will see how to install ns3 on linux box. The home page of ns3 is NS3 Home
the current release is ns3.10 (January 5, 2011) and can be downloaded from DOWNLOAD NS3.10

Step 1: Download ns3.10 and save it some where , but on the same machine where you want to install (Just Kidding :) )
Step 2: open the terminal and go to the folder where ns3.10 tar file is.
Step 3: from the terminal run :
$tar -xvf ns-allinone-3.10.tar.bz2
$cd ns-allinone-3.10
$./build.py
you can provide some options to build.py but as a newbie use the default one :)
When the build process get over (it will take some time), type
$cd ns-3.10
and now we will validate our installation
$./test.py
the out put of this will be some thing like this:
Waf: Entering directory `/media/Installations/ns-allinone-3.10/ns-3.10/build’
Waf: Leaving directory `/media/Installations/ns-allinone-3.10/ns-3.10/build’
‘build’ finished successfully (2.010s)
PASS: TestSuite lte-bearer
PASS: TestSuite lte-propagation-loss-model
PASS: TestSuite wimax-fragmentation
PASS: TestSuite lte-phy
…………..
PASS: Example examples/tcp/tcp-nsc-lfn
PASS: Example examples/wireless/wifi-ap.py
PASS: Example src/contrib/flow-monitor/examples/wifi-olsr-flowmon.py
168 of 168 tests passed (168 passed, 0 skipped, 0 failed, 0 crashed, 0 valgrind errors)
Step 4:  Now our ns3 is installed and ready to run sample programs.
right now we are in directory “ns-3.10″
from terminal :

$ cp examples/tutorial/first.cc scratch/myfirst.cc
$./waf
this time waf will give some thing like this:
Waf: Entering directory `/media/Installations/ns-allinone-3.10/ns-3.10/build’
Waf: Leaving directory `/media/Installations/ns-allinone-3.10/ns-3.10/build’
‘build’ finished successfully (15.900s)
Step 5: now it is the time to run the simulation
$ ./waf –run myfirst
Waf: Entering directory `/media/Installations/ns-allinone-3.10/ns-3.10/build’
Waf: Leaving directory `/media/Installations/ns-allinone-3.10/ns-3.10/build’
‘build’ finished successfully (1.353s)
Sent 1024 bytes to 10.1.1.2
Received 1024 bytes from 10.1.1.1
Received 1024 bytes from 10.1.1.2
Congratulations !! NS3 is installed and running successfully.
Lets try some other example.
Again go to the terminal and type
$cp examples/wireless/ofdm-validation.cc scratch/myofdm.cc
$./waf
out put will be:
Waf: Entering directory `/media/Installations/ns-allinone-3.10/ns-3.10/build’
[1330/1502] cxx: scratch/myofdm.cc -> build/debug/scratch/myofdm_2.o
[1500/1502] cxx_link: build/debug/scratch/myofdm_2.o -> build/debug/scratch/myofdm
Waf: Leaving directory `/media/Installations/ns-allinone-3.10/ns-3.10/build’
‘build’ finished successfully (5.842s)
now run the simulation:
$ ./waf –run myofdm
this time output will be:
Waf: Entering directory `/media/Installations/ns-allinone-3.10/ns-3.10/build’
Waf: Leaving directory `/media/Installations/ns-allinone-3.10/ns-3.10/build’
‘build’ finished successfully (1.448s)
OfdmRate6Mbps
OfdmRate9Mbps
OfdmRate12Mbps
OfdmRate18Mbps
OfdmRate24Mbps
OfdmRate36Mbps
OfdmRate48Mbps
OfdmRate54Mbps
So, this way you can try different examples, and get an introduction of ns3.
Note: As I am using Fedora 14 Complete Installation, I have not feel the need to resolve the dependencies, but if you will come across any error then please make me know, so that I will extend the post to cover the dependencies problems, and to other distributions also like ubuntu etc.
Enjoy Ns3 , soon, I will come back with tutorials on ns3 and dependencies resolution for ns3 (only if some will mention the errors