如何在NS2中添加新的协议

来源:互联网 发布:典型日负荷曲线数据 编辑:程序博客网 时间:2024/04/28 18:55

------翻译自http://blog.chinaunix.net/u3/97846/showart_1961229.html

 

    我经常遇到类似“如何在NS2中运行协议”这样的问题,其实NS2已经支持多种协议,并且在运行这些协议的时候无需对其进行配置,例如DSR、AODV等。

    但是如何知晓TCL中有关协议的语法呢?

    大部分的TCL程序都是类似的,但首先你必须明确协议的特征,当你需要编辑cc文件(C++源文件)的时候首先找出协议文件所在的文件夹,然后看一下协议的构造,举例如下:

 

//protocol.cc

protocol:protocol ()
{
    bind("DelayCount_“, &DelayCount);
}

//simulation.tcl


Agent/Protocol set DelayCount_ 1

 

// 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″

 

//修改common/packet.h, tcl/lib/ns-default.tcl, tcl/lib/ns-packet.tcl 及 Makefile

 

//common/packet.h

class p_info {
public:
    p_info() {
        name_[PT_TCP]= "tcp";
        name_[PT_PROTOCOL] = “protocol”;

   }

}

//tcl/lib/ns-default.tcl


Agent/Protocol set packetSize_ 32

 

//tcl/lib/ns-packet.tcl

foreach prot {
AODV
Protocol

Makefile
OBJ_CC = /
random.o rng.o ranvar.o misc.o timer-handler.o /
...
protocol/protocol.o
$(OBJ_STL)

cmu-trace.cc

#include <protocol/protocol.h>
// 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 …