[Previous section] [Back to the index] [Next section]
In this section I will give you an example for a new protocol that could be implemented in ns. You should probably become fairly familiar with ns before you try this yourself, and some C++ knowledge is definitely necessary. You should also read at least the chapters 3.1-3.3 from "ns Notes and Documentation" (now renamed ns Manual) to understand the interaction between Tcl and C++.
The code in this section implements some sort of simple 'ping' protocol (inspired by the 'ping requestor' in chapter 9.6 of the "ns Notes and Documentation" (now renamed ns Manual), but fairly different). One node will be able to send a packet to another node which will return it immediately, so that the round-trip-time can be calculated.
I understand that the code presented here might not be the best possible implementation, and I am sure it can be improved, though I hope it is easy to understand, which is the main priority here. However, suggestions can be sent here.
VII.1. The header file
In the new header file 'ping.h' we first have to declare the data structure
for the new Ping packet header which is going to carry the relevant data.
|
The following piece of code declares the class 'PingAgent' as a subclass of the class 'Agent'.
|
You can download the full header file here (I suggest you do that and take a quick look at it, since the code that was presented here isn't totally complete).
VII.2. The C++ code
First the linkage between the C++ code and Tcl code has to be defined.
It is not necessary that you fully understand this code, but it would help
you to read the chapters 3.1-3.3 in the
"ns Manual" if you
haven't done that yet to understand it.
|
The next piece of code is the constructor for the class 'PingAgent'. It binds the variables which have to be accessed both in Tcl and C++.
|
The function 'command()' is called when a Tcl command for the class 'PingAgent' is executed. In our case that would be '$pa send' (assuming 'pa' is an instance of the Agent/Ping class), because we want to send ping packets from the Agent to another ping agent. You basically have to parse the command in the 'command()' function, and if no match is found, you have to pass the command with its arguments to the 'command()' function of the base class (in this case 'Agent::command()'). The code might look very long because it's commented heavily.
|
The function 'recv()' defines the actions to be taken when a packet is received. If the 'ret' field is 0, a packet with the same value for the 'send_time' field, but with the 'ret' field set to 1 has to be returned. If 'ret' is 1, a Tcl function (which has to be defined by the user in Tcl) is called and processed the event (Important note to users of the ns version 2.1b2: 'Address::instance().NodeShift_[1]' has to be replaced with 'NODESHIFT' to get the example to work under ns 2.1b2).
|
VII.3. Necessary changes
You will have to change some things in some of the ns source files if you want to add a new agent, especially if it uses a new packet format. I suggest you always mark your changes with comments, use #ifdef, etc., so you can easily remove your changes or port them to new ns releases.
We're going to need a new packet type for the ping agent, so the first step is to edit the file 'packet.h'. There you can find the definitions for the packet protocol IDs (i.e. PT_TCP, PT_TELNET, etc.). Add a new definition for PT_PING there. In my edited version of packet.h, the last few lines of enum packet_t {} looks like the following code (it might look a bit different in earlier/later releases).
|
You also have to edit the p_info() in the same file to include "Ping".
|
Remember that you have to do a 'make depend' before you do the 'make', otherwise these two files might not be recompiled.
The file 'tcl/lib/ns-default.tcl' has to be edited too. This is the file where all default values for the Tcl objects are defined. Insert the following line to set the default packet size for Agent/Ping.
|
You also have to add an entry for the new ping packets in the file 'tcl/lib/ns-packet.tcl' in the list at the beginning of the file. It would look like the following piece of code.
|
The last change is a change that has to be applied to the 'Makefile'. You have to add the file 'ping.o' to the list of object files for ns. In my version the last lines of the edited list look like this:
|
You should be able to recompile ns now simply by typing 'make' in the ns directory. If you are having any problems, please email ns-users.
VII.4. The Tcl code
I'm not going to present the full code for a Tcl example for
the Ping agent now. You can download a full example
here. But I will show you how
to write the 'recv' procedure that is called from the 'recv()'
function in the C++ code when a ping 'echo' packet is received.
|
Now you can try some experiments of your own. A very simple experiment would be to not set the 'ret' field in the packets to 1. You can probably guess what is going to happen. You can also try to add some code that allows the user to send ping packets with '$pa send $node' (where 'pa' is a ping agent and 'node' a node) without having to connect the agent 'pa' with the ping agent on 'node' first, though that might be a little bit more complicated than it sounds at first. You can also read the chapter 9.6 from the "ns Manual" to learn more about creating your own agents. Good luck.
[Previous section] [Back to the index] [Next section]
Marc Greis [email protected]