[svn] commit: r3740 - in /branches/trac356/src/lib/nsas: Makefile.am nameserver_address.h nameserver_entry.cc nameserver_entry.h nsas_entry.h random_number_generator.h tests/nameserver_entry_unittest.cc tests/random_number_generator_unittest.cc

BIND 10 source code commits bind10-changes at lists.isc.org
Tue Dec 7 09:13:51 UTC 2010


Author: ocean
Date: Tue Dec  7 09:13:51 2010
New Revision: 3740

Log:
Made some modifnication as review feedback.

Modified:
    branches/trac356/src/lib/nsas/Makefile.am
    branches/trac356/src/lib/nsas/nameserver_address.h
    branches/trac356/src/lib/nsas/nameserver_entry.cc
    branches/trac356/src/lib/nsas/nameserver_entry.h
    branches/trac356/src/lib/nsas/nsas_entry.h
    branches/trac356/src/lib/nsas/random_number_generator.h
    branches/trac356/src/lib/nsas/tests/nameserver_entry_unittest.cc
    branches/trac356/src/lib/nsas/tests/random_number_generator_unittest.cc

Modified: branches/trac356/src/lib/nsas/Makefile.am
==============================================================================
--- branches/trac356/src/lib/nsas/Makefile.am (original)
+++ branches/trac356/src/lib/nsas/Makefile.am Tue Dec  7 09:13:51 2010
@@ -19,6 +19,7 @@
 libnsas_la_SOURCES += nameserver_entry.cc nameserver_entry.h
 libnsas_la_SOURCES += nsas_entry_compare.h
 libnsas_la_SOURCES += nsas_entry.h
+libnsas_la_SOURCES += random_number_generator.h
 libnsas_la_SOURCES += zone_entry.h
 
 CLEANFILES = *.gcno *.gcda

Modified: branches/trac356/src/lib/nsas/nameserver_address.h
==============================================================================
--- branches/trac356/src/lib/nsas/nameserver_address.h (original)
+++ branches/trac356/src/lib/nsas/nameserver_address.h Tue Dec  7 09:13:51 2010
@@ -60,7 +60,9 @@
     NameserverAddress(boost::shared_ptr<NameserverEntry>& nameserver, uint32_t index, short family):
         ns_(nameserver), index_(index), family_(family)
     {
-        if(!ns_.get()) isc_throw(NullNameserverEntryPointer, "NULL NameserverEntry pointer.");
+        if(!ns_.get()) {
+            isc_throw(NullNameserverEntryPointer, "NULL NameserverEntry pointer.");
+        }
     }
 
     /// \brief Default Constructor

Modified: branches/trac356/src/lib/nsas/nameserver_entry.cc
==============================================================================
--- branches/trac356/src/lib/nsas/nameserver_entry.cc (original)
+++ branches/trac356/src/lib/nsas/nameserver_entry.cc Tue Dec  7 09:13:51 2010
@@ -37,6 +37,7 @@
 using namespace isc::nsas;
 using namespace isc::dns;
 using namespace std;
