You are on page 1of 26

SAT Infosys - Coimbatore

9944219934
ADD AGENT

---------------------------------------------------------------------------

Create the testagent to understand the behavior of c++ and
otcl interaction.
Write your c++ code test.cc in ns-allinone-2.34/ns2.34/test folder
---------------------------------------------------------test.cc

#include <stdio.h>
#include <string.h>
#include "agent.h"

class TestAgent : public Agent {
public:
TestAgent();
protected:
int command(int argc, const char*const* argv);
private:
int
var1;
double var2;
void
test_function(void);
};

static class TestAgentClass : public TclClass {
public:
TestAgentClass() : TclClass("Agent/TestAgentOtcl") {}
TclObject* create(int, const char*const*) {
return(new TestAgent());
}
} class_test_agent;

TestAgent::TestAgent() : Agent(PT_UDP) {
bind("var1_otcl", &var1);
bind("var2_otcl", &var2);
}
1

SAT Infosys - Coimbatore

9944219934

int TestAgent::command(int argc, const char*const* argv) {
if(argc == 2) {
if(strcmp(argv[1], "call_function") == 0) {
test_function();
return(TCL_OK);
}
}
if(argc == 4) {
if(strcmp(argv[1], "add") == 0) {
int x= atoi(argv[2]) + atoi(argv[3]);
printf("\nThe result of ADD %d",x);
return(TCL_OK);
}
}
return(Agent::command(argc, argv));
}

void TestAgent::test_function(void) {
printf("\nThe value of var1=%d",var1);
printf("\nThe value of var2=%f",var2);

}
-----------------------------------------------------------------------------

test.tcl
-----------------------------------------------------------------------------

set testagent [new Agent/TestAgentOtcl]
# Set configurable parameters of TestAgent
$testagent set var1_otcl 2
$testagent set var2_otcl 3.14
# Give a command to MyAgent

2

34/Makefile.o \ 3 ./configure make make install STEP 3 : Run your test.34/ns2.34/newrp folder To Include c++ code into NS-2 STEP 1 : Edit /root/Desktop/ns-allinone-2.Coimbatore 9944219934 $testagent call_function $testagent add 2000 4000 ---------------------------------------------------------------- ADD C++ code into NS_2 To Include c++ code into NS-2 STEP 1 : Edit /root/Desktop/ns-allinone-2.o \ STEP 2 : Recompiling NS-2 go to ~/ns-allinone-2.SAT Infosys .34/ns-2.34/Makefile.in Add following line in OBJ_CC section test/test.tcl file ----------------------------------------------------------- -----------------------------------------------------ADD NEW PROTOCOL IN NS-2 Write your c++ code in /root/Desktop/ns-allinone-2.34/ns2.in Add following line in OBJ_CC section newrp/newrp.34/ directory and do .34/ns2.

name_[PT_NTYPE]= "undefined".Coimbatore 9944219934 STEP 2 : Edit /root/Desktop/ns-allinone-2. ADD protocol Name (line no : 175) NEWRP STEP 4 : Edit /root/Desktop/ns-allinone-2.tcl To configure routing agent .SAT Infosys . name_[PT_NEWRP]="NEWRP".34/common/packet.tcl ADD the following line in switch -exact $routingAgent_ section (line no : 632) NEWRP { set ragent [$self create-newrp-agent $node] } 4 . static packet_t LAST one PT_NTYPE = 63.34/tcl/lib/ns-lib.34/ns2.h file a) add the following line packet_t section static const packet_t PT_NEWRP = 62.34/ns2. // This MUST be the b) add the following line in class p_info section static packetClass classify(packet_t type) { if (type == PT_DSR || type == PT_MESSAGE || type == PT_TORA || type == PT_AODV || type == PT_NEWRP) c) add the following line in class p_info section name_[PT_AOMDV]= "AOMDV".h To define new packet type we have to modify packet.34/tcl/lib/ns-packet.34/ns2. STEP 3 : Edit /root/Desktop/ns-allinone-2.

