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



7 comments:

Unknown said...

thanx raghu.. keep posting more

Unknown said...

hi,
while executing this program ,i getting this error
invalid command name "Agent/BoxAgent"
while executing
"Agent/BoxAgent set height 1.0"
(file "boxagent.tcl" line 1)

Unknown said...

hi,
while executing this program ,i getting this error
invalid command name "Agent/BoxAgent"
while executing
"Agent/BoxAgent set height 1.0"
(file "boxagent.tcl" line 1)


pls reply

Unknown said...

hi,
while executing this program ,i getting this error
invalid command name "Agent/BoxAgent"
while executing
"Agent/BoxAgent set height 1.0"
(file "boxagent.tcl" line 1)


pls reply

Shafqat said...

could you help me plz to create a new agent in NS2 to read a binary file

nonstoplaw said...

very nice article.....

chafikinfo said...

thanks a lot. very helpful article