Browse Source

[trac999] added toText() and stream insertion for Client, mainly for logging.

JINMEI Tatuya 14 years ago
parent
commit
e9a1e75b3d

+ 16 - 0
src/lib/server_common/client.cc

@@ -12,6 +12,9 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
+#include <string>
+#include <sstream>
+
 #include <acl/ip_check.h>
 
 #include <asiolink/io_endpoint.h>
@@ -51,6 +54,19 @@ Client::getRequestSourceIPAddress() const {
     return (impl_->request_src_);
 }
 
+std::string
+Client::toText() const {
+    std::stringstream ss;
+    ss << impl_->request_.getRemoteEndpoint().getAddress().toText()
+       << '#' << impl_->request_.getRemoteEndpoint().getPort();
+    return (ss.str());
+}
+
+std::ostream&
+isc::server_common::operator<<(std::ostream& os, const Client& client) {
+    return (os << client.toText());
+}
+
 template <>
 bool
 IPCheck<Client>::matches(const Client& client) const {

+ 17 - 1
src/lib/server_common/client.h

@@ -15,6 +15,9 @@
 #ifndef __CLIENT_H
 #define __CLIENT_H 1
 
+#include <string>
+#include <ostream>
+
 #include <boost/noncopyable.hpp>
 
 #include <acl/ip_check.h>
@@ -35,16 +38,29 @@ class Client : boost::noncopyable {
 public:
     explicit Client(const isc::asiolink::IOMessage& request_message);
     ~Client();
-    const isc::asiolink::IOEndpoint& getRemoteEndpoint() const;
     const isc::asiolink::IOEndpoint& getRequestSourceEndpoint() const;
 
     // convenience shortcut
     const isc::acl::IPAddress& getRequestSourceIPAddress() const;
 
+    std::string toText() const;
+
 private:
     struct ClientImpl;
     ClientImpl* impl_;
 };
+
+/// \brief Insert the \c Client as a string into stream.
+///
+/// This method convert \c client into a string and inserts it into the
+/// output stream \c os.
+///
+/// \param os A \c std::ostream object on which the insertion operation is
+/// performed.
+/// \param edns A reference to an \c Client object output by the operation.
+/// \return A reference to the same \c std::ostream object referenced by
+/// parameter \c os after the insertion operation.
+std::ostream& operator<<(std::ostream& os, const Client& client);
 }
 
 namespace acl {

+ 15 - 0
src/lib/server_common/tests/client_unittest.cc

@@ -15,6 +15,9 @@
 #include <sys/socket.h>
 #include <string.h>
 
+#include <string>
+#include <sstream>
+
 #include <boost/scoped_ptr.hpp>
 
 #include <acl/ip_check.h>
@@ -109,4 +112,16 @@ TEST_F(ClientTest, ACLCheckIPv6) {
     EXPECT_FALSE(IPCheck<Client>("2001:db8:1::/64").matches(*client6));
     EXPECT_FALSE(IPCheck<Client>("32.1.13.184").matches(*client6));
 }
+
+TEST_F(ClientTest, toText) {
+    EXPECT_EQ("192.0.2.1#53214", client4->toText());
+    EXPECT_EQ("2001:db8::1#53216", client6->toText());
+}
+
+// test operator<<.  We simply confirm it appends the result of toText().
+TEST_F(ClientTest, LeftShiftOperator) {
+    std::ostringstream oss;
+    oss << *client4 << "more text";
+    EXPECT_EQ(client4->toText() + std::string("more text"), oss.str());
+}
 }