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.GetSeconds () << " sec");
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
uint32_t m_currentSrc
Device Id of the source that is currently transmitting on the channel.
Definition: csma-channel.h:346
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
WireState m_state
Current state of the channel.
Definition: csma-channel.h:351
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
void Receive(Ptr< Packet > p, Ptr< CsmaNetDevice > sender)
Receive a packet from a connected CsmaChannel.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
#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
uint32_t GetNumActDevices(void)
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Abstract Channel Base Class.
Definition: channel.h:43
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual std::size_t GetNDevices(void) const
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
WireState GetState()
Ptr< const AttributeChecker > MakeDataRateChecker(void)
Definition: data-rate.cc:30
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:446
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
Class for representing data rates.
Definition: data-rate.h:88
virtual ~CsmaChannel()
Destroy a CsmaChannel.
Definition: csma-channel.cc:62
Channel is BUSY, a packet is being written by a net device.
Definition: csma-channel.h:76
bool Detach(Ptr< CsmaNetDevice > device)
Detach a given netdevice from this channel.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
AttributeValue implementation for Time.
Definition: nstime.h:1076
DataRate m_bps
The assigned data rate of the channel.
Definition: csma-channel.h:314
Time m_delay
The assigned speed-of-light delay of the channel.
Definition: csma-channel.h:319
Time GetDelay(void)
Get the assigned speed-of-light delay of the channel.
int32_t Attach(Ptr< CsmaNetDevice > device)
Attach a given netdevice to this channel.
Definition: csma-channel.cc:69
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:242
Channel is BUSY, packet is propagating to all attached net devices.
Definition: csma-channel.h:77
Ptr< CsmaNetDevice > GetCsmaDevice(std::size_t i) const
WireState
Current state of the channel.
Definition: csma-channel.h:73
bool IsActive(uint32_t deviceId)
Indicates if a net device is currently attached or detached from the channel.
Csma Channel.
Definition: csma-channel.h:90
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void PropagationCompleteEvent()
Indicates that the channel has finished propagating the current packet.
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
CsmaNetDevice Record.
Definition: csma-channel.h:42
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:1077
int32_t GetDeviceNum(Ptr< CsmaNetDevice > device)
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1475
CsmaChannel()
Create a CsmaChannel.
Definition: csma-channel.cc:53
bool TransmitEnd()
Indicates that the net device has finished transmitting the packet over the channel.
bool IsBusy()
Indicates if the channel is busy.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:262
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
bool active
Is net device enabled to TX/RX.
Definition: csma-channel.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
DataRate GetDataRate(void)
Get the assigned data rate of the channel.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
bool TransmitStart(Ptr< const Packet > p, uint32_t srcId)
Start transmitting a packet over the channel.
Ptr< CsmaNetDevice > devicePtr
Pointer to the net device.
Definition: csma-channel.h:44
virtual Ptr< NetDevice > GetDevice(std::size_t i) const
bool Reattach(uint32_t deviceId)
Reattach a previously detached net device to the channel.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
static TypeId GetTypeId(void)
Get the type ID.
Definition: csma-channel.cc:34