怎么在NS2中添加一个协议

来源:互联网 发布:用c语言打开文件夹 编辑:程序博客网 时间:2024/04/28 12:51

很简答的一个历程,就是说明下,在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、测试组件;

0 0