Sunday 10 May 2015

Running ns3 Script

Suppose ns3 is installed in /home/tarballs/ns-allinone-3.19 and netanim installed in home itself Then there will be a folder netanim

Go to terminal
type cd tarballs/ns-allinone-3.19/ns-3.19/

Then run ns3 script using ./waf --run scratch/ProgramName

If it is successful then it will indicate
'build' finished successfully (3.723s)

To run the anim xml file produced in previous example  go to terminal

cd netanim
 

./NetAnim

Then specify the xml file name in example firstPgm.xml


 



Creating Nodes in ns3

The below code creates three nodes

#include "ns3/netanim-module.h"

using namespace ns3;
int main (int argc, char *argv[])
{
    /**************************************************************************************
               Code to create 3 nodes which represent the 3 computers in simulation
             
               Creates NodeContainer topology helper named 'nodes', which is used to create and manage the  nodes  in simulation
     **************************************************************************************/
    NodeContainer nodes;
   
   
   
    /**************************************************************************************
                Creates 3 nodes using 'Create()' method in NodeContainer topology helper class.
    **************************************************************************************/
    nodes.Create (3);  
   
   
   
    /**************************************************************************************
                 Code to specify boundary of simulation window
    **************************************************************************************/
        AnimationInterface::SetBoundary (0, 0, 100, 100);
   
       
       
    /**************************************************************************************
                  Netanim application requires a custom trace file.The below code creates a xml file  called firstPgm.xml which acts as trace file
    **************************************************************************************/
    AnimationInterface anim("firstPgm.xml");
   
   
   
    /**************************************************************************************
                    Code to denote first node as FirstNode
     **************************************************************************************/
     anim.UpdateNodeDescription (0, "FirstNode");
   
   
   
     /**************************************************************************************
                  We can specify the position of a node using SetConstantPosition() in AnimationInterface  class .This method requires 3 parameters
                  1.Node whose position is to be set (From NodeContainer topology  helper object we can  get a specific node by its index. First node will be   in 0th index and we get it using Get() by providing its index)
                  2.Its x position
                  3.Its y position
     **************************************************************************************/
     anim.SetConstantPosition (nodes.Get(0),10.0,10.0 );

   
   
     /**************************************************************************************
                  Code to run simulation
     **************************************************************************************/
      Simulator::Run ();
   
     
     
      /**************************************************************************************
                        Code to clean up simulation
      **************************************************************************************/
      Simulator::Destroy ();
     return 0;
   
}