Browse Source

[trac499] Add ability to determing protocol used by IOFetch

Stephen Morris 14 years ago
parent
commit
5017cbe7d1

+ 16 - 7
src/lib/asiolink/io_fetch.cc

@@ -52,7 +52,7 @@ namespace asiolink {
 
 /// Use the ASIO logger
 
-isc::log::Logger logger("asio");
+isc::log::Logger logger("asiolink");
 
 /// \brief IOFetch Data
 ///
@@ -75,9 +75,10 @@ struct IOFetchData {
     isc::dns::OutputBufferPtr   received;   ///< Received data put here
     boost::shared_array<char>   staging;    ///< Temporary array for received data
     IOFetch::Callback*          callback;   ///< Called on I/O Completion
+    asio::deadline_timer        timer;      ///< Timer to measure timeouts
+    IOFetch::Protocol           protocol;   ///< Protocol being used
     size_t                      cumulative; ///< Cumulative received amount
     bool                        stopped;    ///< Have we stopped running?
-    asio::deadline_timer        timer;      ///< Timer to measure timeouts
     int                         timeout;    ///< Timeout in ms
 
     // In case we need to log an error, the origin of the last asynchronous
@@ -91,7 +92,7 @@ struct IOFetchData {
     ///
     /// Just fills in the data members of the IOFetchData structure
     ///
-    /// \param protocol Either IOFetch::TCP or IOFetch::UDP.
+    /// \param proto Either IOFetch::TCP or IOFetch::UDP.
     /// \param service I/O Service object to handle the asynchronous
     ///        operations.
     /// \param query DNS question to send to the upstream server.
@@ -105,18 +106,18 @@ struct IOFetchData {
     /// \param wait Timeout for the fetch (in ms).
     ///
     /// TODO: May need to alter constructor (see comment 4 in Trac ticket #554)
-    IOFetchData(IOFetch::Protocol protocol, IOService& service,
+    IOFetchData(IOFetch::Protocol proto, IOService& service,
         const isc::dns::Question& query, const IOAddress& address,
         uint16_t port, isc::dns::OutputBufferPtr& buff, IOFetch::Callback* cb,
         int wait)
         :
-        socket((protocol == IOFetch::UDP) ?
+        socket((proto == IOFetch::UDP) ?
             static_cast<IOAsioSocket<IOFetch>*>(
                 new UDPSocket<IOFetch>(service)) :
             static_cast<IOAsioSocket<IOFetch>*>(
                 new TCPSocket<IOFetch>(service))
             ),
-        remote((protocol == IOFetch::UDP) ?
+        remote((proto == IOFetch::UDP) ?
             static_cast<IOEndpoint*>(new UDPEndpoint(address, port)) :
             static_cast<IOEndpoint*>(new TCPEndpoint(address, port))
             ),
@@ -125,9 +126,10 @@ struct IOFetchData {
         received(buff),
         staging(new char[IOFetch::MIN_LENGTH]),
         callback(cb),
+        timer(service.get_io_service()),
+        protocol(proto),
         cumulative(0),
         stopped(false),
-        timer(service.get_io_service()),
         timeout(wait),
         origin(ASIO_UNKORIGIN)
     {}
@@ -144,6 +146,13 @@ IOFetch::IOFetch(Protocol protocol, IOService& service,
 {
 }
 
+// Return protocol in use.
+
+IOFetch::Protocol
+IOFetch::getProtocol() const {
+    return (data_->protocol);
+}
+
 /// The function operator is implemented with the "stackless coroutine"
 /// pattern; see internal/coroutine.h for details.
 

+ 11 - 13
src/lib/asiolink/io_fetch.h

@@ -17,15 +17,14 @@
 
 #include <config.h>
 
-
 #include <boost/shared_array.hpp>
 #include <boost/shared_ptr.hpp>
 #include <boost/date_time/posix_time/posix_time_types.hpp>
 
-#include <asio/error_code.hpp>
-
 #include <coroutine.h>
 
+#include <asio/error_code.hpp>
+
 #include <dns/buffer.h>
 #include <dns/question.h>
 
@@ -138,6 +137,11 @@ public:
         uint16_t port, isc::dns::OutputBufferPtr& buff, Callback* cb,
         int wait = -1);
 
+    /// \brief Return Current Protocol
+    ///
+    /// \return Protocol associated with this IOFetch object.
+    Protocol getProtocol() const;
+
     /// \brief Coroutine entry point
     ///
     /// The operator() method is the method in which the coroutine code enters
@@ -145,16 +149,7 @@ public:
     ///
     /// \param ec Error code, the result of the last asynchronous I/O operation.
     /// \param length Amount of data received on the last asynchronous read
-    void operator()(asio::error_code ec, size_t length);
-
-    void operator()(asio::error_code ec) {
-        operator()(ec, 0);
-    }
-
-    void operator()() {
-        asio::error_code ec;
-        operator()(ec);
-    }
+    void operator()(asio::error_code ec = asio::error_code(), size_t length = 0);
 
     /// \brief Terminate query
     ///
@@ -172,6 +167,9 @@ private:
     /// \param ec ASIO error code
     void logIOFailure(asio::error_code ec);
 
+    // Member variables.  All data is in a structure pointed to by a shared
+    // pointer.  The IOFetch object is copied a number of times during its
+    // life, and only requiring a pointer to be copied reduces overhead.
     boost::shared_ptr<IOFetchData>  data_;   ///< Private data
 
 };

+ 8 - 3
src/lib/asiolink/tests/io_fetch_unittest.cc

@@ -338,18 +338,23 @@ public:
     }
 };
 
+// Check the protocol
+TEST_F(IOFetchTest, Protocol) {
+    EXPECT_EQ(IOFetch::UDP, udp_fetch_.getProtocol());
+    EXPECT_EQ(IOFetch::TCP, tcp_fetch_.getProtocol());
+}
 
-/// UDP Stop test - see IOFetchTest::stopTest() header.
+// UDP Stop test - see IOFetchTest::stopTest() header.
 TEST_F(IOFetchTest, UdpStop) {
     stopTest(IOFetch::UDP, udp_fetch_);
 }
 
-/// UDP premature stop test - see IOFetchTest::prematureStopTest() header.
+// UDP premature stop test - see IOFetchTest::prematureStopTest() header.
 TEST_F(IOFetchTest, UdpPrematureStop) {
     prematureStopTest(IOFetch::UDP, udp_fetch_);
 }
 
-/// UDP premature stop test - see IOFetchTest::timeoutTest() header.
+// UDP premature stop test - see IOFetchTest::timeoutTest() header.
 TEST_F(IOFetchTest, UdpTimeout) {
     timeoutTest(IOFetch::UDP, udp_fetch_);
 }

+ 4 - 2
src/lib/asiolink/tests/run_unittests.cc

@@ -14,13 +14,15 @@
 
 #include <gtest/gtest.h>
 
+#include <log/root_logger_name.h>
 #include <dns/tests/unittest_util.h>
 
 int
 main(int argc, char* argv[])
 {
-    ::testing::InitGoogleTest(&argc, argv);
-    isc::UnitTestUtil::addDataPath(TEST_DATA_DIR);
+    ::testing::InitGoogleTest(&argc, argv);         // Initialize Google test
+    isc::log::setRootLoggerName("unittest");        // Set a root logger name
+    isc::UnitTestUtil::addDataPath(TEST_DATA_DIR);  // Add location of test data
 
     return (RUN_ALL_TESTS());
 }