A Discrete-Event Network Simulator
API
default-channel-scheduler.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: Junling Bu <linlinjavaer@gmail.com>
17  */
19 #include "ns3/log.h"
20 #include "ns3/simulator.h"
21 #include "ns3/wifi-phy.h"
22 
23 namespace ns3 {
24 
25 NS_LOG_COMPONENT_DEFINE ("DefaultChannelScheduler");
26 
27 NS_OBJECT_ENSURE_REGISTERED (DefaultChannelScheduler);
28 
34 {
35 public:
42  : m_scheduler (scheduler)
43  {
44  }
46  {
47  }
48  virtual void NotifyCchSlotStart (Time duration)
49  {
50  m_scheduler->NotifyCchSlotStart (duration);
51  }
52  virtual void NotifySchSlotStart (Time duration)
53  {
54  m_scheduler->NotifySchSlotStart (duration);
55  }
56  virtual void NotifyGuardSlotStart (Time duration, bool cchi)
57  {
58  m_scheduler->NotifyGuardSlotStart (duration, cchi);
59  }
60 private:
62 };
63 
64 TypeId
66 {
67  static TypeId tid = TypeId ("ns3::DefaultChannelScheduler")
69  .SetGroupName ("Wave")
70  .AddConstructor<DefaultChannelScheduler> ()
71  ;
72  return tid;
73 }
74 
76  : m_channelNumber (0),
77  m_extend (EXTENDED_CONTINUOUS),
78  m_channelAccess (NoAccess),
79  m_waitChannelNumber (0),
80  m_waitExtend (0),
81  m_coordinationListener (0)
82 {
83  NS_LOG_FUNCTION (this);
84 }
86 {
87  NS_LOG_FUNCTION (this);
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION (this);
95 }
96 
97 void
99 {
100  NS_LOG_FUNCTION (this);
101  m_coordinator = 0;
102  if (m_coordinationListener != 0)
103  {
105  }
106  if (!m_waitEvent.IsExpired ())
107  {
108  m_waitEvent.Cancel ();
109  }
110  if (!m_extendEvent.IsExpired ())
111  {
112  m_waitEvent.Cancel ();
113  }
114  m_phy = 0;
116 }
117 
118 void
120 {
121  NS_LOG_FUNCTION (this << device);
123  std::vector<Ptr<WifiPhy> > phys = device->GetPhys ();
124  if (phys.size () > 1)
125  {
126  NS_LOG_WARN ("The class is only in the context of single-PHY device, while there are more than one PHY devices");
127  }
128  // since default channel scheduler is in the context of single-PHY, we only use one phy object.
129  m_phy = device->GetPhy (0);
131  m_coordinationListener = Create<CoordinationListener> (this);
133 }
134 
135 enum ChannelAccess
136 DefaultChannelScheduler::GetAssignedAccessType (uint32_t channelNumber) const
137 {
138  NS_LOG_FUNCTION (this << channelNumber);
139  if (m_channelAccess == AlternatingAccess && channelNumber == CCH)
140  {
141  return AlternatingAccess;
142  }
143  return (m_channelNumber == channelNumber) ? m_channelAccess : NoAccess;
144 }
145 
146 
147 bool
148 DefaultChannelScheduler::AssignAlternatingAccess (uint32_t channelNumber, bool immediate)
149 {
150  NS_LOG_FUNCTION (this << channelNumber << immediate);
152  uint32_t sch = channelNumber;
153 
155  {
156  return false;
157  }
158 
160  {
161  if (m_channelNumber != sch)
162  {
163  return false;
164  }
165  else
166  {
167  return true;
168  }
169  }
170 
171  // if we need immediately switch to AlternatingAccess,
172  // we switch to specific SCH.
173  if ((immediate && m_coordinator->IsSchInterval ()))
174  {
176  SwitchToNextChannel (CCH, sch);
177  }
178 
179  m_channelNumber = sch;
181  return true;
182 }
183 
184 bool
185 DefaultChannelScheduler::AssignContinuousAccess (uint32_t channelNumber, bool immediate)
186 {
187  NS_LOG_FUNCTION (this << channelNumber << immediate);
189  uint32_t sch = channelNumber;
191  {
192  return false;
193  }
194 
196  {
197  if (m_channelNumber != sch)
198  {
199  return false;
200  }
201  else
202  {
203  return true;
204  }
205  }
206 
207  // if there is already an wait event for previous non-immediate request
208  if (!m_waitEvent.IsExpired ())
209  {
210  if (m_waitChannelNumber != sch)
211  {
212  // then the coming new request will be rejected because of FCFS
213  return false;
214  }
215  else
216  {
217  if (!immediate)
218  {
219  return true;
220  }
221  // then cancel this wait event and assign access for request immediately
222  m_waitEvent.Cancel ();
223  }
224  }
225 
226  if (immediate || m_coordinator->IsSchInterval ())
227  {
229  m_channelNumber = sch;
231  }
232  else
233  {
236  m_waitChannelNumber = sch;
237  }
238 
239  return true;
240 }
241 
242 bool
243 DefaultChannelScheduler::AssignExtendedAccess (uint32_t channelNumber, uint32_t extends, bool immediate)
244 {
245  NS_LOG_FUNCTION (this << channelNumber << extends << immediate);
247  uint32_t sch = channelNumber;
249  {
250  return false;
251  }
252 
254  {
255  if (m_channelNumber != sch)
256  {
257  return false;
258  }
259  else
260  {
261  // if current remain extends cannot fulfill the requirement for extends
263  uint32_t remainExtends = (remainTime / m_coordinator->GetSyncInterval ()).GetHigh ();
264  if (remainExtends > extends)
265  {
266  return true;
267  }
268  else
269  {
270  return false;
271  }
272  }
273  }
274 
275  // if there is already an wait event for previous non-immediate request
276  if (!m_waitEvent.IsExpired ())
277  {
279  if (m_waitChannelNumber != sch)
280  {
281  // then the coming new request will be rejected because of FCFS
282  return false;
283  }
284  else
285  {
286  if (m_waitExtend < extends)
287  {
288  return false;
289  }
290 
291  if (immediate)
292  {
293  // then cancel previous wait event and
294  // go to below code to assign access for request immediately
295  m_waitEvent.Cancel ();
296  }
297  else
298  {
299  return true;
300  }
301  }
302  }
303 
304  if (immediate || m_coordinator->IsSchInterval ())
305  {
307  m_channelNumber = sch;
309  m_extend = extends;
310 
312  // the wait time to proper interval will not be calculated as extended time.
313  Time extendedDuration = m_coordinator->NeedTimeToCchInterval () + MilliSeconds (extends * sync.GetMilliSeconds ());
314  // after end_duration time, DefaultChannelScheduler will release channel access automatically
316  }
317  else
318  {
321  m_waitChannelNumber = sch;
322  m_waitExtend = extends;
323  }
324  return true;
325 }
326 
327 bool
329 {
330  NS_LOG_FUNCTION (this);
332  {
333  return true;
334  }
335  if (m_channelNumber != 0)
336  {
337  // This class does not support preemptive scheduling
338  NS_LOG_DEBUG ("channel access is already assigned for other SCHs, thus cannot assign default CCH access.");
339  return false;
340  }
341  // CCH MAC is to attach single-PHY device and wake up for transmission.
342  Ptr<OcbWifiMac> cchMacEntity = m_device->GetMac (CCH);
343  if (Now ().GetMilliSeconds() != 0)
344  {
346  Time switchTime = m_phy->GetChannelSwitchDelay ();
347  cchMacEntity->MakeVirtualBusy (switchTime);
348  }
349  cchMacEntity->SetWifiPhy (m_phy);
350  cchMacEntity->Resume ();
351 
355  return true;
356 }
357 
358 void
359 DefaultChannelScheduler::SwitchToNextChannel (uint32_t curChannelNumber, uint32_t nextChannelNumber)
360 {
361  NS_LOG_FUNCTION (this << curChannelNumber << curChannelNumber);
362  if (m_phy->GetChannelNumber () == nextChannelNumber)
363  {
364  return;
365  }
366  Ptr<OcbWifiMac> curMacEntity = m_device->GetMac (curChannelNumber);
367  Ptr<OcbWifiMac> nextMacEntity = m_device->GetMac (nextChannelNumber);
368  // Perfect channel switch operation among multiple MAC entities in the context of single PHY device.
369  // first make current MAC entity in sleep mode.
370  curMacEntity->Suspend ();
371  // second unattached current MAC entity from single PHY device
372  curMacEntity->ResetWifiPhy ();
373  // third switch PHY device from current channel to next channel;
374  m_phy->SetChannelNumber (nextChannelNumber);
375  // four attach next MAC entity to single PHY device
376  nextMacEntity->SetWifiPhy (m_phy);
377  // Here channel switch time is required to notify next MAC entity
378  // that channel access cannot be enabled in channel switch time.
379  Time switchTime = m_phy->GetChannelSwitchDelay ();
380  nextMacEntity->MakeVirtualBusy (switchTime);
381  // finally resume next MAC entity from sleep mode
382  nextMacEntity->Resume ();
383 }
384 
385 bool
387 {
388  NS_LOG_FUNCTION (this << channelNumber);
389  NS_ASSERT (m_channelNumber != 0);
390  if (m_channelNumber != channelNumber)
391  {
392  return false;
393  }
394  // cancel current SCH MAC activity and assigned default CCH access.
399  if (!m_waitEvent.IsExpired ())
400  {
401  m_waitEvent.Cancel ();
402  }
403  if (!m_extendEvent.IsExpired ())
404  {
406  }
408  m_waitExtend = 0;
409  return true;
410 }
411 
412 void
414 {
415  NS_LOG_FUNCTION (this << duration);
416 }
417 
418 void
420 {
421  NS_LOG_FUNCTION (this << duration);
422 }
423 
424 void
426 {
427  NS_LOG_FUNCTION (this << duration << cchi);
428  // only alternating access requires channel coordination events
430  {
431  return;
432  }
433 
434  if (cchi)
435  {
438  // see chapter 6.2.5 Sync tolerance
439  // a medium busy shall be declared during the guard interval.
440  mac->MakeVirtualBusy (duration);
441  }
442  else
443  {
446  // see chapter 6.2.5 Sync tolerance
447  // a medium busy shall be declared during the guard interval.
448  mac->MakeVirtualBusy (duration);
449  }
450 }
451 } // namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::DefaultChannelScheduler::m_coordinationListener
Ptr< ChannelCoordinationListener > m_coordinationListener
coordination listener
Definition: default-channel-scheduler.h:136
ns3::ExtendedAccess
@ ExtendedAccess
Definition: channel-scheduler.h:115
ns3::DefaultChannelScheduler::m_coordinator
Ptr< ChannelCoordinator > m_coordinator
channel coordinator
Definition: default-channel-scheduler.h:115
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
ns3::OcbWifiMac::Suspend
void Suspend(void)
To support MAC extension for multiple channel operation, Suspend the activity in current MAC entity.
Definition: ocb-wifi-mac.cc:416
ns3::DefaultChannelScheduler::SwitchToNextChannel
void SwitchToNextChannel(uint32_t curChannelNumber, uint32_t nextChannelNumber)
Definition: default-channel-scheduler.cc:359
ns3::ContinuousAccess
@ ContinuousAccess
Definition: channel-scheduler.h:113
ns3::DefaultChannelScheduler::m_phy
Ptr< WifiPhy > m_phy
Phy.
Definition: default-channel-scheduler.h:116
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
CCH
#define CCH
Definition: channel-manager.h:52
ns3::ChannelCoordinator::NeedTimeToCchInterval
Time NeedTimeToCchInterval(Time duration=Seconds(0.0)) const
Definition: channel-coordinator.cc:243
ns3::Simulator::GetDelayLeft
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:204
ns3::AlternatingAccess
@ AlternatingAccess
Definition: channel-scheduler.h:114
ns3::CoordinationListener::~CoordinationListener
virtual ~CoordinationListener()
Definition: default-channel-scheduler.cc:45
ns3::ChannelCoordinator::IsSchInterval
bool IsSchInterval(Time duration=Seconds(0.0)) const
Definition: channel-coordinator.cc:195
ns3::WifiPhy::GetChannelNumber
uint8_t GetChannelNumber(void) const
Return current channel number.
Definition: wifi-phy.cc:1199
ns3::DefaultChannelScheduler::m_waitExtend
uint32_t m_waitExtend
wait extend
Definition: default-channel-scheduler.h:134
NS_LOG_WARN
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
ns3::ChannelScheduler::DoInitialize
virtual void DoInitialize(void)
Initialize() implementation.
Definition: channel-scheduler.cc:52
default-channel-scheduler.h
third.mac
mac
Definition: third.py:99
ns3::DefaultChannelScheduler::m_channelNumber
uint32_t m_channelNumber
when m_channelAccess is ContinuousAccess, m_channelNumber is continuous channel number; when m_channe...
Definition: default-channel-scheduler.h:127
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
ns3::WaveNetDevice::GetChannelCoordinator
Ptr< ChannelCoordinator > GetChannelCoordinator(void) const
Definition: wave-net-device.cc:491
ns3::DefaultChannelScheduler::NotifyGuardSlotStart
void NotifyGuardSlotStart(Time duration, bool cchi)
Notify guard slot start.
Definition: default-channel-scheduler.cc:425
ns3::DefaultChannelScheduler::NotifySchSlotStart
void NotifySchSlotStart(Time duration)
Notify SCH slot start.
Definition: default-channel-scheduler.cc:419
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::CoordinationListener::m_scheduler
DefaultChannelScheduler * m_scheduler
the scheduler
Definition: default-channel-scheduler.cc:61
ns3::DefaultChannelScheduler::m_extendEvent
EventId m_extendEvent
extend event
Definition: default-channel-scheduler.h:129
ns3::ChannelScheduler
This class will assign channel access for requests from higher layers.
Definition: channel-scheduler.h:130
ns3::ChannelCoordinator::NeedTimeToSchInterval
Time NeedTimeToSchInterval(Time duration=Seconds(0.0)) const
Definition: channel-coordinator.cc:254
EXTENDED_CONTINUOUS
#define EXTENDED_CONTINUOUS
Definition: channel-scheduler.h:50
ns3::DefaultChannelScheduler::SetWaveNetDevice
virtual void SetWaveNetDevice(Ptr< WaveNetDevice > device)
Definition: default-channel-scheduler.cc:119
ns3::CoordinationListener
CoordinationListener class.
Definition: default-channel-scheduler.cc:34
ns3::Ptr< WaveNetDevice >
ns3::DefaultChannelScheduler::NotifyCchSlotStart
void NotifyCchSlotStart(Time duration)
Notify CCH slot start.
Definition: default-channel-scheduler.cc:413
ns3::ChannelAccess
ChannelAccess
ChannelAccess enumeration.
Definition: channel-scheduler.h:112
ns3::DefaultChannelScheduler::m_extend
uint32_t m_extend
extend
Definition: default-channel-scheduler.h:128
ns3::WaveNetDevice::GetPhys
std::vector< Ptr< WifiPhy > > GetPhys(void) const
Definition: wave-net-device.cc:227
ns3::DefaultChannelScheduler::m_waitChannelNumber
uint32_t m_waitChannelNumber
wait channel number
Definition: default-channel-scheduler.h:133
ns3::Now
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
ns3::NoAccess
@ NoAccess
Definition: channel-scheduler.h:117
ns3::CoordinationListener::NotifyGuardSlotStart
virtual void NotifyGuardSlotStart(Time duration, bool cchi)
Definition: default-channel-scheduler.cc:56
ns3::ChannelCoordinationListener
receive notifications about channel coordination events.
Definition: channel-coordinator.h:31
ns3::DefaultChannelScheduler::GetAssignedAccessType
virtual enum ChannelAccess GetAssignedAccessType(uint32_t channelNumber) const
Definition: default-channel-scheduler.cc:136
ns3::RegularWifiMac::ResetWifiPhy
void ResetWifiPhy(void) override
Remove currently attached WifiPhy device from this MAC.
Definition: regular-wifi-mac.cc:565
ns3::CoordinationListener::NotifySchSlotStart
virtual void NotifySchSlotStart(Time duration)
Definition: default-channel-scheduler.cc:52
ns3::WaveNetDevice::GetMac
Ptr< OcbWifiMac > GetMac(uint32_t channelNumber) const
Definition: wave-net-device.cc:191
ns3::MilliSeconds
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1297
ns3::EventId::Cancel
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
ns3::DefaultChannelScheduler::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: default-channel-scheduler.cc:65
ns3::CoordinationListener::NotifyCchSlotStart
virtual void NotifyCchSlotStart(Time duration)
Definition: default-channel-scheduler.cc:48
ns3::ChannelScheduler::SetWaveNetDevice
virtual void SetWaveNetDevice(Ptr< WaveNetDevice > device)
Definition: channel-scheduler.cc:59
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::DefaultChannelScheduler::ReleaseAccess
virtual bool ReleaseAccess(uint32_t channelNumber)
Definition: default-channel-scheduler.cc:386
ns3::DefaultChannelScheduler::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: default-channel-scheduler.cc:98
ns3::DefaultChannelScheduler
This class uses a simple mechanism to assign channel access with following features: (1) only in the ...
Definition: default-channel-scheduler.h:33
ns3::DefaultChannelScheduler::DoInitialize
virtual void DoInitialize(void)
Initialize() implementation.
Definition: default-channel-scheduler.cc:91
ns3::ChannelScheduler::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: channel-scheduler.cc:164
ns3::OcbWifiMac::MakeVirtualBusy
void MakeVirtualBusy(Time duration)
Definition: ocb-wifi-mac.cc:432
ns3::Time::GetMilliSeconds
int64_t GetMilliSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:384
ns3::DefaultChannelScheduler::AssignAlternatingAccess
virtual bool AssignAlternatingAccess(uint32_t channelNumber, bool immediate)
Definition: default-channel-scheduler.cc:148
ns3::DefaultChannelScheduler::m_channelAccess
enum ChannelAccess m_channelAccess
channel access
Definition: default-channel-scheduler.h:130
ns3::OcbWifiMac::Resume
void Resume(void)
To support MAC extension for multiple channel operation, Resume the activity of suspended MAC entity.
Definition: ocb-wifi-mac.cc:424
ns3::DefaultChannelScheduler::AssignContinuousAccess
virtual bool AssignContinuousAccess(uint32_t channelNumber, bool immediate)
Definition: default-channel-scheduler.cc:185
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::ChannelCoordinator::RegisterListener
void RegisterListener(Ptr< ChannelCoordinationListener > listener)
Definition: channel-coordinator.cc:299
ns3::RegularWifiMac::SetWifiPhy
void SetWifiPhy(const Ptr< WifiPhy > phy) override
Definition: regular-wifi-mac.cc:548
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::WifiPhy::GetChannelSwitchDelay
Time GetChannelSwitchDelay(void) const
Definition: wifi-phy.cc:858
ns3::DefaultChannelScheduler::~DefaultChannelScheduler
virtual ~DefaultChannelScheduler()
Definition: default-channel-scheduler.cc:85
ns3::DefaultChannelScheduler::DefaultChannelScheduler
DefaultChannelScheduler()
Definition: default-channel-scheduler.cc:75
ns3::WifiPhy::SetChannelNumber
virtual void SetChannelNumber(uint8_t id)
Set channel number.
Definition: wifi-phy.cc:1174
ns3::ChannelScheduler::m_device
Ptr< WaveNetDevice > m_device
the device
Definition: channel-scheduler.h:237
ns3::CoordinationListener::CoordinationListener
CoordinationListener(DefaultChannelScheduler *scheduler)
Constructor.
Definition: default-channel-scheduler.cc:41
ns3::DefaultChannelScheduler::AssignExtendedAccess
virtual bool AssignExtendedAccess(uint32_t channelNumber, uint32_t extends, bool immediate)
Definition: default-channel-scheduler.cc:243
ns3::DefaultCchAccess
@ DefaultCchAccess
Definition: channel-scheduler.h:116
ns3::ChannelCoordinator::GetSyncInterval
Time GetSyncInterval(void) const
Definition: channel-coordinator.cc:152
ns3::DefaultChannelScheduler::m_waitEvent
EventId m_waitEvent
wait event
Definition: default-channel-scheduler.h:132
ns3::WaveNetDevice::GetPhy
Ptr< WifiPhy > GetPhy(uint32_t index) const
Definition: wave-net-device.cc:220
ns3::EventId::IsExpired
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:65
ns3::DefaultChannelScheduler::AssignDefaultCchAccess
virtual bool AssignDefaultCchAccess(void)
This method will assign default CCH access for CCH.
Definition: default-channel-scheduler.cc:328