Browse Source

[2974] Extended context methods on both CalloutHandle and LibraryHandle

Stephen Morris 12 years ago
parent
commit
9deba8e6b5

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

@@ -37,7 +37,7 @@ libb10_util_la_SOURCES += encode/base32hex_from_binary.h
 libb10_util_la_SOURCES += encode/base_n.cc encode/hex.h
 libb10_util_la_SOURCES += encode/binary_from_base32hex.h
 libb10_util_la_SOURCES += encode/binary_from_base16.h
-libb10_util_la_SOURCES += hooks/callout_handle.h
+libb10_util_la_SOURCES += hooks/callout_handle.h hooks/callout_handle.cc
 libb10_util_la_SOURCES += hooks/library_handle.h hooks/library_handle.cc
 libb10_util_la_SOURCES += hooks/server_hooks.h hooks/server_hooks.cc
 libb10_util_la_SOURCES += random/qid_gen.h random/qid_gen.cc

+ 43 - 0
src/lib/util/hooks/callout_handle.cc

@@ -0,0 +1,43 @@
+// Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
+//
+// 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.
+
+#include <util/hooks/callout_handle.h>
+
+#include <algorithm>
+#include <functional>
+#include <string>
+#include <vector>
+
+using namespace std;
+using namespace isc::util;
+
+namespace isc {
+namespace util {
+
+// return the name of all context items.
+
+vector<string>
+CalloutHandle::getArgumentNames() const {
+
+    vector<string> names;
+    ArgumentCollection::const_iterator i;
+    for (i = arguments_.begin(); i != arguments_.end(); ++i) {
+        names.push_back(i->first);
+    }
+
+    return (names);
+}
+
+} // namespace util
+} // namespace isc

+ 4 - 4
src/lib/util/hooks/callout_handle.h

@@ -101,16 +101,16 @@ public:
     /// vector.
     ///
     /// @return Vector of strings reflecting argument names
-    std::vector<std::string> getArgumentNames() const {
-        std::vector<std::string> a;
-        return (a);
-    }
+    std::vector<std::string> getArgumentNames() const;
 
     /// @brief Delete argument
     ///
     /// Deletes an argument of the given name.  If an argument of that name
     /// does not exist, the method is a no-op.
     ///
+    /// N.B. If the element is a raw pointer, the pointed-to data is
+    /// NOT deleted by this.
+    ///
     /// @param name Name of the element in the argument list to set.
     void deleteArgument(const std::string& name) {
         static_cast<void>(arguments_.erase(name));

+ 16 - 3
src/lib/util/hooks/library_handle.cc

@@ -28,9 +28,8 @@ namespace util {
 void
 LibraryHandle::checkHookIndex(int index) const {
     if ((index < 0) || (index >= hook_vector_.size())) {
-        isc_throw(NoSuchHook, "unable to call callout for hook index " <<
-                  index << ": index is invalid for the size of the hook "
-                  "vector (" << hook_vector_.size() << ")");
+        isc_throw(NoSuchHook, "hook index " << index << " is invalid for the "
+                  " size of the hook vector (" << hook_vector_.size() << ")");
     }
 }
 
@@ -137,5 +136,19 @@ LibraryHandle::deregisterAll(const std::string& name) {
     hook_vector_[index].clear();
 }
 
+// return the name of all context items.
+
+vector<string>
+LibraryHandle::getContextNames() const {
+
+    vector<string> names;
+    ContextCollection::const_iterator i;
+    for (i = context_.begin(); i != context_.end(); ++i) {
+        names.push_back(i->first);
+    }
+
+    return (names);
+}
+
 } // namespace util
 } // namespace isc

+ 30 - 0
src/lib/util/hooks/library_handle.h

@@ -121,6 +121,36 @@ public:
 
         value = boost::any_cast<T>(element_ptr->second);
     }
+    
+    /// @brief Get context names
+    ///
+    /// Returns a vector holding the names of context items.
+    ///
+    /// @return Vector of strings reflecting argument names
+    std::vector<std::string> getContextNames() const;
+
+    /// @brief Delete context element
+    ///
+    /// Deletes context item of the given name.  If an item  of that name
+    /// does not exist, the method is a no-op.
+    ///
+    /// N.B. If the element is a raw pointer, the pointed-to data is
+    /// NOT deleted by this.
+    ///
+    /// @param name Name of the element in the argument list to set.
+    void deleteContext(const std::string& name) {
+        static_cast<void>(context_.erase(name));
+    }
+
+    /// @brief Delete all arguments
+    ///
+    /// Deletes all arguments associated with this context.
+    ///
+    /// N.B. If any elements are raw pointers, the pointed-to data is
+    /// NOT deleted by this.
+    void deleteAllContext() {
+        context_.clear();
+    }
 
     /// @brief Register a callout
     ///

