You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by as...@apache.org on 2009/10/09 05:38:09 UTC

svn commit: r823390 - in /qpid/trunk/qpid/cpp: include/qpid/sys/posix/PrivatePosix.h src/Makefile.am src/qpid/sys/Socket.h src/qpid/sys/SocketAddress.h src/qpid/sys/posix/AsynchIO.cpp src/qpid/sys/posix/Socket.cpp src/qpid/sys/posix/SocketAddress.cpp

Author: astitcher
Date: Fri Oct  9 03:38:09 2009
New Revision: 823390

URL: http://svn.apache.org/viewvc?rev=823390&view=rev
Log:
Split responsibility for name lookup in AsynchConnector::connect
into new SocketAddress class.

Added:
    qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h
    qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp
Modified:
    qpid/trunk/qpid/cpp/include/qpid/sys/posix/PrivatePosix.h
    qpid/trunk/qpid/cpp/src/Makefile.am
    qpid/trunk/qpid/cpp/src/qpid/sys/Socket.h
    qpid/trunk/qpid/cpp/src/qpid/sys/posix/AsynchIO.cpp
    qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp

Modified: qpid/trunk/qpid/cpp/include/qpid/sys/posix/PrivatePosix.h
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/include/qpid/sys/posix/PrivatePosix.h?rev=823390&r1=823389&r2=823390&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/include/qpid/sys/posix/PrivatePosix.h (original)
+++ qpid/trunk/qpid/cpp/include/qpid/sys/posix/PrivatePosix.h Fri Oct  9 03:38:09 2009
@@ -27,6 +27,7 @@
 
 struct timespec;
 struct timeval;
