Be verbose about IRC server connection failures, and handle them properly

This commit is contained in:
Florent Le Coz
2014-02-08 07:52:22 +01:00
parent de7b3503a4
commit c8b41d5e35
4 changed files with 20 additions and 11 deletions
+6 -1
View File
@@ -27,7 +27,12 @@ IrcClient::~IrcClient()
void IrcClient::start() void IrcClient::start()
{ {
this->connect(this->hostname, "6667"); this->bridge->send_xmpp_message(this->hostname, "", std::string("Connecting to ") +
this->hostname + ":" + "6667");
std::pair<bool, std::string> res = this->connect(this->hostname, "6667");
if (!res.first)
this->bridge->send_xmpp_message(this->hostname, "",
std::string("Connection failed: ") + res.second);
} }
void IrcClient::on_connected() void IrcClient::on_connected()
+11 -8
View File
@@ -23,7 +23,7 @@ SocketHandler::SocketHandler():
throw std::runtime_error("Could not create socket"); throw std::runtime_error("Could not create socket");
} }
bool SocketHandler::connect(const std::string& address, const std::string& port) std::pair<bool, std::string> SocketHandler::connect(const std::string& address, const std::string& port)
{ {
log_info("Trying to connect to " << address << ":" << port); log_info("Trying to connect to " << address << ":" << port);
struct addrinfo hints; struct addrinfo hints;
@@ -35,15 +35,18 @@ bool SocketHandler::connect(const std::string& address, const std::string& port)
struct addrinfo* addr_res; struct addrinfo* addr_res;
const int res = ::getaddrinfo(address.c_str(), port.c_str(), &hints, &addr_res); const int res = ::getaddrinfo(address.c_str(), port.c_str(), &hints, &addr_res);
if (res != 0)
{
log_warning(std::string("getaddrinfo failed: ") + gai_strerror(res));
this->close();
return std::make_pair(false, gai_strerror(res));
}
// Make sure the alloced structure is always freed at the end of the // Make sure the alloced structure is always freed at the end of the
// function // function
utils::ScopeGuard sg([&addr_res](){ freeaddrinfo(addr_res); }); utils::ScopeGuard sg([&addr_res](){ freeaddrinfo(addr_res); });
if (res != 0)
{
perror("getaddrinfo");
throw std::runtime_error("getaddrinfo failed");
}
for (struct addrinfo* rp = addr_res; rp; rp = rp->ai_next) for (struct addrinfo* rp = addr_res; rp; rp = rp->ai_next)
{ {
if (::connect(this->socket, rp->ai_addr, rp->ai_addrlen) == 0) if (::connect(this->socket, rp->ai_addr, rp->ai_addrlen) == 0)
@@ -51,14 +54,14 @@ bool SocketHandler::connect(const std::string& address, const std::string& port)
log_info("Connection success."); log_info("Connection success.");
this->connected = true; this->connected = true;
this->on_connected(); this->on_connected();
return true; return std::make_pair(true, "");
} }
log_info("Connection failed:"); log_info("Connection failed:");
perror("connect"); perror("connect");
} }
log_error("All connection attempts failed."); log_error("All connection attempts failed.");
this->close(); this->close();
return false; return std::make_pair(false, "");
} }
void SocketHandler::set_poller(Poller* poller) void SocketHandler::set_poller(Poller* poller)
+2 -1
View File
@@ -2,6 +2,7 @@
# define SOCKET_HANDLER_INCLUDED # define SOCKET_HANDLER_INCLUDED
#include <string> #include <string>
#include <utility>
typedef int socket_t; typedef int socket_t;
@@ -21,7 +22,7 @@ public:
/** /**
* Connect to the remote server, and call on_connected() if this succeeds * Connect to the remote server, and call on_connected() if this succeeds
*/ */
bool connect(const std::string& address, const std::string& port); std::pair<bool, std::string> connect(const std::string& address, const std::string& port);
/** /**
* Set the pointer to the given Poller, to communicate with it. * Set the pointer to the given Poller, to communicate with it.
*/ */
+1 -1
View File
@@ -49,7 +49,7 @@ XmppComponent::~XmppComponent()
bool XmppComponent::start() bool XmppComponent::start()
{ {
return this->connect("127.0.0.1", "5347"); return this->connect("127.0.0.1", "5347").first;
} }
bool XmppComponent::is_document_open() const bool XmppComponent::is_document_open() const