(三)SDN 控制器 NOX 源碼分析之—— Event_dispatcher

event-dispatcher.hh
typedef boost::function<void(const boost::system::error_code& error)> Callback
回調函數的類型定義
static Component* instantiate(const Component_context*, size_t n_threads);
生成一個Event-dispatcher對象
void post(const Event&);
立刻發送一個事件
std::unique_ptr<boost::asio::deadline_timer>
    post(const Callback& callback, const timeval& duration);
定義一個帶有時間控制的消息/* Posts 'callback' to be called after the given 'duration' elapses. The
     * caller must not destroy the returned Timer, but may use it to cancel or
     * reschedule the timer, up until the point where the timer is actually
     * invoked. */
void dispatch(const Event&) const
立刻發送一個消息出去
boost::asio::io_service& get_io_service()
得到事件發送的io_service
    template <typename T>
    inline
    void register_event()
註冊事件,使用方法爲:
register_event<Event類>();
bool register_handler(const Component_name&,
const Event_name&,
const Event_handler&);
爲事件註冊句柄,帶組件名
void register_handler(const Event_name&,
const Event_handler&, int order);
爲事件註冊句柄,同一個事件有多個句柄的情況下使用這個語句,可以爲句柄定義優先級,句柄按順序先後依次執行
event-dispatcher.cc
1.bool
Event_dispatcher::register_handler(
const Component_name& component_name,
const Event_name& event_name,
const Event_handler& h);
首先查看的是註冊的事件有沒有存在,不存在返回false;接下來根據組件在對應事件中的優先級,把句柄按優先級插入到call_chain_map;

2.Event_dispatcher::dispatch(const Event& event) const
的實現是查找call_chain_map,找到事件對應的句柄,依次執行,直到執行完所有句柄或遇到句柄返回STOP

3.register_event(const Event_name& name)是爲新的事件插入到priority_map中;

4.priority_map保存着響應事件時,應組件的執行優先級;其數據結構是兩級字典:【事件名【組件名,優先級】】;通過讀取註冊文件(etc/nox.meta.json),根據註冊文件中組件的順序,給組件設置優先級;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章