packages/uan/model/uan-address.cc
changeset 6622 66d4eab148ce
child 6624 601c5f38d7ca
equal deleted inserted replaced
6621:b30587eaf9e6 6622:66d4eab148ce
       
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2009 University of Washington
       
     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: Leonard Tracy <lentracy@gmail.com>
       
    19  */
       
    20 
       
    21 #include "uan-address.h"
       
    22 #include "ns3/address.h"
       
    23 
       
    24 namespace ns3 {
       
    25 namespace uan {
       
    26 
       
    27 UanAddress::UanAddress ()
       
    28 {
       
    29   m_address = 255;
       
    30 }
       
    31 
       
    32 UanAddress::UanAddress (uint8_t addr)
       
    33   : m_address (addr)
       
    34 {
       
    35 }
       
    36 
       
    37 UanAddress::~UanAddress ()
       
    38 {
       
    39 }
       
    40 
       
    41 uint8_t
       
    42 UanAddress::GetType (void)
       
    43 {
       
    44   static uint8_t type = Address::Register ();
       
    45   return type;
       
    46 }
       
    47 
       
    48 Address
       
    49 UanAddress::ConvertTo (void) const
       
    50 {
       
    51   return Address (GetType (), &m_address, 1);
       
    52 }
       
    53 
       
    54 UanAddress
       
    55 UanAddress::ConvertFrom (const Address &address)
       
    56 {
       
    57   NS_ASSERT (IsMatchingType (address));
       
    58   UanAddress uAddr;
       
    59   address.CopyTo (&uAddr.m_address);
       
    60   return uAddr;
       
    61 }
       
    62 
       
    63 uint8_t
       
    64 UanAddress::GetAsInt (void) const
       
    65 {
       
    66   return m_address;
       
    67 }
       
    68 bool
       
    69 UanAddress::IsMatchingType (const Address &address)
       
    70 {
       
    71   return address.CheckCompatible (GetType (), 1);
       
    72 }
       
    73 
       
    74 UanAddress::operator Address () const
       
    75 {
       
    76   return ConvertTo ();
       
    77 }
       
    78 
       
    79 void
       
    80 UanAddress::CopyFrom (const uint8_t *pBuffer)
       
    81 {
       
    82   m_address = *pBuffer;
       
    83 }
       
    84 
       
    85 void
       
    86 UanAddress::CopyTo (uint8_t *pBuffer)
       
    87 {
       
    88   *pBuffer = m_address;
       
    89 
       
    90 }
       
    91 
       
    92 UanAddress
       
    93 UanAddress::GetBroadcast ()
       
    94 {
       
    95   return UanAddress (255);
       
    96 }
       
    97 UanAddress
       
    98 UanAddress::Allocate ()
       
    99 {
       
   100   static uint8_t nextAllocated = 0;
       
   101 
       
   102   uint32_t address = nextAllocated++;
       
   103   if (nextAllocated == 255)
       
   104     {
       
   105       nextAllocated = 0;
       
   106     }
       
   107 
       
   108   return UanAddress (address);
       
   109 }
       
   110 
       
   111 bool
       
   112 operator == (const UanAddress &a, const UanAddress &b)
       
   113 {
       
   114   return a.m_address == b.m_address;
       
   115 }
       
   116 
       
   117 bool
       
   118 operator != (const UanAddress &a, const UanAddress &b)
       
   119 {
       
   120   return !(a == b);
       
   121 }
       
   122 
       
   123 std::ostream&
       
   124 operator<< (std::ostream& os, const UanAddress & address)
       
   125 {
       
   126   os << (int) address.m_address;
       
   127   return os;
       
   128 }
       
   129 std::istream&
       
   130 operator>> (std::istream& is, UanAddress & address)
       
   131 {
       
   132   int x;
       
   133   is >> x;
       
   134   NS_ASSERT (0 <= x);
       
   135   NS_ASSERT (x <= 255);
       
   136   address.m_address = x;
       
   137   return is;
       
   138 }
       
   139 
       
   140 } // namespace uan
       
   141 } // namespace ns3