NS2之移动节点邻居节点发现

  1. //You can use the following methods to discover neighbors:  
  2. //1. Use Hello Packets for *neighbor* discovery.  
  3. //2. Use Communication range for neighbour discovery.  
  4. //3. Use Nodes Positions for *neighbor* discovery.  
  5.   
  6. void Node::addNeighbor(Node * neighbor) {  
  7.   
  8. int flag=0;  
  9. neighbor_list_node* my_neighbor_list;  
  10. my_neighbor_list=neighbor_  
  11. list_;  
  12. //cout <<"MMMMM"<<my_neighbor_list->  
  13. nodeid<<endl;  
  14. //return;  
  15.   
  16.    while(my_neighbor_list)  
  17.     {  
  18.         if(my_neighbor_list->nodeid == neighbor->nodeid())  
  19.         {  
  20.         flag = 1;  
  21.     break;  
  22.         }  
  23.         else  
  24.         {  
  25.         my_neighbor_list=my_neighbor_list->next;  
  26.         }  
  27.     }  
  28.   
  29.   
  30. //cout<<"MMM "<<my_neighbor_list->nodeid<<endl;  
  31.     if(flag == 1)  
  32.     {  
  33.        //neighbour already exist do nothing  
  34.     }  
  35.     else  
  36.     {  
  37.        neighbor_list_node* nlistItem = (neighbor_list_node  
  38. *)malloc(sizeof(neighbor_list_node));  
  39.     nlistItem->nodeid = neighbor->nodeid();  
  40.     nlistItem->next = neighbor_list_;  
  41.     neighbor_list_=nlistItem;  
  42.     }  
  43.   
  44.   
  45.   
  46. }  
  47.   
  48.   
  49. //After that you can easily get access to any node's neighbor list by creating  
  50. //its object and calling its neighbor list.  
  51.   
  52. Node* m_node = Node::get_node_by_address(this->addr());  
  53. neighbor_list_node* my_mobile_neighbor_list;  
  54. my_mobile_neighbor_list = m_node->neighbor_list_;  
  55.   
  56.                 while(my_mobile_neighbor_list)  
  57.                             {  
  58.                             cout<<"## Mubashir Neighbor ID:"<<my_mobile_neighbor_list->nodeid<<endl;  
  59.                 my_mobile_neighbor_list=my_mobile_neighbor_list->next;  
  60.                 }  
  61.   
  62. cout<<"node id : " <<put here node id<<" Mubashir Neighbor list contains ID:"<<my_mobile_neighbor_list-> nodeid<<endl;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值