ns2中aodv協議中路由鏈表的操作

aodv協議中對路由鏈表的操作部分大多在lib/bsd-list.h文件中

雙向鏈表

/*
 * List definitions.
 */

//創建指向鏈表第一個元素的指針
#define LIST_HEAD(name, type) \    
struct name { \
type *lh_first;/* first element */\
}

//創建匿名結構體 只包含該節點的前驅和後繼,注意:後繼爲一級指針;前驅爲二級指針

//後繼指向的是下一個節點,而前驅指向的是前一個節點的後繼,即它的前驅是一個指向指針的指針,畫圖會更清晰一些

#define LIST_ENTRY(type)\
struct { \
type *le_next;/* next element */\       
type **le_prev;/* address of previous next element */\
}


/*
 * List functions.
 */

//初始化路由鏈表
#define LIST_INIT(head) {\
(head)->lh_first = NULL;\
}

//在鏈表中某個節點的後面插入一個新的節點

//其中listelm爲鏈表中的某個節點,elm爲新加節點,field爲結構體,包含該節點的前驅和後繼(弄明白這一點後面就都不難理解了)
#define LIST_INSERT_AFTER(listelm, elm, field) { \
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)\
(listelm)->field.le_next->field.le_prev =\
   &(elm)->field.le_next;\
(listelm)->field.le_next = (elm);\
(elm)->field.le_prev = &(listelm)->field.le_next;\
}

//在鏈表中某個節點的前面插入一個新的節點

//其中listelm爲鏈表中的某個節點,elm爲新加節點,field爲結構體,包含該節點的前驅和後繼
#define LIST_INSERT_BEFORE(listelm, elm, field) { \
(elm)->field.le_prev = (listelm)->field.le_prev;\
(elm)->field.le_next = (listelm);\
*(listelm)->field.le_prev = (elm);\
(listelm)->field.le_prev = &(elm)->field.le_next;\
}

//頭節點處插入
#define LIST_INSERT_HEAD(head, elm, field) { \
if (((elm)->field.le_next = (head)->lh_first) != NULL)\
(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
(head)->lh_first = (elm);\
(elm)->field.le_prev = &(head)->lh_first;\
}

//刪除路由鏈表中某個節點,刪除失效路由
#define LIST_REMOVE(elm, field) { \
if ((elm)->field.le_next != NULL)\
(elm)->field.le_next->field.le_prev = \
   (elm)->field.le_prev;\
*(elm)->field.le_prev = (elm)->field.le_next;\
}


#endif /* !_NS_BSD_LIST_H_ */


aodv.rtable.h中對鄰接表類的定義

class AODV_Neighbor {
        friend class AODV;
        friend class aodv_rt_entry;
 public:
        AODV_Neighbor(u_int32_t a) { nb_addr = a; }


 protected:
        LIST_ENTRY(AODV_Neighbor) nb_link;
        nsaddr_t        nb_addr;
        double          nb_expire;      // ALLOWED_HELLO_LOSS * HELLO_INTERVAL
};

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