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 
33  : m_lost (0),
34  m_bitMapSize (0),
35  m_lastMaxSeqNum (0),
36  m_receiveBitMap (0)
37 {
38  SetBitMapSize (bitmapSize);
39 }
40 
42 {
43  delete [] m_receiveBitMap;
44 }
45 
46 uint16_t
48 {
49  return m_bitMapSize * 8;
50 }
51 
52 void
54 {
55 
56  NS_ASSERT_MSG (winSize%8==0,"The packet window size should be a multiple of 8");
57  m_bitMapSize = winSize/8;
58  if (m_receiveBitMap!=0)
59  {
60  delete [] m_receiveBitMap;
61  }
62  m_receiveBitMap = new uint8_t [m_bitMapSize] ();
63  memset (m_receiveBitMap,0xFF,m_bitMapSize);
64 }
65 
66 uint32_t
68 {
69  return m_lost;
70 }
71 
72 bool
73 PacketLossCounter::GetBit (uint32_t seqNum)
74 {
75  return ((m_receiveBitMap[(seqNum%(m_bitMapSize*8))/8] >> (7-(seqNum%8)))&0x01);
76 }
77 
78 void
79 PacketLossCounter::SetBit (uint32_t seqNum, bool val)
80 {
81  if (val)
82  {
83  m_receiveBitMap[(seqNum%(m_bitMapSize*8))/8] |= 0x80 >> (seqNum%8);
84  }
85  else
86  {
87  m_receiveBitMap[(seqNum%(m_bitMapSize*8))/8] &= ~(0x80 >> (seqNum%8));
88  }
89 }
90 
91 /*
92  * This algo works as follows:
93  * When a packet is received:
94  * 1) From the last received packet to the current one:
95  * 1.1) check the corresponding bit in the bitMAP.
96  * This bit indicates if the packet with (SeqNum-bitMapSizeInBit) is
97  * received (1) or not (0)
98  * 1.2) Mark the packet as lost (0) in the bitMap
99  * 2) Mark the current packet as received (1) in the bitMap
100  * 3) Update the value of the last received packet
101  */
102 
103 void
105 {
106  for (uint32_t i=m_lastMaxSeqNum+1; i<=seqNum; i++)
107  {
108  if (GetBit (i)!=1)
109  {
110  NS_LOG_INFO ("Packet lost: " << i-(m_bitMapSize*8));
111  m_lost++;
112  }
113  SetBit (i, 0);
114  }
115  SetBit (seqNum, 1);
116  if (seqNum>m_lastMaxSeqNum)
117  {
118  m_lastMaxSeqNum = seqNum;
119  }
120 }
121 }