Monday, February 3, 2014

TCL commands and NS2 small wireless node creation program

TCL COMMANDS
Displaying Values and Accepting Input from console:
set a 10
set b [gets stdin]
puts $a
puts "the Value of a = $a"
puts "the Value of b = $b"
Expression:
set c [expr $a + $b]
puts "The value of c = $c"
set d [expr [expr $b - $a ] * $c]
puts "The value of d = $d"
Creating Procedure:
proc display {} {
puts "This is the Testing Message in Procedure"
}
display
Creating Procedure with Parameter:
proc add {x y} {
set z [expr $x + $y]
puts "This value of $x + $y = $z"
}
add 10 20
If Statement:
set a1 10
set b1 20
if { $a1 > $b1 } {
puts "$a1 is greater than $b1"
} else {
puts "$b1 is greater than $a1"
}
For Statement:
set k 10
for {set i 0} {$i < $k} {incr i} {
puts "node_($i)"
puts "n$i"
}
File Input and Output:
set test [open file1.txt w]
puts $test "Testing Message"
close $test
set test1 [open file1.txt r]
while { [ eof $test1 ]== 0} {
gets $test1 line
puts $line

}
WIRELESS NETWORK
NODE CREATION
set x 500
set y 500
set n 2
set ns_ [new Simulator]
set topo [new Topography]
$topo load_flatgrid $x $y
set tracefd [open wireless.tr w]
$ns_ trace-all $tracefd
$ns_ use-newtrace
set namtracefd [open first.nam w]
$ns_ namtrace-all-wireless $namtracefd $x $y
set god_ [create-god $n]
$ns_ node-config -adhocRouting AODV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF
for {set i 0} { $i < $n} {incr i} {
set node_($i) [$ns_ node]
}
for {set i 0} {$i < $n} {incr i} {
$ns_ initial_node_pos $node_($i) 30
}
$ns_ at 100.0 "$ns_ halt"
#Run the simulation
$ns_ run
MOBILE NODE POSITION
#node position -static (fixed ) topology
$node_(0) set X_ 50.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 200.0
$node_(1) set Y_ 150.0

$node_(1) set Z_ 0.0
UDP DATA TRAFFIC
#UDP Data Traffic
#create UDP source
set udp0 [new Agent/UDP]
$ns_ attach-agent $node_(0) $udp0
#create UDP destination
set null0 [new Agent/Null]
$ns_ attach-agent $node_(1) $null0
#connect UDP src and destination
$ns_ connect $udp0 $null0
#create Application traffic
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.1
#Application start time
$ns_ at 1 "$cbr0 start"
#Application stop time
$ns_ at 10 "$cbr0 stop"
TCP DATA TRAFFIC
#TCP Data Traffic
#create TCP source
set tcp0 [new Agent/TCP]
$ns_ attach-agent $node_(0) $tcp0
#create TCP destination
set sink0 [new Agent/TCPSink]
$ns_ attach-agent $node_(1) $sink0
#connect TCP src and destination
$ns_ connect $tcp0 $sink0
#create Application traffic
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
#Application start time
$ns_ at 1 "$ftp0 start"
#Application stop time

