1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 Emmanuelle Laprise
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca
19  * Derived from the p2p net device file
20 Transmi */
21 
22 #ifndef BACKOFF_H
23 #define BACKOFF_H
24 
25 #include <stdint.h>
26 #include "ns3/nstime.h"
27 #include "ns3/random-variable-stream.h"
28 
29 namespace ns3 {
30 
31 /**
32  * \ingroup csma
33  * \brief The backoff class is used for calculating backoff times
34  * when many net devices can write to the same channel
35  */
36 
37 class Backoff {
38 public:
39   /**
40    * Minimum number of backoff slots (when multiplied by m_slotTime, determines minimum backoff time)
41    */
42   uint32_t m_minSlots;
43 
44   /**
45    * Maximum number of backoff slots (when multiplied by m_slotTime, determines maximum backoff time)
46    */
47   uint32_t m_maxSlots;
48 
49   /**
50    * Caps the exponential function when the number of retries reaches m_ceiling.
51    */
52   uint32_t m_ceiling;
53 
54   /**
55    * Maximum number of transmission retries before the packet is dropped.
56    */
57   uint32_t m_maxRetries;
58 
59   /**
60    * Length of one slot. A slot time, it usually the packet transmission time, if the packet size is fixed.
61    */
62   Time m_slotTime;
63 
64   Backoff (void);
65   /**
66    * \brief Constructor
67    * \param slotTime Length of one slot
68    * \param minSlots Minimum number of backoff slots
69    * \param maxSlots Maximum number of backoff slots
70    * \param ceiling Cap to the exponential function
71    * \param maxRetries Maximum number of transmission retries
72    */
73   Backoff (Time slotTime, uint32_t minSlots, uint32_t maxSlots, uint32_t ceiling, uint32_t maxRetries);
74 
75   /**
76    * \return The amount of time that the net device should wait before
77    * trying to retransmit the packet
78    */
79   Time GetBackoffTime ();
80 
81   /**
82    * Indicates to the backoff object that the last packet was
83    * successfully transmitted and that the number of retries should be
84    * reset to 0.
85    */
86   void ResetBackoffTime (void);
87 
88   /**
89    * \return True if the maximum number of retries has been reached
90    */
91   bool MaxRetriesReached (void);
92 
93   /**
94    * Increments the number of retries by 1.
95    */
96   void IncrNumRetries (void);
97 
98  /**
99   * Assign a fixed random variable stream number to the random variables
100   * used by this model.  Return the number of streams (possibly zero) that
101   * have been assigned.
102   *
103   * \param stream first stream index to use
104   * \return the number of stream indices assigned by this model
105   */
106   int64_t AssignStreams (int64_t stream);
107 
108 private:
109 
110   /**
111    * Number of times that the transmitter has tried to unsuccessfully transmit the current packet.
112    */
113   uint32_t m_numBackoffRetries;
114 
115   /**
116    * Random number generator
117    */
118   Ptr<UniformRandomVariable> m_rng;
119 };
120 
121 } // namespace ns3
122 
123 #endif /* BACKOFF_H */
124