doc/howtos/howtos-net-device.h
changeset 3313 9410e0ff6a1c
child 3315 85d6b10511fe
equal deleted inserted replaced
3312:c4f001798693 3313:9410e0ff6a1c
       
     1 /*!
       
     2   \page net-device How to create a new OSI layer 1 + 2 implementation ?
       
     3   \anchor howtos-net-device
       
     4 
       
     5   <b>Question:</b> How do I integrate a new OSI layer 1 + 2 implementation ?
       
     6 
       
     7   <b>Answer:</b> The OSI layers 1 and 2 are represented by the ns3::NetDevice
       
     8   and ns3::Channel classes. To plug transparently in ns-3, a new layer 1+2 model
       
     9   thus simply needs to provide two new subclasses of these two base classes.
       
    10 
       
    11   To make that subclassing process easy, two skeleton classes are provided in
       
    12   the src/node directory: simple-net-device.h (ns3::SimpleNetDevice) and 
       
    13   simple-channel.h (ns3::SimpleChannel) implement a broadcast passthru medium 
       
    14   using 48bit MAC addresses without any kind of MAC access algorithm or PHY 
       
    15   layer modeling.
       
    16 
       
    17   The ns3::SimpleChannel class is really very simple: it provides
       
    18   an implementation for the ns3::Channel::GetNDevices and ns3::Channel::GetDevice
       
    19   methods defined in the Channel base class and, then defines the channel-specific
       
    20   send and add methods:
       
    21 - The Add method is used by SimpleNetDevice::SetChannel to register a new 
       
    22   SimpleNetDevice with its associated channel.
       
    23 - The Send method is used by SimpleNetDevice::Send to send a packet over the
       
    24   broadcast medium and ensure that it gets delivered to all associated devices
       
    25   (except the sender).
       
    26 
       
    27 \code
       
    28 class SimpleChannel : public Channel
       
    29 {
       
    30 public:
       
    31   static TypeId GetTypeId (void);
       
    32   SimpleChannel ();
       
    33 
       
    34   void Send (Ptr<Packet> p, uint16_t protocol, Mac48Address to, Mac48Address from,
       
    35 	     Ptr<SimpleNetDevice> sender);
       
    36 
       
    37   void Add (Ptr<SimpleNetDevice> device);
       
    38 
       
    39   // inherited from ns3::Channel
       
    40   virtual uint32_t GetNDevices (void) const;
       
    41   virtual Ptr<NetDevice> GetDevice (uint32_t i) const;
       
    42 
       
    43 private:
       
    44   std::vector<Ptr<SimpleNetDevice> > m_devices;
       
    45 };
       
    46 \endcode
       
    47 
       
    48 The SimpleNetDevice class is also trivial since it implements no special
       
    49 MAC-layer processing:
       
    50 \code
       
    51 class SimpleNetDevice : public NetDevice
       
    52 {
       
    53 public:
       
    54   static TypeId GetTypeId (void);
       
    55   SimpleNetDevice ();
       
    56 
       
    57   void Receive (Ptr<Packet> packet, uint16_t protocol, Mac48Address to, Mac48Address from);
       
    58   void SetChannel (Ptr<SimpleChannel> channel);
       
    59   void SetAddress (Mac48Address address);
       
    60 
       
    61   // inherited from NetDevice base class.
       
    62   virtual void SetName(const std::string name);
       
    63   ...
       
    64 };
       
    65 \endcode
       
    66 
       
    67 The code below illustrates how the three model-specific methods defined above are
       
    68 implemented:
       
    69 
       
    70 \code
       
    71 void 
       
    72 SimpleNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol, 
       
    73 			  Mac48Address to, Mac48Address from)
       
    74 {
       
    75   if (to == m_address || to == Mac48Address::GetBroadcast ())
       
    76     {
       
    77       m_rxCallback (this, packet, protocol, from);
       
    78     }
       
    79 }
       
    80 void 
       
    81 SimpleNetDevice::SetChannel (Ptr<SimpleChannel> channel)
       
    82 {
       
    83   m_channel = channel;
       
    84   m_channel->Add (this);
       
    85 }
       
    86 void 
       
    87 SimpleNetDevice::SetAddress (Mac48Address address)
       
    88 {
       
    89   m_address = address;
       
    90 }
       
    91 \endcode
       
    92 
       
    93 */