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 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,
236 }
237 devId++;
238 }
239
240 // also schedule for the tx side to go back to IDLE
242 this);
243 return retVal;
244}
245
246void
248{
250 NS_LOG_INFO ("UID is " << m_currentPkt->GetUid () << ")");
251
253 m_state = IDLE;
254}
255
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
271std::size_t
273{
274 return m_deviceList.size ();
275}
276
278CsmaChannel::GetCsmaDevice (std::size_t i) const
279{
280 return m_deviceList[i].devicePtr;
281}
282
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
306bool
308{
309 if (m_state == IDLE)
310 {
311 return false;
312 }
313 else
314 {
315 return true;
316 }
317}
318
321{
322 return m_bps;
323}
324
325Time
327{
328 return m_delay;
329}
330
333{
334 return m_state;
335}
336
338CsmaChannel::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
360bool
362{
363 return active;
364}
365
366} // 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:314
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:319
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:351
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
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:332
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:346
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:556
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
@ S
second
Definition: nstime.h:114
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:432
AttributeValue implementation for Time.
Definition: nstime.h:1308
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:1309
#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:1244
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:536
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