$ns_ at 10 "$ftp0 stop"
MOBILE NODE MOVEMENT – MANUAL MOVEMENT
#Mobile Node Movement
$ns_ at 3.0 "$node_(1) setdest 450 400 50"
$ns_ at 10.0 "$node_(0) setdest 400 350 50"
SCENARIO GENERATION – NODE POSITION AND RANDOM MOVEMENT
./setdest [-n num_of_nodes] [-p pausetime] [-s maxspeed] [-t simtime] \
[-x maxx] [-y maxy] > [filename]
example:
setdest -n 20 -p 10 -M 20 -t 100 -x 500 -y 500 > scen-20-10-20
setdest -n 30 -p 10 -M 20 -t 100 -x 500 -y 500 > scen-30-10-20
setdest -n 40 -p 10 -M 20 -t 100 -x 500 -y 500 > scen-40-10-20
NOTE: include scen-20-10-20 in tcl file by source scen-20-10-20
Creating random traffic-pattern for wireless scenarios
ns cbrgen.tcl [-type cbr|tcp] [-nn nodes] [-seed seed] [-mc connections]
[-rate rate] > [filename]
example:
ns cbrgen.tcl -type cbr -nn 10 -seed 1.0 -mc 3 -rate 4.0 > cbr-10nodes-3con
ns cbrgen.tcl -type cbr -nn 20 -seed 1.0 -mc 5 -rate 4.0 > cbr-20nodes-5con
NOTE: include cbr-10nodes-3con in tcl file by source cbr-10nodes-3con
TRACE FILE ANALYSIS
New wireless trace file format:
Example
Consider the following trace string:
s -t 31.000000000 -Hs 0 -Hd -2 -Ni 0 -Nx 19.36 -Ny 17.32 -Nz 0.00
-Ne -1.000000 -Nl AGT -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 0.0 -Id 1.0
-It tcp -Il 40 -If 2 -Ii 3 -Iv 32 -Pn tcp -Ps 0 -Pa 0 -Pf 0 -Po 0
5
first field
s : send r : receive d : drop f : forward
RUN AWK SCRIPT

