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

index 167945f2a5dfc85ac14ebb5163e3f21af01ed499..faa125cec27d34b81518125940fdab2747ddd9da 100644 (file)
@@ -43,21 +43,11 @@ BridgeNetDevice::GetTypeId (void)
                    MakeUintegerAccessor (&BridgeNetDevice::SetMtu,
                                          &BridgeNetDevice::GetMtu),
                    MakeUintegerChecker<uint16_t> ())    
-    .AddAttribute ("StateSize", "The size of the State Table",
-                   UintegerValue (8000),
-                   MakeUintegerAccessor (&BridgeNetDevice::SetMaxStateSize,
-                                         &BridgeNetDevice::GetMaxStateSize),
-                   MakeUintegerChecker<unsigned long> ())                  
     .AddAttribute ("EnableLearning",
                    "Enable the learning mode of the Learning Bridge",
                    BooleanValue (true),
                    MakeBooleanAccessor (&BridgeNetDevice::m_enableLearning),
                    MakeBooleanChecker ())
-    .AddAttribute ("ExpirationTime",
-                   "Time it takes for learned MAC state entry to expire.",
-                   TimeValue (Seconds (300)),
-                   MakeTimeAccessor (&BridgeNetDevice::m_expirationTime),
-                   MakeTimeChecker ())
     ;
   return tid;
 }
@@ -69,6 +59,7 @@ BridgeNetDevice::BridgeNetDevice ()
 {
   NS_LOG_FUNCTION_NOARGS ();
   m_channel = CreateObject<BridgeChannel> ();
+  m_state = CreateObject<BridgeState> ();
 }
 
 BridgeNetDevice::~BridgeNetDevice()
@@ -182,12 +173,7 @@ void BridgeNetDevice::Learn (Address const &src, Ptr<BridgePortNetDevice> port)
 
   if (m_enableLearning)
     {
-       std::map<Mac48Address, LearnedState>::iterator it = m_learnState.find(src48);
-       if(it != m_learnState.end() || m_learnState.size() < m_maxStateSize){
-             LearnedState &state = m_learnState[src48];
-             state.associatedPort = port;
-             state.expirationTime = Simulator::Now () + m_expirationTime;
-       }
+       m_state->Learn(src48, port);
     }
 }
 
@@ -196,21 +182,7 @@ Ptr<BridgePortNetDevice> BridgeNetDevice::GetLearnedState (Mac48Address source)
   NS_LOG_FUNCTION_NOARGS ();
   if (m_enableLearning)
     {
-      Time now = Simulator::Now ();
-      std::map<Mac48Address, LearnedState>::iterator iter =
-        m_learnState.find (source);
-      if (iter != m_learnState.end ())
-        {
-          LearnedState &state = iter->second;
-          if (state.expirationTime > now)
-            {
-              return state.associatedPort;
-            }
-          else
-            {
-              m_learnState.erase (iter);
-            }
-        }
+       return m_state->GetPort(source);
     }
   return NULL;
 }
@@ -313,7 +285,7 @@ bool
 BridgeNetDevice::SetMaxStateSize (const unsigned long maxStateSize )
 {
   NS_LOG_FUNCTION_NOARGS ();
-  m_maxStateSize = maxStateSize ;
+  m_state->SetMaxSize(maxStateSize);
   return true;
 }
 
@@ -321,14 +293,14 @@ unsigned long
 BridgeNetDevice::GetMaxStateSize (void) const
 {
   NS_LOG_FUNCTION_NOARGS ();
-  return m_maxStateSize;
+  return m_state->GetMaxSize();
 }
 
 unsigned long 
 BridgeNetDevice::GetStateSize (void) const
 {
   NS_LOG_FUNCTION_NOARGS ();
-  return m_learnState.size();
+  return m_state->GetSize();
 }
 
 bool 
index 270991954acd12e36e8661fef1120d3cfc3d24aa..8b0c1868d1a3dd496b7197d838bcb6bf8039dc89 100644 (file)
@@ -20,6 +20,7 @@
 #define BRIDGE_NET_DEVICE_H
 
 #include "bridge-port-net-device.h"
+#include "bridge-state.h"
 #include "ns3/net-device.h"
 #include "ns3/mac48-address.h"
 #include "ns3/nstime.h"
@@ -140,22 +141,16 @@ private:
   NetDevice::PromiscReceiveCallback m_promiscRxCallback;
 
   Mac48Address m_address;
-  Time m_expirationTime; // time it takes for learned MAC state to expire
-  struct LearnedState
-  {
-    Ptr<BridgePortNetDevice> associatedPort;
-    Time expirationTime;
-  };
-  std::map<Mac48Address, LearnedState> m_learnState;
   Ptr<Node> m_node;
   Ptr<BridgeChannel> m_channel;
 
+  Ptr<BridgeState> m_state;
+
   std::vector<Ptr<BridgePortNetDevice> > m_ports;
 
   uint16_t mPortNumber;
   uint32_t m_ifIndex;
   uint16_t m_mtu;
