Thursday, May 23, 2013

How to write a good discussion

Here is some interesting presentation about "How to write a good discussion in research papers and dissertations": follow the link

http://www.slideshare.net/GerardPaulSharpling/writing-a-good-discussion-gs-may-2013

Saturday, May 18, 2013

Creating a Broadcast Agent in NS-2

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";



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
}



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



Monday, May 13, 2013

Creating new agents in NS2

In this post I am going to explain how to create a new agent to perform certain task. On behalf of nodes  in NS-2 agents will perform the tasks such as collection of information, maintaining data structures (such as routing table, tree information etc), receiving or forwarding messages. The following example compute volume of a cube.

In NS-2, any user defined agent class should inherit Agent super class.

Required header file "agent.h"

Before starting the program, at first create a folder in the ns-2 main folder

$ cd /ns-allinone-2.35/ns-2.35/
:~/ns-allinone-2.35/ns-2.35$ mkdir boxagent
:~/ns-allinone-2.35/ns-2.35$ cd boxagent
:~/ns-allinone-2.35/ns-2.35/ boxagent $  gedit boxagent.cc


boxagent.cc
--------------

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

class BoxAgent : public Agent {
           double height;
           double depth;
           double width;
       
            public:
                      BoxAgent();
                      int command(int, const  char*const*);
                      void volume();
};


static class BoxAgentClass : public TclClass {
         public:
                  BoxAgentClass():TclClass("Agent/BoxAgent") {}
                  // Creating BoxAgent
                  TclObject *create(int argc,const char*const*) {
                               return (new BoxAgent);
                  }
}class_box_agent;

BoxAgent::BoxAgent() : Agent(PT_UDP) {
          // TCL binding
          bind("height",&height);
          bind("depth",&depth);
          bind("width",&width);
}

int BoxAgent::command(int argc,const char*const* argv) {
       if(argc==2) {
                 // to check for volume command supplied from tcl file
                 if(strcmp(argv[1],"volume")==0) {
                                       volume(); // calling volume function
                                       return (TCL_OK); // compulsory to return success
                 }
       }
return (Agent::command(argc,argv)); // Return the variable to base class
}

void BoxAgent::volume() {
          printf("\n Volume is %f",height*width*depth);
}

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

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 = boxagent/boxagent.o \

save and close the Makefile.in

:~/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

Now the changes are reflected on to the system.

To check the agent now we write a tcl file.

$ gedit boxagent.tcl


# default values for height, width and depth
Agent/BoxAgent set height 1.0
Agent/BoxAgent set width 1.0
Agent/BoxAgent set  depth 1.0

#values of variables can be changed with instances
set bagent [new Agent/BoxAgent]

$bagent set height 10.0
$bagent set width 10.0
$bagent set depth 10.0

$bagent volume

set bagent1 [new Agent/BoxAgent]

$bagent1 set height 20.0
$bagent1 set width 20.0
$bagent1 set depth 20.0

$bagent1 volume


Save and close the file

$ ns boxagent.tcl

Volume is 1000.000000
 Volume is 8000.000000