The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ... The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

nmlab.korea.ac.kr
from nmlab.korea.ac.kr More from this publisher
12.07.2015 Views

static class ECHOClass : public TclClass {public:ECHOClass() : TclClass("Agent/ECHO") {}TclObject* create(int argc, const char*const* argv) {return (new ECHO_Agent());}} class_echo;Here, a static object “class_echo” is created. It’s constructor (executed immediately when the simulator is executed) placesthe class name “Agent/ECHO” into the OTcl name space. The mixing of case is by convention; recall from Section 3.5 in theearlier chapters that the “/” character is a hierarchy delimiter for the interpreted hierarchy. The definition of the create()method specifies how a C++ shadow object should be created when the OTcl interpreter is instructed to create an objectof class “Agent/ECHO”. In this case, a dynamically-allocated object is returned. This is the normal way new C++ shadowobjects are created.Once we have the object creation set up, we will want to link C++ member variables with corresponding variables in the OTclnname space, so that accesses to OTcl variables are actually backed by member variables in C++. Assume we would likeOTcl to be able to adjust the sending interval and the packet size. This is accomplished in the class’s constructor:ECHO_Agent::ECHO_Agent() : Agent(PT_ECHO){bind_time("interval_", &interval_);bind("packetSize_", &size_);}Here, the C++ variables interval_ and size_ are linked to the OTcl instance variables interval_ and packetSize_,respectively. Any read or modify operation to the Otcl variables will result in a corresponding access to the underlying C++variables. The details of the bind() methods are described elsewhere (Section 3.4.2). The defined constant PT_ECHO ispassed to the Agent() constuctor so that the Agent::allocpkt() method may set the packet type field used by the tracesupport (Section 26.5). In this case, PT_ECHO represents a new packet type and must be defined in ~ns/trace.h (Section 26.4).Once object creation and variable binding is set up, we may want to create methods implemented in C++ but which can beinvoked from OTcl (Section 3.4.4). These are often control functions that initiate, terminate or modify behavior. In our presentexample, we may wish to be able to start the ping query agent from OTcl using a “start” directive. This may be implementedas follows:int ECHO_Agent::command(int argc, const char*const* argv){if (argc == 2) {if (strcmp(argv[1], "start") == 0) {timeout(0);return (TCL_OK);}}return (Agent::command(argc, argv));}Here, the start() method available to OTcl simply calls the C++ member function timeout() which initiates the firstpacket generation and schedules the next. Note this class is so simple it does not even include a way to be stopped.105

10.6.4 Using the agent through OTclThe agent we have created will have to be instantiated and attached to a node. Note that a node and simulator object isassumed to have already been created. The following OTcl code performs these functions:set echoagent [new Agent/ECHO]$simulator attach-agent $node $echoagentTo set the interval and packet size, and start packet generation, the following OTcl code is executed:$echoagent set dst_ $dest$echoagent set fid_ 0$echoagent set prio_ 0$echoagent set flags_ 0$echoagent set interval_ 1.5$echoagent set packetSize_ 1024$echoagent startThis will cause our agent to generate one 1024-byte packet destined for node $dest every 1.5 seconds.10.7 The Agent APISimulated applications may be implemented on top of protocol agents. Chapter 38 describes the API used by applications toaccess the services provided by the protocol agent.10.8 Different agent objectsClass Agent forms the base class from which different types of objects like Nullobject, TCP etc are derived. The methods forAgent class are described in the next section. Configuration parameters for:fid_ Flowid.prio_ Priority.agent_addr_ Address of this agent.agent_port_ Port adress of this agent.dst_addr_Destination address for the agent.dst_port_ Destination port address for the agent.flags_ttl_ TTL defaults to 32.There are no state variables specific to the generic agent class. Other objects derived from Agent are given below:106

st<strong>at</strong>ic class ECHOClass : public TclClass {public:ECHOClass() : TclClass("Agent/ECHO") {}TclObject* cre<strong>at</strong>e(int argc, co<strong>ns</strong>t char*co<strong>ns</strong>t* argv) {return (new ECHO_Agent());}} class_echo;Here, a st<strong>at</strong>ic object “class_echo” is cre<strong>at</strong>ed. It’s co<strong>ns</strong>tructor (executed immedi<strong>at</strong>ely when the simul<strong>at</strong>or is executed) placesthe class name “Agent/ECHO” into the OTcl name space. <strong>The</strong> mixing of case is by convention; recall from Section 3.5 in theearlier chapters th<strong>at</strong> the “/” character is a hierarchy delimiter for the interpreted hierarchy. <strong>The</strong> definition of the cre<strong>at</strong>e()method specifies how a C++ shadow object should be cre<strong>at</strong>ed when the OTcl interpreter is i<strong>ns</strong>tructed to cre<strong>at</strong>e an objectof class “Agent/ECHO”. In this case, a dynamically-alloc<strong>at</strong>ed object is returned. This is the normal way new C++ shadowobjects are cre<strong>at</strong>ed.Once we have the object cre<strong>at</strong>ion set up, we will want to link C++ member variables with corresponding variables in the OTclnname space, so th<strong>at</strong> accesses to OTcl variables are actually backed by member variables in C++. Assume we would likeOTcl to be able to adjust the sending interval <strong>and</strong> the packet size. This is accomplished in the class’s co<strong>ns</strong>tructor:ECHO_Agent::ECHO_Agent() : Agent(PT_ECHO){bind_time("interval_", &interval_);bind("packetSize_", &size_);}Here, the C++ variables interval_ <strong>and</strong> size_ are linked to the OTcl i<strong>ns</strong>tance variables interval_ <strong>and</strong> packetSize_,respectively. Any read or modify oper<strong>at</strong>ion to the Otcl variables will result in a corresponding access to the underlying C++variables. <strong>The</strong> details of the bind() methods are described elsewhere (Section 3.4.2). <strong>The</strong> defined co<strong>ns</strong>tant PT_ECHO ispassed to the Agent() co<strong>ns</strong>tuctor so th<strong>at</strong> the Agent::allocpkt() method may set the packet type field used by the tracesupport (Section 26.5). In this case, PT_ECHO represents a new packet type <strong>and</strong> must be defined in ~<strong>ns</strong>/trace.h (Section 26.4).Once object cre<strong>at</strong>ion <strong>and</strong> variable binding is set up, we may want to cre<strong>at</strong>e methods implemented in C++ but which can beinvoked from OTcl (Section 3.4.4). <strong>The</strong>se are often control functio<strong>ns</strong> th<strong>at</strong> initi<strong>at</strong>e, termin<strong>at</strong>e or modify behavior. In our presentexample, we may wish to be able to start the ping query agent from OTcl using a “start” directive. This may be implementedas follows:int ECHO_Agent::comm<strong>and</strong>(int argc, co<strong>ns</strong>t char*co<strong>ns</strong>t* argv){if (argc == 2) {if (strcmp(argv[1], "start") == 0) {timeout(0);return (TCL_OK);}}return (Agent::comm<strong>and</strong>(argc, argv));}Here, the start() method available to OTcl simply calls the C++ member function timeout() which initi<strong>at</strong>es the firstpacket gener<strong>at</strong>ion <strong>and</strong> schedules the next. Note this class is so simple it does not even include a way to be stopped.105

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!