ns-3移动模块
ns-3所支持的移动模块:
1.提供一系列移动模型供用户选择。
2.可以为移动模型提供Trace Source,供用户追踪节点的动态。
3.提供了大量的Helper类以便安排节点的初始分布和节点的移动方式(包括节点移动方向、速度)
1.移动模型简述:ns-3中为移动节点设计的网络模型都是基于坐标制的,在仿真中移动模型会集成到可移动的节点中,可使用GetObject()函数从移动模型的节点中提取移动模型,ns-3中提供的大量不同的移动模型供不同用户使用,都继承自ns3::MobilityModel类。另外,移动节点初始位置的分布是由类PositionAllocator负责,每个节点的初始位置在仿真进行前就由该类指定完毕。
ns-3提供Helper类,有助于用户方便地使用ns-3提供的服务,同样的,移动模型也有与之对应的Helper类-MobileHelper,该类把移动模型和位置分配整合到一起,从而方便为Node节点安装移动模型。
ns-3提供了类ns-3::Vector作为坐标系统的基类,针对位置坐标用(x,y,z),针对速度用矢量坐标的(x,y,z),另外,ns-3还提供了Rectangle、Box、Waypoint 作为补充。
2.MobilityModel类:为所有移动模型的基类,
class MobilityModel : public Object
{
public:
/**
* Register this type with the TypeId system.
* \return the object TypeId
*/
static TypeId GetTypeId (void);
MobilityModel ();
virtual ~MobilityModel () = 0;
/**
返回当前节点位置
*/
Vector GetPosition (void) const;
/**
设置节点的位置
*/
void SetPosition (const Vector &position);
/**
返回当前节点的移动速度
*/
Vector GetVelocity (void) const;
/**
参数position为另一个节点的位置
返回该节点和另一个节点间的距离
*/
double GetDistanceFrom (Ptr<const MobilityModel> position) const;
/**
* 参数other为另一个节点的模型
* 返回两个节点的相对移动速度
*/
double GetRelativeSpeed (Ptr<const MobilityModel> other) const;
/**
*在一些模型中可能用到随机数,比如随机位置,随机移动速度等,因此提供这么一个函数为移动模型提供随机数
*/
int64_t AssignStreams (int64_t stream);
/**
* TracedCallback signature.
*
* \param [in] model Value of the MobilityModel.
*/
typedef void (* TracedCallback)(Ptr<const MobilityModel> model);
protected:
/**
* Must be invoked by subclasses when the course of the
* position changes to notify course change listeners.
*/
void NotifyCourseChange (void) const;
private:
/**
* \return the current position.
*
* Concrete subclasses of this base class must
* implement this method.
*/
virtual Vector DoGetPosition (void) const = 0;
/**
* \param position the position to set.
*
* Concrete subclasses of this base class must
* implement this method.
*/
virtual void DoSetPosition (const Vector &position) = 0;
/**
* \return the current velocity.
*
* Concrete subclasses of this base class must
* implement this method.
*/
virtual Vector DoGetVelocity (void) const = 0;
/**
* The default implementation does nothing but return the passed-in
* parameter. Subclasses using random variables are expected to
* override this.
* \param start starting stream index
* \return the number of streams used
*/
virtual int64_t DoAssignStreams (int64_t start);
/**
* Used to alert subscribers that a change in direction, velocity,
* or position has occurred.向用户报警,方向、速度、位置发生变化。
*/
ns3::TracedCallback<Ptr<const MobilityModel> > m_courseChangeTrace;
};
3.各类移动模型:ns-3提供多种移动模型供用户选择使用,主要有:ConstantPosition(固定位置模型)、ConstantVelocity(固定移动速度)、RandomWayPoint(随机路径)、RandomWalk2D(随机游走)、RandomDirection2D(随机方向)、Waypoint(普通路径)、ConstantAcceleration(固定加速度)、SteadyStateRandomWayPoint(稳态随机路径)、GaussMarkov(高斯马尔可夫随机过程)、Hierarchical(分层)。
third.cc中示例代码:分析:首先使用MobilityHelper类创建对象mobility,然后通过调用SetPositionAllocator()函数初始化节点的初始位置,其函数原型为:
void SetPositionAllocator (std::string type,
std::string n1 = "", const AttributeValue &v1 = EmptyAttributeValue (),
std::string n2 = "", const AttributeValue &v2 = EmptyAttributeValue (),
std::string n3 = "", const AttributeValue &v3 = EmptyAttributeValue (),
std::string n4 = "", const AttributeValue &v4 = EmptyAttributeValue (),
std::string n5 = "", const AttributeValue &v5 = EmptyAttributeValue (),
std::string n6 = "", const AttributeValue &v6 = EmptyAttributeValue (),
std::string n7 = "", const AttributeValue &v7 = EmptyAttributeValue (),
std::string n8 = "", const AttributeValue &v8 = EmptyAttributeValue (),
std::string n9 = "", const AttributeValue &v9 = EmptyAttributeValue ());
通过调用SetPositionAllocator函数,将实参传递给形参(没有必要必须传递9个,小于其就行)。在准备好所需要的配置工作后,调用Install()函数将这些准备好的移动模型安装到每一个节点上。