如何在NS2中添加新的協議

------翻譯自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 …

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章