+ 2 - 2
src/lib/util/hooks/server_hooks.cc

@@ -77,10 +77,10 @@ ServerHooks::getIndex(const string& name) const {
 
 // Return list of hooks
 
-std::vector<std::string>
+vector<string>
 ServerHooks::getHookNames() const {
 
-    std::vector<std::string> names;
+    vector<string> names;
     HookCollection::const_iterator i;
     for (i = hooks_.begin(); i != hooks_.end(); ++i) {
         names.push_back(i->first);

+ 24 - 0
src/lib/util/tests/callout_handle_unittest.cc

@@ -218,6 +218,30 @@ TEST_F(CalloutHandleTest, PointerTypes) {
                  boost::bad_any_cast);
 }
 
+// Check that we can get the names of the arguments.
+
+TEST_F(CalloutHandleTest, ContextItemNames) {
+    CalloutHandle handle(getHookManager());
+
+    vector<string> expected_names;
+    int value = 42;
+
+    expected_names.push_back("faith");
+    handle.setArgument("faith", value++);
+    expected_names.push_back("hope");
+    handle.setArgument("hope", value++);
+    expected_names.push_back("charity");
+    handle.setArgument("charity", value++);
+
+    // Get the names and check against the expected names.  We'll sort
+    // both arrays to simplify the checking.
+    vector<string> actual_names = handle.getArgumentNames();
+
+    sort(actual_names.begin(), actual_names.end());
+    sort(expected_names.begin(), expected_names.end());
+    EXPECT_TRUE(expected_names == actual_names);
+}
+
 // Test that we can delete and argument.
 
 TEST_F(CalloutHandleTest, DeleteArgument) {

+ 67 - 0
src/lib/util/tests/library_handle_unittest.cc

@@ -18,6 +18,10 @@
 
 #include <gtest/gtest.h>
 
+#include <algorithm>
+#include <string>
+#include <vector>
+
 using namespace isc::util;
 using namespace std;
 
@@ -237,6 +241,69 @@ TEST_F(LibraryHandleTest, PointerTypes) {
                  boost::bad_any_cast);
 }
 
+// Check that we can get the names of the context items.
+
+TEST_F(LibraryHandleTest, ContextItemNames) {
+    LibraryHandle handle(getServerHooks(), 1);
+
+    vector<string> expected_names;
+    int value = 42;
+
+    expected_names.push_back("faith");
+    handle.setContext("faith", value++);
+    expected_names.push_back("hope");
+    handle.setContext("hope", value++);
+    expected_names.push_back("charity");
+    handle.setContext("charity", value++);
+
+    // Get the names and check against the expected names.  We'll sort
+    // both arrays to simplify the checking.
+    vector<string> actual_names = handle.getContextNames();
+
+    sort(actual_names.begin(), actual_names.end());
+    sort(expected_names.begin(), expected_names.end());
+    EXPECT_TRUE(expected_names == actual_names);
+}
+
+// Check that we can delete one item of context.
+
+TEST_F(LibraryHandleTest, DeleteContext) {
+    LibraryHandle handle(getServerHooks(), 1);
+
+    int value = 42;
+    handle.setContext("faith", value++);
+    handle.setContext("hope", value++);
+    value = 0;
+
+    // Delete "faith" and verify that getting it throws an exception
+    handle.deleteContext("faith");
+    EXPECT_THROW(handle.getContext("faith", value), NoSuchContext);
+
+    // Check that the other item is untouched.
+    EXPECT_NO_THROW(handle.getContext("hope", value));
+    EXPECT_EQ(43, value);
+}
+
+// Delete all all items of context.
+
+TEST_F(LibraryHandleTest, DeleteAllContext) {
+    LibraryHandle handle(getServerHooks(), 1);
+
+    int value = 42;
+    handle.setContext("faith", value++);
+    handle.setContext("hope", value++);
+    handle.setContext("charity", value++);
+    value = 0;
+
+    // Delete all items of context and verify that they are gone.
+    handle.deleteAllContext();
+    EXPECT_THROW(handle.getContext("faith", value), NoSuchContext);
+    EXPECT_THROW(handle.getContext("hope", value), NoSuchContext);
+    EXPECT_THROW(handle.getContext("charity", value), NoSuchContext);
+}
+
+
+
 // *** Callout Tests ***
 //
 // The next set of tests check that callouts can be registered.