+struct addrinfo;
 
 namespace qpid {
 namespace sys {
@@ -36,6 +37,10 @@
 struct timeval& toTimeval(struct timeval& tv, const Duration& t);
 Duration toTime(const struct timespec& ts);
 
+// Private SocketAddress details
+class SocketAddress;
+const struct addrinfo& getAddrInfo(const SocketAddress&);
+
 // Private fd related implementation details
 class IOHandlePrivate {
 public:

Modified: qpid/trunk/qpid/cpp/src/Makefile.am
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/Makefile.am?rev=823390&r1=823389&r2=823390&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/Makefile.am (original)
+++ qpid/trunk/qpid/cpp/src/Makefile.am Fri Oct  9 03:38:09 2009
@@ -144,6 +144,7 @@
   qpid/log/posix/SinkOptions.cpp		\
   qpid/sys/posix/IOHandle.cpp			\
   qpid/sys/posix/Socket.cpp			\
+  qpid/sys/posix/SocketAddress.cpp		\
   qpid/sys/posix/AsynchIO.cpp			\
   qpid/sys/posix/FileSysDir.cpp			\
   qpid/sys/posix/LockFile.cpp			\
@@ -456,6 +457,7 @@
   qpid/sys/Shlib.h				\
   qpid/sys/ShutdownHandler.h			\
   qpid/sys/Socket.h				\
+  qpid/sys/SocketAddress.h			\
   qpid/sys/StateMonitor.h			\
   qpid/sys/TimeoutHandler.h			\
   qpid/sys/Timer.cpp				\

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/Socket.h
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/Socket.h?rev=823390&r1=823389&r2=823390&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/Socket.h (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/Socket.h Fri Oct  9 03:38:09 2009
@@ -31,6 +31,7 @@
 namespace sys {
 
 class Duration;
+class SocketAddress;
 
 class Socket : public IOHandle
 {
@@ -48,6 +49,7 @@
     void setNonblocking() const;
 
     QPID_COMMON_EXTERN void connect(const std::string& host, uint16_t port) const;
+    QPID_COMMON_EXTERN void connect(const SocketAddress&) const;
 
     QPID_COMMON_EXTERN void close() const;
 

Added: qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h?rev=823390&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h (added)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h Fri Oct  9 03:38:09 2009
@@ -0,0 +1,51 @@
+#ifndef _sys_SocketAddress_h
+#define _sys_SocketAddress_h
+
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#include "qpid/sys/IntegerTypes.h"
+#include "qpid/CommonImportExport.h"
+#include <string>
+
+struct addrinfo;
+
+namespace qpid {
+namespace sys {
+
+class SocketAddress {
+    friend const ::addrinfo& getAddrInfo(const SocketAddress&);
+
+public:
+    /** Create a SocketAddress from hostname and port*/
+    QPID_COMMON_EXTERN SocketAddress(const std::string& host, const std::string& port);
+    QPID_COMMON_EXTERN ~SocketAddress();
+
+    std::string asString() const;
+
+private:
+    std::string host;
+    std::string port;
+    ::addrinfo* addrInfo;
+};
+
+}}
+#endif  /*!_sys_SocketAddress_h*/

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/posix/AsynchIO.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/posix/AsynchIO.cpp?rev=823390&r1=823389&r2=823390&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/posix/AsynchIO.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/posix/AsynchIO.cpp Fri Oct  9 03:38:09 2009
@@ -21,6 +21,7 @@
 
 #include "qpid/sys/AsynchIO.h"
 #include "qpid/sys/Socket.h"
+#include "qpid/sys/SocketAddress.h"
 #include "qpid/sys/Poller.h"
 #include "qpid/sys/DispatchHandle.h"
 #include "qpid/sys/Time.h"
@@ -37,6 +38,7 @@
 #include <string.h>
 
 #include <boost/bind.hpp>
+#include <boost/lexical_cast.hpp>
 
 using namespace qpid::sys;
 
@@ -193,8 +195,9 @@
     socket(s)
 {
     socket.setNonblocking();
+    SocketAddress sa(hostname, boost::lexical_cast<std::string>(port));
     try {
-        socket.connect(hostname, port);
+        socket.connect(sa);
     } catch(std::exception& e) {
         // Defer reporting failure
         startWatch(poller);

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp?rev=823390&r1=823389&r2=823390&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp Fri Oct  9 03:38:09 2009
@@ -21,6 +21,7 @@
 
 #include "qpid/sys/Socket.h"
 
+#include "qpid/sys/SocketAddress.h"
 #include "qpid/sys/posix/check.h"
 #include "qpid/sys/posix/PrivatePosix.h"
 
@@ -36,6 +37,7 @@
 #include <iostream>
 
 #include <boost/format.hpp>
+#include <boost/lexical_cast.hpp>
 
 namespace qpid {
 namespace sys {
@@ -126,42 +128,23 @@
     QPID_POSIX_CHECK(::fcntl(impl->fd, F_SETFL, O_NONBLOCK));
 }
 
-namespace {
-const char* h_errstr(int e) {
-    switch (e) {
-      case HOST_NOT_FOUND: return "Host not found";
-      case NO_ADDRESS: return "Name does not have an IP address";
-      case TRY_AGAIN: return "A temporary error occurred on an authoritative name server.";
-      case NO_RECOVERY: return "Non-recoverable name server error";
-      default: return "Unknown error";
-    }
-}
+void Socket::connect(const std::string& host, uint16_t port) const
+{
+    SocketAddress sa(host, boost::lexical_cast<std::string>(port));
+    connect(sa);
 }
 
-void Socket::connect(const std::string& host, uint16_t p) const
+void Socket::connect(const SocketAddress& addr) const
 {
-    std::stringstream portstream;
-    portstream << p;
-    std::string port = portstream.str();
-    connectname = host + ":" + port;
+    connectname = addr.asString();
 
     const int& socket = impl->fd;
 
-    ::addrinfo *res;
-    ::addrinfo hints;
-    ::memset(&hints, 0, sizeof(hints));
-    hints.ai_family = AF_INET; // In order to allow AF_INET6 we'd have to change createTcp() as well
-    hints.ai_socktype = SOCK_STREAM;
-    int n = ::getaddrinfo(host.c_str(), port.c_str(), &hints, &res);
-    if (n != 0)
-        throw Exception(QPID_MSG("Cannot resolve " << host << ": " << ::gai_strerror(n)));
     // TODO the correct thing to do here is loop on failure until you've used all the returned addresses
-    if ((::connect(socket, res->ai_addr, res->ai_addrlen) < 0) &&
+    if ((::connect(socket, getAddrInfo(addr).ai_addr, getAddrInfo(addr).ai_addrlen) < 0) &&
         (errno != EINPROGRESS)) {
-        ::freeaddrinfo(res);
-        throw qpid::Exception(QPID_MSG(strError(errno) << ": " << host << ":" << port));
+        throw Exception(QPID_MSG(strError(errno) << ": " << connectname));
     }
-    ::freeaddrinfo(res);
 }
 
 void
@@ -226,9 +209,10 @@
 
 std::string Socket::getPeerAddress() const
 {
-    if (!connectname.empty())
-        return std::string (connectname);
-    return getName(impl->fd, false, true);
+    if (connectname.empty()) {
+        connectname = getName(impl->fd, false, true);
+    }
+    return connectname;
 }
 
 std::string Socket::getLocalAddress() const

Added: qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp?rev=823390&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp (added)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp Fri Oct  9 03:38:09 2009
@@ -0,0 +1,62 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#include "qpid/sys/SocketAddress.h"
+
+#include "qpid/sys/posix/check.h"
+
+#include <sys/socket.h>
+#include <string.h>
+#include <netdb.h>
+
+namespace qpid {
+namespace sys {
+
+SocketAddress::SocketAddress(const std::string& host0, const std::string& port0) :
+    host(host0),
+    port(port0),
+    addrInfo(0)
+{
+    ::addrinfo hints;
+    ::memset(&hints, 0, sizeof(hints));
+    hints.ai_family = AF_INET; // In order to allow AF_INET6 we'd have to change createTcp() as well
+    hints.ai_socktype = SOCK_STREAM;
+    int n = ::getaddrinfo(host.c_str(), port.c_str(), &hints, &addrInfo);
+    if (n != 0)
+        throw Exception(QPID_MSG("Cannot resolve " << host << ": " << ::gai_strerror(n)));
+}
+
+SocketAddress::~SocketAddress()
+{
+    ::freeaddrinfo(addrInfo);
+}
+
+std::string SocketAddress::asString() const
+{
+    return host + ":" + port;
+}
+
+const ::addrinfo& getAddrInfo(const SocketAddress& sa)
+{
+    return *sa.addrInfo;
+}
+
+}}



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org