00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2007 Emmanuelle Laprise 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Emmanuelle Laprise<emmanuelle.laprise@bluekazoo.ca> 00019 */ 00020 00021 #ifndef CSMA_CHANNEL_H 00022 #define CSMA_CHANNEL_H 00023 00024 #include "ns3/channel.h" 00025 #include "ns3/ptr.h" 00026 #include "ns3/nstime.h" 00027 #include "ns3/data-rate.h" 00028 00029 namespace ns3 { 00030 00031 class Packet; 00032 00033 class CsmaNetDevice; 00034 00035 /** 00036 * \brief CsmaNetDevice Record 00037 * 00038 * Stores the information related to each net device that is 00039 * connected to the channel. 00040 */ 00041 class CsmaDeviceRec { 00042 public: 00043 Ptr< CsmaNetDevice > devicePtr; /// Pointer to the net device 00044 bool active; /// Is net device enabled to TX/RX 00045 00046 CsmaDeviceRec(); 00047 CsmaDeviceRec(Ptr< CsmaNetDevice > device); 00048 00049 /** 00050 * \return If the net device pointed to by the devicePtr is active 00051 * and ready to RX/TX. 00052 */ 00053 bool IsActive(); 00054 }; 00055 00056 /** 00057 * Current state of the channel 00058 */ 00059 enum WireState 00060 { 00061 IDLE, /**< Channel is IDLE, no packet is being transmitted */ 00062 TRANSMITTING, /**< Channel is BUSY, a packet is being written by a net device */ 00063 PROPAGATING /**< Channel is BUSY, packet is propagating to all attached net devices */ 00064 }; 00065 00066 /** 00067 * \brief Csma Channel. 00068 * 00069 * This class represents a simple Csma channel that can be used 00070 * when many nodes are connected to one wire. It uses a single busy 00071 * flag to indicate if the channel is currently in use. It does not 00072 * take into account the distances between stations or the speed of 00073 * light to determine collisions. 00074 */ 00075 class CsmaChannel : public Channel 00076 { 00077 public: 00078 static TypeId GetTypeId (void); 00079 00080 /** 00081 * \brief Create a CsmaChannel 00082 */ 00083 CsmaChannel (); 00084 00085 /** 00086 * \brief Attach a given netdevice to this channel 00087 * 00088 * \param device Device pointer to the netdevice to attach to the channel 00089 * \return The assigned device number 00090 */ 00091 int32_t Attach (Ptr<CsmaNetDevice> device); 00092 00093 /** 00094 * \brief Detach a given netdevice from this channel 00095 * 00096 * The net device is marked as inactive and it is not allowed to 00097 * receive or transmit packets 00098 * 00099 * \param device Device pointer to the netdevice to detach from the channel 00100 * \return True if the device is found and attached to the channel, 00101 * false if the device is not currently connected to the channel or 00102 * can't be found. 00103 */ 00104 bool Detach (Ptr<CsmaNetDevice> device); 00105 00106 /** 00107 * \brief Detach a given netdevice from this channel 00108 * 00109 * The net device is marked as inactive and it is not allowed to 00110 * receive or transmit packets 00111 * 00112 * \param deviceId The deviceID assigned to the net device when it 00113 * was connected to the channel 00114 * \return True if the device is found and attached to the channel, 00115 * false if the device is not currently connected to the channel or 00116 * can't be found. 00117 */ 00118 bool Detach (uint32_t deviceId); 00119 00120 /** 00121 * \brief Reattach a previously detached net device to the channel 00122 * 00123 * The net device is marked as active. It is now allowed to receive 00124 * or transmit packets. The net device must have been previously 00125 * attached to the channel using the attach function. 00126 * 00127 * \param deviceId The device ID assigned to the net device when it 00128 * was connected to the channel 00129 * \return True if the device is found and is not attached to the 00130 * channel, false if the device is currently connected to the 00131 * channel or can't be found. 00132 */ 00133 bool Reattach(uint32_t deviceId); 00134 00135 /** 00136 * \brief Reattach a previously detached net device to the channel 00137 * 00138 * The net device is marked as active. It is now allowed to receive 00139 * or transmit packets. The net device must have been previously 00140 * attached to the channel using the attach function. 00141 * 00142 * \param device Device pointer to the netdevice to detach from the channel 00143 * \return True if the device is found and is not attached to the 00144 * channel, false if the device is currently connected to the 00145 * channel or can't be found. 00146 */ 00147 bool Reattach(Ptr<CsmaNetDevice> device); 00148 00149 /** 00150 * \brief Start transmitting a packet over the channel 00151 * 00152 * If the srcId belongs to a net device that is connected to the 00153 * channel, packet transmission begins, and the channel becomes busy 00154 * until the packet has completely reached all destinations. 00155 * 00156 * \param p A reference to the packet that will be transmitted over 00157 * the channel 00158 * \param srcId The device Id of the net device that wants to 00159 * transmit on the channel. 00160 * \return True if the channel is not busy and the transmitting net 00161 * device is currently active. 00162 */ 00163 bool TransmitStart (Ptr<Packet> p, uint32_t srcId); 00164 00165 /** 00166 * \brief Indicates that the net device has finished transmitting 00167 * the packet over the channel 00168 * 00169 * The channel will stay busy until the packet has completely 00170 * propagated to all net devices attached to the channel. The 00171 * TransmitEnd function schedules the PropagationCompleteEvent which 00172 * will free the channel for further transmissions. Stores the 00173 * packet p as the m_currentPkt, the packet being currently 00174 * transmitting. 00175 * 00176 * \return Returns true unless the source was detached before it 00177 * completed its transmission. 00178 */ 00179 bool TransmitEnd (); 00180 00181 /** 00182 * \brief Indicates that the channel has finished propagating the 00183 * current packet. The channel is released and becomes free. 00184 * 00185 * Calls the receive function of every active net device that is 00186 * attached to the channel. 00187 */ 00188 void PropagationCompleteEvent (); 00189 00190 /** 00191 * \return Returns the device number assigned to a net device by the 00192 * channel 00193 * 00194 * \param device Device pointer to the netdevice for which the device 00195 * number is needed 00196 */ 00197 int32_t GetDeviceNum (Ptr<CsmaNetDevice> device); 00198 00199 /** 00200 * \return Returns the state of the channel (IDLE -- free, 00201 * TRANSMITTING -- busy, PROPAGATING - busy ) 00202 */ 00203 WireState GetState (); 00204 00205 /** 00206 * \brief Indicates if the channel is busy. The channel will only 00207 * accept new packets for transmission if it is not busy. 00208 * 00209 * \return Returns true if the channel is busy and false if it is 00210 * free. 00211 */ 00212 bool IsBusy (); 00213 00214 /** 00215 * \brief Indicates if a net device is currently attached or 00216 * detached from the channel. 00217 * 00218 * \param deviceId The ID that was assigned to the net device when 00219 * it was attached to the channel. 00220 * \return Returns true if the net device is attached to the 00221 * channel, false otherwise. 00222 */ 00223 bool IsActive (uint32_t deviceId); 00224 00225 /** 00226 * \return Returns the number of net devices that are currently 00227 * attached to the channel. 00228 */ 00229 uint32_t GetNumActDevices (void); 00230 00231 /** 00232 * \return Returns the total number of devices including devices 00233 * that have been detached from the channel. 00234 */ 00235 virtual uint32_t GetNDevices (void) const; 00236 00237 /** 00238 * \return Get a NetDevice pointer to a connected network device. 00239 * 00240 * \param i The index of the net device. 00241 * \return Returns the pointer to the net device that is associated 00242 * with deviceId i. 00243 */ 00244 virtual Ptr<NetDevice> GetDevice (uint32_t i) const; 00245 00246 /** 00247 * \return Get a CsmaNetDevice pointer to a connected network device. 00248 * 00249 * \param i The deviceId of the net device for which we want the 00250 * pointer. 00251 * \return Returns the pointer to the net device that is associated 00252 * with deviceId i. 00253 */ 00254 Ptr<CsmaNetDevice> GetCsmaDevice (uint32_t i) const; 00255 00256 /** 00257 * Get the assigned data rate of the channel 00258 * 00259 * \return Returns the DataRate to be used by device transmitters. 00260 * with deviceId i. 00261 */ 00262 virtual DataRate GetDataRate (void); 00263 00264 /** 00265 * Get the assigned speed-of-light delay of the channel 00266 * 00267 * \return Returns the delay used by the channel. 00268 */ 00269 virtual Time GetDelay (void); 00270 00271 private: 00272 00273 /** 00274 * The assigned data rate of the channel 00275 */ 00276 DataRate m_bps; 00277 00278 /** 00279 * The assigned speed-of-light delay of the channel 00280 */ 00281 Time m_delay; 00282 00283 /** 00284 * List of the net devices that have been or are currently connected 00285 * to the channel. 00286 * 00287 * Devices are nor removed from this list, they are marked as 00288 * inactive. Otherwise the assigned device IDs will not refer to the 00289 * correct NetDevice. The DeviceIds are used so that it is possible 00290 * to have a number to refer to an entry in the list so that the 00291 * whole list does not have to be searched when making sure that a 00292 * source is attached to a channel when it is transmitting data. 00293 */ 00294 std::vector<CsmaDeviceRec> m_deviceList; 00295 00296 /** 00297 * The Packet that is currently being transmitted on the channel (or last 00298 * packet to have been transmitted on the channel if the channel is 00299 * free.) 00300 */ 00301 Ptr<Packet> m_currentPkt; 00302 00303 /** 00304 * Device Id of the source that is currently transmitting on the 00305 * channel. Or last source to have transmitted a packet on the 00306 * channel, if the channel is currently not busy. 00307 */ 00308 uint32_t m_currentSrc; 00309 00310 /** 00311 * Current state of the channel 00312 */ 00313 WireState m_state; 00314 }; 00315 00316 } // namespace ns3 00317 00318 #endif /* CSMA_CHANNEL_H */