Changeset - fedd9216b2c9
[Not reviewed]
0 2 0
Jan Kaluza - 10 years ago 2015-11-25 16:55:35
jkaluza@redhat.com
Reconnect Slack RTM on connection error
2 files changed with 17 insertions and 6 deletions:
0 comments (0 inline, 0 general)
include/transport/WebSocketClient.h
Show inline comments
 
@@ -27,12 +27,13 @@
 
#include <Swiften/Network/DomainNameAddressQuery.h>
 
#include <Swiften/Network/DomainNameResolver.h>
 
#include <Swiften/Network/HostAddress.h>
 
#include <Swiften/Network/Connection.h>
 
#include <Swiften/Base/SafeByteArray.h>
 
#include "Swiften/Version.h"
 
#include "Swiften/Network/Timer.h"
 

	
 
#define HAVE_SWIFTEN_3  (SWIFTEN_VERSION >= 0x030000)
 

	
 
#if HAVE_SWIFTEN_3
 
#include <Swiften/TLS/TLSOptions.h>
 
#endif
 
@@ -61,19 +62,22 @@ class WebSocketClient {
 

	
 
	private:
 
		void handleDNSResult(const std::vector<Swift::HostAddress>&, boost::optional<Swift::DomainNameResolveError>);
 
		void handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data);
 
		void handleConnected(bool error);
 

	
 
		void connectServer();
 

	
 
	private:
 
		Component *m_component;
 
		boost::shared_ptr<Swift::DomainNameAddressQuery> m_dnsQuery;
 
		boost::shared_ptr<Swift::Connection> m_conn;
 
		Swift::TLSConnectionFactory *m_tlsConnectionFactory;
 
		Swift::PlatformTLSFactories *m_tlsFactory;
 
		std::string m_host;
 
		std::string m_path;
 
		std::string m_buffer;
 
		bool m_upgraded;
 
		Swift::Timer::ref m_reconnectTimer;
 
};
 

	
 
}
src/WebSocketClient.cpp
Show inline comments
 
@@ -42,33 +42,39 @@ WebSocketClient::WebSocketClient(Component *component) {
 
	m_tlsFactory = new Swift::PlatformTLSFactories();
 
#if HAVE_SWIFTEN_3
 
	m_tlsConnectionFactory = new Swift::TLSConnectionFactory(m_tlsFactory->getTLSContextFactory(), component->getNetworkFactories()->getConnectionFactory(), o);
 
#else
 
	m_tlsConnectionFactory = new Swift::TLSConnectionFactory(m_tlsFactory->getTLSContextFactory(), component->getNetworkFactories()->getConnectionFactory());
 
#endif
 

	
 
	m_reconnectTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(1000);
 
	m_reconnectTimer->onTick.connect(boost::bind(&WebSocketClient::connectServer, this));
 
}
 

	
 
WebSocketClient::~WebSocketClient() {
 
	if (m_conn) {
 
		m_conn->onDataRead.disconnect(boost::bind(&WebSocketClient::handleDataRead, this, _1));
 
		m_conn->disconnect();
 
	}
 

	
 
	delete m_tlsFactory;
 
	delete m_tlsConnectionFactory;
 
}
 

	
 
void WebSocketClient::connectServer(const std::string &url) {
 
	std::string u = url.substr(6);
 
	m_host = u.substr(0, u.find("/"));
 
	m_path = u.substr(u.find("/"));
 

	
 
void WebSocketClient::connectServer() {
 
	LOG4CXX_INFO(logger, "Starting DNS query for " << m_host << " " << m_path);
 
	m_dnsQuery = m_component->getNetworkFactories()->getDomainNameResolver()->createAddressQuery(m_host);
 
	m_dnsQuery->onResult.connect(boost::bind(&WebSocketClient::handleDNSResult, this, _1, _2));
 
	m_dnsQuery->run();
 
	m_reconnectTimer->stop();
 
}
 

	
 
void WebSocketClient::connectServer(const std::string &url) {
 
	std::string u = url.substr(6);
 
	m_host = u.substr(0, u.find("/"));
 
	m_path = u.substr(u.find("/"));
 
}
 

	
 
void WebSocketClient::write(const std::string &data) {
 
	if (!m_conn) {
 
		return;
 
	}
 
@@ -162,13 +168,14 @@ void WebSocketClient::handleDataRead(boost::shared_ptr<Swift::SafeByteArray> dat
 
		}
 
	}
 
}
 

	
 
void WebSocketClient::handleConnected(bool error) {
 
	if (error) {
 
		LOG4CXX_ERROR(logger, "Connection to " << m_host << " failed");
 
		LOG4CXX_ERROR(logger, "Connection to " << m_host << " failed. Will reconnect in 1 second.");
 
		m_reconnectTimer->start();
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, "Connected to " << m_host);
 

	
 
	std::string req = "";
0 comments (0 inline, 0 general)