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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
7 */
8
9#include "csma-channel.h"
10
11#include "csma-net-device.h"
12
13#include "ns3/log.h"
14#include "ns3/packet.h"
15#include "ns3/simulator.h"
16
17namespace ns3
18{
19
20NS_LOG_COMPONENT_DEFINE("CsmaChannel");
21
23
24TypeId
26{
27 static TypeId tid =
28 TypeId("ns3::CsmaChannel")
30 .SetGroupName("Csma")
31 .AddConstructor<CsmaChannel>()
32 .AddAttribute(
33 "DataRate",
34 "The transmission data rate to be provided to devices connected to the channel",
35 DataRateValue(DataRate(0xffffffff)),
38 .AddAttribute("Delay",
39 "Transmission delay through the channel",
43 return tid;
44}
45
53
59
62{
63 NS_LOG_FUNCTION(this << device);
64 NS_ASSERT(device);
65
66 CsmaDeviceRec rec(device);
67
68 m_deviceList.push_back(rec);
69 return (m_deviceList.size() - 1);
70}
71
72bool
74{
75 NS_LOG_FUNCTION(this << device);
76 NS_ASSERT(device);
77
78 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
79 {
80 if (it->devicePtr == device)
81 {
82 if (!it->active)
83 {
84 it->active = true;
85 return true;
86 }
87 else
88 {
89 return false;
90 }
91 }
92 }
93 return false;
94}
95
96bool
98{
99 NS_LOG_FUNCTION(this << deviceId);
100
101 if (deviceId < m_deviceList.size())
102 {
103 return false;
104 }
105
106 if (m_deviceList[deviceId].active)
107 {
108 return false;
109 }
110 else
111 {
112 m_deviceList[deviceId].active = true;
113 return true;
114 }
115}
116
117bool
119{
120 NS_LOG_FUNCTION(this << deviceId);
121
122 if (deviceId < m_deviceList.size())
123 {
124 if (!m_deviceList[deviceId].active)
125 {
126 NS_LOG_WARN("CsmaChannel::Detach(): Device is already detached (" << deviceId << ")");
127 return false;
128 }
129
130 m_deviceList[deviceId].active = false;
131
132 if ((m_state == TRANSMITTING) && (m_currentSrc == deviceId))
133 {
134 NS_LOG_WARN("CsmaChannel::Detach(): Device is currently"
135 << "transmitting (" << deviceId << ")");
136 }
137
138 return true;
139 }
140 else
141 {
142 return false;
143 }
144}
145
146bool
148{
149 NS_LOG_FUNCTION(this << device);
150 NS_ASSERT(device);
151
152 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
153 {
154 if ((it->devicePtr == device) && (it->active))
155 {
156 it->active = false;
157 return true;
158 }
159 }
160 return false;
161}
162
163bool
165{
166 NS_LOG_FUNCTION(this << p << srcId);
167 NS_LOG_INFO("UID is " << p->GetUid() << ")");
168
169 if (m_state != IDLE)
170 {
171 NS_LOG_WARN("CsmaChannel::TransmitStart(): State is not IDLE");
172 return false;
173 }
174
175 if (!IsActive(srcId))
176 {
178 "CsmaChannel::TransmitStart(): Seclected source is not currently attached to network");
179 return false;
180 }
181
182 NS_LOG_LOGIC("switch to TRANSMITTING");
183 m_currentPkt = p;
184 m_currentSrc = srcId;
186 return true;
187}
188
189bool
191{
192 return m_deviceList[deviceId].active;
193}
194
195bool
197{
199 NS_LOG_INFO("UID is " << m_currentPkt->GetUid() << ")");
200
203
204 bool retVal = true;
205
207 {
208 NS_LOG_ERROR("CsmaChannel::TransmitEnd(): Seclected source was detached before the end of "
209 "the transmission");
210 retVal = false;
211 }
212
213 NS_LOG_LOGIC("Schedule event in " << m_delay.As(Time::S));
214
215 NS_LOG_LOGIC("Receive");
216
217 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
218 {
219 if (it->IsActive() && it->devicePtr != m_deviceList[m_currentSrc].devicePtr)
220 {
221 // schedule reception events
222 Simulator::ScheduleWithContext(it->devicePtr->GetNode()->GetId(),
223 m_delay,
225 it->devicePtr,
227 m_deviceList[m_currentSrc].devicePtr);
228 }
229 }
230
231 // also schedule for the tx side to go back to IDLE
233 return retVal;
234}
235
236void
245
248{
249 int numActDevices = 0;
250 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
251 {
252 if (it->active)
253 {
254 numActDevices++;
255 }
256 }
257 return numActDevices;
258}
259
260std::size_t
262{
263 return m_deviceList.size();
264}
265
267CsmaChannel::GetCsmaDevice(std::size_t i) const
268{
269 return m_deviceList[i].devicePtr;
270}
271
274{
275 int i = 0;
276 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
277 {
278 if (it->devicePtr == device)
279 {
280 if (it->active)
281 {
282 return i;
283 }
284 else
285 {
286 return -2;
287 }
288 }
289 i++;
290 }
291 return -1;
292}
293
294bool
296{
297 return m_state != IDLE;
298}
299
302{
303 return m_bps;
304}
305
306Time
308{
309 return m_delay;
310}
311
314{
315 return m_state;
316}
317
319CsmaChannel::GetDevice(std::size_t i) const
320{
321 return GetCsmaDevice(i);
322}
323
325{
326 active = false;
327}
328
330{
331 devicePtr = device;
332 active = true;
333}
334
336{
337 devicePtr = deviceRec.devicePtr;
338 active = deviceRec.active;
339}
340
341bool
343{
344 return active;
345}
346
347} // namespace ns3
Abstract Channel Base Class.
Definition channel.h:34
Csma Channel.
~CsmaChannel() override
Destroy a CsmaChannel.
Ptr< CsmaNetDevice > GetCsmaDevice(std::size_t i) const
uint32_t GetNumActDevices()
DataRate m_bps
The assigned data rate of the channel.
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.
Ptr< const Packet > m_currentPkt
The Packet that is currently being transmitted on the channel (or last packet to have been transmitte...
bool TransmitStart(Ptr< const Packet > p, uint32_t srcId)
Start transmitting a packet over the channel.
static TypeId GetTypeId()
Get the type ID.
WireState m_state
Current state of the channel.
CsmaChannel()
Create a CsmaChannel.
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.
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.
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.
std::size_t GetNDevices() const override
CsmaNetDevice Record.
bool IsActive() const
Ptr< CsmaNetDevice > devicePtr
Pointer to the net device.
bool active
Is net device enabled to TX/RX.
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:78
uint64_t GetUid() const
Returns the packet's Uid.
Definition packet.cc:401
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
@ S
second
Definition nstime.h:105
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:243
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#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:250
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeDataRateAccessor(T1 a1)
Definition data-rate.h:285
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition nstime.h:1396
Ptr< const AttributeChecker > MakeDataRateChecker()
Definition data-rate.cc:20
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1416
WireState
Current state of the channel.
@ TRANSMITTING
Channel is BUSY, a packet is being written by a net device.
@ PROPAGATING
Channel is BUSY, packet is propagating to all attached net devices.
@ IDLE
Channel is IDLE, no packet is being transmitted.