对HOWTO_create_a_traffic_generator的更正
来源:互联网 发布:华为网络 编辑:程序博客网 时间:2024/06/07 06:44
原文来自:https://www.nsnam.org/wiki/HOWTO_create_a_traffic_generator
我的更正部分如下:(红色标记)
It is often desirable to create a new traffic generator for your simulation purposes. Since it is possible to instantiate Packet objects, schedule events, and call functions from any piece of code in ns-3, technically, there is no single answer to the question of how we can write a new traffic generator. However, the Socket API was really designed to be the single point of entry for traffic generators or traffic analyzers and the Application class was designed to hold together any number of sockets.
HOWTO create a traffic generator
Implementing a new traffic generator thus boils down to:
* implementing a new subclass of the Application base class * instantiate one or many sockets within that application * start scheduling events when StartApplication is called * stop scheduling events when StopApplication is called * create packets and send them over one or many sockets in each event
The following "random" generator generates packets separated by a random delay and with a random size.
class RandomGenerator : public Application { public: RandomGenerator (); void SetDelay (RandomVariable delay); void SetSize (RandomVariable size); void SetRemote (std::string socketType, Address remote); private: virtual void StartApplication (void); virtual void StopApplication (void); void DoGenerate (void); RandomVariable m_delay; RandomVariable m_size; Ptr<Socket> m_socket; };
The socket is created in the SetRemote method:
void RandomGenerator::SetRemote (std::string socketType, Address remote) { TypeId tid = TypeId::LookupByName (socketType); m_socket = Socket::CreateSocket (GetNode (), tid); m_socket->Bind (); m_socket->ShutdownRecv (); m_socket->Connect (remote); }
While the the crux of the logic is located in the DoGenerate method which is called from within StartApplication:
void RandomGenerator::DoGenerate (void) { m_next = Simulator::Schedule (Seconds (m_delay.GetValue ()), &RandomGenerator::DoGenerate, this); Ptr<Packet> p = Create<Packet> (m_size.GetIntValue ()); m_socket->Send (p); }
To make that application more integrated in ns-3, it needs an associated helper class:
class RandomAppHelper { public: RandomAppHelper (std::string protocol, Address remote); void SetPacketSize (RandomVariable packetSize); void SetDelay (RandomVariable delay); ApplicationContainer Install (NodeContainer nodes); private: std::string m_protocol; Address m_remote; RandomVariable m_packetSize; RandomVariable m_delay; };
which could be trivially implemented as:
ApplicationContainer RandomAppHelper::Install (NodeContainer nodes) { ApplicationContainer applications; for (NodeContainer::Iterator i = nodes.Begin (); i != nodes.End (); ++i) { Ptr<RandomGenerator> app = CreateObject<RandomGenerator> (); app->SetSize (m_packetSize); app->SetDelay (m_delay); app->SetRemote (m_protocol, m_remote); (*i)->AddApplication (app); applications.Add (app); } return applications; }
Despite being functional, this API is not very consistant with the style of the other helper classes, all of which allow you to control the parameters of the underlying class through attributes and not explicit setters. The following API thus replaces the pair SetPacketSize/SetDelay with a single method SetAttribute:
class RandomAppHelper { public: RandomAppHelper (std::string protocol, Address remote); void SetAttribute (std::string name, const AttributeValue &value); ApplicationContainer Install (NodeContainer c); private: std::string m_protocol; Address m_remote; ObjectFactory m_factory; };
And which can be used as follows:
RandomAppHelper app = RandomAppHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address ("192.168.1.10"), 10)); app.SetAttribute ("Delay", StringValue ("Constant:2.5")); app.SetAttribute ("Size", StringValue ("Constant:2100")); app.Install (nodes);
The implementation, in this case, is not necessarily longer but its simplicity hides a lot of behind-the-scenes complexity:
void RandomAppHelper::SetAttribute (std::string name, const AttributeValue &value) { m_factory.Set (name, value); } ApplicationContainer RandomAppHelper::Install (NodeContainer nodes) { ApplicationContainer applications; for (NodeContainer::Iterator i = nodes.Begin (); i != nodes.End (); ++i) { Ptr<RandomGenerator> app = m_factory.Create<RandomGenerator> (); app->SetRemote (m_socketType, m_remote); (*i)->AddApplication (app); applications.Add (app); } return applications; }
The key difference between this implementation and the previous one is that this helper does not handle explicitely the attributes delay and packet size. Instead, it stores them in an ObjectFactory object. This, of course, does not work magically, and requires extra support from the underlying RandomGenerator class. Specifically, it requires that the underlying RandomGenerator defines its attributes in its TypeId in a new public static method:
class RandomGenerator { public: static TypeId GetTypeId (void); };
The corresponding implementation is shown below:
TypeId RandomGenerator::GetTypeId (void) { static TypeId tid = TypeId ("RandomGenerator") .SetParent<Application> () .AddConstructor<RandomGenerator> () .AddAttribute ("Delay", "The delay between two packets (s)", RandomVariableValue (ConstantVariable (1.0)), MakeRandomVariableAccessor (&RandomGenerator::m_delay), MakeRandomVariableChecker ()) .AddAttribute ("PacketSize", "The size of each packet (bytes)", RandomVariableValue (ConstantVariable (2000)), MakeRandomVariableAccessor (&RandomGenerator::m_size), MakeRandomVariableChecker ()) ; return tid; }
- 对HOWTO_create_a_traffic_generator的更正
- 对遍历二叉树的更正
- 对binary-search-tree的tree-delete算法的更正
- 更正!
- 错帐的更正方法
- 【码上杂谈】关于之前对Server上允许的最大TCP连接数理解错误的更正
- 【jiasuba】教你快速对异常错误进行更正
- 【7gyy】教你快速对异常错误进行更正
- 猴子吃桃子【对Java试题库1更正】
- asp.net2.0 最新的一些更正
- 银行业的长期估值,预测更正
- char 与 wchar_t的转换,欢迎更正
- 更正一个上课时讲的BUG
- vim 自动更正输入错误的命令
- RN中关于 popToRoute的用法更正
- cifar分类可能遇到的错误更正
- 无聊的测试-更正 慧通 某个参考答案 的说法
- Python3中重新加载一个新的更正的代码
- 服务计算基础知识 UDDI SOAP WSDL特性 SOA 设计原则
- leetCode(18):Construct Binary Tree from Preorder and Inorder (Inorder and Postorder) Traversal
- 写给程序员的一封信
- IOS 开发 Objective-C 入门1
- [C++ primer]第二章笔记--变量和基本类型
- 对HOWTO_create_a_traffic_generator的更正
- 搜索帮助中根据界面条件显示实现方法一
- Java "object reference" demo
- leetCode(19):Count Complete Tree Nodes
- WCF医院管理系统技术解析(十二)化验登记
- POJ 1477 && HDU 1326 Box of Bricks(水~)
- Afaria 7 SP5中的SSP URL
- Java--Reflect(反射)专题4——获取成员变量以及构造函数信息
- Extract ISBN From PDF and EPUB and Query Matadata From ISBN Database