Split MOOSE Bridge State into separate class
authorRichard Whitehouse <github@richardwhiuk.com>
Sat, 7 May 2011 14:58:27 +0000 (15:58 +0100)
committerRichard Whitehouse <github@richardwhiuk.com>
Sat, 7 May 2011 14:58:27 +0000 (15:58 +0100)
src/devices/bridge/model/moose-bridge-net-device.cc
src/devices/bridge/model/moose-bridge-net-device.h
src/devices/bridge/model/moose-bridge-state.cc [new file with mode: 0644]
src/devices/bridge/model/moose-bridge-state.h [new file with mode: 0644]
src/devices/bridge/wscript

index 261291149676072193c9cf043a85504013098a95..6ed97ddfa4601eb02d4367ed06e4aadbf87e7c86 100644 (file)
@@ -41,11 +41,7 @@ MooseBridgeNetDevice::GetTypeId (void)
                   MooseAddressValue(),
                   MakeMooseAddressAccessor (&MooseBridgeNetDevice::m_mooseAddress),
                   MakeMooseAddressChecker ())
-    .AddAttribute ("MooseExpirationTime",
-                   "Time it takes for learned MOOSE state entry to expire.",
-                   TimeValue (Seconds (30)),
-                   MakeTimeAccessor (&MooseBridgeNetDevice::m_expirationTime),
-                   MakeTimeChecker ());
+       ;
   return tid;
 }
 
@@ -53,6 +49,7 @@ MooseBridgeNetDevice::MooseBridgeNetDevice() : BridgeNetDevice() {
   NS_LOG_FUNCTION_NOARGS ();
 
   m_mooseAddress = MooseAddress::Allocate();
+  m_state = CreateObject<MooseBridgeState> ();
 
 }
 
