10.5.1 Creating the Agent

The following OTcl code fragment creates a TCP agent and sets it up:

        set tcp [new Agent/TCP]         # create sender agent;
        $tcp set fid_ 2                 # set IP-layer flow ID;
        set sink [new Agent/TCPSink]    # create receiver agent;
        $ns attach-agent $n0 $tcp       # put sender on node $n0;
        $ns attach-agent $n3 $sink      # put receiver on node $n3;
        $ns connect $tcp $sink          # establish TCP connection;
        set ftp [new Application/FTP]        # create an FTP source "application";
        $ftp attach-agent $tcp            # associate FTP with the TCP sender;
        $ns at 1.2 "$ftp start"  #arrange for FTP to start at time 1.2 sec;
The OTcl instruction new Agent/TCP results in the creation of a C++ TcpAgent class object. Its constructor first invokes the constructor of the Agent base class and then performs its own bindings. These two constructors appear as follows:
{\rm The TcpSimpleAgent constructor (\textasciitilde\emph{ns}/{tcp.cc}):}

        TcpAgent::TcpAgent() : Agent(PT_TCP), rtt_active_(0), rtt_seq_(-1),
                        rtx_timer_(this), delsnd_timer_(this)
                bind("window_", &wnd_);
                bind("windowInit_", &wnd_init_);
                bind("windowOption_", &wnd_option_);
                bind("windowConstant_", &wnd_const_);
                bind("off_ip_", &off_ip_);
                bind("off_tcp_", &off_tcp_);

{\rm The Agent constructor (\textasciitilde\emph{ns}/{agent.cc}):}

        Agent::Agent(int pkttype) : 
                addr_(-1), dst_(-1), size_(0), type_(pkttype), fid_(-1),
                prio_(-1), flags_(0)
                memset(pending_, 0, sizeof(pending_)); /* timers /
                // this is really an IP agent, so set up
                // for generating the appropriate IP fields\ldots
                bind("addr_", (int*)&addr_);
                bind("dst_", (int*)&dst_);
                bind("fid_", (int*)&fid_);
                bind("prio_", (int*)&prio_);
                bind("flags_", (int*)&flags_);
These code fragments illustrate the common case where an agent's constructor passes a packet type identifier to the Agent constructor. The values for the various packet types are used by the packet tracing facilitySectionsec:traceptype and are defined in ~ns/trace.h. The variables which are bound in the TcpAgent constructor are ordinary instance/member variables for the class with the exception of the special integer values off_tcp_ and off_ip_. These are needed in order to access a TCP header and IP header, respectively. Additional details are in the section on packet headersSectionsec:ppackethdr.

Note that the TcpAgent constructor contains initializations for two timers, rtx_timer_ and delsnd_timer_.

TimerHandler objects are initialized by providing a pointer (the this pointer) to the relevant agent.

Tom Henderson 2011-11-05