In this post I am going to explain how a broadcast agent can be created. The agent will broadcast beacon message consisting of node id and packet sequence number. This broadcast takes place periodically using timers. The over all use of entire code is to create an agent which will broadcast beacon messages periodically. This agent will not receive any messages, however, it broadcast messages.
:~/ns-allinone-2.35/ns-2.35$ mkdir sbcast
:~/ns-allinone-2.35/ns-2.35/sbcast$
/* This Protocol perform Simple broad cast
* sbcast.h
*
* Created on: 19-May-2013
* Author: P. RAGHU VAMSI
* @ JAYPEE INSTITUTE OF INFORMATION TECHNOLOGY UNIVERSITY, INDIA
* @ prvonline@yahoo.co.in
*/
#ifndef SBCAST_H_
#define SBCAST_H_
#include "agent.h"
#include "classifier-port.h"
#include "ip.h"
#include "packet.h"
#include "cmu-trace.h"
#define CURRENT_TIME Schedular::instance().clock()
#define HDR_BEACON(p) (hdr_beacon *) hdr_beacon::access(p)
// Beacon packet header
struct hdr_beacon {
nsaddr_t addr_;
u_int8_t seq_num_;
inline nsaddr_t & addr() { return addr_; }
inline u_int8_t & seq_num() { return seq_num_; }
static int offset_;
inline static int & offset() { return offset_; }
inline static hdr_beacon * access(const Packet *p) {
return (hdr_beacon*) p->access(offset_);
}
};
// Simple Broadcast Agent
class SBAgent;
//Broadcast Timer class
class BcastTimer : public TimerHandler {
protected:
SBAgent *agent_;
virtual void expire(Event *e);
public:
BcastTimer(SBAgent *agent):TimerHandler(),agent_(agent) { }
};
class SBAgent : public Agent {
protected:
friend class BcastTimer;
private:
BcastTimer btimer_;
PortClassifier *dmux_;
Trace *logtarget_;
nsaddr_t my_addr_;
int seq_num_;
public:
SBAgent();
int command(int,const char*const*);
void recv(Packet *,Handler *);
void sendBeacon();
void resetBcastTimer();
inline nsaddr_t & my_addr() { return my_addr_;}
};
#endif /* SBCAST_H_ */
------------------------------------------------------------------------------------------------
/*
* sbcast.cc
* This Protocol perform Simple broad cast
* Created on: 19-May-2013
* Author: P. RAGHU VAMSI
* @ JAYPEE INSTITUTE OF INFORMATION TECHNOLOGY UNIVERSITY, INDIA
* @ prvonline@yahoo.co.in
*/
#include "sbcast.h"
// TCL Hooks
int hdr_beacon::offset_;
static class SBcastHeaderClass : public PacketHeaderClass {
public:
SBcastHeaderClass():PacketHeaderClass("PacketHeader/SB",sizeof(hdr_beacon)) {
bind_offset(&hdr_beacon::offset_);
}
}class_hdr_sbcast;
static class SBcastClass : public TclClass {
public:
SBcastClass():TclClass("Agent/SB") { }
TclObject *create(int argc,const char*const* argv) {
return (new SBAgent());
}
}class_sbagent;
SBAgent::SBAgent():Agent(PT_SB),btimer_(this) { }
int SBAgent::command(int argc,const char*const* argv) {
if(argc==2){
if(strcmp(argv[1],"start")==0){
// Reschedule the timer for every 0.1 seconds
btimer_.resched((double)0.1);
my_addr_ = addr();
return TCL_OK;
}
}else if (argc == 3) {
// Obtains corresponding dmux to carry packets to upper layers
if (strcmp(argv[1], "port-dmux") == 0) {
dmux_ = (PortClassifier*)TclObject::lookup(argv[2]);
if (dmux_ == 0) {
fprintf(stderr, "%s: %s lookup of %s failed\n",
__FILE__,
argv[1],
argv[2]);
return TCL_ERROR;
}
return TCL_OK;
}
if (strcmp(argv[1], "log-target") == 0 ||
strcmp(argv[1], "tracetarget") == 0) {
logtarget_ = (Trace*)TclObject::lookup(argv[2]);
if (logtarget_ == 0)
return TCL_ERROR;
return TCL_OK;
}
}
return (Agent::command(argc,argv));
}
void BcastTimer::expire(Event *e) {
agent_->sendBeacon();
agent_->resetBcastTimer();
}
void SBAgent::resetBcastTimer() {
// Reschedule the timer for every 0.1 seconds
btimer_.resched((double)0.1);
}
void SBAgent::sendBeacon() {
Packet* p = allocpkt();
struct hdr_cmn* ch = HDR_CMN(p);
struct hdr_ip* ih = HDR_IP(p);
struct hdr_beacon * ph = HDR_BEACON(p);
ph->addr() = my_addr();
ph->seq_num() = seq_num_++;
ch->ptype() = PT_SB;
ch->direction() = hdr_cmn::DOWN;
ch->size() = IP_HDR_LEN;
ch->error() = 0;
ch->next_hop() = IP_BROADCAST;
ch->addr_type() = NS_AF_INET;
ih->saddr() = my_addr();
ih->daddr() = IP_BROADCAST;
ih->sport() = RT_PORT;
ih->dport() = RT_PORT;
ih->ttl() = IP_DEF_TTL;
send(p,0);
}
void SBAgent::recv(Packet *p,Handler *h) {
}
---------------------------------------------------------------------------------------------------
Perform additions in the following files
Add entry in ns-2.35/Makefile.in
:~/ns-allinone-2.35/ns-2.35$ gedit Makefile.in
Make entry in OBJ_CC
OBJ_CC = sbcast/sbcast.o \
:~/ns-allinone-2.35/ns-2.35$ cd common
:~/ns-allinone-2.35/ns-2.35/common$ gedit packet.h
add the packet PT_SB
static const packet_t PT_SB = 74;
// insert new packet types here
static packet_t PT_NTYPE = 75; // This MUST be the LAST one
make entry in the function
static bool data_packet(packet_t type) {
return ( (type) == PT_TCP || \
(type) == PT_TELNET || \
(type) == PT_CBR || \
(type) == PT_AUDIO || \
(type) == PT_VIDEO || \
(type) == PT_ACK || \
(type) == PT_SCTP || \
(type) == PT_SCTP_APP1 || \
(type) == PT_SB || \
(type) == PT_HDLC \
);
}
provide meaning of packet in the same file
name_[PT_SB] = "SB";
name_[PT_NTYPE]= "undefined";
:~/ns-allinone-2.35/ns-2.35$ mkdir sbcast
:~/ns-allinone-2.35/ns-2.35/sbcast$
/* This Protocol perform Simple broad cast
* sbcast.h
*
* Created on: 19-May-2013
* Author: P. RAGHU VAMSI
* @ JAYPEE INSTITUTE OF INFORMATION TECHNOLOGY UNIVERSITY, INDIA
* @ prvonline@yahoo.co.in
*/
#ifndef SBCAST_H_
#define SBCAST_H_
#include "agent.h"
#include "classifier-port.h"
#include "ip.h"
#include "packet.h"
#include "cmu-trace.h"
#define CURRENT_TIME Schedular::instance().clock()
#define HDR_BEACON(p) (hdr_beacon *) hdr_beacon::access(p)
// Beacon packet header
struct hdr_beacon {
nsaddr_t addr_;
u_int8_t seq_num_;
inline nsaddr_t & addr() { return addr_; }
inline u_int8_t & seq_num() { return seq_num_; }
static int offset_;
inline static int & offset() { return offset_; }
inline static hdr_beacon * access(const Packet *p) {
return (hdr_beacon*) p->access(offset_);
}
};
// Simple Broadcast Agent
class SBAgent;
//Broadcast Timer class
class BcastTimer : public TimerHandler {
protected:
SBAgent *agent_;
virtual void expire(Event *e);
public:
BcastTimer(SBAgent *agent):TimerHandler(),agent_(agent) { }
};
class SBAgent : public Agent {
protected:
friend class BcastTimer;
private:
BcastTimer btimer_;
PortClassifier *dmux_;
Trace *logtarget_;
nsaddr_t my_addr_;
int seq_num_;
public:
SBAgent();
int command(int,const char*const*);
void recv(Packet *,Handler *);
void sendBeacon();
void resetBcastTimer();
inline nsaddr_t & my_addr() { return my_addr_;}
};
#endif /* SBCAST_H_ */
------------------------------------------------------------------------------------------------
/*
* sbcast.cc
* This Protocol perform Simple broad cast
* Created on: 19-May-2013
* Author: P. RAGHU VAMSI
* @ JAYPEE INSTITUTE OF INFORMATION TECHNOLOGY UNIVERSITY, INDIA
* @ prvonline@yahoo.co.in
*/
#include "sbcast.h"
// TCL Hooks
int hdr_beacon::offset_;
static class SBcastHeaderClass : public PacketHeaderClass {
public:
SBcastHeaderClass():PacketHeaderClass("PacketHeader/SB",sizeof(hdr_beacon)) {
bind_offset(&hdr_beacon::offset_);
}
}class_hdr_sbcast;
static class SBcastClass : public TclClass {
public:
SBcastClass():TclClass("Agent/SB") { }
TclObject *create(int argc,const char*const* argv) {
return (new SBAgent());
}
}class_sbagent;
SBAgent::SBAgent():Agent(PT_SB),btimer_(this) { }
int SBAgent::command(int argc,const char*const* argv) {
if(argc==2){
if(strcmp(argv[1],"start")==0){
// Reschedule the timer for every 0.1 seconds
btimer_.resched((double)0.1);
my_addr_ = addr();
return TCL_OK;
}
}else if (argc == 3) {
// Obtains corresponding dmux to carry packets to upper layers
if (strcmp(argv[1], "port-dmux") == 0) {
dmux_ = (PortClassifier*)TclObject::lookup(argv[2]);
if (dmux_ == 0) {
fprintf(stderr, "%s: %s lookup of %s failed\n",
__FILE__,
argv[1],
argv[2]);
return TCL_ERROR;
}
return TCL_OK;
}
if (strcmp(argv[1], "log-target") == 0 ||
strcmp(argv[1], "tracetarget") == 0) {
logtarget_ = (Trace*)TclObject::lookup(argv[2]);
if (logtarget_ == 0)
return TCL_ERROR;
return TCL_OK;
}
}
return (Agent::command(argc,argv));
}
void BcastTimer::expire(Event *e) {
agent_->sendBeacon();
agent_->resetBcastTimer();
}
void SBAgent::resetBcastTimer() {
// Reschedule the timer for every 0.1 seconds
btimer_.resched((double)0.1);
}
void SBAgent::sendBeacon() {
Packet* p = allocpkt();
struct hdr_cmn* ch = HDR_CMN(p);
struct hdr_ip* ih = HDR_IP(p);
struct hdr_beacon * ph = HDR_BEACON(p);
ph->addr() = my_addr();
ph->seq_num() = seq_num_++;
ch->ptype() = PT_SB;
ch->direction() = hdr_cmn::DOWN;
ch->size() = IP_HDR_LEN;
ch->error() = 0;
ch->next_hop() = IP_BROADCAST;
ch->addr_type() = NS_AF_INET;
ih->saddr() = my_addr();
ih->daddr() = IP_BROADCAST;
ih->sport() = RT_PORT;
ih->dport() = RT_PORT;
ih->ttl() = IP_DEF_TTL;
send(p,0);
}
void SBAgent::recv(Packet *p,Handler *h) {
}
---------------------------------------------------------------------------------------------------
Perform additions in the following files
Add entry in ns-2.35/Makefile.in
:~/ns-allinone-2.35/ns-2.35$ gedit Makefile.in
Make entry in OBJ_CC
OBJ_CC = sbcast/sbcast.o \
:~/ns-allinone-2.35/ns-2.35$ cd common
:~/ns-allinone-2.35/ns-2.35/common$ gedit packet.h
add the packet PT_SB
static const packet_t PT_SB = 74;
// insert new packet types here
static packet_t PT_NTYPE = 75; // This MUST be the LAST one
make entry in the function
static bool data_packet(packet_t type) {
return ( (type) == PT_TCP || \
(type) == PT_TELNET || \
(type) == PT_CBR || \
(type) == PT_AUDIO || \
(type) == PT_VIDEO || \
(type) == PT_ACK || \
(type) == PT_SCTP || \
(type) == PT_SCTP_APP1 || \
(type) == PT_SB || \
(type) == PT_HDLC \
);
}
provide meaning of packet in the same file
name_[PT_SB] = "SB";
name_[PT_NTYPE]= "undefined";
close packet.h
:~/ns-allinone-2.35/ns-2.35$ cd queue
:~/ns-allinone-2.35/ns-2.35/queue$ gedit priqueue.cc
add the following in switch case
case PT_SB:
recvHighPriority(p, h);
break;
close priqueue.cc
:~/ns-allinone-2.35/ns-2.35$ cd tcl/lib/
:~/ns-allinone-2.35/ns-2.35/tcl/lib$ gedit ns-lib.tcl
SB {
set ragent [$self create-sb-agent $node]
}
Simulator instproc create-sb-agent { node } {
# Create AODV routing agent
set ragent [new Agent/SB [$node node-addr]]
$self at 0.0 "$ragent start" ;# start BEACON/HELLO Messages
$node set ragent_ $ragent
return $ragent
}
:~/ns-allinone-2.35/ns-2.35/queue$ gedit priqueue.cc
add the following in switch case
case PT_SB:
recvHighPriority(p, h);
break;
close priqueue.cc
:~/ns-allinone-2.35/ns-2.35$ cd tcl/lib/
:~/ns-allinone-2.35/ns-2.35/tcl/lib$ gedit ns-lib.tcl
SB {
set ragent [$self create-sb-agent $node]
}
Simulator instproc create-sb-agent { node } {
# Create AODV routing agent
set ragent [new Agent/SB [$node node-addr]]
$self at 0.0 "$ragent start" ;# start BEACON/HELLO Messages
$node set ragent_ $ragent
return $ragent
}
save and close ns-lib.tcl
:~/ns-allinone-2.35/ns-2.35/tcl/lib$ gedit ns-agent.tcl
Agent/SB instproc init args {
$self next $args
}
save and close ns-agent.tcl
:~/ns-allinone-2.35/ns-2.35/tcl/lib$ gedit ns-packet.tcl
make an entry for SB
SB
save and close ns-packet.tcl
:~/ns-allinone-2.35/ns-2.35$ ./configure
:~/ns-allinone-2.35/ns-2.35$ make
:~/ns-allinone-2.35/ns-2.35$ sudo make install
TCL script for running the simulation
#===================================
# Simulation parameters setup
#===================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 5 ;# number of mobilenodes
set val(rp) SB ;# routing protocol
set val(ie) 100 ; # initial energy of a node
set val(em) EnergyModel ; # Energy model
set val(x) 100 ;# X dimension of topography
set val(y) 100 ;# Y dimension of topography
set val(stop) 10
#===================================
# Attaching selected headers
#===================================
remove-all-packet-headers
add-packet-header Mac LL IP ARP LL SB
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#Setup topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)
#Open the NS trace file
set tracefile [open out.tr w]
$ns trace-all $tracefile
#Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel
Mac/802_11 set dataRate 512Kbps
#===================================
# Node parameter setup
#===================================
$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF \
-energyModel $val(em) \
-initialEnergy $val(ie) \
-rxPower 35.25e-23 \
-txPower 31.21e-23 \
-idlePower 712e-6 \
-sleepPower 144e-9
#===================================
# Nodes Definition
#===================================
for {set i 0} {$i<$val(nn)} {incr i} {
set node_($i) [$ns node]
}
for {set i 0} {$i<$val(nn)} {incr i} {
set u_($i) [new Agent/SB]
$ns attach-agent $node_($i) $u_($i)
}
$node_(0) set X_ 0.0
$node_(0) set Y_ 0.0
$node_(0) set Z_ 0
$node_(1) set X_ 40.0
$node_(1) set Y_ 0.0
$node_(1) set Z_ 0
$node_(2) set X_ 25.0
$node_(2) set Y_ 0.0
$node_(2) set Z_ 0
$node_(3) set X_ 0.0
$node_(3) set Y_ 75.0
$node_(3) set Z_ 0
$node_(4) set X_ 60.0
$node_(4) set Y_ 20.0
$node_(4) set Z_ 0
for {set i 0} {$i < $val(nn)} {incr i} {
$ns initial_node_pos $node_($i) 10
}
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam out.nam &
exit 0
}
#===================================
# Reset the nodes
#===================================
for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "$node_($i) reset"
}
#===================================
# Simulation start up
#===================================
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
puts "before sim run"
$ns run
6 comments:
thnxs for guidance.
If any one face error related to SB then add
Agent/SB set sport_ 0
Agent/SB set dport_ 0
in the ns-agent.tcl.
i follow all the steps and also the suggestion from Ashutosh Bhardwaj.
but i got this error :
[code omitted because of length]
: invalid command name "SB"
while executing
"SB {
set ragent [$self create-sb-agent $node]
}"
any comment please...
Oka, I know it may be late to help you, but my comment may be helpful to someone else. The problem is that you probably defined SB in the wrong place.
Indeed it must be defined under:
switch -exact $routingAgent_ {
...
}
i follow all the steps and also the suggestion from Ashutosh Bhardwaj
but i got this error
ns:
[code omitted because of length]
: invalid command name "Agent/FIRST"
while executing
"Agent/FIRST instproc init args {
$self next $args
}"
any comment plzzz..
ns:
[code omitted because of length]
: invalid command name "Agent/SB"
while executing
"Agent/SB instproc init args {
$self next $args
}"
Even m getting the same error can anyone please help me??
hello everyone, can you help me? I get this error when i run tcl script. I have been add code to ns2 is corrected
(_o17 cmd line 1)
invoked from within
"_o17 cmd if-queue _o20"
invoked from within
"catch "$self cmd $args" ret"
invoked from within
"if [catch "$self cmd $args" ret] {
set cls [$self info class]
global errorInfo
set savedInfo $errorInfo
error "error when calling class $cls: $args" $..."
(procedure "_o17" line 2)
(SplitObject unknown line 2)
invoked from within
"$agent if-queue [$self set ifq_(0)] "
(procedure "_o14" line 43)
(Node/MobileNode add-target line 43)
invoked from within
"$self add-target $agent $port"
(procedure "_o14" line 15)
(Node attach line 15)
invoked from within
"$node attach $ragent [Node set rtagent_port_]"
(procedure "_o3" line 97)
(Simulator create-wireless-node line 97)
invoked from within
"_o3 create-wireless-node"
("eval" body line 1)
invoked from within
"eval $self create-wireless-node $args"
(procedure "_o3" line 23)
(Simulator node line 23)
invoked from within
"$ns node"
("for" body line 2)
invoked from within
"for {set i 0} {$i<$val(nn)} {incr i} {
set node_($i) [$ns node]
}"
(file "sbcastExampple.tcl" line 79)
Post a Comment