From ad08b5c6eb216e5f35a10df25220687c51b1de77 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Fri, 18 Jan 2008 00:11:27 +0000 Subject: Add optional host, port arguments to all example clients. Verify can start private broker & pass host/port to examples, by default it still uses local host/standard port. Added host:port to Socket error messages. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@613018 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/sys/posix/Socket.cpp | 16 ++++++++++++++-- cpp/src/qpid/sys/posix/check.h | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) (limited to 'cpp/src') diff --git a/cpp/src/qpid/sys/posix/Socket.cpp b/cpp/src/qpid/sys/posix/Socket.cpp index f0cc8cd5a5..6e872c4fbc 100644 --- a/cpp/src/qpid/sys/posix/Socket.cpp +++ b/cpp/src/qpid/sys/posix/Socket.cpp @@ -113,6 +113,17 @@ void Socket::setNonblocking() const { 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, int port) const { @@ -123,10 +134,11 @@ void Socket::connect(const std::string& host, int port) const // TODO: Be good to make this work for IPv6 as well as IPv4 // Use more modern lookup functions struct hostent* hp = gethostbyname ( host.c_str() ); - if (hp == 0) throw QPID_POSIX_ERROR(errno); + if (hp == 0) + throw Exception(QPID_MSG("Cannot resolve " << host << ": " << h_errstr(h_errno))); memcpy(&name.sin_addr.s_addr, hp->h_addr_list[0], hp->h_length); if (::connect(socket, (struct sockaddr*)(&name), sizeof(name)) < 0) - throw QPID_POSIX_ERROR(errno); + throw qpid::Exception(QPID_MSG(strError(errno) << ": " << host << ":" << port)); } void diff --git a/cpp/src/qpid/sys/posix/check.h b/cpp/src/qpid/sys/posix/check.h index f864bf8762..40aa0d4d27 100644 --- a/cpp/src/qpid/sys/posix/check.h +++ b/cpp/src/qpid/sys/posix/check.h @@ -26,7 +26,7 @@ #include -#define QPID_POSIX_ERROR(ERRNO) qpid::Exception(QPID_MSG(qpid::strError(ERRNO)) << " " << ERRNO) +#define QPID_POSIX_ERROR(ERRNO) qpid::Exception(QPID_MSG(qpid::strError(ERRNO))); /** THROW QPID_POSIX_ERROR(errno) if RESULT is less than zero */ #define QPID_POSIX_CHECK(RESULT) \ -- cgit v1.2.1