在NS2中添加路由协议

来源:互联网 发布:什么是端口 编辑:程序博客网 时间:2024/04/28 16:11
step 1:比如我们新建的协议名字就叫做protoname,以ns2.27平台为例,我们在ns2.27目录下建立一个protoname目录。此目录包含protoname.h,protoname.cc,protoname_pkt.h,protoname_rtable.h,protoname_rtable.cc五个文件。
其中五个文件的具体功能和作用如下:
(1)protoname.h 定义必要的计时器和路由代理
(2)protoname.cc 执行计时器、路由代理和Tcl文件
(3)protoname_pkt.h 声明protoname路由协议需要在无线自组网节点交换的数据包
(4)protoname_rtable.h 声明我们自己的路由选择表
(5)protoname_rtable.cc 执行路由选择表
step 2:相应文件的代码
(1)protoname.h
#ifndef __protoname_h__
#define __protoname_h__
// 下面包含一些需要的头文件
#include "protoname_pkt.h" //数据包报头
#include "protoname_rtable.h" 
#include   //代理基本类
#include //数据包类
#include //跟踪类,用于在跟踪文件里记录输出的仿真结果
#include //计时器基本类,创建我们自定义的计时器
#include //随机类,用于产生伪随机数
#include //端口分类器类,用于淘汰向上层传输的数据包
#include
#include "arp.h"
#include "ll.h"
#include "mac.h"
#include "ip.h"
#include "delay.h"
#define CURRENT_TIME Scheduler::instance().clock() //定义了一个用于得到当前仿真时间的宏
                                                                                      //通过一个调度类的实例完成
#define JITTER (Random::uniform()*0.5) //在0-0.5之间去随机数作为发送数据的延迟时间
class Protoname; // forward declaration
   //自定义计时器发送定时的控制包
class Protoname_PktTimer : public TimerHandler {
    public:
    Protoname_PktTimer(Protoname* agent) : TimerHandler() {
    agent_ = agent;
    }
    protected:
    Protoname* agent_;
    virtual void expire(Event* e);
};
//定义Protoname 类
class Protoname : public Agent {

friend class Protoname_PktTimer;
//封装了自身的地址、内状态、路由表、可变的Tcl
                                   //以及一个负责指定输出数量的计数器
nsaddr_t ra_addr_;
//protoname_state state_;
protoname_rtable rtable_;
int accesible_var_; //用来读取Tcl代码或脚本语言
u_int8_t seq_num_;
protected:
MobileNode* node_;
PortClassifier* dmux_; // For passing packets up to agents.端口分类器
Trace* logtarget_; // For logging.跟踪器
Protoname_PktTimer pkt_timer_; // Timer for sending packets.自定义计时器
//内部属性
inline nsaddr_t& ra_addr() { return ra_addr_; }
//inline protoname_state& state() { return state_; }
inline int& accessible_var() { return accesible_var_; }
void forward_data(Packet*); //数据包被正确传输的目的地
void recv_protoname_pkt(Packet*);
void send_protoname_pkt();
void reset_protoname_pkt_timer();
public:
Protoname(nsaddr_t);
int command(int, const char*const*);
void recv(Packet*, Handler*);
//void mac_failed(Packet*);
};
#endif
原创粉丝点击