rtqueue.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:3k
源码类别:

通讯编程

开发平台:

Visual C++

  1. #include <assert.h>
  2. #include <rtqueue.h>
  3. #include <cmu-trace.h>
  4. #define CURRENT_TIME    Scheduler::instance().clock()
  5. #define DEBUG
  6. /* ======================================================================
  7.    Packet Queue used by TORA and AODV.
  8.    ===================================================================== */
  9. rtqueue::rtqueue()
  10. {
  11.         head_ = tail_ = 0;
  12.         len_ = 0;
  13.         limit_ = RTQ_MAX_LEN;
  14.         timeout_ = RTQ_TIMEOUT;
  15. }
  16. void
  17. rtqueue::enque(Packet *p)
  18. {
  19.         struct hdr_cmn *ch = HDR_CMN(p);
  20.  
  21. p->next_ = 0;
  22.         ch->ts_ = CURRENT_TIME + timeout_;
  23.         if (len_ == limit_) {
  24. Packet *p0 = remove_head(); // decrements len_
  25. assert(p0);
  26.                 if(HDR_CMN(p0)->ts_ > CURRENT_TIME) {
  27.         drop(p0, DROP_RTR_QFULL);
  28.                 }
  29.                 else {
  30.                         drop(p0, DROP_RTR_QTIMEOUT);
  31.                 }
  32.         }
  33.         if(head_ == 0) {
  34.                 head_ = tail_ = p;
  35.         }
  36.         else {
  37.                 tail_->next_ = p;
  38.                 tail_ = p;
  39.         }
  40.         len_++;
  41. #ifdef DEBUG
  42. verifyQueue();
  43. #endif
  44. }
  45.                 
  46. Packet*
  47. rtqueue::deque()
  48. {
  49. Packet *p;
  50.         /*
  51.          * Purge any packets that have timed out.
  52.          */
  53.         purge();
  54. p = remove_head();
  55. #ifdef DEBUG
  56. verifyQueue();
  57. #endif
  58. return p;
  59. }
  60. Packet*
  61. rtqueue::deque(nsaddr_t dst)
  62. {
  63.         Packet *p, *prev;
  64.         /*
  65.          * Purge any packets that have timed out.
  66.          */
  67.         purge();
  68. findPacketWithDst(dst, p, prev);
  69. assert(p == 0 || (p == head_ && prev == 0) || (prev->next_ == p));
  70. if(p == 0) return 0;
  71.         if (p == head_) {
  72. p = remove_head();
  73.         }
  74.         else if (p == tail_) {
  75.                 prev->next_ = 0;
  76.                 tail_ = prev;
  77.                 len_--;
  78.         }
  79.         else {
  80.                 prev->next_ = p->next_;
  81.                 len_--;
  82.         }
  83. #ifdef DEBUG
  84. verifyQueue();
  85. #endif
  86. return p;
  87. }
  88. char 
  89. rtqueue::find(nsaddr_t dst)
  90. {
  91.         Packet *p, *prev;  
  92. findPacketWithDst(dst, p, prev);
  93. if (0 == p)
  94.        return 0;
  95. else
  96.        return 1;
  97. }
  98. /* ======================================================================
  99.    Private Routines
  100.    ====================================================================== */
  101. Packet*
  102. rtqueue::remove_head()
  103. {
  104.         Packet *p = head_;
  105.         
  106.         if(head_ == tail_) {
  107.                 head_ = tail_ = 0;
  108.         }
  109.         else {
  110.                 head_ = head_->next_;
  111.         }
  112.         if(p) len_--;
  113.         return p;
  114. }
  115. void
  116. rtqueue::purge()
  117. {
  118.         Packet *p;
  119.         while((p = head_) && HDR_CMN(p)->ts_ < CURRENT_TIME) {
  120.                 Packet *temp;
  121. temp = remove_head();     
  122.                 assert(p == temp);     
  123.                 drop(p, DROP_RTR_QTIMEOUT);
  124.         }
  125. }
  126. void
  127. rtqueue::findPacketWithDst(nsaddr_t dst, Packet*& p, Packet*& prev)
  128. {
  129. p = prev = 0;
  130. for(p = head_; p; p = p->next_) {
  131.   // if(HDR_IP(p)->dst() == dst) {
  132.        if(HDR_IP(p)->daddr() == dst) {
  133. return;
  134. }
  135. prev = p;
  136. }
  137. if(p == 0) prev = 0; // not found
  138. }
  139. void
  140. rtqueue::verifyQueue()
  141. {
  142. Packet *p, *prev = 0;
  143. int cnt = 0;
  144. for(p = head_; p; p = p->next_) {
  145. cnt++;
  146. prev = p;
  147. }
  148. assert(cnt == len_);
  149. assert(prev == tail_);
  150. }