+using namespace boost;
 
 namespace isc {
 namespace nsas {
@@ -151,22 +152,20 @@
 }
 
 // Return one address matching the given family
-bool NameserverEntry::getAddress(boost::shared_ptr<NameserverEntry>& nameserver, 
-        NameserverAddress& address, short family)
-{
-
-    // The shared_ptr must contain this pointer
-    assert(nameserver.get() == this);
+bool NameserverEntry::getAddress(NameserverAddress& address, short family)
+{
+    // Get the shared_ptr object that point to "this" object
+    shared_ptr<NameserverEntry> shared_ptr_to_this = shared_from_this();
 
     if(family == AF_INET){
         if(v4_addresses_.size() == 0) return false;
 
-        address = NameserverAddress(nameserver, v4_address_selector_(), AF_INET);
+        address = NameserverAddress(shared_ptr_to_this, v4_address_selector_(), AF_INET);
         return true;
     } else if(family == AF_INET6){
         if(v6_addresses_.size() == 0) return false;
 
-        address = NameserverAddress(nameserver, v6_address_selector_(), AF_INET6);
+        //address = NameserverAddress(shared_from_this(), v6_address_selector_(), AF_INET6);
         return true;
     }
     return false;
@@ -230,8 +229,11 @@
     (*addresses)[index].setRTT(new_rtt);
 
     // Update the selector
-    if(family == AF_INET) updateAddressSelector(v4_addresses_, v4_address_selector_);
-    else if(family == AF_INET6) updateAddressSelector(v6_addresses_, v6_address_selector_);
+    if(family == AF_INET) { 
+        updateAddressSelector(v4_addresses_, v4_address_selector_);
+    } else if(family == AF_INET6) {
+        updateAddressSelector(v6_addresses_, v6_address_selector_);
+    }
 }
 
 // Sets the address to be unreachable

Modified: branches/trac356/src/lib/nsas/nameserver_entry.h
==============================================================================
--- branches/trac356/src/lib/nsas/nameserver_entry.h (original)
+++ branches/trac356/src/lib/nsas/nameserver_entry.h Tue Dec  7 09:13:51 2010
@@ -91,7 +91,7 @@
 /// As this object will be stored in the nameserver address store LRU list,
 /// it is derived from the LRU list entry class.
 
-class NameserverEntry : public NsasEntry<NameserverEntry> {
+class NameserverEntry : public NsasEntry<NameserverEntry>{
 public:
     /// List of addresses associated with this nameserver
     typedef std::vector<AddressEntry>   AddressVector;
@@ -142,13 +142,10 @@
     /// \brief Return one address
     ///
     /// Return one address corresponding to this nameserver
-    /// \param nameserver The NamerserverEntry shared_ptr object. The NameserverAddress
-    ///        need to hold it to avoid NameserverEntry being released
     /// \param address NameserverAddress object used to receive the address
     /// \param family The family of user request, AF_INET or AF_INET6
     /// \return true if one address is found, false otherwise
-    virtual bool getAddress(boost::shared_ptr<NameserverEntry>& nameserver, 
-            NameserverAddress& address, short family);
+    virtual bool getAddress(NameserverAddress& address, short family);
 
     /// \brief Return Address that corresponding to the index
     ///

Modified: branches/trac356/src/lib/nsas/nsas_entry.h
==============================================================================
--- branches/trac356/src/lib/nsas/nsas_entry.h (original)
+++ branches/trac356/src/lib/nsas/nsas_entry.h Tue Dec  7 09:13:51 2010
@@ -17,6 +17,7 @@
 #ifndef __NSAS_ENTRY_H
 #define __NSAS_ENTRY_H
 
+#include <boost/enable_shared_from_this.hpp>
 #include <iostream>
 
 #include "exceptions/exceptions.h"
@@ -38,7 +39,6 @@
         Exception(file, line, what)
     {}
 };
-
 
 /// \brief Element of NSAS Internal Lists
 ///
@@ -64,9 +64,11 @@
 /// pointers, but a shared pointer to a base class is not a subclass of a
 /// shared pointer to a derived class.  For this reason, the type of element
 /// being stored is a template parameter.
-
+///
+/// This class is inherited from boost::enable_shared_from_this class
+/// So within a member function a shared_ptr to current object can be obtained
 template <typename T>
-class NsasEntry {
+class NsasEntry : public boost::enable_shared_from_this <T>  {
 public:
 
     /// \brief Default Constructor

Modified: branches/trac356/src/lib/nsas/random_number_generator.h
==============================================================================
--- branches/trac356/src/lib/nsas/random_number_generator.h (original)
+++ branches/trac356/src/lib/nsas/random_number_generator.h Tue Dec  7 09:13:51 2010
@@ -17,6 +17,7 @@
 #ifndef __NSAS_RANDOM_NUMBER_GENERATOR_H
 #define __NSAS_RANDOM_NUMBER_GENERATOR_H
 
+#include <cmath>
 #include <numeric>
 #include <boost/random/mersenne_twister.hpp>
 #include <boost/random/uniform_int.hpp>
@@ -133,19 +134,19 @@
     bool isProbabilitiesValid(const std::vector<double>& probabilities) const
     {
         typedef std::vector<double>::const_iterator Iterator;
-        double sum = probabilities.empty() ? 1 : 0;
+        double sum = probabilities.empty() ? 1.0 : 0.0;
         for(Iterator it = probabilities.begin(); it != probabilities.end(); ++it){
             //The probability must be in [0, 1.0]
-            if(*it < 0) return false;
-
-            if(*it > 1) return false;
+            if(*it < 0.0 || *it > 1.0) {
+                return false;
+            }
 
             sum += *it;
         }
 
         double epsilon = 0.0001;
         // The sum must be equal to 1
-        return fabs(sum - 1) < epsilon;
+        return fabs(sum - 1.0) < epsilon;
     }
 
     // Shortcut typedefs

Modified: branches/trac356/src/lib/nsas/tests/nameserver_entry_unittest.cc
==============================================================================
--- branches/trac356/src/lib/nsas/tests/nameserver_entry_unittest.cc (original)
+++ branches/trac356/src/lib/nsas/tests/nameserver_entry_unittest.cc Tue Dec  7 09:13:51 2010
@@ -437,7 +437,7 @@
     int c3 = 0;
     NameserverAddress ns_address;
     for(int i = 0; i < 10000; ++i){
-        ns.get()->getAddress(ns, ns_address, AF_INET);
+        ns.get()->getAddress(ns_address, AF_INET);
         asiolink::IOAddress io_address = ns_address.getAddress();
         if(io_address.toText() == v4Addresses[0].getAddress().toText()) ++c1;
         else if(io_address.toText() == v4Addresses[1].getAddress().toText()) ++c2;
@@ -453,7 +453,7 @@
     ns->setAddressRTT(v4Addresses[2].getAddress(), 3);
     c1 = c2 = c3 = 0; 
     for(int i = 0; i < 100000; ++i){
-        ns.get()->getAddress(ns, ns_address, AF_INET);
+        ns.get()->getAddress(ns_address, AF_INET);
         asiolink::IOAddress io_address = ns_address.getAddress();
         if(io_address.toText() == v4Addresses[0].getAddress().toText()) ++c1;
         else if(io_address.toText() == v4Addresses[1].getAddress().toText()) ++c2;
@@ -471,7 +471,7 @@
     ns->setAddressUnreachable(v4Addresses[2].getAddress());
     c1 = c2 = c3 = 0;
     for(int i = 0; i < 100000; ++i){
-        ns.get()->getAddress(ns, ns_address, AF_INET);
+        ns.get()->getAddress(ns_address, AF_INET);
         asiolink::IOAddress io_address = ns_address.getAddress();
         if(io_address.toText() == v4Addresses[0].getAddress().toText()) ++c1;
         else if(io_address.toText() == v4Addresses[1].getAddress().toText()) ++c2;

Modified: branches/trac356/src/lib/nsas/tests/random_number_generator_unittest.cc
==============================================================================
--- branches/trac356/src/lib/nsas/tests/random_number_generator_unittest.cc (original)
+++ branches/trac356/src/lib/nsas/tests/random_number_generator_unittest.cc Tue Dec  7 09:13:51 2010
@@ -52,11 +52,13 @@
     const static int max_ = 10;
 };
 
+#ifndef NDEBUG
 // Test of the constructor
 TEST_F(UniformRandomIntegerGeneratorTest, Constructor) {
     // The range must be min<=max
     ASSERT_DEATH(UniformRandomIntegerGenerator(3, 2), "");
 }
+#endif
 
 // Test of the generated integers are in the range [min, max]
 TEST_F(UniformRandomIntegerGeneratorTest, IntegerRange) {
@@ -97,6 +99,7 @@
         ASSERT_EQ(gen(), 123);
     }
 
+#ifndef NDEBUG
     //The probability must be >= 0
     probabilities.push_back(-0.1);
     probabilities.push_back(1.1);
@@ -120,6 +123,7 @@
     probabilities.push_back(0.2);
     probabilities.push_back(0.1);
     ASSERT_DEATH(WeightedRandomIntegerGenerator gen5(probabilities), "");
+#endif
 }
 
 // Test the randomization of the generator




More information about the bind10-changes mailing list