A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-channel.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 Emmanuelle Laprise
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
18 */
19
20#include "csma-channel.h"
21
22#include "csma-net-device.h"
23
24#include "ns3/log.h"
25#include "ns3/packet.h"
26#include "ns3/simulator.h"
27
28namespace ns3
29{
30
31NS_LOG_COMPONENT_DEFINE("CsmaChannel");
32
34
35TypeId
37{
38 static TypeId tid =
39 TypeId("ns3::CsmaChannel")
41 .SetGroupName("Csma")
42 .AddConstructor<CsmaChannel>()
43 .AddAttribute(
44 "DataRate",
45 "The transmission data rate to be provided to devices connected to the channel",
46 DataRateValue(DataRate(0xffffffff)),
49 .AddAttribute("Delay",
50 "Transmission delay through the channel",
54 return tid;
55}
56
58 : Channel()
59{
61 m_state = IDLE;
62 m_deviceList.clear();
63}
64
66{
67 NS_LOG_FUNCTION(this);
68 m_deviceList.clear();
69}
70
73{
74 NS_LOG_FUNCTION(this << device);
75 NS_ASSERT(device);
76
77 CsmaDeviceRec rec(device);
78
79 m_deviceList.push_back(rec);
80 return (m_deviceList.size() - 1);
81}
82
83bool
85{
86 NS_LOG_FUNCTION(this << device);
87 NS_ASSERT(device);
88
89 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
90 {
91 if (it->devicePtr == device)
92 {
93 if (!it->active)
94 {
95 it->active = true;
96 return true;
97 }
98 else
99 {
100 return false;
101 }
102 }
103 }
104 return false;
105}
106
107bool
109{
110 NS_LOG_FUNCTION(this << deviceId);
111
112 if (deviceId < m_deviceList.size())
113 {
114 return false;
115 }
116
117 if (m_deviceList[deviceId].active)
118 {
119 return false;
120 }
121 else
122 {
123 m_deviceList[deviceId].active = true;
124 return true;
125 }
126}
127
128bool
130{
131 NS_LOG_FUNCTION(this << deviceId);
132
133 if (deviceId < m_deviceList.size())
134 {
135 if (!m_deviceList[deviceId].active)
136 {
137 NS_LOG_WARN("CsmaChannel::Detach(): Device is already detached (" << deviceId << ")");
138 return false;
139 }
140
141 m_deviceList[deviceId].active = false;
142
143 if ((m_state == TRANSMITTING) && (m_currentSrc == deviceId))
144 {
145 NS_LOG_WARN("CsmaChannel::Detach(): Device is currently"
146 << "transmitting (" << deviceId << ")");
147 }
148
149 return true;
150 }
151 else
152 {
153 return false;
154 }
155}
156
157bool
159{
160 NS_LOG_FUNCTION(this << device);
161 NS_ASSERT(device);
162
163 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
164 {
165 if ((it->devicePtr == device) && (it->active))
166 {
167 it->active = false;
168 return true;
169 }
170 }
171 return false;
172}
173
174bool
176{
177 NS_LOG_FUNCTION(this << p << srcId);
178 NS_LOG_INFO("UID is " << p->GetUid() << ")");
179
180 if (m_state != IDLE)
181 {
182 NS_LOG_WARN("CsmaChannel::TransmitStart(): State is not IDLE");
183 return false;
184 }
185
186 if (!IsActive(srcId))
187 {
189 "CsmaChannel::TransmitStart(): Seclected source is not currently attached to network");
190 return false;
191 }
192
193 NS_LOG_LOGIC("switch to TRANSMITTING");
194 m_currentPkt = p;
195 m_currentSrc = srcId;
197 return true;
198}
199
200bool
202{
203 return m_deviceList[deviceId].active;
204}
205
206bool
208{
210 NS_LOG_INFO("UID is " << m_currentPkt->GetUid() << ")");
211
214
215 bool retVal = true;
216
218 {
219 NS_LOG_ERROR("CsmaChannel::TransmitEnd(): Seclected source was detached before the end of "
220 "the transmission");
221 retVal = false;
222 }
223
224 NS_LOG_LOGIC("Schedule event in " << m_delay.As(Time::S));
225
226 NS_LOG_LOGIC("Receive");
227
228 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
229 {
230 if (it->IsActive() && it->devicePtr != m_deviceList[m_currentSrc].devicePtr)
231 {
232 // schedule reception events
233 Simulator::ScheduleWithContext(it->devicePtr->GetNode()->GetId(),
234 m_delay,
236 it->devicePtr,
238 m_deviceList[m_currentSrc].devicePtr);
239 }
240 }
241
242 // also schedule for the tx side to go back to IDLE
244 return retVal;
245}
246
247void
249{
251 NS_LOG_INFO("UID is " << m_currentPkt->GetUid() << ")");
252
254 m_state = IDLE;
255}
256
259{
260 int numActDevices = 0;
261 for (auto 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 int i = 0;
287 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
288 {
289 if (it->devicePtr == device)
290 {
291 if (it->active)
292 {
293 return i;
294 }
295 else
296 {
297 return -2;
298 }
299 }
300 i++;
301 }
302 return -1;
303}
304
305bool
307{
308 return m_state != IDLE;
309}
310
313{
314 return m_bps;
315}
316
317Time
319{
320 return m_delay;
321}
322
325{
326 return m_state;
327}
328
330CsmaChannel::GetDevice(std::size_t i) const
331{
332 return GetCsmaDevice(i);
333}
334
336{
337 active = false;
338}
339
341{
342 devicePtr = device;
343 active = true;
344}
345
347{
348 devicePtr = deviceRec.devicePtr;
349 active = deviceRec.active;
350}
351
352bool
354{
355 return active;
356}
357
358} // namespace ns3
Abstract Channel Base Class.
Definition: channel.h:45
Csma Channel.
Definition: csma-channel.h:92
~CsmaChannel() override
Destroy a CsmaChannel.
Definition: csma-channel.cc:65
Ptr< CsmaNetDevice > GetCsmaDevice(std::size_t i) const
uint32_t GetNumActDevices()
DataRate m_bps
The assigned data rate of the channel.
Definition: csma-channel.h:303
DataRate GetDataRate()
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.
Time GetDelay()
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
Ptr< const 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
bool TransmitStart(Ptr< const Packet > p, uint32_t srcId)
Start transmitting a packet over the channel.
static TypeId GetTypeId()
Get the type ID.
Definition: csma-channel.cc:36
WireState m_state
Current state of the channel.
Definition: csma-channel.h:340
CsmaChannel()
Create a CsmaChannel.
Definition: csma-channel.cc:57
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.
int32_t Attach(Ptr< CsmaNetDevice > device)
Attach a given netdevice to this channel.
Definition: csma-channel.cc:72
Ptr< NetDevice > GetDevice(std::size_t i) const override
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
std::size_t GetNDevices() const override
CsmaNetDevice Record.
Definition: csma-channel.h:43
bool IsActive() const
Ptr< CsmaNetDevice > devicePtr
Pointer to the net device.
Definition: csma-channel.h:45
bool active
Is net device enabled to TX/RX.
Definition: csma-channel.h:46
void Receive(Ptr< const 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.
Definition: data-rate.h:296
uint64_t GetUid() const
Returns the packet's Uid.
Definition: packet.cc:412
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ S
second
Definition: nstime.h:116
AttributeValue implementation for Time.
Definition: nstime.h:1413
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Ptr< const AttributeAccessor > MakeDataRateAccessor(T1 a1)
Definition: data-rate.h:296
Ptr< const AttributeChecker > MakeDataRateChecker()
Definition: data-rate.cc:31
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1434
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#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:261
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
WireState
Current state of the channel.
Definition: csma-channel.h:75
@ TRANSMITTING
Channel is BUSY, a packet is being written by a net device.
Definition: csma-channel.h:77
@ PROPAGATING
Channel is BUSY, packet is propagating to all attached net devices.
Definition: csma-channel.h:78
@ IDLE
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:76