satellite-base-encapsulator.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Magister Solutions Ltd.
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: Jani Puttonen <jani.puttonen@magister.fi>
19  */
20 
22 
23 #include "satellite-mac-tag.h"
24 #include "satellite-queue.h"
25 #include "satellite-time-tag.h"
26 
27 #include <ns3/log.h>
28 #include <ns3/simulator.h>
29 
30 NS_LOG_COMPONENT_DEFINE("SatBaseEncapsulator");
31 
32 namespace ns3
33 {
34 
35 NS_OBJECT_ENSURE_REGISTERED(SatBaseEncapsulator);
36 
38  : m_encapAddress(),
39  m_decapAddress(),
40  m_flowId(0)
41 {
42  NS_LOG_FUNCTION(this);
43  NS_ASSERT(false);
44 
48 }
49 
50 SatBaseEncapsulator::SatBaseEncapsulator(Mac48Address encapAddress,
51  Mac48Address decapAddress,
52  Mac48Address sourceE2EAddress,
53  Mac48Address destE2EAddress,
54  uint8_t flowId,
55  uint32_t additionalHeaderSize)
56  : m_encapAddress(encapAddress),
57  m_decapAddress(decapAddress),
58  m_sourceE2EAddress(sourceE2EAddress),
59  m_destE2EAddress(destE2EAddress),
60  m_flowId(flowId),
61  m_additionalHeaderSize(additionalHeaderSize)
62 {
63  NS_LOG_FUNCTION(this);
64 }
65 
67 {
68  NS_LOG_FUNCTION(this);
69  m_txQueue = NULL;
70  m_rxCallback.Nullify();
71 }
72 
73 TypeId
75 {
76  static TypeId tid = TypeId("ns3::SatBaseEncapsulator").SetParent<Object>();
77  return tid;
78 }
79 
80 void
82 {
83  NS_LOG_FUNCTION(this);
84  if (m_txQueue)
85  {
86  m_txQueue->DoDispose();
87  }
88  m_rxCallback.Nullify();
89  m_ctrlCallback.Nullify();
90 }
91 
92 void
93 SatBaseEncapsulator::EnquePdu(Ptr<Packet> p, Mac48Address dest)
94 {
95  NS_LOG_FUNCTION(this << p->GetSize() << dest);
96 
97  // Add flow id tag
98  SatFlowIdTag flowIdTag;
99  if (!p->PeekPacketTag(flowIdTag))
100  {
101  flowIdTag.SetFlowId(m_flowId);
102  p->AddPacketTag(flowIdTag);
103  }
104 
105  // Add MAC tag to identify the packet in lower layers
106  SatMacTag mTag;
107  if (!p->PeekPacketTag(mTag))
108  {
109  mTag.SetDestAddress(dest);
111  p->AddPacketTag(mTag);
112  }
113 
114  NS_LOG_INFO("Tx Buffer: New packet added of size: " << p->GetSize());
115 
116  if (!m_txQueue->Enqueue(p))
117  {
118  NS_LOG_INFO("Packet is dropped!");
119  }
120 
121  NS_LOG_INFO("NumPackets = " << m_txQueue->GetNPackets());
122  NS_LOG_INFO("NumBytes = " << m_txQueue->GetNBytes());
123 }
124 
125 Ptr<Packet>
126 SatBaseEncapsulator::NotifyTxOpportunity(uint32_t bytes, uint32_t& bytesLeft, uint32_t& nextMinTxO)
127 {
128  NS_LOG_FUNCTION(this << bytes);
129  NS_LOG_INFO("TxOpportunity for flowId: " << (uint32_t)m_flowId << " of " << bytes << " bytes");
130 
131  Ptr<Packet> packet;
132 
133  // No packets in buffer
134  if (m_txQueue->IsEmpty())
135  {
136  NS_LOG_INFO("No data pending, return NULL packet");
137  return packet;
138  }
139 
140  // Peek the first PDU from the buffer.
141  Ptr<const Packet> peekPacket = m_txQueue->Peek();
142 
143  // Initialize with current packet size
144  nextMinTxO = peekPacket->GetSize();
145 
146  // If control packet fits into TxO
147  if (peekPacket->GetSize() <= bytes)
148  {
149  // Peek the first PDU from the buffer.
150  packet = m_txQueue->Dequeue();
151 
152  if (!packet)
153  {
154  NS_FATAL_ERROR(
155  "Packet not dequeued from txQueue even though the peek PDU should have been fit!");
156  }
157 
158  // Update bytes left
159  bytesLeft = m_txQueue->GetNBytes();
160 
161  // Update the next min TxO
162  Ptr<const Packet> nextPacket = m_txQueue->Peek();
163  if (nextPacket)
164  {
165  nextMinTxO = nextPacket->GetSize();
166  }
167  else
168  {
169  nextMinTxO = 0;
170  }
171  }
172 
173  return packet;
174 }
175 
176 void
178 {
179  NS_LOG_FUNCTION(this);
180 
181  if (m_flowId != 0)
182  {
183  NS_ASSERT(false);
184 
189  }
190 
192 }
193 
194 void
195 SatBaseEncapsulator::ReceiveAck(Ptr<SatArqAckMessage> ack)
196 {
197  NS_LOG_FUNCTION(this);
198  NS_ASSERT(false);
199 
204 }
205 
206 uint32_t
208 {
209  NS_LOG_FUNCTION(this);
210 
211  return m_txQueue->GetNBytes();
212 }
213 
214 Time
216 {
217  NS_LOG_FUNCTION(this);
218 
219  Time delay(Seconds(0.0));
220 
221  if (m_txQueue->GetNPackets() > 0)
222  {
223  // Peek the first PDU from the buffer.
224  Ptr<const Packet> peekPacket = m_txQueue->Peek();
225 
226  SatTimeTag timeTag;
227  peekPacket->PeekPacketTag(timeTag);
228 
229  delay = Simulator::Now() - timeTag.GetSenderTimestamp();
230  }
231  return delay;
232 }
233 
234 void
236 {
237  NS_LOG_FUNCTION(this << &cb);
238 
239  m_rxCallback = cb;
240 }
241 
242 void
244 {
245  NS_LOG_FUNCTION(this << &cb);
246 
247  m_ctrlCallback = cb;
248 }
249 
250 void
251 SatBaseEncapsulator::SetQueue(Ptr<SatQueue> queue)
252 {
253  NS_LOG_FUNCTION(this);
254 
255  m_txQueue = queue;
256 }
257 
258 Ptr<SatQueue>
260 {
261  NS_LOG_FUNCTION(this);
262 
263  return m_txQueue;
264 }
265 
266 uint32_t
268 {
269  NS_LOG_FUNCTION(this);
270 
271  return m_txQueue->Peek()->GetSize();
272 }
273 
274 } // namespace ns3
SatBaseEncapsulator()
Default constructor not used.
void SetCtrlMsgCallback(SatBaseEncapsulator::SendCtrlCallback cb)
virtual Time GetHolDelay() const
Get Head-of-Line packet buffering delay.
virtual uint32_t GetMinTxOpportunityInBytes() const
Get minimum Tx opportunity in bytes, which takes the assumed header sizes into account.
virtual void ReceiveAck(Ptr< SatArqAckMessage > ack)
Receive a control message (ARQ ACK)
virtual uint32_t GetTxBufferSizeInBytes() const
Get the buffered packets for this encapsulator.
static TypeId GetTypeId(void)
Get the type ID.
void SetReceiveCallback(ReceiveCallback cb)
Method to set receive callback.
ReceiveCallback m_rxCallback
Receive callback.
virtual Ptr< Packet > NotifyTxOpportunity(uint32_t bytes, uint32_t &bytesLeft, uint32_t &nextMinTxO)
Notify a Tx opportunity to this base encapsulator.
Callback< bool, Ptr< SatControlMessage >, const Address & > SendCtrlCallback
Control msg sending callback.
virtual ~SatBaseEncapsulator()
Destructor for SatBaseEncapsulator.
virtual void EnquePdu(Ptr< Packet > p, Mac48Address dest)
Enqueue a packet to txBuffer.
virtual void DoDispose()
Dispose of this class instance.
Mac48Address m_encapAddress
Source and destination mac addresses.
Ptr< SatQueue > m_txQueue
Used queue in satellite encapsulator.
Callback< void, Ptr< Packet >, Mac48Address, Mac48Address > ReceiveCallback
Callback to send packet to lower layer.
SendCtrlCallback m_ctrlCallback
Callback to send control messages.
virtual void ReceivePdu(Ptr< Packet > p)
Receive a packet.
void SetQueue(Ptr< SatQueue > queue)
Set the used queue from outside.
Ptr< SatQueue > GetQueue()
Get the queue instance.
SatFlowIdTag implements a tag which carries the flow identifier of a packet.
void SetFlowId(uint8_t flowId)
Set flow id.
This class implements a tag that carries the satellite MAC specific information, such as source and d...
void SetDestAddress(Mac48Address dest)
Set destination MAC address.
void SetSourceAddress(Mac48Address source)
Set source MAC address.
Time tag used to identify the time when packet is enqueued at LLC level.
Time GetSenderTimestamp(void) const
Get sender time stamp of this tag.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.