Browse Source

Zone entries have callback queue

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac408@3478 e5f2f494-b856-4b98-b285-d166d9295462
Michal Vaner 14 years ago
parent
commit
6e9430a2a8

+ 1 - 1
src/lib/nsas/Makefile.am

@@ -19,6 +19,6 @@ libnsas_la_SOURCES += nameserver_address.h
 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 += zone_entry.h
+libnsas_la_SOURCES += zone_entry.cc zone_entry.h
 
 CLEANFILES = *.gcno *.gcda

+ 29 - 0
src/lib/nsas/tests/zone_entry_unittest.cc

@@ -15,16 +15,19 @@
 // $Id$
 
 #include <gtest/gtest.h>
+#include <boost/shared_ptr.hpp>
 
 #include "rrclass.h"
 
 #include "asiolink.h"
 #include "zone_entry.h"
+#include "../address_request_callback.h"
 
 #include "nsas_test.h"
 
 using namespace asiolink;
 using namespace std;
+using namespace boost;
 using namespace isc::dns;
 
 namespace isc {
@@ -48,5 +51,31 @@ TEST_F(ZoneEntryTest, DefaultConstructor) {
     EXPECT_EQ(RRClass::IN().getCode(), alpha.getClass());
 }
 
+namespace {
+// Just something that can be created and passed
+class Callback : public AddressRequestCallback {
+    public:
+        void success(const asiolink::IOAddress&) { };
+        void unreachable() { };
+};
+}
+
+TEST_F(ZoneEntryTest, Callbacks) {
+    const size_t count(3);
+    shared_ptr<AddressRequestCallback> callbacks[count];
+
+    ZoneEntry zone(EXAMPLE_CO_UK, RRClass::IN().getCode());
+    EXPECT_FALSE(zone.hasCallbacks());
+    for (size_t i(0); i < count; ++ i) {
+        zone.addCallback(callbacks[i] = shared_ptr<AddressRequestCallback>(
+            new Callback));
+    }
+    for (size_t i(0); i < count; ++ i) {
+        ASSERT_TRUE(zone.hasCallbacks());
+        EXPECT_EQ(callbacks[i], zone.popCallback());
+    }
+    EXPECT_FALSE(zone.hasCallbacks());
+}
+
 }   // namespace nsas
 }   // namespace isc

+ 50 - 0
src/lib/nsas/zone_entry.cc

@@ -0,0 +1,50 @@
+// Copyright (C) 2010  CZ NIC
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $id$
+
+#include "zone_entry.h"
+#include "address_request_callback.h"
+
+namespace isc {
+namespace nsas {
+
+namespace {
+// Shorter aliases for frequently used types
+typedef boost::mutex::scoped_lock Lock;
+typedef boost::shared_ptr<AddressRequestCallback> CallbackPtr;
+}
+
+void
+ZoneEntry::addCallback(CallbackPtr callback) {
+    Lock lock(mutex_);
+    callbacks_.push_back(callback);
+}
+
+bool
+ZoneEntry::hasCallbacks() const {
+    Lock lock(mutex_);
+    return (!callbacks_.empty());
+}
+
+CallbackPtr
+ZoneEntry::popCallback() {
+    Lock lock(mutex_);
+    CallbackPtr result(callbacks_.front());
+    callbacks_.pop_front();
+    return (result);
+}
+
+}; // namespace nsas
+}; // namespace isc

+ 10 - 5
src/lib/nsas/zone_entry.h

@@ -19,6 +19,7 @@
 
 #include <string>
 #include <vector>
+#include <list>
 #include <boost/thread.hpp>
 #include <boost/shared_ptr.hpp>
 
@@ -32,6 +33,7 @@ namespace isc {
 namespace nsas {
 
 class NameserverEntry;
+class AddressRequestCallback;
 
 /// \brief Zone Entry
 ///
@@ -80,17 +82,20 @@ public:
         return HashKey(name_, classCode_);
     }
 
-    /// \brief Lookup Address
-    ///
-    /// Returns the address with the lowest RTT.
-    //virtual asiolink::IOAddress getAddress() const;
+    /// \short Add another callback here
+    void addCallback(boost::shared_ptr<AddressRequestCallback> callback);
+    /// \short Is there at last one callback waiting?
+    bool hasCallbacks() const;
+    /// \short Remove a callback from queue and return it
+    boost::shared_ptr<AddressRequestCallback> popCallback();
 
 private:
-    boost::mutex    mutex_;     ///< Mutex protecting this zone entry
+    mutable boost::mutex    mutex_;     ///< Mutex protecting this zone entry
     std::string     name_;      ///< Canonical zone name
     uint16_t        classCode_; ///< Class code
     std::vector<boost::shared_ptr<NameserverEntry> > nameservers_; ///< Nameservers
     time_t          expiry_;    ///< Expiry time of this entry
+    std::list<boost::shared_ptr<AddressRequestCallback> > callbacks_;
 };
 
 } // namespace nsas