src/devices/csma-cd/csma-cd-channel.h
changeset 1376 ad7e61edfb9d
parent 1375 4f45bec005b8
parent 1299 394c16278532
child 1377 2fcc78ee9558
equal deleted inserted replaced
1375:4f45bec005b8 1376:ad7e61edfb9d
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2007 Emmanuelle Laprise
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License version 2 as
       
     7  * published by the Free Software Foundation;
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    17  *
       
    18  * Author: Emmanuelle Laprise<emmanuelle.laprise@bluekazoo.ca>
       
    19  */
       
    20 
       
    21 #ifndef CSMA_CD_CHANNEL_H
       
    22 #define CSMA_CD_CHANNEL_H
       
    23 
       
    24 #include "ns3/channel.h"
       
    25 #include "ns3/ptr.h"
       
    26 #include "ns3/packet.h"
       
    27 #include "ns3/nstime.h"
       
    28 #include "ns3/data-rate.h"
       
    29 
       
    30 namespace ns3 {
       
    31 
       
    32 class CsmaCdNetDevice;
       
    33 
       
    34   /**
       
    35    * \brief CsmaCdNetDevice Record 
       
    36    *
       
    37    * Stores the information related to each net device that is
       
    38    * connected to the channel. 
       
    39    */
       
    40   class CsmaCdDeviceRec {
       
    41   public:
       
    42     Ptr< CsmaCdNetDevice > devicePtr; /// Pointer to the net device
       
    43     bool                       active;    /// Is net device enabled to TX/RX
       
    44 
       
    45     CsmaCdDeviceRec();
       
    46     CsmaCdDeviceRec(Ptr< CsmaCdNetDevice > device);
       
    47     /*
       
    48      * \return If the net device pointed to by the devicePtr is active
       
    49      * and ready to RX/TX.
       
    50      */
       
    51     bool IsActive();                   
       
    52   };
       
    53 
       
    54   /**
       
    55    * Current state of the channel
       
    56    */ 
       
    57   enum WireState
       
    58     {
       
    59       IDLE,          /**< Channel is IDLE, no packet is being
       
    60                         transmitted */
       
    61       TRANSMITTING,  /**< Channel is BUSY, a packet is being written
       
    62                         by a net device */
       
    63       PROPAGATING    /**< Channel is BUSY, packet is propagating to
       
    64                         all attached net devices */
       
    65     };
       
    66 
       
    67 /**
       
    68  * \brief CsmaCd Channel.
       
    69  *
       
    70  * This class represents a simple Csma/Cd channel that can be used
       
    71  * when many nodes are connected to one wire. It uses a single busy
       
    72  * flag to indicate if the channel is currently in use. It does not
       
    73  * take into account the distances between stations or the speed of
       
    74  * light to determine collisions.
       
    75  *
       
    76  * Each net device must query the state of the channel and make sure
       
    77  * that it is IDLE before writing a packet to the channel.
       
    78  *
       
    79  * When the channel is instaniated, the constructor takes parameters
       
    80  * for a single speed, in bits per second, and a speed-of-light delay
       
    81  * time as a Time object.  When a net device is attached to a channel,
       
    82  * it is assigned a device ID, this is in order to facilitate the
       
    83  * check that makes sure that a net device that is trying to send a
       
    84  * packet to the channel is really connected to this channel
       
    85  *
       
    86  */
       
    87 class CsmaCdChannel : public Channel {
       
    88 public:
       
    89   /**
       
    90    * \brief Create a CsmaCdChannel
       
    91    *
       
    92    * By default, you get a channel with the name "CsmaCd Channel" that
       
    93    * has an "infitely" fast transmission speed and zero delay.
       
    94    */
       
    95   CsmaCdChannel ();
       
    96   
       
    97   /**
       
    98    * \brief Create a CsmaCdChannel
       
    99    *
       
   100    * \param bps The bitrate of the channel
       
   101    * \param delay Transmission delay through the channel
       
   102    */  
       
   103   CsmaCdChannel (const DataRate& bps, const Time& delay);
       
   104   
       
   105   /**
       
   106    * \brief Create a CsmaCdChannel
       
   107    *
       
   108    * \param name the name of the channel for identification purposes
       
   109    * \param bps The bitrate of the channel
       
   110    * \param delay Transmission delay through the channel
       
   111    */
       
   112   CsmaCdChannel (const std::string& name,
       
   113                      const DataRate& bps, const Time& delay);
       
   114 
       
   115   /**
       
   116    * \brief Attach a given netdevice to this channel
       
   117    *
       
   118    * \param device Device pointer to the netdevice to attach to the channel
       
   119    * \return The assigned device number
       
   120    */
       
   121   int32_t Attach (Ptr<CsmaCdNetDevice> device);
       
   122   /**
       
   123    * \brief Detach a given netdevice from this channel
       
   124    *
       
   125    * The net device is marked as inactive and it is not allowed to
       
   126    * receive or transmit packets
       
   127    *
       
   128    * \param device Device pointer to the netdevice to detach from the channel
       
   129    * \return True if the device is found and attached to the channel,
       
   130    * false if the device is not currently connected to the channel or
       
   131    * can't be found.
       
   132    */
       
   133   bool Detach (Ptr<CsmaCdNetDevice> device);
       
   134   /**
       
   135    * \brief Detach a given netdevice from this channel
       
   136    *
       
   137    * The net device is marked as inactive and it is not allowed to
       
   138    * receive or transmit packets
       
   139    *
       
   140    * \param deviceId The deviceID assigned to the net device when it
       
   141    * was connected to the channel
       
   142    * \return True if the device is found and attached to the channel,
       
   143    * false if the device is not currently connected to the channel or
       
   144    * can't be found.
       
   145    */
       
   146   bool Detach (uint32_t deviceId);
       
   147   /**
       
   148    * \brief Reattach a previously detached net device to the channel
       
   149    *
       
   150    * The net device is marked as active. It is now allowed to receive
       
   151    * or transmit packets. The net device must have been previously
       
   152    * attached to the channel using the attach function.
       
   153    *
       
   154    * \param deviceId The device ID assigned to the net device when it
       
   155    * was connected to the channel
       
   156    * \return True if the device is found and is not attached to the
       
   157    * channel, false if the device is currently connected to the
       
   158    * channel or can't be found.
       
   159    */
       
   160   bool Reattach(uint32_t deviceId);
       
   161   /**
       
   162    * \brief Reattach a previously detached net device to the channel
       
   163    *
       
   164    * The net device is marked as active. It is now allowed to receive
       
   165    * or transmit packets. The net device must have been previously
       
   166    * attached to the channel using the attach function.
       
   167    *
       
   168    * \param device Device pointer to the netdevice to detach from the channel
       
   169    * \return True if the device is found and is not attached to the
       
   170    * channel, false if the device is currently connected to the
       
   171    * channel or can't be found.
       
   172    */
       
   173   bool Reattach(Ptr<CsmaCdNetDevice> device);
       
   174   /**
       
   175    * \brief Start transmitting a packet over the channel
       
   176    *
       
   177    * If the srcId belongs to a net device that is connected to the
       
   178    * channel, packet transmission begins, and the channel becomes busy
       
   179    * until the packet has completely reached all destinations.
       
   180    *
       
   181    * \param p A reference to the packet that will be transmitted over
       
   182    * the channel
       
   183    * \param srcId The device Id of the net device that wants to
       
   184    * transmit on the channel.
       
   185    * \return True if the channel is not busy and the transmitting net
       
   186    * device is currently active.
       
   187    */
       
   188   bool TransmitStart (Packet& p, uint32_t srcId);
       
   189   /**
       
   190    * \brief Indicates that the net device has finished transmitting
       
   191    * the packet over the channel
       
   192    *
       
   193    * The channel will stay busy until the packet has completely
       
   194    * propagated to all net devices attached to the channel. The
       
   195    * TransmitEnd function schedules the PropagationCompleteEvent which
       
   196    * will free the channel for further transmissions. Stores the
       
   197    * packet p as the m_currentPkt, the packet being currently
       
   198    * transmitting.
       
   199    *
       
   200    * \return Returns true unless the source was detached before it
       
   201    * completed its transmission.
       
   202    */
       
   203   bool TransmitEnd ();
       
   204   /**
       
   205    * \brief Indicates that the channel has finished propagating the
       
   206    * current packet. The channel is released and becomes free.
       
   207    *
       
   208    * Calls the receive function of every active net device that is
       
   209    * attached to the channel.
       
   210    */
       
   211   void PropagationCompleteEvent();
       
   212   /**
       
   213    * \return Returns the device number assigned to a net device by the
       
   214    * channel
       
   215    *
       
   216    * \param device Device pointer to the netdevice for which the device
       
   217    * number is needed
       
   218    */
       
   219   int32_t GetDeviceNum (Ptr<CsmaCdNetDevice> device);
       
   220   /**
       
   221    * \return Returns the state of the channel (IDLE -- free,
       
   222    * TRANSMITTING -- busy, PROPAGATING - busy )
       
   223    */
       
   224   WireState GetState();
       
   225 
       
   226   /**
       
   227    * \brief Indicates if the channel is busy. The channel will only
       
   228    * accept new packets for transmission if it is not busy.
       
   229    *
       
   230    * \return Returns true if the channel is busy and false if it is
       
   231    * free.
       
   232    */
       
   233   bool IsBusy();
       
   234   
       
   235   /**
       
   236    * \brief Indicates if a net device is currently attached or
       
   237    * detached from the channel.
       
   238    *
       
   239    * \param deviceId The ID that was assigned to the net device when
       
   240    * it was attached to the channel.
       
   241    * \return Returns true if the net device is attached to the
       
   242    * channel, false otherwise.
       
   243    */
       
   244   bool IsActive(uint32_t deviceId);
       
   245   /**
       
   246    * \return Returns the number of net devices that are currently
       
   247    * attached to the channel.
       
   248    */
       
   249   uint32_t GetNumActDevices (void);
       
   250   /**
       
   251    * \return Returns the total number of devices including devices
       
   252    * that have been detached from the channel.
       
   253    */
       
   254   virtual uint32_t GetNDevices (void) const;
       
   255   /**
       
   256    * \param i The deviceId of the net device for which we want the
       
   257    * pointer.
       
   258    * \return Returns the pointer to the net device that is associated
       
   259    * with deviceId i.
       
   260    */
       
   261   virtual Ptr<NetDevice> GetDevice (uint32_t i) const;
       
   262 
       
   263   virtual DataRate GetDataRate (void);
       
   264   virtual Time GetDelay (void);
       
   265 
       
   266 private:
       
   267   DataRate      m_bps;    /// Data rate of the channel
       
   268   Time          m_delay;  /// Delay of the channel.
       
   269 
       
   270   /**
       
   271    * List of the net devices that have been or are currently connected
       
   272    * to the channel.
       
   273    *
       
   274    * Devices are nor removed from this list, they are marked as
       
   275    * inactive. Otherwise the assigned device IDs will not refer to the
       
   276    * correct NetDevice. The DeviceIds are used so that it is possible
       
   277    * to have a number to refer to an entry in the list so that the
       
   278    * whole list does not have to be searched when making sure that a
       
   279    * source is attached to a channel when it is transmitting data.
       
   280    */
       
   281   std::vector< CsmaCdDeviceRec >            m_deviceList;
       
   282   /**
       
   283    * Packet that is currently being transmitted on the channel (or last
       
   284    * packet to have been transmitted on the channel if the channel is
       
   285    * free.)
       
   286    */
       
   287   Packet                              m_currentPkt;
       
   288   /**
       
   289    * Device Id of the source that is currently transmitting on the
       
   290    * channel. Or last source to have transmitted a packet on the
       
   291    * channel, if the channel is currently not busy.
       
   292    */
       
   293   uint32_t                            m_currentSrc;
       
   294   /**
       
   295    * Current state of the channel
       
   296    */
       
   297   WireState          m_state;
       
   298   /**
       
   299    * Initializes the channel when it is constructed. Resets the
       
   300    * deviceList and sets the channel state to IDLE.
       
   301    */
       
   302   void Init (void);
       
   303 };
       
   304 
       
   305 } // namespace ns3
       
   306 
       
   307 #endif /* CSMA_CD_CHANNEL_H */