A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
packet-loss-counter.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 INRIA, UDCAST
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: Amine Ismail <amine.ismail@sophia.inria.fr>
19  * <amine.ismail@udcast.com>
20  */
21 
22 #include "ns3/log.h"
23 #include "ns3/simulator.h"
24 #include "ns3/uinteger.h"
25 #include "packet-loss-counter.h"
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("PacketLossCounter")
30  ;
31 
32 
34  : m_lost (0),
35  m_bitMapSize (0),
36  m_lastMaxSeqNum (0),
37  m_receiveBitMap (0)
38 {
39  NS_LOG_FUNCTION (this << bitmapSize);
40  SetBitMapSize (bitmapSize);
41 }
42 
44 {
45  NS_LOG_FUNCTION (this);
46  delete [] m_receiveBitMap;
47 }
48 
49 uint16_t
51 {
52  NS_LOG_FUNCTION (this);
53  return m_bitMapSize * 8;
54 }
55 
56 void
58 {
59  NS_LOG_FUNCTION (this << winSize);
60 
61  NS_ASSERT_MSG (winSize%8==0,"The packet window size should be a multiple of 8");
62  m_bitMapSize = winSize/8;
63  if (m_receiveBitMap!=0)
64  {
65  delete [] m_receiveBitMap;
66  }
67  m_receiveBitMap = new uint8_t [m_bitMapSize] ();
68  memset (m_receiveBitMap,0xFF,m_bitMapSize);
69 }
70 
71 uint32_t
73 {
74  NS_LOG_FUNCTION (this);
75  return m_lost;
76 }
77 
78 bool
79 PacketLossCounter::GetBit (uint32_t seqNum)
80 {
81  NS_LOG_FUNCTION (this << seqNum);
82  return ((m_receiveBitMap[(seqNum%(m_bitMapSize*8))/8] >> (7-(seqNum%8)))&0x01);
83 }
84 
85 void
86 PacketLossCounter::SetBit (uint32_t seqNum, bool val)
87 {
88  NS_LOG_FUNCTION (this << seqNum << val);
89  if (val)
90  {
91  m_receiveBitMap[(seqNum%(m_bitMapSize*8))/8] |= 0x80 >> (seqNum%8);
92  }
93  else
94  {
95  m_receiveBitMap[(seqNum%(m_bitMapSize*8))/8] &= ~(0x80 >> (seqNum%8));
96  }
97 }
98 
99 /*
100  * This algo works as follows:
101  * When a packet is received:
102  * 1) From the last received packet to the current one:
103  * 1.1) check the corresponding bit in the bitMAP.
104  * This bit indicates if the packet with (SeqNum-bitMapSizeInBit) is
105  * received (1) or not (0)
106  * 1.2) Mark the packet as lost (0) in the bitMap
107  * 2) Mark the current packet as received (1) in the bitMap
108  * 3) Update the value of the last received packet
109  */
110 
111 void
113 {
114  NS_LOG_FUNCTION (this << seqNum);
115  for (uint32_t i=m_lastMaxSeqNum+1; i<=seqNum; i++)
116  {
117  if (GetBit (i)!=1)
118  {
119  NS_LOG_INFO ("Packet lost: " << i-(m_bitMapSize*8));
120  m_lost++;
121  }
122  SetBit (i, 0);
123  }
124  SetBit (seqNum, 1);
125  if (seqNum>m_lastMaxSeqNum)
126  {
127  m_lastMaxSeqNum = seqNum;
128  }
129 }
130 
131 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
void NotifyReceived(uint32_t seq)
Record a successfully received packet.
uint32_t m_lastMaxSeqNum
Last sequence number seen.
#define NS_LOG_INFO(msg)
Definition: log.h:298
bool GetBit(uint32_t seqNum)
Check if a sequence number in the window has been received.
uint32_t GetLost(void) const
Get the number of lost packets.
void SetBitMapSize(uint16_t size)
Set the size of the window used to compute the packet loss.
uint32_t m_lost
Lost packets counter.
void SetBit(uint32_t seqNum, bool val)
Set a sequence number to a given state.
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
uint8_t * m_receiveBitMap
Received packets in the window size.
uint16_t m_bitMapSize
Window size.
PacketLossCounter(uint8_t bitmapSize)
Constructor.
uint16_t GetBitMapSize(void) const
Return the size of the window used to compute the packet loss.