# gawk –f wireless-trace-analysis.awk wireless.tr
XGRAPH
# xgraph -x "No. of Nodes " -y " Packet Deliveery Ratio " -t "Analysis of PDR "
-geometry "600x600" -lw 2 AODV-PDR DSDV-PDR DSR-PDR
NS-2 MODIFICATION
EXERCISE 1.ADD NEW AGENT
---------------------------------------------------------------------
Write your C++ code MyAgent.cc in ns-allinone-2.34/ns-2.34/test folder
---------------------------------------------------------------------
MyAgent.cc
#include "agent.h"
#include <stdio.h>
class MyAgent : public Agent
{
private:
int n1,n2;
protected:
int command(int argc, const char*const* argv);
void multiply();
void show();
public:
MyAgent();
};
static class MyAgentClass : public TclClass
{
public:
MyAgentClass() : TclClass("Agent/MyAgent")
{}
TclObject* create(int, const char*const*)
{
return (new MyAgent());
}
} class_MyAgent;
MyAgent::MyAgent() : Agent(PT_UDP)
{
bind("n1_", &n1);
bind("n2_", &n2);
}
int MyAgent::command(int argc, const char*const* argv)
{
if (argc == 4)
{
if (strcmp(argv[1], "sum") == 0)
{
n1=atoi(argv[2]); n2=atoi(argv[3]);
int x=n1+n2;
printf("\n %d + %d = %d ",n1,n2,x);
}
return (TCL_OK);
}
if (argc == 2)
{
if (strcmp(argv[1], "sub") == 0)
{
int x=n1-n2;
printf("\n %d - %d = %d ",n1,n2,x);
}
if (strcmp(argv[1], "show") == 0)
{
printf("\n The value of n1 = %d \n",n1);
printf("\n The value of n2 = %d \n",n2);
}
if (strcmp(argv[1], "mul") == 0)
{
multiply();
}
return (TCL_OK);
}
return(Agent::command(argc,argv));
}
void MyAgent::multiply()
{
printf("\n Enter value for n1 and n2 \n");
scanf("%d%d",&n1,&n2);
int x=n1*n2;
printf("\n %d * %d = %d ",n1,n2,x);
}
--------------------------------------------------------------------------
MyAgent.tcl
--------------------------------------------------------------------------
set agent1 [new Agent/MyAgent]
$agent1 sum 10 20
$agent1 show
$agent1 set n1_ 5
$agent1 set n2_ 10
$agent1 sub
$agent1 show
$agent1 mul
$agent1 show
------------------------------------------------------------------
Steps for adding C++ code into NS2
------------------------------------------------------------------
Step 1 : Edit ns-allinone-2.34/ns-2.34/Makefile.in
Add following line in OBJ_CC section
Test/MyAgent.o \
Step 2 : Recompile NS-2
Open terminal and go to ns-allinone-2.34/ns-2.34 directory and type
./configure
make
make install
Step 3 : Run your Myagent.tcl file
------------------------*******************----------------------------
EXERCISE 2.ADD NEW PROTOCOL - MYPING
--------------------------------------------------------------------------
Write your C++ code MyPing.cc in ns-allinone-2.34/ns-2.34/test folder
--------------------------------------------------------------------------
MyPing.cc
#include "agent.h"
#include "tclcl.h"
#include "packet.h"
#include "address.h"
#include "ip.h"
#include <stdio.h>
struct MyPingHeader
{
char flag;
double send_time;
// Header access methods
static int offset_; //required by PacketHeaderManager
inline static MyPingHeader* access(const Packet* p)
{
return (MyPingHeader*) p->access(offset_);
}
};
class MyPingAgent : public Agent
{
public:
MyPingAgent();
protected:
int command(int argc, const char*const* argv);
void recv(Packet*, Handler*);
};
int MyPingHeader::offset_;
static class MyPingHeaderClass : public PacketHeaderClass
{
public:
MyPingHeaderClass():PacketHeaderClass("PacketHeader/MyPing",
sizeof(MyPingHeader))
{
bind_offset(&MyPingHeader::offset_);
}
} class_Pinghdr;
static class MyPingClass : public TclClass
{
public:
MyPingClass() : TclClass("Agent/MyPing")
{}
TclObject* create(int, const char*const*)
{
return (new MyPingAgent());
}
} class_Ping;
MyPingAgent::MyPingAgent() : Agent(PT_MYPING)
{ }
int MyPingAgent::command(int argc, const char*const* argv)
{
if (argc == 2)
{
if (strcmp(argv[1], "send") == 0)
{
// Create a new packet
Packet* pkt = allocpkt();
// Access the Ping header for the new packet
MyPingHeader* hdr = MyPingHeader::access(pkt);
// Set the 'flag' field to 0, so the receiving node knows it
has //to generate an echo packet
hdr->flag = 0;
// Store the current time in the 'send_time' field
hdr->send_time = Scheduler::instance().clock();
// Send the packet
send(pkt, 0);
return (TCL_OK);
}
}
return (Agent::command(argc, argv));
}
void MyPingAgent::recv(Packet* pkt, Handler*)
{
// Access the IP header for the received packet
hdr_ip* iphdr = hdr_ip::access(pkt);
// Access the Ping header for the received packet
MyPingHeader* pinghdr = MyPingHeader::access(pkt);
// Is the 'flag' field = 0 (i.e. the receiving node is being //pinged)?
if (pinghdr->flag == 0)
{
// Send an 'echo'. First save the old packet's send_time
double stime = pinghdr->send_time;
// Discard the packet
Packet::free(pkt);
// Create a new packet
Packet* pktret = allocpkt();
// Access the Ping header for the new packet
MyPingHeader* hdrret = MyPingHeader::access(pktret);
// Set the 'ret' field to 1, so the receiver won't send another echo
hdrret->flag = 1;
// Set the send_time field to the correct value
hdrret->send_time = stime;
// Send the packet
send(pktret, 0);
}
else
{
double rtt=Scheduler::instance().clock()- pinghdr->send_time;
printf("Node %d received ping answer from node %d with round-trip-time = %lf
rtt seconds\n",iphdr->src_.addr_ , iphdr->dst_.addr_, rtt);
Packet::free(pkt);
}
}
--------------------------------------------------------------------------
MyPing.tcl
--------------------------------------------------------------------------
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
#Connect the nodes with one link
$ns duplex-link $n0 $n1 1Mb 1s DropTail
#Create two ping agents & attach them to nodes n0 and n1
set p0 [new Agent/MyPing]
$ns attach-agent $n0 $p0
set p1 [new Agent/MyPing]
$ns attach-agent $n1 $p1
#Connect the two agents
$ns connect $p0 $p1
#Schedule events
$ns at 1 "$p0 send"
$ns at 3 "$p0 send"
$ns at 5 "$p0 send"
$ns at 7 "$p0 send"
$ns at 10.0 "$ns halt"
#Run the simulation
$ns run
----------------------------------------------------------------------------
Steps for adding new protocol into NS2
---------------------------------------------------------------------------
Step 1 : Edit ns-allinone-2.34/ns-2.34/Makefile.in
Add following line in OBJ_CC section
Test/MyAgent.o Test/MyPing.o \
Step 2: Edit ns-allinone-2.34/ns-2.34/common/packet.h (2 changes to be done)
To define new packet type we have to modify packet.h file
a) Add the following line in packet_t section
static const packet_t PT_MYPING = 62;
static packet_t PT_NTYPE = 63; // this must be at last
b) Add the following line in class p_info section
name_[PT_AOMDV]=”AOMDV”;
name_[PT_MYPING]=”MyPing”;
name_[PT_NTYPE]=”undefined”;
Step 3: Edit ns-allinone-2.34/ns-2.34/tcl/lib/ns-packet.tcl
To configure new Agent , Add protocol name under
foreach prot {
AODV
ARP
……
MYPING
Step 4: Edit ns-allinone-2.34/ns-2.34/tcl/lib/ns-default.tcl
Agent/MyPing set packetSize_ 64
Step 5: Recompile NS-2
Open terminal and go to ns-allinone-2.34/ns-2.34 directory and type
./configure
make
make install
Step 6 : Run your MyPing.tcl file
############################################################################
EXERCISE 3.HANDLING TIMERS with MYPING PROTOCOL
MyPing.cc
#include "agent.h"
#include "tclcl.h"
#include "packet.h"
#include "address.h"
#include "ip.h"
#include <stdio.h>
struct MyPingHeader
{
char flag;
double send_time;
// Header access methods
static int offset_; //required by PacketHeaderManager
inline static MyPingHeader* access(const Packet* p)
{
return (MyPingHeader*) p->access(offset_);
}
};
class MyPingAgent;
class MyPingAgentTimer : public TimerHandler {
public:
MyPingAgentTimer(MyPingAgent *t) : TimerHandler()
{ t_ = t; }
protected:
void expire(Event *e);
MyPingAgent *t_;
};
class MyPingAgent : public Agent
{
private:
int packets;
int count;
public:
MyPingAgent();
MyPingAgentTimer timer_;
int command(int argc, const char*const* argv);
void recv(Packet*, Handler*);
void send_packets();
};
int MyPingHeader::offset_;
static class MyPingHeaderClass : public PacketHeaderClass
{
public:
MyPingHeaderClass():PacketHeaderClass("PacketHeader/MyPing",
sizeof(MyPingHeader))
{
bind_offset(&MyPingHeader::offset_);
}
} class_Pinghdr;
static class MyPingClass : public TclClass
{
public:
MyPingClass() : TclClass("Agent/MyPing")
{}
TclObject* create(int, const char*const*)
{
return (new MyPingAgent());
}
} class_Ping;
MyPingAgent::MyPingAgent() : Agent(PT_MYPING),timer_(this)
{
count=0;
bind("pkts_", &packets);
}
/* if timer expires just do a timeout */
void MyPingAgentTimer::expire(Event*)
{
t_->send_packets();
}
int MyPingAgent::command(int argc, const char*const* argv)
{
if (argc == 2)
{
if (strcmp(argv[1], "send") == 0)
{
send_packets();
return (TCL_OK);
}
}
return (Agent::command(argc, argv));
}
void MyPingAgent::send_packets()
{
if(count < packets)
{
count++;
// Create a new packet
Packet* pkt = allocpkt();
// Access the Ping header for the new packet:
MyPingHeader* hdr = MyPingHeader::access(pkt);
// Set the 'flag' field to 0, so the receiving node knows
//that it has to generate an echo packet
hdr->flag = 0;
// Store the current time in the 'send_time' field
hdr->send_time = Scheduler::instance().clock();
// Send the packet
send(pkt, 0);
}
if(count < packets)
timer_.resched(2);
}
void MyPingAgent::recv(Packet* pkt, Handler*)
{
// Access the IP header for the received packet:
hdr_ip* iphdr = hdr_ip::access(pkt);
// Access the Ping header for the received packet:
MyPingHeader* pinghdr = MyPingHeader::access(pkt);
// Is the 'flag' field = 0 (i.e. the receiving node is being pinged)?
if (pinghdr->flag == 0)
{
// Send an 'echo'. First save the old packet's send_time
double stime = pinghdr->send_time;
// Discard the packet
Packet::free(pkt);
// Create a new packet
Packet* pktret = allocpkt();
// Access the Ping header for the new packet:
MyPingHeader* hdrret = MyPingHeader::access(pktret);
// Set the 'ret' field to 1, so the receiver won't send another echo
hdrret->flag = 1;
// Set the send_time field to the correct value
hdrret->send_time = stime;
// Send the packet
send(pktret, 0);
}
else
{
double rtt=Scheduler::instance().clock()- pinghdr->send_time;
printf("Node %d received ping answer from node %d at time %5.2lf
with round-trip-time %5.2lf seconds \n",iphdr->src_.addr_,
iphdr->dst_.addr_,Scheduler::instance().clock(),rtt);
Packet::free(pkt);
}
}
-----------------------------------------------------
MyPing.tcl
------------------------------------------------------
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
#Create three nodes
#Connect the nodes with one link
$ns duplex-link $n0 $n1 1Mb 1s DropTail
#Create two ping agents and attach them to the nodes n0 and n1
set p0 [new Agent/MyPing]
$ns attach-agent $n0 $p0
set p1 [new Agent/MyPing]
$ns attach-agent $n1 $p1
#Connect the two agents
$ns connect $p0 $p1
#Schedule events
$p0 set pkts_ 6
$ns at 1 "$p0 send"
$ns at 100.0 "$ns halt"
#Run the simulation

$ns run
Ex.1
1.WIRELESS NODE CREATION AND UDP DATA TRANSFER METHOD
set x 500
set y 500
set n 2
set ns_ [new Simulator]
set topo [new Topography]
$topo load_flatgrid $x $y
set tracefd [open wireless.tr w]
$ns_ trace-all $tracefd
$ns_ use-newtrace
set namtracefd [open first.nam w]
$ns_ namtrace-all-wireless $namtracefd $x $y
set god_ [create-god $n]
$ns_ node-config -adhocRouting AODV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF
for {set i 0} { $i < $n} {incr i} {
set node_($i) [$ns_ node]
}
for {set i 0} {$i < $n} {incr i} {
$ns_ initial_node_pos $node_($i) 30
}
#MOBILE NODE POSITION
#node position -static (fixed ) topology
$node_(0) set X_ 50.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 200.0
$node_(1) set Y_ 150.0
$node_(1) set Z_ 0.0
UDP DATA TRAFFIC
#UDP Data Traffic
#create UDP source
set udp0 [new Agent/UDP]
$ns_ attach-agent $node_(0) $udp0
#create UDP destination
set null0 [new Agent/Null]
$ns_ attach-agent $node_(1) $null0
#connect UDP src and destination
$ns_ connect $udp0 $null0
#create Application traffic
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.1
#Application start time
$ns_ at 1 "$cbr0 start"
#Application stop time
$ns_ at 10 "$cbr0 stop"
$ns_ at 100.0 "$ns_ halt"
#Run the simulation
$ns_ run

OUTPUT SNAPSHOT


1 comment:

rajeshkumar said...
This comment has been removed by the author.