怎么在NS2中添加一个协议
2016-09-26 15:38
351 查看
很简答的一个历程,就是说明下,在C++层面添加一个组件,需要做哪些工作,设计到哪些系统文件,请高手飞过。
一、到ns-allinone-2.35\ns-2.35\queue目录
复制drop-tail.cc和drop-tail.h(本目录),并重名为:myfifo.cc和myfifo.h
1、修改myfifo.h
替换掉drop_tail为myfifo
替换掉DropTail为Myfifo
#ifndef ns_myfifo_h
#define ns_myfifo_h
#include <string.h>
#include "queue.h"
#include "config.h"
/*
* A bounded, myfifo queue
*/
class Myfifo : public Queue {
public:
Myfifo() {
q_ = new PacketQueue;
pq_ = q_;
bind_bool("drop_front_", &drop_front_);
bind_bool("summarystats_", &summarystats);
bind_bool("queue_in_bytes_", &qib_); // boolean: q in bytes?
bind("mean_pktsize_", &mean_pktsize_);
// _RENAMED("drop-front_", "drop_front_");
}
~Myfifo() {
delete q_;
}
protected:
void reset();
int command(int argc, const char*const* argv);
void enque(Packet*);
Packet* deque();
void shrink_queue();// To shrink queue and drop excessive packets.
PacketQueue *q_;/* underlying FIFO queue */
int drop_front_;/* drop-from-front (rather than from tail) */
int summarystats;
void print_summarystats();
int qib_;
/* bool: queue measured in bytes? */
int mean_pktsize_;/* configured mean packet size in bytes */
};
#endif
2、修改myfifo.cc
替换
替换掉drop_tail为myfifo
替换掉DropTail为Myfifo
#include "myfifo.h"
static class MyfifoClass : public TclClass {
public:
MyfifoClass() : TclClass("Queue/Myfifo") {}
TclObject* create(int, const char*const*) {
return (new Myfifo);
}
} class_drop_tail;//我这里没有改也能用
void Myfifo::reset()
{
Queue::reset();
}
int
Myfifo::command(int argc, const char*const* argv)
{
if (argc==2) {
if (strcmp(argv[1], "printstats") == 0) {
print_summarystats();
return (TCL_OK);
}
if (strcmp(argv[1], "shrink-queue") == 0) {
shrink_queue();
return (TCL_OK);
}
}
if (argc == 3) {
if (!strcmp(argv[1], "packetqueue-attach")) {
delete q_;
if (!(q_ = (PacketQueue*) TclObject::lookup(argv[2])))
return (TCL_ERROR);
else {
pq_ = q_;
return (TCL_OK);
}
}
}
return Queue::command(argc, argv);
}
/*
* myfifo
*/
void Myfifo::enque(Packet* p)
{
if (summarystats) {
Queue::updateStats(qib_?q_->byteLength():q_->length());
}
int qlimBytes = qlim_ * mean_pktsize_;
if ((!qib_ && (q_->length() + 1) >= qlim_) ||
(qib_ && (q_->byteLength() + hdr_cmn::access(p)->size()) >= qlimBytes)){
// if the queue would overflow if we added this packet...
if (drop_front_) { /* remove from head of queue */
q_->enque(p);
Packet *pp = q_->deque();
drop(pp);
} else {
drop(p);
}
} else {
q_->enque(p);
}
}
//AG if queue size changes, we drop excessive packets...
void Myfifo::shrink_queue()
{
int qlimBytes = qlim_ * mean_pktsize_;
if (debug_)
printf("shrink-queue: time %5.2f qlen %d, qlim %d\n",
Scheduler::instance().clock(),
q_->length(), qlim_);
while ((!qib_ && q_->length() > qlim_) ||
(qib_ && q_->byteLength() > qlimBytes)) {
if (drop_front_) { /* remove from head of queue */
Packet *pp = q_->deque();
drop(pp);
} else {
Packet *pp = q_->tail();
q_->remove(pp);
drop(pp);
}
}
}
Packet* Myfifo::deque()
{
if (summarystats && &Scheduler::instance() != NULL) {
Queue::updateStats(qib_?q_->byteLength():q_->length());
}
return q_->deque();
}
void Myfifo::print_summarystats()
{
//double now = Scheduler::instance().clock();
printf("True average queue: %5.3f", true_ave_);
if (qib_)
printf(" (in bytes)");
printf(" time: %5.3f\n", total_time_);
}
二、修改makefile
这里设计到三个文件 ns-allinone-2.35/ns-2.35
makefile makefile.in makefile.vc
我全部改掉到了,不知道makefile.vc作用;makefile.in在做 ./configure时候会把内容复制到makefile 。
三、修改ns-default.tcl
目录在ns-allinone-2.35\ns-2.35\tcl\lib
找到:DropTail
把参数复制一遍,并修改
Queue/DropTail set drop_front_ false
Queue/DropTail set summarystats_ false
Queue/DropTail set queue_in_bytes_ false
Queue/DropTail set mean_pktsize_ 500
Queue/Myfifo set drop_front_ false
Queue/Myfifo set summarystats_ false
Queue/Myfifo set queue_in_bytes_ false
Queue/Myfifo set mean_pktsize_ 500
四、CD到ns-allinone-2.35/ns-2.35
然后make,如果提示错误:
$ make myfifo.cc
Makefile.in is newer than Makefile.
You need to re-run configure.
false
Makefile:439: recipe for target 'Makefile' failed
make: *** [Makefile] Error 1
先输入 ./configure
在make clean
在make
注:makes不能指定文件名,新手见笑了
至于在用C++去写一个新的组件,后期再更新
总结:如果把新增模组添加到ns2核心步骤:
1、准备好组件(XX.CC和XX.H);
2、如果需要做初始化设定的话,修改ns-default.tcl;
3、修改makefile(把XX.O加到OBJ_CC内);
4、重新编译;
5、测试组件;
一、到ns-allinone-2.35\ns-2.35\queue目录
复制drop-tail.cc和drop-tail.h(本目录),并重名为:myfifo.cc和myfifo.h
1、修改myfifo.h
替换掉drop_tail为myfifo
替换掉DropTail为Myfifo
#ifndef ns_myfifo_h
#define ns_myfifo_h
#include <string.h>
#include "queue.h"
#include "config.h"
/*
* A bounded, myfifo queue
*/
class Myfifo : public Queue {
public:
Myfifo() {
q_ = new PacketQueue;
pq_ = q_;
bind_bool("drop_front_", &drop_front_);
bind_bool("summarystats_", &summarystats);
bind_bool("queue_in_bytes_", &qib_); // boolean: q in bytes?
bind("mean_pktsize_", &mean_pktsize_);
// _RENAMED("drop-front_", "drop_front_");
}
~Myfifo() {
delete q_;
}
protected:
void reset();
int command(int argc, const char*const* argv);
void enque(Packet*);
Packet* deque();
void shrink_queue();// To shrink queue and drop excessive packets.
PacketQueue *q_;/* underlying FIFO queue */
int drop_front_;/* drop-from-front (rather than from tail) */
int summarystats;
void print_summarystats();
int qib_;
/* bool: queue measured in bytes? */
int mean_pktsize_;/* configured mean packet size in bytes */
};
#endif
2、修改myfifo.cc
替换
替换掉drop_tail为myfifo
替换掉DropTail为Myfifo
#include "myfifo.h"
static class MyfifoClass : public TclClass {
public:
MyfifoClass() : TclClass("Queue/Myfifo") {}
TclObject* create(int, const char*const*) {
return (new Myfifo);
}
} class_drop_tail;//我这里没有改也能用
void Myfifo::reset()
{
Queue::reset();
}
int
Myfifo::command(int argc, const char*const* argv)
{
if (argc==2) {
if (strcmp(argv[1], "printstats") == 0) {
print_summarystats();
return (TCL_OK);
}
if (strcmp(argv[1], "shrink-queue") == 0) {
shrink_queue();
return (TCL_OK);
}
}
if (argc == 3) {
if (!strcmp(argv[1], "packetqueue-attach")) {
delete q_;
if (!(q_ = (PacketQueue*) TclObject::lookup(argv[2])))
return (TCL_ERROR);
else {
pq_ = q_;
return (TCL_OK);
}
}
}
return Queue::command(argc, argv);
}
/*
* myfifo
*/
void Myfifo::enque(Packet* p)
{
if (summarystats) {
Queue::updateStats(qib_?q_->byteLength():q_->length());
}
int qlimBytes = qlim_ * mean_pktsize_;
if ((!qib_ && (q_->length() + 1) >= qlim_) ||
(qib_ && (q_->byteLength() + hdr_cmn::access(p)->size()) >= qlimBytes)){
// if the queue would overflow if we added this packet...
if (drop_front_) { /* remove from head of queue */
q_->enque(p);
Packet *pp = q_->deque();
drop(pp);
} else {
drop(p);
}
} else {
q_->enque(p);
}
}
//AG if queue size changes, we drop excessive packets...
void Myfifo::shrink_queue()
{
int qlimBytes = qlim_ * mean_pktsize_;
if (debug_)
printf("shrink-queue: time %5.2f qlen %d, qlim %d\n",
Scheduler::instance().clock(),
q_->length(), qlim_);
while ((!qib_ && q_->length() > qlim_) ||
(qib_ && q_->byteLength() > qlimBytes)) {
if (drop_front_) { /* remove from head of queue */
Packet *pp = q_->deque();
drop(pp);
} else {
Packet *pp = q_->tail();
q_->remove(pp);
drop(pp);
}
}
}
Packet* Myfifo::deque()
{
if (summarystats && &Scheduler::instance() != NULL) {
Queue::updateStats(qib_?q_->byteLength():q_->length());
}
return q_->deque();
}
void Myfifo::print_summarystats()
{
//double now = Scheduler::instance().clock();
printf("True average queue: %5.3f", true_ave_);
if (qib_)
printf(" (in bytes)");
printf(" time: %5.3f\n", total_time_);
}
二、修改makefile
这里设计到三个文件 ns-allinone-2.35/ns-2.35
makefile makefile.in makefile.vc
我全部改掉到了,不知道makefile.vc作用;makefile.in在做 ./configure时候会把内容复制到makefile 。
三、修改ns-default.tcl
目录在ns-allinone-2.35\ns-2.35\tcl\lib
找到:DropTail
把参数复制一遍,并修改
Queue/DropTail set drop_front_ false
Queue/DropTail set summarystats_ false
Queue/DropTail set queue_in_bytes_ false
Queue/DropTail set mean_pktsize_ 500
Queue/Myfifo set drop_front_ false
Queue/Myfifo set summarystats_ false
Queue/Myfifo set queue_in_bytes_ false
Queue/Myfifo set mean_pktsize_ 500
四、CD到ns-allinone-2.35/ns-2.35
然后make,如果提示错误:
$ make myfifo.cc
Makefile.in is newer than Makefile.
You need to re-run configure.
false
Makefile:439: recipe for target 'Makefile' failed
make: *** [Makefile] Error 1
先输入 ./configure
在make clean
在make
注:makes不能指定文件名,新手见笑了
至于在用C++去写一个新的组件,后期再更新
总结:如果把新增模组添加到ns2核心步骤:
1、准备好组件(XX.CC和XX.H);
2、如果需要做初始化设定的话,修改ns-default.tcl;
3、修改makefile(把XX.O加到OBJ_CC内);
4、重新编译;
5、测试组件;
相关文章推荐
- 在NS2中添加一个新的协议
- 如何添加一个MAC协议--NS2
- NS2:添加一个新的自写协议的方法
- 在NS2中添加一个新的协议
- 向NS2中添加协议PING
- ns2添加注册新协议
- 怎么把豆瓣FM添加到一个wordpress页面中
- (原创)怎么直接建立一个DataTable并且为之添加数据
- 使用exchange做为outlook2003的服务器,现在我想添加一个新的邮件账户进去,怎么操作
- 在NS2 AODV协议中添加blackhole attacker(黑洞攻击)
- 在VS2005和2008的对话框里,添加了一个MSCOMM通信控件,怎么mscomm.h和mscomm.cpp没有自动出现?
- 急急急 ! 谁知道用vsto 为OFFICE添加一个选项卡怎么添加
- 我手上有一个ASP文件,添加用户时,没有添加密码的功能,添加用户成功时,更换到添加的用户要输入密码,密码不能为空,不怎么么办!
- 我的channel表里有一个字段childIds,我要添加一个新的channel,必须得同时将其父channel的childIds更新了,请问我该怎么做?
- 在NS中添加一个新协议的步骤
- HTTTP协议头中,添加某一个字段,获取返回数据中头信息的某一个字段。
- js怎么添加一个下拉列表的值
- 如何往NS2中添加新的MAC协议
- 如何在ns2中实现一个简单的网络协议
- 如何在ns2中实现一个简单的网络协议