I got frequently question about how to running protocol on NS2(Network Simulator). Actually there are so many protocol that have been already supported by NS2 without configuring anymore, such as DSR, AODV, Tora etc. You just write your TCL program correctly.
How I can know the protocol syntax on TCL ?
Mostly the TCL program is similar, but you should make sure the characteristic of protocol. Find it out on its folder then go into cc file. Take a look on protocol constructor. For example :
protocol.cc
protocol:protocol ()
{
bind("DelayCount_“, &DelayCount);
}
simulation.tcl
Agent/Protocol set DelayCount_ 1
Then, you should look at command function
int protocol::command(int argc, const char*const* argv)
{
if (argc == 4) {
if (strcmp(argv[1], "sendData“) == 0)
{
// doing what …..
}
}
simulation.tcl
$ns_ at $time "$g(0) sendData 0 512″
If you wanna add new protocol that is not available before, you should modify some files on NS2 directory, such as common/packet.h, tcl/lib/ns-default.tcl, tcl/lib/ns-packet.tcl and Makefile
common/packet.h
class p_info {
public:
p_info() {
name_[PT_TCP]= "tcp";
name_[PT_PROTOCOL] = “protocol”;
tcl/lib/ns-default.tclAgent/Protocol set packetSize_ 32
tcl/lib/ns-packet.tcl
foreach prot {
AODV
Protocol
MakefileOBJ_CC = \
random.o rng.o ranvar.o misc.o timer-handler.o \
...
protocol/protocol.o
$(OBJ_STL)
Sometimes we need to change cmu-trace.cc dan cmu-trace.h so that our protocol generates trace files for wireless simulation
cmu-trace.cc
#include
// command
void CMUTrace::format_protocol(Packet *p, int offset)
{
// packet
}
// another syntax
void CMUTrace::format(Packet* p, const char *why)
hdr_cmn *ch = HDR_CMN(p);
int offset = 0;
/*
* Log the MAC Header
*/
format_mac(p, why, offset);
if (namChan_)
nam_format(p, offset);
offset = strlen(wrk_);
switch(ch->ptype()) {
case PT_MAC:
break;
case PT_ARP:
format_arp(p, offset);
break;
default:
format_ip(p, offset);
offset = strlen(wrk_);
switch(ch->ptype()) {
case PT_AODV:
format_aodv(p, offset);
break;
case PT_TORA:
format_tora(p, offset);
break;
case PT_IMEP:
format_imep(p, offset);
break;
case PT_DSR:
format_dsr(p, offset);
break;
case PT_MESSAGE:
case PT_UDP:
format_msg(p, offset);
break;
case PT_TCP:
case PT_ACK:
format_tcp(p, offset);
break;
case PT_CBR:
format_rtp(p, offset);
break;
case PT_PROTOCOL:
format_protocol(p, offset);
break;
default:
fprintf(stderr, “%s - invalid packet type (%s).\n”,
__PRETTY_FUNCTION__, packet_info.name(ch->ptype()));
exit(1);
}
}
}
}
cmu-trace.h
class CMUTrace : public Trace {
public:
CMUTrace(const char *s, char t);
void recv(Packet *p, Handler *h);
void recv(Packet *p, const char* why);
private:
char tracename[MAX_ID_LEN + 1];
int nodeColor[MAX_NODE];
int tracetype;
MobileNode *node_;
int newtrace_;
int initialized() { return node_ && 1; }
int node_energy();
int command(int argc, const char*const* argv);
void format(Packet *p, const char *why);
void nam_format(Packet *p, int offset);
void format_mac(Packet *p, const char *why, int offset);
void format_ip(Packet *p, int offset);
void format_arp(Packet *p, int offset);
void format_dsr(Packet *p, int offset);
void format_msg(Packet *p, int offset);
void format_tcp(Packet *p, int offset);
void format_rtp(Packet *p, int offset);
void format_tora(Packet *p, int offset);
void format_imep(Packet *p, int offset);
void format_aodv(Packet *p, int offset);
void format_protocol(Packet *p, int offset);
};
Please make sure and modify the files carefully …
转自