A Discrete-Event Network Simulator
API
csma-channel.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 Emmanuelle Laprise
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: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
19  */
20 
21 #include "csma-channel.h"
22 #include "csma-net-device.h"
23 #include "ns3/packet.h"
24 #include "ns3/simulator.h"
25 #include "ns3/log.h"
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("CsmaChannel");
30 
31 NS_OBJECT_ENSURE_REGISTERED (CsmaChannel);
32 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::CsmaChannel")
37  .SetParent<Channel> ()
38  .SetGroupName ("Csma")
39  .AddConstructor<CsmaChannel> ()
40  .AddAttribute ("DataRate",
41  "The transmission data rate to be provided to devices connected to the channel",
42  DataRateValue (DataRate (0xffffffff)),
45  .AddAttribute ("Delay", "Transmission delay through the channel",
46  TimeValue (Seconds (0)),
48  MakeTimeChecker ())
49  ;
50  return tid;
51 }
52 
54  :
55  Channel ()
56 {
58  m_state = IDLE;
59  m_deviceList.clear ();
60 }
61 
63 {
64  NS_LOG_FUNCTION (this);
65  m_deviceList.clear ();
66 }
67 
68 int32_t
70 {
71  NS_LOG_FUNCTION (this << device);
72  NS_ASSERT (device != 0);
73 
74  CsmaDeviceRec rec (device);
75 
76  m_deviceList.push_back (rec);
77  return (m_deviceList.size () - 1);
78 }
79 
80 bool
82 {
83  NS_LOG_FUNCTION (this << device);
84  NS_ASSERT (device != 0);
85 
86  std::vector<CsmaDeviceRec>::iterator it;
87  for (it = m_deviceList.begin (); it < m_deviceList.end ( ); it++)
88  {
89  if (it->devicePtr == device)
90  {
91  if (!it->active)
92  {
93  it->active = true;
94  return true;
95  }
96  else
97  {
98  return false;
99  }
100  }
101  }
102  return false;
103 }
104 
105 bool
106 CsmaChannel::Reattach (uint32_t deviceId)
107 {
108  NS_LOG_FUNCTION (this << deviceId);
109 
110  if (deviceId < m_deviceList.size ())
111  {
112  return false;
113  }
114 
115  if (m_deviceList[deviceId].active)
116  {
117  return false;
118  }
119  else
120  {
121  m_deviceList[deviceId].active = true;
122  return true;
123  }
124 }
125 
126 bool
127 CsmaChannel::Detach (uint32_t deviceId)
128 {
129  NS_LOG_FUNCTION (this << deviceId);
130 
131  if (deviceId < m_deviceList.size ())
132  {
133  if (!m_deviceList[deviceId].active)
134  {
135  NS_LOG_WARN ("CsmaChannel::Detach(): Device is already detached (" << deviceId << ")");
136  return false;
137  }
138 
139  m_deviceList[deviceId].active = false;
140 
141  if ((m_state == TRANSMITTING) && (m_currentSrc == deviceId))
142  {
143  NS_LOG_WARN ("CsmaChannel::Detach(): Device is currently" << "transmitting (" << deviceId << ")");
144  }
145 
146  return true;
147  }
148  else
149  {
150  return false;
151  }
152 }
153 
154 bool
156 {
157  NS_LOG_FUNCTION (this << device);
158  NS_ASSERT (device != 0);
159 
160  std::vector<CsmaDeviceRec>::iterator it;
161  for (it = m_deviceList.begin (); it < m_deviceList.end (); it++)
162  {
163  if ((it->devicePtr == device) && (it->active))
164  {
165  it->active = false;
166  return true;
167  }
168  }
169  return false;
170 }
171 
172 bool
174 {
175  NS_LOG_FUNCTION (this << p << srcId);
176  NS_LOG_INFO ("UID is " << p->GetUid () << ")");
177 
178  if (m_state != IDLE)
179  {
180  NS_LOG_WARN ("CsmaChannel::TransmitStart(): State is not IDLE");
181  return false;
182  }
183 
184  if (!IsActive (srcId))
185  {
186  NS_LOG_ERROR ("CsmaChannel::TransmitStart(): Seclected source is not currently attached to network");
187  return false;
188  }
189 
190  NS_LOG_LOGIC ("switch to TRANSMITTING");
191  m_currentPkt = p->Copy ();
192  m_currentSrc = srcId;
194  return true;
195 }
196 
197 bool
198 CsmaChannel::IsActive (uint32_t deviceId)
199 {
200  return (m_deviceList[deviceId].active);
201 }
202 
203 bool
205 {
207  NS_LOG_INFO ("UID is " << m_currentPkt->GetUid () << ")");
208 
211 
212  bool retVal = true;
213 
214  if (!IsActive (m_currentSrc))
215  {
216  NS_LOG_ERROR ("CsmaChannel::TransmitEnd(): Seclected source was detached before the end of the transmission");
217  retVal = false;
218  }
219 
220  NS_LOG_LOGIC ("Schedule event in " << m_delay.As (Time::S));
221 
222 
223  NS_LOG_LOGIC ("Receive");
224 
225  std::vector<CsmaDeviceRec>::iterator it;
226  uint32_t devId = 0;
227  for (it = m_deviceList.begin (); it < m_deviceList.end (); it++)
228  {
229  if (it->IsActive ())
230  {
231  // schedule reception events
232  Simulator::ScheduleWithContext (it->devicePtr->GetNode ()->GetId (),
233  m_delay,
234  &CsmaNetDevice::Receive, it->devicePtr,
235  m_currentPkt->Copy (), m_deviceList[m_currentSrc].devicePtr);
236  }
237  devId++;
238  }
239 
240  // also schedule for the tx side to go back to IDLE
242  this);
243  return retVal;
244 }
245 
246 void
248 {
249  NS_LOG_FUNCTION (this << m_currentPkt);
250  NS_LOG_INFO ("UID is " << m_currentPkt->GetUid () << ")");
251 
253  m_state = IDLE;
254 }
255 
256 uint32_t
258 {
259  int numActDevices = 0;
260  std::vector<CsmaDeviceRec>::iterator it;
261  for (it = m_deviceList.begin (); it < m_deviceList.end (); it++)
262  {
263  if (it->active)
264  {
265  numActDevices++;
266  }
267  }
268  return numActDevices;
269 }
270 
271 std::size_t
273 {
274  return m_deviceList.size ();
275 }
276 
278 CsmaChannel::GetCsmaDevice (std::size_t i) const
279 {
280  return m_deviceList[i].devicePtr;
281 }
282 
283 int32_t
285 {
286  std::vector<CsmaDeviceRec>::iterator it;
287  int i = 0;
288  for (it = m_deviceList.begin (); it < m_deviceList.end (); it++)
289  {
290  if (it->devicePtr == device)
291  {
292  if (it->active)
293  {
294  return i;
295  }
296  else
297  {
298  return -2;
299  }
300  }
301  i++;
302  }
303  return -1;
304 }
305 
306 bool
308 {
309  if (m_state == IDLE)
310  {
311  return false;
312  }
313  else
314  {
315  return true;
316  }
317 }
318 
319 DataRate
321 {
322  return m_bps;
323 }
324 
325 Time
327 {
328  return m_delay;
329 }
330 
331 WireState
333 {
334  return m_state;
335 }
336 
338 CsmaChannel::GetDevice (std::size_t i) const
339 {
340  return GetCsmaDevice (i);
341 }
342 
344 {
345  active = false;
346 }
347 
349 {
350  devicePtr = device;
351  active = true;
352 }
353 
355 {
356  devicePtr = deviceRec.devicePtr;
357  active = deviceRec.active;
358 }
359 
360 bool
362 {
363  return active;
364 }
365 
366 } // 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::CsmaChannel::m_bps
DataRate m_bps
The assigned data rate of the channel.
Definition: csma-channel.h:314
ns3::DataRateValue
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
ns3::CsmaChannel::m_delay
Time m_delay
The assigned speed-of-light delay of the channel.
Definition: csma-channel.h:319
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::MakeTimeChecker
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
ns3::CsmaDeviceRec::CsmaDeviceRec
CsmaDeviceRec()
Definition: csma-channel.cc:343
ns3::Simulator::ScheduleWithContext
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:572
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::CsmaDeviceRec::devicePtr
Ptr< CsmaNetDevice > devicePtr
Pointer to the net device.
Definition: csma-channel.h:44
ns3::MakeDataRateAccessor
Ptr< const AttributeAccessor > MakeDataRateAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: data-rate.h:298
ns3::CsmaChannel::GetDevice
virtual Ptr< NetDevice > GetDevice(std::size_t i) const
Definition: csma-channel.cc:338
ns3::CsmaDeviceRec::IsActive
bool IsActive()
Definition: csma-channel.cc:361
ns3::CsmaChannel
Csma Channel.
Definition: csma-channel.h:91
NS_LOG_WARN
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
ns3::WireState
WireState
Current state of the channel.
Definition: csma-channel.h:74
ns3::CsmaChannel::GetState
WireState GetState()
Definition: csma-channel.cc:332
ns3::CsmaChannel::GetCsmaDevice
Ptr< CsmaNetDevice > GetCsmaDevice(std::size_t i) const
Definition: csma-channel.cc:278
ns3::CsmaChannel::m_currentPkt
Ptr< Packet > m_currentPkt
The Packet that is currently being transmitted on the channel (or last packet to have been transmitte...
Definition: csma-channel.h:339
ns3::CsmaChannel::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: csma-channel.cc:34
ns3::Time::As
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:429
ns3::CsmaDeviceRec
CsmaNetDevice Record.
Definition: csma-channel.h:42
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::CsmaChannel::Attach
int32_t Attach(Ptr< CsmaNetDevice > device)
Attach a given netdevice to this channel.
Definition: csma-channel.cc:69
ns3::CsmaChannel::m_deviceList
std::vector< CsmaDeviceRec > m_deviceList
List of the net devices that have been or are currently connected to the channel.
Definition: csma-channel.h:332
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
csma-channel.h
ns3::CsmaChannel::CsmaChannel
CsmaChannel()
Create a CsmaChannel.
Definition: csma-channel.cc:53
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::DataRate
Class for representing data rates.
Definition: data-rate.h:89
ns3::CsmaChannel::IsBusy
bool IsBusy()
Indicates if the channel is busy.
Definition: csma-channel.cc:307
ns3::IDLE
@ IDLE
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:75
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::CsmaChannel::GetNDevices
virtual std::size_t GetNDevices(void) const
Definition: csma-channel.cc:272
ns3::CsmaDeviceRec::active
bool active
Is net device enabled to TX/RX.
Definition: csma-channel.h:45
ns3::CsmaChannel::IsActive
bool IsActive(uint32_t deviceId)
Indicates if a net device is currently attached or detached from the channel.
Definition: csma-channel.cc:198
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::CsmaChannel::PropagationCompleteEvent
void PropagationCompleteEvent()
Indicates that the channel has finished propagating the current packet.
Definition: csma-channel.cc:247
ns3::CsmaChannel::GetDeviceNum
int32_t GetDeviceNum(Ptr< CsmaNetDevice > device)
Definition: csma-channel.cc:284
ns3::CsmaChannel::GetDelay
Time GetDelay(void)
Get the assigned speed-of-light delay of the channel.
Definition: csma-channel.cc:326
NS_LOG_LOGIC
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
NS_LOG_ERROR
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log-macros-enabled.h:209
ns3::TRANSMITTING
@ TRANSMITTING
Channel is BUSY, a packet is being written by a net device.
Definition: csma-channel.h:76
ns3::CsmaNetDevice::Receive
void Receive(Ptr< Packet > p, Ptr< CsmaNetDevice > sender)
Receive a packet from a connected CsmaChannel.
Definition: csma-net-device.cc:687
csma-net-device.h
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Packet::Copy
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
ns3::Time::S
@ S
second
Definition: nstime.h:115
ns3::Channel
Abstract Channel Base Class.
Definition: channel.h:44
ns3::CsmaChannel::TransmitStart
bool TransmitStart(Ptr< const Packet > p, uint32_t srcId)
Start transmitting a packet over the channel.
Definition: csma-channel.cc:173
ns3::CsmaChannel::m_state
WireState m_state
Current state of the channel.
Definition: csma-channel.h:351
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
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::CsmaChannel::~CsmaChannel
virtual ~CsmaChannel()
Destroy a CsmaChannel.
Definition: csma-channel.cc:62
ns3::CsmaChannel::m_currentSrc
uint32_t m_currentSrc
Device Id of the source that is currently transmitting on the channel.
Definition: csma-channel.h:346
ns3::CsmaChannel::GetDataRate
DataRate GetDataRate(void)
Get the assigned data rate of the channel.
Definition: csma-channel.cc:320
ns3::PROPAGATING
@ PROPAGATING
Channel is BUSY, packet is propagating to all attached net devices.
Definition: csma-channel.h:77
ns3::CsmaChannel::Detach
bool Detach(Ptr< CsmaNetDevice > device)
Detach a given netdevice from this channel.
Definition: csma-channel.cc:155
ns3::CsmaChannel::TransmitEnd
bool TransmitEnd()
Indicates that the net device has finished transmitting the packet over the channel.
Definition: csma-channel.cc:204
ns3::MakeDataRateChecker
Ptr< const AttributeChecker > MakeDataRateChecker(void)
Definition: data-rate.cc:30
ns3::MakeTimeAccessor
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: nstime.h:1354
ns3::CsmaChannel::GetNumActDevices
uint32_t GetNumActDevices(void)
Definition: csma-channel.cc:257
ns3::Packet::GetUid
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:390
ns3::CsmaChannel::Reattach
bool Reattach(uint32_t deviceId)
Reattach a previously detached net device to the channel.
Definition: csma-channel.cc:106