A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ss-service-flow-manager.cc
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  * Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
19  */
20 
21 #include <stdint.h>
22 #include "ns3/node.h"
23 #include "ns3/simulator.h"
24 #include "ns3/packet.h"
25 #include "service-flow.h"
26 #include "service-flow-manager.h"
27 #include "ns3/log.h"
28 #include "wimax-net-device.h"
29 #include "bs-net-device.h"
30 #include "ss-net-device.h"
31 #include "ss-record.h"
32 #include "ns3/pointer.h"
33 #include "ns3/enum.h"
34 #include "wimax-connection.h"
35 #include "ss-manager.h"
36 #include "connection-manager.h"
37 #include "bs-uplink-scheduler.h"
38 #include "ss-scheduler.h"
39 #include "ns3/buffer.h"
40 #include "service-flow-record.h"
41 NS_LOG_COMPONENT_DEFINE ("SsServiceFlowManager");
42 
43 namespace ns3 {
44 
46  : m_device (device),
47  m_sfidIndex (100),
48  m_maxDsaReqRetries (100),
49  m_dsaReq (DsaReq ()),
50  m_dsaAck (DsaAck ()),
51  m_currentTransactionId (0),
52  m_transactionIdIndex (1),
53  m_dsaReqRetries (0),
54  m_pendingServiceFlow (0)
55 {
56 }
57 
58 SsServiceFlowManager::~SsServiceFlowManager (void)
59 {
60 }
61 
62 void
64 {
66 }
67 
68 void
70 {
71  m_maxDsaReqRetries = maxDsaReqRetries;
72 }
73 
74 uint8_t
76 {
77  return m_maxDsaReqRetries;
78 }
79 
80 EventId
81 SsServiceFlowManager::GetDsaRspTimeoutEvent (void) const
82 {
83  return m_dsaRspTimeoutEvent;
84 }
85 
86 EventId
87 SsServiceFlowManager::GetDsaAckTimeoutEvent (void) const
88 {
89  return m_dsaAckTimeoutEvent;
90 }
91 
92 void
94 {
95  ServiceFlow * sf = new ServiceFlow ();
96  sf->CopyParametersFrom (serviceFlow);
97  ServiceFlowManager::AddServiceFlow (sf);
98 }
99 
100 void
102 {
103  ServiceFlowManager::AddServiceFlow (serviceFlow);
104 }
105 
106 
107 void
108 SsServiceFlowManager::InitiateServiceFlows (void)
109 {
110  ServiceFlow *serviceFlow = GetNextServiceFlowToAllocate ();
111  NS_ASSERT_MSG (serviceFlow != 0,"Error while initiating a new service flow: All service flows have been initiated");
112  m_pendingServiceFlow = serviceFlow;
113  ScheduleDsaReq (m_pendingServiceFlow);
114 }
115 
116 DsaReq
117 SsServiceFlowManager::CreateDsaReq (const ServiceFlow *serviceFlow)
118 {
119  DsaReq dsaReq;
120  dsaReq.SetTransactionId (m_transactionIdIndex);
121  m_currentTransactionId = m_transactionIdIndex++;
122 
123  /*as it is SS-initiated DSA therefore SFID and CID will
124  not be included, see 6.3.2.3.10.1 and 6.3.2.3.11.1*/
125  dsaReq.SetServiceFlow (*serviceFlow);
126  // dsaReq.SetParameterSet (*serviceFlow->GetParameterSet ());
127  return dsaReq;
128 }
129 
130 Ptr<Packet>
131 SsServiceFlowManager::CreateDsaAck (void)
132 {
133  DsaAck dsaAck;
134  dsaAck.SetTransactionId (m_dsaReq.GetTransactionId ());
135  dsaAck.SetConfirmationCode (CONFIRMATION_CODE_SUCCESS);
136  m_dsaAck = dsaAck;
137  Ptr<Packet> p = Create<Packet> ();
138  p->AddHeader (dsaAck);
139  p->AddHeader (ManagementMessageType (ManagementMessageType::MESSAGE_TYPE_DSA_ACK));
140  return p;
141 }
142 
143 void
144 SsServiceFlowManager::ScheduleDsaReq (const ServiceFlow *serviceFlow)
145 {
146  Ptr<Packet> p = Create<Packet> ();
147  DsaReq dsaReq;
148  Ptr<SubscriberStationNetDevice> ss = m_device->GetObject<SubscriberStationNetDevice> ();
149 
150  if (m_dsaReqRetries == 0)
151  {
152  dsaReq = CreateDsaReq (serviceFlow);
153  p->AddHeader (dsaReq);
154  m_dsaReq = dsaReq;
155  }
156  else
157  {
158  if (m_dsaReqRetries <= m_maxDsaReqRetries)
159  {
160  p->AddHeader (m_dsaReq);
161  }
162  else
163  {
164  NS_LOG_DEBUG ("Service flows could not be initialized!");
165  }
166  }
167 
168  m_dsaReqRetries++;
169  p->AddHeader (ManagementMessageType (ManagementMessageType::MESSAGE_TYPE_DSA_REQ));
170 
171  if (m_dsaRspTimeoutEvent.IsRunning ())
172  {
173  Simulator::Cancel (m_dsaRspTimeoutEvent);
174  }
175 
176  m_dsaRspTimeoutEvent = Simulator::Schedule (ss->GetIntervalT7 (),
177  &SsServiceFlowManager::ScheduleDsaReq,
178  this,
179  serviceFlow);
180 
181  m_device->Enqueue (p, MacHeaderType (), ss->GetPrimaryConnection ());
182 }
183 
184 
185 void
186 SsServiceFlowManager::ProcessDsaRsp (const DsaRsp &dsaRsp)
187 {
188 
189  Ptr<SubscriberStationNetDevice> ss = m_device->GetObject<SubscriberStationNetDevice> ();
190 
191  // already received DSA-RSP for that particular DSA-REQ
192  if (dsaRsp.GetTransactionId () != m_currentTransactionId)
193  {
194  return;
195  }
196 
197  Ptr<Packet> dsaAck = CreateDsaAck ();
198  m_device->Enqueue (dsaAck, MacHeaderType (), ss->GetPrimaryConnection ());
199 
200  m_dsaReqRetries = 0;
201  if (m_pendingServiceFlow == NULL)
202  {
203  // May be the DSA-ACK was not received by the SS
204  return;
205  }
206  ServiceFlow sf = dsaRsp.GetServiceFlow ();
207  (*m_pendingServiceFlow) = sf;
208  m_pendingServiceFlow->SetUnsolicitedGrantInterval (1);
209  m_pendingServiceFlow->SetUnsolicitedPollingInterval (1);
210  Ptr<WimaxConnection> transportConnection = CreateObject<WimaxConnection> (sf.GetCid (),
211  Cid::TRANSPORT);
212 
213  m_pendingServiceFlow->SetConnection (transportConnection);
214  transportConnection->SetServiceFlow (m_pendingServiceFlow);
215  ss->GetConnectionManager ()->AddConnection (transportConnection,
216  Cid::TRANSPORT);
217  m_pendingServiceFlow->SetIsEnabled (true);
218  m_pendingServiceFlow = 0;
219  // check if all service flow have been initiated
220  ServiceFlow * serviceFlow = GetNextServiceFlowToAllocate ();
221  if (serviceFlow == 0)
222  {
223  ss->SetAreServiceFlowsAllocated (true);
224  }
225  else
226  {
227  m_pendingServiceFlow = serviceFlow;
228  ScheduleDsaReq (m_pendingServiceFlow);
229  }
230 }
231 
232 } // namespace ns3
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
ServiceFlow * GetNextServiceFlowToAllocate()
static void Cancel(const EventId &id)
Definition: simulator.cc:267
bool IsRunning(void) const
Definition: event-id.cc:59
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
void AddServiceFlow(ServiceFlow *serviceFlow)
add a service flow to the list
void SetMaxDsaReqRetries(uint8_t maxDsaReqRetries)
sets the maximum retries on DSA request message
SsServiceFlowManager(Ptr< SubscriberStationNetDevice > device)
creates a service flow manager and attaches it to a device
uint8_t GetMaxDsaReqRetries(void) const
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
an identifier for simulation events.
Definition: event-id.h:46
#define NS_LOG_DEBUG(msg)
Definition: log.h:255