-  unsigned long m_maxStateSize;
   bool m_enableLearning;
 
 };
diff --git a/src/devices/bridge/model/bridge-state.cc b/src/devices/bridge/model/bridge-state.cc
new file mode 100644 (file)
index 0000000..ca2ffa4
--- /dev/null
@@ -0,0 +1,101 @@
+/* -*- 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 "bridge-state.h"
+#include "ns3/log.h"
+#include "ns3/boolean.h"
+#include "ns3/simulator.h"
+#include "ns3/uinteger.h"
+
+NS_LOG_COMPONENT_DEFINE ("BridgeState");
+
+namespace ns3 {
+
+NS_OBJECT_ENSURE_REGISTERED (BridgeState);
+
+TypeId BridgeState::GetTypeId (void){
+
+static TypeId tid = TypeId ("ns3::BridgeState")
+.SetParent<Object> ()
+.AddConstructor<BridgeState> ()
+.AddAttribute ("ExpirationTime",
+"Time it takes for learned MAC state entry to expire.",
+TimeValue (Seconds (300)),
+MakeTimeAccessor (&BridgeState::m_time),
+MakeTimeChecker ())
+.AddAttribute ("StateSize", "The size of the State Table",
+UintegerValue (8000),
+MakeUintegerAccessor (&BridgeState::m_max),
+MakeUintegerChecker<unsigned long> ())
+;
+return tid;
+}
+
+unsigned long BridgeState::GetMaxSize() const {
+       return m_max;
+}
+
+BridgeState::BridgeState(){
+
+}
+
+BridgeState::~BridgeState(){
+
+}
+
+void BridgeState::Learn(ns3::Mac48Address addr, Ptr<BridgePortNetDevice> port){
+       std::map<Mac48Address, Host>::iterator it = m_learnState.find(addr);
+       if(it != m_learnState.end() || m_learnState.size() < m_max){
+             Host &state = m_learnState[addr];
+             state.associatedPort = port;
+             state.expirationTime = Simulator::Now () + m_time;
+       }
+}
+
+void BridgeState::SetMaxSize(unsigned long max){
+       m_max = max;
+}
+
+Ptr<BridgePortNetDevice> BridgeState::GetPort(ns3::Mac48Address addr){
+       Time now = Simulator::Now ();
+       std::map<Mac48Address, Host>::iterator iter = m_learnState.find(addr);
+       if (iter != m_learnState.end()){
+               Host &state = iter->second;
+               if (state.expirationTime > now){
+                       return state.associatedPort;
+               } else {
+                       m_learnState.erase (iter);
+               }
+       }
+}
+
+unsigned long BridgeState::GetSize(){
+       return m_learnState.size();
+}
+
+void BridgeState::SetExpirationTime(Time time){
+       m_time = time;
+}
+
+Time BridgeState::GetExpirationTime(){
+       return m_time;
+}
+
+}
+
+
diff --git a/src/devices/bridge/model/bridge-state.h b/src/devices/bridge/model/bridge-state.h
new file mode 100644 (file)
index 0000000..d696366
--- /dev/null
@@ -0,0 +1,65 @@
+/* -*- 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 BRIDGE_STATE_H
+#define BRIDGE_STATE_H
+
+#include "bridge-port-net-device.h"
+#include "ns3/object-factory.h"
+#include "ns3/mac48-address.h"
+#include "ns3/nstime.h"
+
+namespace ns3 {
+
+class BridgeState : public Object {
+
+public:
+
+       static TypeId GetTypeId (void);
+       BridgeState ();
+       virtual ~BridgeState ();
+
+       unsigned long GetSize();
+
+       unsigned long GetMaxSize() const;
+       void SetMaxSize(const unsigned long max);
+
+       void Learn(Mac48Address addr, Ptr<BridgePortNetDevice> port);
+       Ptr<BridgePortNetDevice> GetPort(Mac48Address addr);
+
+       void SetExpirationTime(Time time);
+       
+       Time GetExpirationTime();
+
+private:
+
+       unsigned long m_max;
+       Time m_time;
+
+       struct Host {
+               Ptr<BridgePortNetDevice> associatedPort;
+               Time expirationTime;
+       };
+
+       std::map<Mac48Address, Host> m_learnState;
+
+};
+
+}
+
+#endif
+
index a8738f9ef0dd9277031faa87ddaefe4b05bdb39f..c612d559d92a856859f4037ac021d115e57b99a4 100644 (file)
@@ -3,6 +3,7 @@
 def build(bld):
     obj = bld.create_ns3_module('bridge', ['node'])
     obj.source = [
+       'model/bridge-state.cc',
        'model/moose-bridge-port-net-device.cc',
        'model/moose-bridge-net-device.cc',
         'model/bridge-net-device.cc',
@@ -14,6 +15,7 @@ def build(bld):
     headers = bld.new_task_gen('ns3header')
     headers.module = 'bridge'
     headers.source = [
+       'model/bridge-state.h',
         'model/moose-bridge-port-net-device.h',
        'model/moose-bridge-net-device.h',
         'model/bridge-net-device.h',