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
27namespace ns3 {
28
29NS_LOG_COMPONENT_DEFINE ("CsmaChannel");
30
31NS_OBJECT_ENSURE_REGISTERED (CsmaChannel);
32
33TypeId
35{
36 static TypeId tid = TypeId ("ns3::CsmaChannel")
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)),
43 MakeDataRateAccessor (&CsmaChannel::m_bps),
44 MakeDataRateChecker ())
45 .AddAttribute ("Delay", "Transmission delay through the channel",
46 TimeValue (Seconds (0)),
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
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
80bool
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
105bool
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
126bool
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
154bool
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
172bool
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
197bool
199{
200 return (m_deviceList[deviceId].active);
201}
202
203bool
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 for (it = m_deviceList.begin (); it < m_deviceList.end (); it++)
227 {
228 if (it->IsActive () && it->devicePtr != m_deviceList[m_currentSrc].devicePtr)
229 {
230 // schedule reception events
231 Simulator::ScheduleWithContext (it->devicePtr->GetNode ()->GetId (),
232 m_delay,
233 &CsmaNetDevice::Receive, it->devicePtr,
235 }
236 }
237
238 // also schedule for the tx side to go back to IDLE
240 this);
241 return retVal;
242}
243
244void
246{
248 NS_LOG_INFO ("UID is " << m_currentPkt->GetUid () << ")");
249
251 m_state = IDLE;
252}
253
256{
257 int numActDevices = 0;
258 std::vector<CsmaDeviceRec>::iterator it;
259 for (it = m_deviceList.begin (); it < m_deviceList.end (); it++)
260 {
261 if (it->active)
262 {
263 numActDevices++;
264 }
265 }
266 return numActDevices;
267}
268
269std::size_t
271{
272 return m_deviceList.size ();
273}
274
276CsmaChannel::GetCsmaDevice (std::size_t i) const
277{
278 return m_deviceList[i].devicePtr;
279}
280
283{
284 std::vector<CsmaDeviceRec>::iterator it;
285 int i = 0;
286 for (it = m_deviceList.begin (); it < m_deviceList.end (); it++)
287 {
288 if (it->devicePtr == device)
289 {
290 if (it->active)
291 {
292 return i;
293 }
294 else
295 {
296 return -2;
297 }
298 }
299 i++;
300 }
301 return -1;
302}
303
304bool
306{
307 if (m_state == IDLE)
308 {
309 return false;
310 }
311 else
312 {
313 return true;
314 }
315}
316
319{
320 return m_bps;
321}
322
323Time
325{
326 return m_delay;
327}
328
331{
332 return m_state;
333}
334
336CsmaChannel::GetDevice (std::size_t i) const
337{
338 return GetCsmaDevice (i);
339}
340
342{
343 active = false;
344}
345
347{
348 devicePtr = device;
349 active = true;
350}
351
353{
354 devicePtr = deviceRec.devicePtr;
355 active = deviceRec.active;
356}
357
358bool
360{
361 return active;
362}
363
364} // namespace ns3
Abstract Channel Base Class.
Definition: channel.h:44
Csma Channel.
Definition: csma-channel.h:91
virtual Ptr< NetDevice > GetDevice(std::size_t i) const
uint32_t GetNumActDevices(void)
Ptr< CsmaNetDevice > GetCsmaDevice(std::size_t i) const
DataRate m_bps
The assigned data rate of the channel.
Definition: csma-channel.h:303
DataRate GetDataRate(void)
Get the assigned data rate of the channel.
bool Reattach(uint32_t deviceId)
Reattach a previously detached net device to the channel.
bool IsActive(uint32_t deviceId)
Indicates if a net device is currently attached or detached from the channel.
static TypeId GetTypeId(void)
Get the type ID.
Definition: csma-channel.cc:34
Time GetDelay(void)
Get the assigned speed-of-light delay of the channel.
int32_t GetDeviceNum(Ptr< CsmaNetDevice > device)
bool TransmitEnd()
Indicates that the net device has finished transmitting the packet over the channel.
Time m_delay
The assigned speed-of-light delay of the channel.
Definition: csma-channel.h:308
virtual ~CsmaChannel()
Destroy a CsmaChannel.
Definition: csma-channel.cc:62
bool TransmitStart(Ptr< const Packet > p, uint32_t srcId)
Start transmitting a packet over the channel.
WireState m_state
Current state of the channel.
Definition: csma-channel.h:340
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:328
CsmaChannel()
Create a CsmaChannel.
Definition: csma-channel.cc:53
bool Detach(Ptr< CsmaNetDevice > device)
Detach a given netdevice from this channel.
std::vector< CsmaDeviceRec > m_deviceList
List of the net devices that have been or are currently connected to the channel.
Definition: csma-channel.h:321
void PropagationCompleteEvent()
Indicates that the channel has finished propagating the current packet.
virtual std::size_t GetNDevices(void) const
int32_t Attach(Ptr< CsmaNetDevice > device)
Attach a given netdevice to this channel.
Definition: csma-channel.cc:69
bool IsBusy()
Indicates if the channel is busy.
WireState GetState()
uint32_t m_currentSrc
Device Id of the source that is currently transmitting on the channel.
Definition: csma-channel.h:335
CsmaNetDevice Record.
Definition: csma-channel.h:42
Ptr< CsmaNetDevice > devicePtr
Pointer to the net device.
Definition: csma-channel.h:44
bool active
Is net device enabled to TX/RX.
Definition: csma-channel.h:45
void Receive(Ptr< Packet > p, Ptr< CsmaNetDevice > sender)
Receive a packet from a connected CsmaChannel.
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:390
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:555
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:570
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
@ S
second
Definition: nstime.h:115
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:418
AttributeValue implementation for Time.
Definition: nstime.h:1309
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#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
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1310
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1245
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:522
WireState
Current state of the channel.
Definition: csma-channel.h:74
@ TRANSMITTING
Channel is BUSY, a packet is being written by a net device.
Definition: csma-channel.h:76
@ PROPAGATING
Channel is BUSY, packet is propagating to all attached net devices.
Definition: csma-channel.h:77
@ IDLE
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:75