00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2007, Emmanuelle Laprise 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca> 00019 */ 00020 00021 #include "backoff.h" 00022 00023 namespace ns3 { 00024 00025 Backoff::Backoff () 00026 { 00027 m_slotTime = MicroSeconds(1); 00028 m_minSlots = 1; 00029 m_maxSlots = 1000; 00030 m_ceiling = 10; 00031 m_maxRetries = 1000; 00032 00033 ResetBackoffTime(); 00034 } 00035 00036 Backoff::Backoff(Time slotTime, uint32_t minSlots, uint32_t maxSlots, uint32_t ceiling, uint32_t maxRetries) 00037 { 00038 m_slotTime = slotTime; 00039 m_minSlots = minSlots; 00040 m_maxSlots = maxSlots; 00041 m_ceiling = ceiling; 00042 m_maxRetries = maxRetries; 00043 } 00044 00045 Time 00046 Backoff::GetBackoffTime (void) 00047 { 00048 Time backoff; 00049 uint32_t ceiling; 00050 00051 if ((m_ceiling > 0) &&(m_numBackoffRetries > m_ceiling)) 00052 { 00053 ceiling = m_ceiling; 00054 } 00055 else 00056 { 00057 ceiling = m_numBackoffRetries; 00058 } 00059 00060 uint32_t minSlot = m_minSlots; 00061 uint32_t maxSlot = (uint32_t)pow (2, ceiling) - 1; 00062 if (maxSlot > m_maxSlots) 00063 { 00064 maxSlot = m_maxSlots; 00065 } 00066 00067 uint32_t backoffSlots = (uint32_t)UniformVariable::GetSingleValue(minSlot, maxSlot); 00068 00069 backoff = Scalar(backoffSlots) * m_slotTime; 00070 return (backoff); 00071 } 00072 00073 void 00074 Backoff::ResetBackoffTime (void) 00075 { 00076 m_numBackoffRetries = 0; 00077 } 00078 00079 bool 00080 Backoff::MaxRetriesReached (void) 00081 { 00082 return (m_numBackoffRetries >= m_maxRetries); 00083 } 00084 00085 void 00086 Backoff::IncrNumRetries (void) 00087 { 00088 m_numBackoffRetries++; 00089 } 00090 00091 } // namespace ns3