Schedule和context
- 正在执行的事件node id 由Simulator类追踪并存储于event的’context’(32 bit integer)
- ScheduleWithContext主要用于将接收事件和接收节点的id关联起来,而非关联于发送节点
- NodeList类:当node被建立时,NodeList使用ScheduleWithContext为其schedule一个initialize事件,调用Node::Initialize方法(该方法通过调用DoInitialize将事件传给于该node关联的所有对象)
- context id可用于NS3多线程的扩展(就是说当前是没有多线程的??)
- NS3中,网络模块将context当成是产生事件的node的id
Scheduler
@(NS3相关)[core]
定义的结构
struct EventKey {
uint64_t m_ts; /**< Event time stamp. */
uint32_t m_uid; /**< Event unique id. */
uint32_t m_context; /**< Event context. */
};
struct Event {
EventImpl *impl; /**< Pointer to the event implementation. */
EventKey key; /**< Key for sorting and ordering Events. */
};
类定义
class Scheduler : public Object
{
public:
virtual ~Scheduler () = 0;
virtual void Insert (const Event &ev) = 0;
virtual bool IsEmpty (void) const = 0;
virtual Event PeekNext (void) const = 0;
//什么时候调用呢?
virtual Event RemoveNext (void) = 0;
virtual void Remove (const Event &ev) = 0;
};
MapScheduler
整体说明
Mapscheduler将事件按时间顺序处理,实现方式:定义一个map,键值为EventKey,利用顺序容器自动将键值排序的特性(按键值按’<’排序)实现该目的。键值小的放在靠近begin()的位置,所以只需要每次取出mapbegin的事件就可以了。
类型定义
EventMap的<索引,值>和Event结构对应,EventMap是用来存储的结构,Event则是insert函数的形参
private:
typedef std::map<Scheduler::EventKey, EventImpl*> EventMap;
typedef std::map<Scheduler::EventKey, EventImpl*>::iterator EventMapI;
typedef std::map<Scheduler::EventKey, EventImpl*>::const_iterator EventMapCI;
数据成员
EventMap m_list;
EventKey的比较操作
- event的比较则是通过比较event的eventkey实现的
- 每个事件的uid的确定:
SimulatorImp的成员m_uid初始值为4。此后在每一次schedule的时候将其赋给event的key的m_uid,然后自增1,实现了每个事件m_uid的唯一性。
operator < (EventKey &a, EventKey &b){
if (a.m_ts < b.m_ts)
return true;
else if (a.m_ts == b.m_ts&& a.m_uid < b.m_uid)
return true;
else return false;
}
operator != (EventKey &a,EventKey &b){
return a.m_uid != b.m_uid;
}
operator > (EventKey &a, EventKey &b){
if (a.m_ts > b.m_ts)
return true;
else if (a.m_ts == b.m_ts && a.m_uid > b.m_uid)
return true;
else return false;
}
其他定义
class MapScheduler : public Scheduler
{
public:
static TypeId GetTypeId (void);
MapScheduler ();
virtual ~MapScheduler ();
virtual void Insert (const Event &ev);//ev的key和imp作为一个pair存进m_list中
virtual bool IsEmpty (void) const;
virtual Scheduler::Event
MapScheduler::PeekNext (void) const{
EventMapCI i = m_list.begin ();
NS_ASSERT (i != m_list.end ());
Event ev;
ev.impl = i->second;
ev.key = i->first;
return ev;
}
virtual Event RemoveNext (void);
virtual void Remove (const Event &ev);
}