NS2相关学习——完成一个新协议(2)

在上节中,我们把教程要求的3.1-3.3过了一遍,这一次回到正途上来。看看到底是怎么完成一个新的协议的。

本节中的代码实现了一些简单的“ping”协议(灵感来自“ns注释和文档”(现在更名为ns手册)的第9.6章中的“ping请求者”,但相当不同)。 一个节点将能够发送一个数据包到另一个节点,它将立即返回,以便可以计算往返时间。

1、头文件

在新的头文件'ping.h'中,首先必须声明将携带相关数据的新的Ping包头的数据结构。

struct hdr_ping {char ret;double send_time;
};
如果数据包正在从发送方正在被ping通的节点上,则char'ret'将被设置为“0”,而在返回路由时将被设置为“1”。 double “send_time”是发送时在分组上设置的时间戳,用于后来计算往返时间。

以下代码段将类“PingAgent”声明为类“Agent”的子类。

class PingAgent : public Agent {public:PingAgent();int command(int argc, const char*const* argv);void recv(Packet*, Handler*);protected:int off_ping_;  
};

 int off_ping_将用于访问数据包的ping头。 请注意,对于具有本地对象范围的变量通常使用尾随“_”。

在下面的部分中,将介绍定义构造函数“PingAgent()”的C ++代码以及在本声明中重新定义的函数'command()'和'recv()'。

2、c++代码

首先必须定义C ++代码和Tcl代码之间的联系。

首先,定义了协议头类以及应用类(C++中)


static class PingHeaderClass : public PacketHeaderClass {
public:PingHeaderClass() : PacketHeaderClass("PacketHeader/Ping", sizeof(hdr_ping)) {}
} class_pinghdr;static class PingClass : public TclClass {
public:PingClass() : TclClass("Agent/Ping") {}TclObject* create(int, const char*const*) {return (new PingAgent());}
} class_ping;

下一段代码是类“PingAgent”的构造函数。 它绑定了必须在Tcl和C ++中访问的变量。TCL中的(packetSize)——>C++(size)

PingAgent::PingAgent() : Agent(PT_PING)
{bind("packetSize_", &size_);bind("off_ping_", &off_ping_);
}

