A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-channel.h
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#ifndef CSMA_CHANNEL_H
10#define CSMA_CHANNEL_H
11
12#include "ns3/channel.h"
13#include "ns3/data-rate.h"
14#include "ns3/nstime.h"
15#include "ns3/ptr.h"
16
17namespace ns3
18{
19
20class Packet;
21
22class CsmaNetDevice;
23
24/**
25 * \ingroup csma
26 * \brief CsmaNetDevice Record
27 *
28 * Stores the information related to each net device that is
29 * connected to the channel.
30 */
32{
33 public:
34 Ptr<CsmaNetDevice> devicePtr; //!< Pointer to the net device
35 bool active; //!< Is net device enabled to TX/RX
36
38
39 /**
40 * \brief Constructor
41 * Builds a record of the given NetDevice, its status is initialized to enabled.
42 *
43 * \param device the device to record
44 */
46
47 /**
48 * Copy constructor
49 * \param o the object to copy
50 */
52
53 /**
54 * \return If the net device pointed to by the devicePtr is active
55 * and ready to RX/TX.
56 */
57 bool IsActive() const;
58};
59
60/**
61 * Current state of the channel
62 */
64{
65 IDLE, /**< Channel is IDLE, no packet is being transmitted */
66 TRANSMITTING, /**< Channel is BUSY, a packet is being written by a net device */
67 PROPAGATING /**< Channel is BUSY, packet is propagating to all attached net devices */
68};
69
70/**
71 * \ingroup csma
72 * \brief Csma Channel.
73 *
74 * This class represents a simple Csma channel that can be used
75 * when many nodes are connected to one wire. It uses a single busy
76 * flag to indicate if the channel is currently in use. It does not
77 * take into account the distances between stations or the speed of
78 * light to determine collisions.
79 */
80class CsmaChannel : public Channel
81{
82 public:
83 /**
84 * \brief Get the type ID.
85 * \return the object TypeId
86 */
87 static TypeId GetTypeId();
88
89 /**
90 * \brief Create a CsmaChannel
91 */
93 /**
94 * \brief Destroy a CsmaChannel
95 */
96 ~CsmaChannel() override;
97
98 // Delete copy constructor and assignment operator to avoid misuse
99 CsmaChannel(const CsmaChannel&) = delete;
101
102 /**
103 * \brief Attach a given netdevice to this channel
104 *
105 * \param device Device pointer to the netdevice to attach to the channel
106 * \return The assigned device number
107 */
109
110 /**
111 * \brief Detach a given netdevice from this channel
112 *
113 * The net device is marked as inactive and it is not allowed to
114 * receive or transmit packets
115 *
116 * \param device Device pointer to the netdevice to detach from the channel
117 * \return True if the device is found and attached to the channel,
118 * false if the device is not currently connected to the channel or
119 * can't be found.
120 */
121 bool Detach(Ptr<CsmaNetDevice> device);
122
123 /**
124 * \brief Detach a given netdevice from this channel
125 *
126 * The net device is marked as inactive and it is not allowed to
127 * receive or transmit packets
128 *
129 * \param deviceId The deviceID assigned to the net device when it
130 * was connected to the channel
131 * \return True if the device is found and attached to the channel,
132 * false if the device is not currently connected to the channel or
133 * can't be found.
134 */
135 bool Detach(uint32_t deviceId);
136
137 /**
138 * \brief Reattach a previously detached net device to the channel
139 *
140 * The net device is marked as active. It is now allowed to receive
141 * or transmit packets. The net device must have been previously
142 * attached to the channel using the attach function.
143 *
144 * \param deviceId The device ID assigned to the net device when it
145 * was connected to the channel
146 * \return True if the device is found and is not attached to the
147 * channel, false if the device is currently connected to the
148 * channel or can't be found.
149 */
150 bool Reattach(uint32_t deviceId);
151
152 /**
153 * \brief Reattach a previously detached net device to the channel
154 *
155 * The net device is marked as active. It is now allowed to receive
156 * or transmit packets. The net device must have been previously
157 * attached to the channel using the attach function.
158 *
159 * \param device Device pointer to the netdevice to detach from the channel
160 * \return True if the device is found and is not attached to the
161 * channel, false if the device is currently connected to the
162 * channel or can't be found.
163 */
164 bool Reattach(Ptr<CsmaNetDevice> device);
165
166 /**
167 * \brief Start transmitting a packet over the channel
168 *
169 * If the srcId belongs to a net device that is connected to the
170 * channel, packet transmission begins, and the channel becomes busy
171 * until the packet has completely reached all destinations.
172 *
173 * \param p A reference to the packet that will be transmitted over
174 * the channel
175 * \param srcId The device Id of the net device that wants to
176 * transmit on the channel.
177 * \return True if the channel is not busy and the transmitting net
178 * device is currently active.
179 */
181
182 /**
183 * \brief Indicates that the net device has finished transmitting
184 * the packet over the channel
185 *
186 * The channel will stay busy until the packet has completely
187 * propagated to all net devices attached to the channel. The
188 * TransmitEnd function schedules the PropagationCompleteEvent which
189 * will free the channel for further transmissions. Stores the
190 * packet p as the m_currentPkt, the packet being currently
191 * transmitting.
192 *
193 * \return Returns true unless the source was detached before it
194 * completed its transmission.
195 */
196 bool TransmitEnd();
197
198 /**
199 * \brief Indicates that the channel has finished propagating the
200 * current packet. The channel is released and becomes free.
201 *
202 * Calls the receive function of every active net device that is
203 * attached to the channel.
204 */
206
207 /**
208 * \return Returns the device number assigned to a net device by the
209 * channel
210 *
211 * \param device Device pointer to the netdevice for which the device
212 * number is needed
213 */
215
216 /**
217 * \return Returns the state of the channel (IDLE -- free,
218 * TRANSMITTING -- busy, PROPAGATING - busy )
219 */
221
222 /**
223 * \brief Indicates if the channel is busy. The channel will only
224 * accept new packets for transmission if it is not busy.
225 *
226 * \return Returns true if the channel is busy and false if it is
227 * free.
228 */
229 bool IsBusy();
230
231 /**
232 * \brief Indicates if a net device is currently attached or
233 * detached from the channel.
234 *
235 * \param deviceId The ID that was assigned to the net device when
236 * it was attached to the channel.
237 * \return Returns true if the net device is attached to the
238 * channel, false otherwise.
239 */
240 bool IsActive(uint32_t deviceId);
241
242 /**
243 * \return Returns the number of net devices that are currently
244 * attached to the channel.
245 */
247
248 /**
249 * \return Returns the total number of devices including devices
250 * that have been detached from the channel.
251 */
252 std::size_t GetNDevices() const override;
253
254 /**
255 * \return Get a NetDevice pointer to a connected network device.
256 *
257 * \param i The index of the net device.
258 * \return Returns the pointer to the net device that is associated
259 * with deviceId i.
260 */
261 Ptr<NetDevice> GetDevice(std::size_t i) const override;
262
263 /**
264 * \return Get a CsmaNetDevice pointer to a connected network device.
265 *
266 * \param i The deviceId of the net device for which we want the
267 * pointer.
268 * \return Returns the pointer to the net device that is associated
269 * with deviceId i.
270 */
271 Ptr<CsmaNetDevice> GetCsmaDevice(std::size_t i) const;
272
273 /**
274 * Get the assigned data rate of the channel
275 *
276 * \return Returns the DataRate to be used by device transmitters.
277 * with deviceId i.
278 */
280
281 /**
282 * Get the assigned speed-of-light delay of the channel
283 *
284 * \return Returns the delay used by the channel.
285 */
286 Time GetDelay();
287
288 private:
289 /**
290 * The assigned data rate of the channel
291 */
293
294 /**
295 * The assigned speed-of-light delay of the channel
296 */
298
299 /**
300 * List of the net devices that have been or are currently connected
301 * to the channel.
302 *
303 * Devices are nor removed from this list, they are marked as
304 * inactive. Otherwise the assigned device IDs will not refer to the
305 * correct NetDevice. The DeviceIds are used so that it is possible
306 * to have a number to refer to an entry in the list so that the
307 * whole list does not have to be searched when making sure that a
308 * source is attached to a channel when it is transmitting data.
309 */
310 std::vector<CsmaDeviceRec> m_deviceList;
311
312 /**
313 * The Packet that is currently being transmitted on the channel (or last
314 * packet to have been transmitted on the channel if the channel is
315 * free.)
316 */
318
319 /**
320 * Device Id of the source that is currently transmitting on the
321 * channel. Or last source to have transmitted a packet on the
322 * channel, if the channel is currently not busy.
323 */
325
326 /**
327 * Current state of the channel
328 */
330};
331
332} // namespace ns3
333
334#endif /* CSMA_CHANNEL_H */
Abstract Channel Base Class.
Definition channel.h:34
Csma Channel.
CsmaChannel(const CsmaChannel &)=delete
~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 & operator=(const CsmaChannel &)=delete
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.
Class for representing data rates.
Definition data-rate.h:78
Smart pointer class similar to boost::intrusive_ptr.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
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.