How to Create a New Agent in NS2

  • // Create the New Agent (XY) class as a subclass of Agentclass XYAgent : public Agent {

  • // Declare the constructor of XYAgentXYAgent();

  • // Create command function with two arguments argc – argument count and argv – argument valueint command(int argc, const char*const* argv);

  • // Declare two variablesint a,b;

  • // Declare own functionint add(int, int);};static class XYAgentClass : public TclClass {public:XYAgentClass() : TclClass("Agent/XYAgentOtcl") {}TclObject* create(int, const char*const*) {return(new XYAgent());}}


  • //Define Addition functionint XYAgent::add(int num1, int num2){Tcl& tcl = Tcl::instance();tcl.evalf("puts \" Result of Addition = %d\"", num1+num2);}

  • Invoking the creating agent in Tcl
    set xyagent [new Agent / XYAgent]
    $xyagent set a 5
    $xyagent set b 10
    $xyagent add $a $b