@@ -76,38 +73,14 @@ MooseAddress MooseBridgeNetDevice::FromMoose(MooseAddress const& addr){
   if(addr.GetMoosePrefix() != m_mooseAddress.GetMoosePrefix()){
      return addr;
   }
-  std::map<MooseSuffixAddress, SuffixState*>::iterator iter = m_ethernetState.find(addr.GetMooseSuffix());
-
-  if(iter == m_ethernetState.end()){
-
-     // Expired state - must broadcast.     
-
-     return MooseAddress(Mac48Address::GetBroadcast());
 
-  } else {
-
-     Time now = Simulator::Now();     
-
-     SuffixState* state = iter->second;
-    
-     if(state->expirationTime > now){ 
-        return MooseAddress(iter->second->ethernet);
-     } else {
-        m_ethernetState.erase(iter);
-        m_suffixState.erase(iter->second->ethernet);
-        return MooseAddress(Mac48Address::GetBroadcast());
-     }
-
-  }
-  
+  return m_state->GetMac(addr.GetMooseSuffix());
 }
 
 MooseAddress MooseBridgeNetDevice::ToMoose(MooseAddress const& addr){
   NS_LOG_FUNCTION_NOARGS();
 
-  Time now = Simulator::Now();
-
   MooseAddress::MooseType mt = addr.GetMooseType();
 
   if(mt == MooseAddress::MOOSE){ 
@@ -117,55 +90,9 @@ MooseAddress MooseBridgeNetDevice::ToMoose(MooseAddress const& addr){
 
      Mac48Address addr48 = addr.GetMacAddress();
 
-     // Fix Suffix.
-     std::map<Mac48Address, SuffixState>::iterator iter = m_suffixState.find(addr48);
-     if(iter == m_suffixState.end()){
-         // Allocate New Suffix
-
-         moose = MooseAddress::Allocate(m_mooseAddress.GetMoosePrefix()); 
-
-         SuffixState &state = m_suffixState[addr48];
-         state.ethernet = addr48;
-         state.suffix = moose.GetMooseSuffix();
-         state.expirationTime = now + m_expirationTime;
-
-        NS_LOG_LOGIC("Allocating New MOOSE Suffix: (" << moose.GetMoosePrefix().GetInt() << "," << moose.GetMooseSuffix().GetInt() << ") for " << addr48);
+     MooseSuffixAddress suffix = m_state->GetSuffix(addr48, m_mooseAddress.GetMoosePrefix());
 
-         m_ethernetState[state.suffix] = &m_suffixState[addr48];
-
-     } else {
-        SuffixState &state = iter->second;
-        if(state.expirationTime > now){
-            moose = MooseAddress::Combine(m_mooseAddress.GetMoosePrefix(), state.suffix);
-
-           NS_LOG_LOGIC("Using Allocated MOOSE Suffix: (" << moose.GetMoosePrefix().GetInt() << "," << moose.GetMooseSuffix().GetInt() << ")");
-
-            if(now + m_expirationTime > state.expirationTime){
-               state.expirationTime = now + m_expirationTime;
-            }
-        } else {
-            // Delete old and alloc new
-
-            m_ethernetState.erase(state.suffix);
-            m_suffixState.erase(iter);
-
-            // Allocate New
-
-            SuffixState &state = m_suffixState[addr48];
-            
-            moose = MooseAddress::Allocate(m_mooseAddress.GetMoosePrefix());
-           state.ethernet = addr48;
-            state.suffix = moose.GetMooseSuffix();
-            state.expirationTime = now + m_expirationTime;
-
-           NS_LOG_LOGIC("Expired MOOSE Suffix: (" << moose.GetMoosePrefix().GetInt() << "," << moose.GetMooseSuffix().GetInt() << ")");
-
-            m_ethernetState[state.suffix] = &m_suffixState[addr48];
-
-        }
-     }
-
-     return moose;
+     return MooseAddress::Combine(m_mooseAddress.GetMoosePrefix(), suffix);
 
   } else {
      NS_ASSERT( (mt == MooseAddress::HOST) || (mt == MooseAddress::MOOSE) );  
@@ -179,11 +106,7 @@ void MooseBridgeNetDevice::AddRoutes(std::map<MoosePrefixAddress, Ptr<BridgePort
 
        for(std::map<MoosePrefixAddress, Ptr<BridgePortNetDevice> >::iterator it = routes.begin(); it != routes.end(); it ++){
                
-               NS_LOG_LOGIC("Adding Prefix State: (port: " << it->second << ", time: " << Simulator::GetMaximumSimulationTime() << ")");
-               
-               PrefixState &state = m_prefixState[it->first];
-               state.associatedPort = it->second;
-               state.expirationTime = Simulator::GetMaximumSimulationTime();
+               m_state->Fixed(it->first, it->second);
 
        }
 
@@ -191,29 +114,16 @@ void MooseBridgeNetDevice::AddRoutes(std::map<MoosePrefixAddress, Ptr<BridgePort
 
 void MooseBridgeNetDevice::Learn(MooseAddress const& addr, Ptr<BridgePortNetDevice> port){
   NS_LOG_FUNCTION_NOARGS ();
-  
-  Time now = Simulator::Now();
 
   NS_ASSERT(addr.GetMooseType() == MooseAddress::MOOSE);
 
   if(addr.GetMoosePrefix() != m_mooseAddress.GetMoosePrefix()){
      
-     PrefixState &state = m_prefixState[addr.GetMoosePrefix()];
-     if(now + m_expirationTime > state.expirationTime){
-            NS_LOG_LOGIC("Updating Learned Prefix State: (port: " << port << ", time: " << now + m_expirationTime << ", address: " << addr << ") - Previous State: (port: " << state.associatedPort << ", time: " << state.expirationTime << ")");
-
-            state.associatedPort = port;                               // Only update if the expiration is not in the future
-            state.expirationTime = now + m_expirationTime;
-     }
+       m_state->Learn(addr.GetMoosePrefix(), port);
 
   } else {
 
-     PortState &state = m_portState[addr.GetMooseSuffix()];
-     if(now + m_expirationTime > state.expirationTime){
-            NS_LOG_LOGIC("Updating Learned Suffix State: (port: " << port << ", time: " << now + m_expirationTime << ", address: " << addr << ") - Previous State: (port: " << state.associatedPort << ", time: " << state.expirationTime << ")");
-            state.associatedPort = port;
-            state.expirationTime = now + m_expirationTime;
-     }
+       m_state->Learn(addr.GetMooseSuffix(), port);
 
   }
  
@@ -222,24 +132,14 @@ void MooseBridgeNetDevice::Learn(MooseAddress const& addr, Ptr<BridgePortNetDevi
 Ptr<BridgePortNetDevice> MooseBridgeNetDevice::GetLearnedPort(MooseAddress const&addr){
   NS_LOG_FUNCTION_NOARGS();
 
-  Time now = Simulator::Now();
-
   if(addr.GetMooseType() != MooseAddress::MOOSE){
       return NULL;
   }
 
   if(addr.GetMoosePrefix() != m_mooseAddress.GetMoosePrefix()){
-     std::map<MoosePrefixAddress, PrefixState>::iterator iter = m_prefixState.find(addr.GetMoosePrefix());
-
-     if(iter != m_prefixState.end()){
-          return iter->second.associatedPort;
-     }
+       return m_state->GetPort(addr.GetMoosePrefix());
   } else {
-     std::map<MooseSuffixAddress, PortState>::iterator iter = m_portState.find(addr.GetMooseSuffix());
-
-     if(iter != m_portState.end()){
-          return iter->second.associatedPort;
-     }
+       return m_state->GetPort(addr.GetMooseSuffix());
   }
   
   return NULL;
index b22ade3a7277c5c8eaa91c214e432c809cdf9b2a..8e37932581712e8b481edd30d874252f860a0c22 100644 (file)
@@ -19,6 +19,7 @@
 #define MOOSE_BRIDGE_NET_DEVICE_H
 
 #include "bridge-net-device.h"
+#include "moose-bridge-state.h"
 #include "ns3/moose-address.h"
 #include "ns3/moose-prefix-address.h"
 #include "ns3/moose-suffix-address.h"
@@ -58,39 +59,9 @@ protected:
 
 private:
 
-// Need more types of state
-
   MooseAddress m_mooseAddress;
 
-  // Remote Moose State
-
-  struct PrefixState
-  {
-    Ptr<BridgePortNetDevice> associatedPort;
-    Time expirationTime;
-  };  
-
-  // Local Moose State
-
-  struct SuffixState
-  {
-    Mac48Address ethernet;
-    MooseSuffixAddress suffix;
-    Time expirationTime;
-  };  
-
-  struct PortState 
-  {
-    Ptr<BridgePortNetDevice> associatedPort;
-    Time expirationTime;
-  };
-  Time m_expirationTime;
-
-  std::map<MoosePrefixAddress, PrefixState> m_prefixState;
-  std::map<Mac48Address, SuffixState> m_suffixState;
-  std::map<MooseSuffixAddress, SuffixState*> m_ethernetState;
-  std::map<MooseSuffixAddress, PortState> m_portState;
+  Ptr<MooseBridgeState> m_state;
 
 };
 
diff --git a/src/devices/bridge/model/moose-bridge-state.cc b/src/devices/bridge/model/moose-bridge-state.cc
new file mode 100644 (file)
index 0000000..4bc77a5
--- /dev/null
@@ -0,0 +1,213 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation;
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*
+* Author: Richard Whitehouse <ns3@richardwhiuk.com>
+*/
+
+#include "moose-bridge-state.h"
+#include "ns3/log.h"
+#include "ns3/boolean.h"
+#include "ns3/simulator.h"
+#include "ns3/uinteger.h"
+
+NS_LOG_COMPONENT_DEFINE ("MooseBridgeState");
+
+namespace ns3 {
+
+NS_OBJECT_ENSURE_REGISTERED (MooseBridgeState);
+
+TypeId MooseBridgeState::GetTypeId (void)
+{
+       static TypeId tid = TypeId ("ns3::MooseBridgeState")
+                       .SetParent<Object> ()
+                       .AddConstructor<MooseBridgeState> ()
+                       .AddAttribute ( "ExpirationTime",
+                                       "Time it takes for learned state entry to expire.",
+                                       TimeValue (Seconds (300)),
+                                       MakeTimeAccessor (&MooseBridgeState::m_time),
+                                       MakeTimeChecker ())
+                       .AddAttribute ( "StateSize", "The size of the State Table",
+                                       UintegerValue (8000),
+                                       MakeUintegerAccessor (&MooseBridgeState::m_max),
+                                       MakeUintegerChecker<unsigned long> ())
+                       ;
+       return tid;
+}
+
+MooseBridgeState::MooseBridgeState(){
+
+}
+
+MooseBridgeState::~MooseBridgeState(){
+
+}
+
+void MooseBridgeState::Learn(MoosePrefixAddress addr, Ptr<BridgePortNetDevice> port){
+
+       Time now = Simulator::Now ();   
+
+       Prefix &state = m_prefixState[addr];
+       // Only update if the expiration is not in the future
+       if(now + m_time > state.time){
+               state.port = port;
+               state.time = now + m_time;
+       }
+}
+
+void MooseBridgeState::Learn(MooseSuffixAddress addr, Ptr<BridgePortNetDevice> port){
+       
+       Time now = Simulator::Now ();
+       
+       Port &state = m_portState[addr];
+       // Only update if the expiration is not in the future
+       if(now + m_time > state.time){
+               state.port = port;
+               state.time = now + m_time;
+       }
+
+}
+
+void MooseBridgeState::Fixed(MoosePrefixAddress addr, Ptr<BridgePortNetDevice> port){
+       
+
+       Prefix &state = m_prefixState[addr];
+       state.port = port;
+       state.time = Simulator::GetMaximumSimulationTime();
+}
+
+Mac48Address MooseBridgeState::GetMac(MooseSuffixAddress addr){
+
+       std::map<MooseSuffixAddress, Suffix*>::iterator iter = m_sufSuffixState.find(addr);
+       
+       if(iter == m_sufSuffixState.end()){
+
+               // Not found.
+
+               return Mac48Address::GetBroadcast();
+
+       } else {
+               
+               Time now = Simulator::Now();     
+
+               Suffix* state = iter->second;
+       
+               if(state->time > now){ 
+                       return iter->second->ethernet;
+               } else {
+                       m_sufSuffixState.erase(iter);
+                       m_ethSuffixState.erase(iter->second->ethernet);
+                       return Mac48Address::GetBroadcast();
+               }
+       }
+
+}
+
+MooseSuffixAddress MooseBridgeState::GetSuffix(Mac48Address addr, MoosePrefixAddress prefix){
+
+       Time now = Simulator::Now();
+
+       std::map<Mac48Address, Suffix>::iterator iter = m_ethSuffixState.find(addr);
+
+       if(iter == m_ethSuffixState.end()){
+               // Allocate New Suffix
+               MooseAddress moose = MooseAddress::Allocate(prefix); 
+               Suffix &state = m_ethSuffixState[addr];
+               state.ethernet = addr;
+               state.suffix = moose.GetMooseSuffix();
+               state.time = now + m_time;
+               NS_LOG_LOGIC("Allocating New MOOSE Suffix: (" << moose.GetMoosePrefix().GetInt() << "," << moose.GetMooseSuffix().GetInt() << ") for " << addr);
+               m_sufSuffixState[state.suffix] = &m_ethSuffixState[addr];
+
+
+               return moose.GetMooseSuffix();
+
+       } else {
+               Suffix &state = iter->second;
+
+               // Valid Entry
+
+               if(state.time > now){
+                       MooseAddress moose = MooseAddress::Combine(prefix, state.suffix);
+                       NS_LOG_LOGIC("Using Allocated MOOSE Suffix: (" << prefix.GetInt() << "," << state.suffix.GetInt() << ")");
+
+                       if(now + m_time > state.time){
+                               state.time = now + m_time;
+                       }
+
+
+                       return moose.GetMooseSuffix();
+
+               } else {
+               
+                       // Expired Entry - Delete old and alloc new
+
+                       m_sufSuffixState.erase(state.suffix);
+                       m_ethSuffixState.erase(iter);
+
+                       // Allocate New
+
+                       Suffix &state = m_ethSuffixState[addr];
+                       MooseAddress moose = MooseAddress::Allocate(prefix);
+                       state.ethernet = addr;
+                       state.suffix = moose.GetMooseSuffix();
+                       state.time = now + m_time;
+
+                       NS_LOG_LOGIC("Expired MOOSE Suffix: (" << prefix.GetInt() << "," << state.suffix.GetInt() << ")");
+
+                       m_sufSuffixState[state.suffix] = &m_ethSuffixState[addr];
+
+
+                       return moose.GetMooseSuffix();
+
+               }
+       }
+}
+
+Ptr<BridgePortNetDevice> MooseBridgeState::GetPort(MoosePrefixAddress addr){
+
+       Time now = Simulator::Now();
+
+       std::map<MoosePrefixAddress, Prefix>::iterator iter = m_prefixState.find(addr);
+
+       if(iter != m_prefixState.end() && iter->second.time > now){
+
+               return iter->second.port;
+       }
+
+       return NULL;
+
+}
+
+Ptr<BridgePortNetDevice> MooseBridgeState::GetPort(MooseSuffixAddress addr){
+
+       Time now = Simulator::Now();
+
+       std::map<MooseSuffixAddress, Port>::iterator iter = m_portState.find(addr);
+
+       if(iter != m_portState.end() && iter->second.time > now){
+
+               return iter->second.port;
+       }
+
+       return NULL;
+
+}
+
+unsigned long MooseBridgeState::GetSize(){
+       return m_prefixState.size() + m_portState.size();
+}
+
+}
+
diff --git a/src/devices/bridge/model/moose-bridge-state.h b/src/devices/bridge/model/moose-bridge-state.h
new file mode 100644 (file)
index 0000000..d7cfb29
--- /dev/null
@@ -0,0 +1,94 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 2 as
+* published by the Free Software Foundation;
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*
+* Author: Richard Whitehouse <ns3@richardwhiuk.com>
+*/
+
+#ifndef MOOSE_BRIDGE_STATE_H
+#define MOOSE_BRIDGE_STATE_H
+
+#include "bridge-port-net-device.h"
+#include "ns3/object-factory.h"
+#include "ns3/mac48-address.h"
+#include "ns3/nstime.h"
+#include "ns3/moose-address.h"
+#include "ns3/moose-prefix-address.h"
+#include "ns3/moose-suffix-address.h"
+
+namespace ns3 {
+
+class MooseBridgeState : public Object {
+
+public:
+
+       static TypeId GetTypeId (void);
+       MooseBridgeState();
+       ~MooseBridgeState();
+
+       unsigned long GetSize();
+
+       unsigned long GetMaxSize() const;
+       bool SetMaxSize(const unsigned long max);
+
+       void Learn(MooseSuffixAddress suffix, Ptr<BridgePortNetDevice> port);
+       void Learn(MoosePrefixAddress prefix, Ptr<BridgePortNetDevice> port);
+
+       void Fixed(MoosePrefixAddress prefix, Ptr<BridgePortNetDevice> port);
+
+       MooseSuffixAddress GetSuffix(Mac48Address addr, MoosePrefixAddress prefix);
+
+       Ptr<BridgePortNetDevice> GetPort(MoosePrefixAddress addr);
+       Ptr<BridgePortNetDevice> GetPort(MooseSuffixAddress addr);
+
+       Mac48Address GetMac(MooseSuffixAddress addr);
+
+private:
+
+       unsigned long m_max;
+
+       // Remote Moose State
+
+       struct Prefix {
+               Ptr<BridgePortNetDevice> port;
+               Time time;
+       };  
+
+       // Local Moose State
+
+       struct Suffix {
+               Mac48Address ethernet;
+               MooseSuffixAddress suffix;
+               Time time;
+       };
+
+       struct Port {
+               Ptr<BridgePortNetDevice> port;
+               Time time;
+       };
+
+       Time m_time;
+
+       std::map<MoosePrefixAddress, Prefix> m_prefixState;
+       std::map<Mac48Address, Suffix> m_ethSuffixState;
+       std::map<MooseSuffixAddress, Suffix*> m_sufSuffixState;
+       std::map<MooseSuffixAddress, Port> m_portState;
+
+
+};
+
+}
+
+#endif
+
index c612d559d92a856859f4037ac021d115e57b99a4..110bd5f9d1ae0e739a211c7c0b73c241b7d73885 100644 (file)
@@ -4,6 +4,7 @@ def build(bld):
     obj = bld.create_ns3_module('bridge', ['node'])
     obj.source = [
        'model/bridge-state.cc',
+       'model/moose-bridge-state.cc',
        'model/moose-bridge-port-net-device.cc',
        'model/moose-bridge-net-device.cc',
         'model/bridge-net-device.cc',
@@ -16,6 +17,7 @@ def build(bld):
     headers.module = 'bridge'
     headers.source = [
        'model/bridge-state.h',
+       'model/moose-bridge-state.h',
         'model/moose-bridge-port-net-device.h',
        'model/moose-bridge-net-device.h',
         'model/bridge-net-device.h',