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 return false;
125 }
126
127 if (!m_deviceList[deviceId].active)
128 {
129 NS_LOG_WARN("CsmaChannel::Detach(): Device is already detached (" << deviceId << ")");
130 return false;
131 }
132
133 m_deviceList[deviceId].active = false;
134
135 if ((m_state == TRANSMITTING) && (m_currentSrc == deviceId))
136 {
137 NS_LOG_WARN("CsmaChannel::Detach(): Device is currently transmitting (" << deviceId << ")");
138 }
139
140 return true;
141}
142
143bool
145{
146 NS_LOG_FUNCTION(this << device);
147 NS_ASSERT(device);
148
149 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
150 {
151 if ((it->devicePtr == device) && (it->active))
152 {
153 it->active = false;
154 return true;
155 }
156 }
157 return false;
158}
159
160bool
162{
163 NS_LOG_FUNCTION(this << p << srcId);
164 NS_LOG_INFO("UID is " << p->GetUid() << ")");
165
166 if (m_state != IDLE)
167 {
168 NS_LOG_WARN("CsmaChannel::TransmitStart(): State is not IDLE");
169 return false;
170 }
171
172 if (!IsActive(srcId))
173 {
175 "CsmaChannel::TransmitStart(): Seclected source is not currently attached to network");
176 return false;
177 }
178
179 NS_LOG_LOGIC("switch to TRANSMITTING");
180 m_currentPkt = p;
181 m_currentSrc = srcId;
183 return true;
184}
185
186bool
188{
189 return m_deviceList[deviceId].active;
190}
191
192bool
194{
196 NS_LOG_INFO("UID is " << m_currentPkt->GetUid() << ")");
197
200
201 bool retVal = true;
202
204 {
205 NS_LOG_ERROR("CsmaChannel::TransmitEnd(): Seclected source was detached before the end of "
206 "the transmission");
207 retVal = false;
208 }
209
210 NS_LOG_LOGIC("Schedule event in " << m_delay.As(Time::S));
211
212 NS_LOG_LOGIC("Receive");
213
214 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
215 {
216 if (it->IsActive() && it->devicePtr != m_deviceList[m_currentSrc].devicePtr)
217 {
218 // schedule reception events
219 Simulator::ScheduleWithContext(it->devicePtr->GetNode()->GetId(),
220 m_delay,
222 it->devicePtr,
224 m_deviceList[m_currentSrc].devicePtr);
225 }
226 }
227
228 // also schedule for the tx side to go back to IDLE
230 return retVal;
231}
232
233void
242
245{
246 int numActDevices = 0;
247 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
248 {
249 if (it->active)
250 {
251 numActDevices++;
252 }
253 }
254 return numActDevices;
255}
256
257std::size_t
259{
260 return m_deviceList.size();
261}
262
264CsmaChannel::GetCsmaDevice(std::size_t i) const
265{
266 return m_deviceList[i].devicePtr;
267}
268
271{
272 int i = 0;
273 for (auto it = m_deviceList.begin(); it < m_deviceList.end(); it++)
274 {
275 if (it->devicePtr == device)
276 {
277 if (it->active)
278 {
279 return i;
280 }
281 else
282 {
283 return -2;
284 }
285 }
286 i++;
287 }
288 return -1;
289}
290
291bool
293{
294 return m_state != IDLE;
295}
296
299{
300 return m_bps;
301}
302
303Time
305{
306 return m_delay;
307}
308
311{
312 return m_state;
313}
314
316CsmaChannel::GetDevice(std::size_t i) const
317{
318 return GetCsmaDevice(i);
319}
320
322{
323 active = false;
324}
325
327{
328 devicePtr = device;
329 active = true;
330}
331
333{
334 devicePtr = deviceRec.devicePtr;
335 active = deviceRec.active;
336}
337
338bool
340{
341 return active;
342}
343
344} // 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
AttributeValue implementation for DataRate.
Definition data-rate.h:285
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:561
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:578
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:403
@ S
second
Definition nstime.h:105
AttributeValue implementation for Time.
Definition nstime.h:1432
a unique identifier for an interface.
Definition type-id.h:49
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
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:285
Ptr< const AttributeChecker > MakeDataRateChecker()
Definition data-rate.cc:20
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:1433
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1453
#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:1345
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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.