tcl script Change Routing Protocol name as NEWRP Run the tcl file and check the output. 5 .34/ directory and do ./configure make clean make make install STEP 7 : Test Edit file1.Coimbatore 9944219934 The following code should be added in same ns-lib.tcl at the end of the file Simulator instproc create-newrp-agent { node } { # Create NEWRP routing agent set ragent [new Agent/NEWRP [$node node-addr]] $self at 0.34/tcl/lib/ns-agent.0 "$ragent start" $node set ragent_ $ragent return $ragent } STEP 5 : Edit /root/Desktop/ns-allinone-2.34/ns-2.tcl Add the following code at the end of the file Agent/NEWRP instproc init args { $self next $args } Agent/NEWRP set sport_ Agent/NEWRP set dport_ 0 0 STEP 6 : Recompiling NS-2 go to /root/Desktop/ns-allinone-2.SAT Infosys .34/ns2.

h> class NEWRP : public Agent { public: NEWRP(nsaddr_t id). 6 .h files ---------------------------------------------------------newrp. PriQueue *ifqueue.h> #include <priqueue. PortClassifier *dmux_.SAT Infosys . newrp.Development TEST-I Write a new routing protocol in the name of NEWRP. Create newrp folder inside ns-allinone-2. nsaddr_t index.34/ns-2.cc.h> #include <classifier/classifier-port. int command(int.h. Trace *logtarget. newrp_packet.h ---------------------------------------------------------#include <cmu-trace.Coimbatore 9944219934 NEW Routing Protocol .34 folder Create newrp. const char *const *). }.

Coimbatore 9944219934 ---------------------------------------------------------newrp.h> #include <newrp/newrp_packet. } } class_rtProtoNEWRP_hdr. static class NEWRPclass : public TclClass { public: NEWRPclass() : TclClass("Agent/NEWRP") {} 7 .cc ---------------------------------------------------------#include <newrp/newrp.h> #include <cmu-trace. sizeof(hdr_all_newrp)) { bind_offset(&hdr_newrp::offset_).b) ( (a) > (b) ? (a) : (b) ) #define CURRENT_TIME Scheduler::instance().SAT Infosys .h> #include <random.h> #include <energy-model.clock() int hdr_newrp::offset_. static class NEWRPHeaderClass : public PacketHeaderClass { public: NEWRPHeaderClass() : PacketHeaderClass("PacketHeader/NEWRP".h> #define max(a.

const char*const* argv) { assert(argc == 5). return TCL_OK. "start".str2addr(argv[4]))). return TCL_OK. return (new NEWRP((nsaddr_t) Address::instance(). int NEWRP::command(int argc. } 8 . const char*const* argv) { if(argc == 2) { Tcl& tcl = Tcl::instance(). "id".handle((Event*) 0). } } class_rtProtoNEWRP.resultf("%d".SAT Infosys . index). } } else if(argc == 3) { if(strcmp(argv[1]. "index") == 0) { index = atoi(argv[2]). } if(strncasecmp(argv[1]. return TCL_OK. 2) == 0) { tcl. if(strncasecmp(argv[1].Coimbatore 9944219934 TclObject* create(int argc. 5) == 0) { // rtcTimer.

"log-target") == 0 || strcmp(argv[1]. } else if (strcmp(argv[1].Coimbatore 9944219934 else if(strcmp(argv[1]. return TCL_OK. if(logtarget == 0) return TCL_ERROR. "port-dmux") == 0) { dmux_ = (PortClassifier *)TclObject::lookup(argv[2]). } return TCL_OK.SAT Infosys . "tracetarget") == 0) { logtarget = (Trace*) TclObject::lookup(argv[2]). return TCL_OK. "if-queue") == 0) { ifqueue = (PriQueue*) TclObject::lookup(argv[2]). __FILE__. "drop-target") == 0) { return TCL_OK. } else if(strcmp(argv[1]. argv[1]. return TCL_ERROR. } else if(strcmp(argv[1]. } } 9 . if (dmux_ == 0) { fprintf (stderr. argv[2]). if(ifqueue == 0) return TCL_ERROR. "%s: %s lookup of %s failed\n".

}. ---------------------------------------------------------10 .} inline static hdr_newrp* access(const Packet *p) { return (hdr_newrp*) p->access(offset_). id). } ---------------------------------------------------------newrp_packet. } NEWRP::NEWRP(nsaddr_t id) : Agent(PT_NEWRP) { printf("N (%. argv). } }. logtarget = 0.SAT Infosys . union hdr_all_newrp { hdr_newrp newrp. static int offset_.6f): Routing agent is initialized for node %d \n".Coimbatore 9944219934 return Agent::command(argc. inline static int& offset() { return offset_. index = id. CURRENT_TIME. ifqueue = 0.h ---------------------------------------------------------#define HDR_NEWRP(p) ((struct hdr_newrp*)hdr_newrp::access(p)) struct hdr_newrp { u_int8_t pkt_type.

# how many nodes NEWRP # create simulator instance set ns_ [new Simulator] # setup topography object set topo [new Topography] # define topology $topo load_flatgrid $val(x) $val(y) set tracefd [open out.# Y dimension of the topography set val(nn) 50 set val(stop) 200.tcl ---------------------------------------------------------set val(x) 800 .tr w] $ns_ trace-all $tracefd $ns_ use-newtrace set namtrace [open out.# simulation time set val(routing) .0 .# X dimension of the topography set val(y) 800 .nam w] $ns_ namtrace-all-wireless $namtrace $val(x) $val(y) # Create God set god_ [create-god $val(nn)] $ns_ node-config -adhocRouting $val(routing) \ -llType LL \ -macType Mac/802_11 \ -ifqType Queue/DropTail/PriQueue \ 11 .SAT Infosys .Coimbatore 9944219934 wdemo.

Coimbatore 9944219934 -ifqLen 50 \ -antType Antenna/OmniAntenna \ -propType Propagation/TwoRayGround \ -phyType Phy/WirelessPhy \ -channelType Channel/WirelessChannel \ -topoInstance $topo \ -agentTrace ON \ -routerTrace ON \ -macTrace OFF # Create the specified number of nodes [$val(nn)] for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node] } source scen-50-5 set udp_(0) [new Agent/UDP] $ns_ attach-agent $node_(10) $udp_(0) set null_(0) [new Agent/Null] $ns_ attach-agent $node_(0) $null_(0) set cbr_(0) [new Application/Traffic/CBR] $cbr_(0) attach-agent $udp_(0) $ns_ connect $udp_(0) $null_(0) #$ns_ at 7.0 "$cbr_(0) start" #$ns_ at 9.0 "$cbr_(0) stop" # Define node initial position in nam for {set i 0} {$i < $val(nn)} {incr i} { 12 .SAT Infosys .

0 "$node_($i) reset".SAT Infosys ..tcl 13 . $ns_ initial_node_pos $node_($i) 50 } for {set i 0} {$i < $val(nn) } {incr i} { $ns_ at $val(stop). } #$ns_ at 1.0 "[$node_(0) set ragent_] sink" $ns_ at $val(stop).." $ns_ run ---------------------------------------------------------Create secenario file using setdest by using this command ---------------------------------------------------------setdest –n 50 –p 100 –M 5 –t 200 –x 800 –y 800 >scen-50-5 ---------------------------------------------------------Recompile ns-2 Run the wdemo.0002 " $ns_ halt" puts "Starting Simulation.Coimbatore 9944219934 # 30 defines the node size in nam.

ch->size()). struct hdr_ip *ih = HDR_IP(p).ch->uid().Coimbatore 9944219934 TEST -II Add receive function ---------------------------------------------------------newrp. ---------------------------------------------------------newrp.ih->daddr().cc ---------------------------------------------------------- void NEWRP::recv(Packet *p.tcl 14 . printf("\nNode Id %d Source Node %d \t Destination Node Hop %d ".index. } %d next } ---------------------------------------------------------- Recompile NS-2 Run wdemo.ih->saddr(). Handler*) { struct hdr_cmn *ch = HDR_CMN(p). return.h ---------------------------------------------------------void recv(Packet *p. Handler *). if(ch->ptype() == PT_CBR) { ih->ttl_ -= 1.ch->next_hop()).SAT Infosys .index. //printf("\nNode Id %d Packet Id %d \tPacket size %d ".

Coimbatore 9944219934 TEST -III Sending Announcement Message: Create announcement packet ---------------------------------------------------------newrp_packet. 15 .h ---------------------------------------------------------#define HDR_NEWRP(p) ((struct hdr_newrp*)hdr_newrp::access(p)) #define NEWRP_ANNOUNCE 0x01 #define HDR_NEWRP_ANNOUNCE(p) ((struct hdr_newrp_announce*)hdr_newrp::access(p)) struct hdr_newrp { u_int8_t pkt_type. // hop count.} inline static hdr_newrp* access(const Packet *p) { return (hdr_newrp*) p->access(offset_). } }. static int offset_. struct hdr_newrp_announce { u_int8_t pkt_type. // source address of announce. // emission time of announce inline int size() { int sz = 0. // type of packet : Announce or Error u_int8_t hop_count. this is sink address double message timestamp.SAT Infosys . inline static int& offset() { return offset_. // unique identifier for the announce nsaddr_t src_id. sz = sizeof(struct hdr_newrp_announce). increadecreases as announce is forwarded u_int32_t seq_no.

SAT Infosys . forward(Packet *p. ---------------------------------------------------------newrp.Coimbatore 9944219934 assert(sz>=0). recv_announce(Packet *p).h ---------------------------------------------------------int seqno. } 16 . void void void send_announce(). CURRENT_TIME. union hdr_all_newrp { hdr_newrp newrp. ifqueue = 0.6f): Routing agent is initialized for node %d \n". return sz.cc In the constructor initialize seqno value ---------------------------------------------------------- NEWRP::NEWRP(nsaddr_t id) : Agent(PT_NEWRP) { printf("N (%. ---------------------------------------------------------newrp. nsaddr_t nexthop. }. logtarget = 0. double delay). id). hdr_newrp_announce announce. seqno=0. } }. index = id.

} } 17 .ih->saddr(). recv_announce(p).index.index. return. Handler*) { struct hdr_cmn *ch = HDR_CMN(p). %d printf("\nNode Id %d Source Node %d \t Destination Node Hop %d ".ih->daddr(). //printf("\nNode Id %d Packet Id %d \tPacket size ". struct hdr_ip *ih = HDR_IP(p).ch->uid().Coimbatore 9944219934 ---------------------------------------------------------In the recv function method. %d next return. } if(ch->ptype() == PT_NEWRP) { ih->ttl_ -= 1.ch->next_hop()). define PT_NEWRP type packet handling ---------------------------------------------------------void NEWRP::recv(Packet *p.ch->size()).SAT Infosys . if(ch->ptype() == PT_CBR) { ih->ttl_ -= 1.

} if(strncasecmp(argv[1]. "id". } if(strncasecmp(argv[1]. "start". const char*const* argv) { if(argc == 2) { Tcl& tcl = Tcl::instance(). CURRENT_TIME. "sink". } } 18 .handle((Event*) 0).Coimbatore 9944219934 ---------------------------------------------------------In command function write send_announce() the calling method for sink node ---------------------------------------------------------int NEWRP::command(int argc. 4) == 0) { send_announce().SAT Infosys . return TCL_OK. printf("N (%. 5) == 0) { // rtcTimer. 2) == 0) { tcl. if(strncasecmp(argv[1]. return TCL_OK.resultf("%d".6f): sink node is set to %d. return TCL_OK. index). index). start announceing \n".

ch->size() = IP_HDR_LEN + am->size().0). // Write IP Header ih->saddr() = index. p.6f): send announce by %d index). struct hdr_ip *ih = HDR_IP(p). ih->ttl_ = 10. ih->sport() = RT_PORT.SAT Infosys . ih->daddr() = IP_BROADCAST. am->src_id = index.cc file ---------------------------------------------------------void NEWRP::send_announce() { Packet *p = Packet::alloc(). am->seq_no = seqno. Scheduler::instance(). // Write Announce Header am->pkt_type = NEWRP_ANNOUNCE. ch->addr_type() = NS_AF_NONE. forward(Packet *p.schedule(target_. CURRENT_TIME. } 19 . // Write Channel Header ch->ptype() = PT_NEWRP. seqno++. nsaddr_t nexthop. ch->prev_hop_ = index. printf("S (%. ih->dport() = RT_PORT. am->hop_count = 1. double delay) add in end of the newrp. 0.recv_announce(Packet *p).Coimbatore 9944219934 ---------------------------------------------------------Definition for send_announce(). am->timestamp = CURRENT_TIME. struct hdr_newrp_announce *am = HDR_NEWRP_ANNOUNCE(p). \n". struct hdr_cmn *ch = HDR_CMN(p).

} if (nexthop != (nsaddr_t) IP_BROADCAST) { ch->next_hop_ = nexthop. ch->prev_hop_ = index. // I have originated the packet. nsaddr_t nexthop. ch->direction() = hdr_cmn::DOWN. DROP_RTR_TTL). IP_BROADCAST. delay). am->seq_no. } printf("R (%. ch->addr_type() = NS_AF_NONE. am>hop_count). am->hop_count++. src:%d. ch->prev_hop_ = index. struct hdr_newrp_announce *am = HDR_NEWRP_ANNOUNCE(p). just drop it if (am->src_id == index) { Packet::free(p). hop: %d \n". return. } 20 . } void NEWRP::forward(Packet *p. double delay) { struct hdr_cmn *ch = HDR_CMN(p).6f): recv announce by %d. forward(p. } else { assert(ih->daddr() == (nsaddr_t) IP_BROADCAST). CURRENT_TIME. struct hdr_ip *ih = HDR_IP(p). index.SAT Infosys .Coimbatore 9944219934 void NEWRP::recv_announce(Packet *p) { struct hdr_ip *ih = HDR_IP(p). seqno:%d. if (ih->ttl_ == 0) { drop(p. ch->addr_type() = NS_AF_INET.1 + Random::uniform(). double delay = 0. ch->direction() = hdr_cmn::DOWN. am->src_id.

i<count.schedule(target_.Coimbatore 9944219934 Scheduler::instance(). p. } ---------------------------------------------------------wdemo.tcl TEST -IV To ADD ROUTING TABLE ---------------------------------------------------------newrp.} int lookup(nsaddr_t nid) { for (int i=0.SAT Infosys . int seq_no[100].i++) { if(node_id[i]==nid){ return i. delay).h Write the routing class within NEWRP class in newrp.tcl Assign node_(0) as the sink node ---------------------------------------------------------$ns_ at 1. int count. nsaddr_t next_hop[100].0 "[$node_(0) set ragent_] sink" ---------------------------------------------------------Recompile ns-2 Run the wdemo. } } 21 .h ---------------------------------------------------------class routetable { public: nsaddr_t node_id[100]. routetable(){count=0. int hop_count[100].

} } } int get_seqno(nsaddr_t nid) { for (int i=0. seq_no[tmp]=sqno.i<count.SAT Infosys .i++) { if(node_id[i]==nid){ return seq_no[i]. hop_count[tmp]=hcount.Coimbatore 9944219934 return -1.nsaddr_t nxt_hop.int sqno ) { int tmp=lookup(nid).i++) { if(node_id[i]==nid){ return next_hop[i]. } } } int get_hopcount(nsaddr_t nid){ for (int i=0. hop_count[count]=hcount. } void add(nsaddr_t nid. } } nsaddr_t get_nexthop(nsaddr_t nid){ for (int i=0. next_hop[tmp]=nxt_hop. next_hop[count]=nxt_hop.i++) { if(node_id[i]==nid){ return hop_count[i]. count++. seq_no[count]=sqno. if(tmp==-1) { node_id[count]=nid. } 22 . } //else if ((sqno==seq_no[tmp]&& hop_count[tmp] > hcount)||(sqno>seq_no[tmp])) { else if (sqno>seq_no[tmp]) { node_id[tmp]=nid.i<count.i<count.int hcount.

cc Define routing table add function in recv_announce(p) function ---------------------------------------------------------void NEWRP::recv_announce(Packet *p) { struct hdr_ip *ih = HDR_IP(p).6f): recv announce by %d. just drop it if (am->src_id == index) { Packet::free(p).add(am->src_id. seqno:%d. am>hop_count). struct hdr_cmn *ch = HDR_CMN(p). index. src:%d. am->src_id. } rtable_.ch->prev_hop_. } printf("R (%.lookup(am->src_id)==-1) { fwd=1.i<count.Coimbatore 9944219934 } } void print() { for (int i=0. if(fwd==1){ 23 . int fwd =0.node_id[i].am->hop_count. hop: %d \n".i++) { printf("\nDst: %d Next_hop: %d hop_count: %d seq_no: %d".am->seq_no). ---------------------------------------------------------newrp. return.seq_no[i]). if(rtable_. am->seq_no. // I have originated the packet.hop_count[i]. CURRENT_TIME.SAT Infosys .next_hop[i]. } } }rtable_. struct hdr_newrp_announce *am = HDR_NEWRP_ANNOUNCE(p).

ih->saddr(). return.tcl TEST-V To Add data packet forward function ---------------------------------------------------------newrp. forward(p.0). } } ---------------------------------------------------------Recompile ns-2 Run the wdemo. 24 .cc In the recv(Packet *p. struct hdr_ip *ih = HDR_IP(p).ih->daddr(). Handler*) { struct hdr_cmn *ch = HDR_CMN(p). } else { Packet::free(p). if(ch->ptype() == PT_CBR) { ih->ttl_ -= 1.SAT Infosys .index. printf("\nNode Id %d Source Node %d \t Destination Node Hop %d ". Handler*) function check the packet type and add forwarding method ---------------------------------------------------------void NEWRP::recv(Packet *p. 0. %d next if((ih->saddr() == index) && (ch->num_forwards() == 0)) { ch->size() += IP_HDR_LEN. IP_BROADCAST.ch->next_hop()).Coimbatore 9944219934 am->hop_count++.

tcl 25 .nxthop). forward(p. return. } } ---------------------------------------------------------Recompile ns-2 Run the wdemo.Coimbatore 9944219934 if ( (u_int32_t)ih->daddr() != IP_BROADCAST) ih->ttl_ =10.get_nexthop(ih->daddr()).0). DROP_RTR_TTL).index. return. } } if(ch->ptype() == PT_NEWRP) { ih->ttl_ -= 1. then discard. } } if(rtable_. recv_announce(p). DROP_RTR_ROUTE_LOOP). } else if(ih->saddr() == index) { drop(p. printf("\nnodeid %d Nexthop from Route Table %d". nxthop. return.SAT Infosys . 0. If it is zero.lookup(ih->daddr())!=-1) { nsaddr_t nxthop = rtable_. */ if(--ih->ttl_ == 0) { drop(p. } else { /* * Check the TTL.

Coimbatore 9944219934 26 .SAT Infosys .