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)),
47 MakeDataRateAccessor(&CsmaChannel::m_bps),
48 MakeDataRateChecker())
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 std::vector<CsmaDeviceRec>::iterator it;
90 for (it = m_deviceList.begin(); it < m_deviceList.end(); it++)
91 {
92 if (it->devicePtr == device)
93 {
94 if (!it->active)
95 {
96 it->active = true;
97 return true;
98 }
99 else
100 {
101 return false;
102 }
103 }
104 }
105 return false;
106}
107
108bool
110{
111 NS_LOG_FUNCTION(this << deviceId);
112
113 if (deviceId < m_deviceList.size())
114 {
115 return false;
116 }
117
118 if (m_deviceList[deviceId].active)
119 {
120 return false;
121 }
122 else
123 {
124 m_deviceList[deviceId].active = true;
125 return true;
126 }
127}
128
129bool
131{
132 NS_LOG_FUNCTION(this << deviceId);
133
134 if (deviceId < m_deviceList.size())
135 {
136 if (!m_deviceList[deviceId].active)
137 {
138 NS_LOG_WARN("CsmaChannel::Detach(): Device is already detached (" << deviceId << ")");
139 return false;
140 }
141
142 m_deviceList[deviceId].active = false;
143
144 if ((m_state == TRANSMITTING) && (m_currentSrc == deviceId))
145 {
146 NS_LOG_WARN("CsmaChannel::Detach(): Device is currently"
147 << "transmitting (" << deviceId << ")");
148 }
149
150 return true;
151 }
152 else
153 {
154 return false;
155 }
156}
157
158bool
160{
161 NS_LOG_FUNCTION(this << device);
162 NS_ASSERT(device);
163
164 std::vector<CsmaDeviceRec>::iterator it;
165 for (it = m_deviceList.begin(); it < m_deviceList.end(); it++)
166 {
167 if ((it->devicePtr == device) && (it->active))
168 {
169 it->active = false;
170 return true;
171 }
172 }
173 return false;
174}
175
176bool
178{
179 NS_LOG_FUNCTION(this << p << srcId);
180 NS_LOG_INFO("UID is " << p->GetUid() << ")");
181
182 if (m_state != IDLE)
183 {
184 NS_LOG_WARN("CsmaChannel::TransmitStart(): State is not IDLE");
185 return false;
186 }
187
188 if (!IsActive(srcId))
189 {
191 "CsmaChannel::TransmitStart(): Seclected source is not currently attached to network");
192 return false;
193 }
194
195 NS_LOG_LOGIC("switch to TRANSMITTING");
196 m_currentPkt = p->Copy();
197 m_currentSrc = srcId;
199 return true;
200}
201
202bool
204{
205 return (m_deviceList[deviceId].active);
206}
207
208bool
210{
212 NS_LOG_INFO("UID is " << m_currentPkt->GetUid() << ")");
213
216
217 bool retVal = true;
218
220 {
221 NS_LOG_ERROR("CsmaChannel::TransmitEnd(): Seclected source was detached before the end of "
222 "the transmission");
223 retVal = false;
224 }
225
226 NS_LOG_LOGIC("Schedule event in " << m_delay.As(Time::S));
227
228 NS_LOG_LOGIC("Receive");
229
230 std::vector<CsmaDeviceRec>::iterator it;
231 for (it = m_deviceList.begin(); it < m_deviceList.end(); it++)
232 {
233 if (it->IsActive() && it->devicePtr != m_deviceList[m_currentSrc].devicePtr)
234 {
235 // schedule reception events
236 Simulator::ScheduleWithContext(it->devicePtr->GetNode()->GetId(),
237 m_delay,
239 it->devicePtr,
241 m_deviceList[m_currentSrc].devicePtr);
242 }
243 }
244
245 // also schedule for the tx side to go back to IDLE
247 return retVal;
248}
249
250void
252{
254 NS_LOG_INFO("UID is " << m_currentPkt->GetUid() << ")");
255
257 m_state = IDLE;
258}
259
262{
263 int numActDevices = 0;
264 std::vector<CsmaDeviceRec>::iterator it;
265 for (it = m_deviceList.begin(); it < m_deviceList.end(); it++)
266 {
267 if (it->active)
268 {
269 numActDevices++;
270 }
271 }
272 return numActDevices;
273}
274
275std::size_t
277{
278 return m_deviceList.size();
279}
280
282CsmaChannel::GetCsmaDevice(std::size_t i) const
283{
284 return m_deviceList[i].devicePtr;
285}
286
289{
290 std::vector<CsmaDeviceRec>::iterator it;
291 int i = 0;
292 for (it = m_deviceList.begin(); it < m_deviceList.end(); it++)
293 {
294 if (it->devicePtr == device)
295 {
296 if (it->active)
297 {
298 return i;
299 }
300 else
301 {
302 return -2;
303 }
304 }
305 i++;
306 }
307 return -1;
308}
309
310bool
312{
313 return m_state != IDLE;
314}
315
318{
319 return m_bps;
320}
321
322Time
324{
325 return m_delay;
326}
327
330{
331 return m_state;
332}
333
335CsmaChannel::GetDevice(std::size_t i) const
336{
337 return GetCsmaDevice(i);
338}
339
341{
342 active = false;
343}
344
346{
347 devicePtr = device;
348 active = true;
349}
350
352{
353 devicePtr = deviceRec.devicePtr;
354 active = deviceRec.active;
355}
356
357bool
359{
360 return active;
361}
362
363} // 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
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
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: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< 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.
Ptr< Packet > Copy() const
performs a COW copy of the packet.
Definition: packet.cc:131
uint64_t GetUid() const
Returns the packet's Uid.
Definition: packet.cc:412
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:587
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:417
@ S
second
Definition: nstime.h:116
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:936
#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 AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1444
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1424
#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:1336
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