当执行类“PingAgent”的Tcl命令时,将调用函数'command()'。 在本案例中,将是'$ pa send'(假设'pa'是Agent / Ping类的一个实例),因为我们希望将ping数据包从代理发送到另一个ping代理。 我们基本上必须在'command()'函数中解析命令,如果没有匹配,则必须将其参数传递给基类的command()函数(在这种情况下为“代理” ::命令()'(详见代码倒数第二行的return))

int PingAgent::command(int argc, const char*const* argv)
{if (argc == 2) {if (strcmp(argv[1], "send") == 0) {// Create a new packetPacket* pkt = allocpkt();// Access the Ping header for the new packet:hdr_ping* hdr = (hdr_ping*)pkt->access(off_ping_);// Set the 'ret' field to 0, so the receiving node knows// that it has to generate an echo packethdr->ret = 0;// Store the current time in the 'send_time' fieldhdr->send_time = Scheduler::instance().clock();// Send the packetsend(pkt, 0);// return TCL_OK, so the calling function knows that the// command has been processedreturn (TCL_OK);}}// If the command hasn't been processed by PingAgent()::command,// call the command() function for the base classreturn (Agent::command(argc, argv));
}

函数recv()定义了接收到数据包时要执行的操作。 如果'ret'字段为0,则必须返回“send_time”字段的值相同但“ret”字段设置为1的数据包。 如果'ret'为1,则会调用Tcl函数(必须由Tcl中的用户定义)函数并处理该事件。

void PingAgent::recv(Packet* pkt, Handler*)
{// Access the IP header for the received packet:hdr_ip* hdrip = (hdr_ip*)pkt->access(off_ip_);// Access the Ping header for the received packet:hdr_ping* hdr = (hdr_ping*)pkt->access(off_ping_);// Is the 'ret' field = 0 (i.e. the receiving node is being pinged)?if (hdr->ret == 0) {// Send an 'echo'. First save the old packet's send_timedouble stime = hdr->send_time;// Discard the packetPacket::free(pkt);// Create a new packetPacket* pktret = allocpkt();    // Access the Ping header for the new packet:hdr_ping* hdrret = (hdr_ping*)pktret->access(off_ping_);// Set the 'ret' field to 1, so the receiver won't send another echohdrret->ret = 1;                // Set the send_time field to the correct valuehdrret->send_time = stime;      // Send the packet              send(pktret, 0);                } else {                          // A packet was received. Use tcl.eval to call the Tcl// interpreter with the ping results.// Note: In the Tcl code, a procedure 'Agent/Ping recv {from rtt}'// has to be defined which allows the user to react to the ping// result.                      char out[100];                  // Prepare the output to the Tcl interpreter. Calculate the round// trip time                    sprintf(out, "%s recv %d %3.1f", name(), hdrip->src_.addr_ >> Address::instance().NodeShift_[1],(Scheduler::instance().clock()-hdr->send_time) * 1000);Tcl& tcl = Tcl::instance();     tcl.eval(out);                  // Discard the packet           Packet::free(pkt);              }                                 
}                                 

完整代码:

/** File: Code for a new 'Ping' Agent Class for the ns*       network simulator* Author: Marc Greis (greis@cs.uni-bonn.de), May 1998**/#include "ping.h"static class PingHeaderClass : public PacketHeaderClass {
public:PingHeaderClass() : PacketHeaderClass("PacketHeader/Ping", sizeof(hdr_ping)) {}
} class_pinghdr;static class PingClass : public TclClass {
public:PingClass() : TclClass("Agent/Ping") {}TclObject* create(int, const char*const*) {return (new PingAgent());}
} class_ping;PingAgent::PingAgent() : Agent(PT_PING)
{bind("packetSize_", &size_);bind("off_ping_", &off_ping_);
}int PingAgent::command(int argc, const char*const* argv)
{if (argc == 2) {if (strcmp(argv[1], "send") == 0) {// Create a new packetPacket* pkt = allocpkt();// Access the Ping header for the new packet:hdr_ping* hdr = (hdr_ping*)pkt->access(off_ping_);// Set the 'ret' field to 0, so the receiving node knows// that it has to generate an echo packethdr->ret = 0;// Store the current time in the 'send_time' fieldhdr->send_time = Scheduler::instance().clock();// Send the packetsend(pkt, 0);// return TCL_OK, so the calling function knows that the// command has been processedreturn (TCL_OK);}}// If the command hasn't been processed by PingAgent()::command,// call the command() function for the base classreturn (Agent::command(argc, argv));
}void PingAgent::recv(Packet* pkt, Handler*)
{// Access the IP header for the received packet:hdr_ip* hdrip = (hdr_ip*)pkt->access(off_ip_);// Access the Ping header for the received packet:hdr_ping* hdr = (hdr_ping*)pkt->access(off_ping_);// Is the 'ret' field = 0 (i.e. the receiving node is being pinged)?if (hdr->ret == 0) {// Send an 'echo'. First save the old packet's send_timedouble stime = hdr->send_time;// Discard the packetPacket::free(pkt);// Create a new packetPacket* pktret = allocpkt();// Access the Ping header for the new packet:hdr_ping* hdrret = (hdr_ping*)pktret->access(off_ping_);// Set the 'ret' field to 1, so the receiver won't send another echohdrret->ret = 1;// Set the send_time field to the correct valuehdrret->send_time = stime;// Send the packetsend(pktret, 0);} else {// A packet was received. Use tcl.eval to call the Tcl// interpreter with the ping results.// Note: In the Tcl code, a procedure 'Agent/Ping recv {from rtt}'// has to be defined which allows the user to react to the ping// result.char out[100];// Prepare the output to the Tcl interpreter. Calculate the round// trip timesprintf(out, "%s recv %d %3.1f", name(), hdrip->src_ >> Address::instance().NodeShift_[1], (Scheduler::instance().clock()-hdr->send_time) * 1000);Tcl& tcl = Tcl::instance();tcl.eval(out);// Discard the packetPacket::free(pkt);}
}



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/508617.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

NS2相关学习——完成一个新协议(3)

在前面已经基本学习了怎么完成一个新协议(一个神奇的ping协议,然鹅还是有点懵。。。) 接下来继续学习相关知识 接着上一部分从1开始 1、必要的修改 如果想要添加添加新的代理程序,就需要修改NS源文件中的内容,特别…

NS2相关学习——创建Xgraph的输出文件

经过前面学习代码的编写,这一部分,我们要学会如何进行分析,一个很直观的方式就是将结果图形化表示出来。 ns-allinone包的一部分是“xgraph”,一个绘图程序,可用于创建模拟结果的图形表示。 在本节中,将向…

NS2相关学习——在ns中模拟无线场景

之前学习的都是有线场景下的NS2相关应用,现在开始,终于要学习无线啦!无线是我研究的重点,要好好学习呀!在本节中,我们将学习使用ns中提供的移动无线仿真模型。 该部分由两部分组成。 在第一小节中&#xff…

An Energy-Efficient Ant-Based Routing Algorithm for Wireless Sensor Networks (无线传感网中一种基于蚁群算法的能量有效路由)

牙说:这篇论文是研究蚁群算法在能量有效路由协议的过程中必读的一篇文章,原是全英文,在这里按照自己的理解大致翻译成中文,好好学习,与君共勉。 论文题目:An Energy-Efficient Ant-Based Routing Algorith…

活在幻梦中的你我

其实仔细想想,人类和地球上的其它物种有什么不同呢?可能仅有的不同是,人类会去相信那本来并不存在的事情. 并且会为了那种虚幻的东西为止拼搏、努力。比如科技的发展,不就是人类在实现自己想象中的事物么,飞机、轮船、家电、计算机等等,无一…

An Energy-Efficient Ant-Based Routing Algorithm for Wireless Sensor Networks (无线传感网中基于蚁群算法的能量有效路由)2

牙说:接着上一篇继续写。论文标题:An Energy-Efficient Ant-Based Routing Algorithm forWireless Sensor Networks作者:Tiago Camilo, Carlos Carreto, Jorge S Silva, Fernando Boavida正文: 2、相关工作可以考虑无线传感器网络…

NS2仿真分析无线网络的攻击防御(1)

这个学期有个选题是NS2仿真分析无线网络的攻击防御,比较有意思的样子,现在来慢慢学一下这个是什么东西。 首先,还是一篇文章(老长老长了),还是全英文的,还是先来分析一下它到底在说什么&#x…

NS2仿真分析无线网络的攻击防御(2)

牙说:继续上一篇博文进行翻译。 4. NS和我们的工作 我们试图评估黑洞攻击在无线Ad-hoc网络中的影响。 为了实现这一点,我们已经使用NS 网络模拟 [14]程序模拟了一个含有黑洞节点的无线自组网络场景。为了模拟无线自组织网络中的黑洞节点,我…

Java集合之HashMap源码分析

以下源码均为jdk1.7 HashMap概述 HashMap是基于哈希表的Map接口的非同步实现. 提供所有可选的映射操作, 并允许使用null值和null健. 此类不保证映射的顺序. 需要注意的是: HashMap不是同步的. 哈希表 哈希表定义: 哈希表是一种根据关键码去寻找值的数据映射结构, 该结构通…

NS2相关学习——可靠的MANET应用程序的Gossip协议分析

好久不写,应该努力啦!老师把这篇论文给了我,现在还不知道它在讲什么,来边翻译边学习吧! 文章链接:https://www.researchgate.net/publication/316844643_Analyzing_Gossip_Protocols_for_Reliable_MANET_Ap…

Java集合之LinkedList源码分析

概述 LinkedLIst和ArrayLIst一样, 都实现了List接口, 但其内部的数据结构不同, LinkedList是基于链表实现的(从名字也能看出来), 随机访问效率要比ArrayList差. 它的插入和删除操作比ArrayList更加高效, 但还是要遍历部分链表的指针才能移动到下标所指的位置, 只有在链表两头的…

lex和yacc环境配置

lex和yacc的使用很简单,但环境配置却是各种问题,本章说明lex和yacc在windows下的环境配置。 软件需求: 系统 win7-64位(win7-32, win8, win10全部通过) c编译器: vs2010(2008,2013,2015也全部通过) lex和yacc编译器&#xff1a…

Java集合之Vector源码分析

概述 Vector与ArrayLIst类似, 内部同样维护一个数组, Vector是线程安全的. 方法与ArrayList大体一致, 只是加上 synchronized 关键字, 保证线程安全, 下面就不具体分析源码了, 具体可以查看ArrayList中的源码分析. Vector源码分析 1.主要字段 2.构造函数 3.增删改查 其他方法…

Gossip协议的P2P会员管理

阅读此论文主要目的在于理解gossip协议及其背后的原理,此部分详细翻译,其余部分看时间 文章标题:Gossip协议的P2P会员管理 作者:Ayalvadi J. Ganesh, Anne-Marie Kermarrec, and Laurent Massoulie Abstract:基于…

Java集合之LinkedHashSet源码分析

概述 LinkedHashSet与HashSet类似, 不同的是LinkedHashSet底层使用LinkedHashMap维护元素插入的顺序. LinkedHashSet继承自HashSet, 只是重写了HashSet的构造方法, 初始化一个LinkedHashMap, 其他均与HashSet相同. LinkedHashSet构造方法 HashSet的构造方法: 以上几乎就是Li…

2016-2017NBU期末考试记录

又是一年期末考 这个学期考的少,就两门 还是来记录一下都考了什么东西吧 首先编译:编译的题目是开始十道判断题,后面全都是大题。大题内容有:画出第二次递归过程中,活动记录中静态链和动态链的情况;给出一…

Java集合之ArrayList源码分析

概述 ArrayList可以理解为动态数组, 根据MSDN的说法, 就是Array的复杂版本. 与数组相比, 它的容量能动态增长. ArrayList是List接口的可变数组的实现. 实现了所有可选列表操作, 允许包括null在内的所有元素. 数组的特点, 查询快增删慢. 每个ArrayList实例都有一个容量, 该容…

视频业务原理

最近要学习如何进行视频的用户体验测量,首先学习最基础的知识,视频业务的原理是什么。 研究的视频的应用层协议是HTTP,使用的传输层协议是TCP。 工作过程如下:客户端向服务器请求相应的视频信息;服务器响应请求发回视…

Java集合之Hashtable源码分析

概述 Hashtable也是基于哈希表实现的, 与map相似, 不过Hashtable是线程安全的, Hashtable不允许 key或value为null. 成员变量 Hashtable的数据结构和HashMap一样, 采用 数组加链表的方式实现. 几个成员变量与HashMap一样: 方法 Hashtable的方法与HashMap基本一样, 只是 Ha…

视频质量检测中的TP、FP、Reacll、Precision

在看论文《Measuring Vedio QoE from Encrypted Traffic》的时候看到TP(True Positives)、FP(False Positives)、Precison、Recall的概念,这属于数据挖掘方面的内容,学习之后特来记录。 首先,下…