Changeset - 004f9410384a
[Not reviewed]
0 9 0
Jan Kaluza - 10 years ago 2015-12-21 13:41:30
jkaluza@redhat.com
Slack: Handle disconnection from 3rd-party network
9 files changed with 39 insertions and 3 deletions:
0 comments (0 inline, 0 general)
include/transport/User.h
Show inline comments
 
@@ -131,12 +131,16 @@ class User {
 
		void setCacheMessages(bool cacheMessages);
 

	
 
		bool shouldCacheMessages() {
 
			return m_cacheMessages;
 
		}
 

	
 
		void setReconnectLimit(int limit) {
 
			m_reconnectLimit = limit;
 
		}
 

	
 
		boost::signal<void ()> onReadyToConnect;
 
		boost::signal<void (Swift::Presence::ref presence)> onPresenceChanged;
 
		boost::signal<void (Swift::Presence::ref presence)> onRawPresenceReceived;
 
		boost::signal<void (const Swift::JID &who, const std::string &room, const std::string &nickname, const std::string &password)> onRoomJoined;
 
		boost::signal<void (const std::string &room)> onRoomLeft;
 
		boost::signal<void ()> onDisconnected;
 
@@ -162,9 +166,10 @@ class User {
 
		std::vector<boost::shared_ptr<Swift::OutgoingFileTransfer> > m_filetransfers;
 
		int m_resources;
 
		int m_reconnectCounter;
 
		std::list<Swift::Presence::ref> m_joinedRooms;
 
		std::map<std::string, std::string> m_settings;
 
		bool m_cacheMessages;
 
		int m_reconnectLimit;
 
};
 

	
 
}
spectrum/src/frontends/slack/SlackFrontend.cpp
Show inline comments
 
@@ -95,13 +95,15 @@ void SlackFrontend::reconnectUser(const std::string &user) {
 

	
 
RosterManager *SlackFrontend::createRosterManager(User *user, Component *component) {
 
	return new SlackRosterManager(user, component);
 
}
 

	
 
User *SlackFrontend::createUser(const Swift::JID &jid, UserInfo &userInfo, Component *component, UserManager *userManager) {
 
	return new SlackUser(jid, userInfo, component, userManager);
 
	SlackUser *user = new SlackUser(jid, userInfo, component, userManager);
 
	user->setReconnectLimit(-1);
 
	return user;
 
}
 

	
 
UserManager *SlackFrontend::createUserManager(Component *component, UserRegistry *userRegistry, StorageBackend *storageBackend) {
 
	m_userManager = new SlackUserManager(component, userRegistry, storageBackend);
 
	return m_userManager;
 
}
spectrum/src/frontends/slack/SlackFrontendPlugin.cpp
Show inline comments
 
@@ -31,13 +31,13 @@ SlackFrontendPlugin::SlackFrontendPlugin() {
 

	
 
SlackFrontendPlugin::~SlackFrontendPlugin() {
 
	
 
}
 

	
 
std::string SlackFrontendPlugin::name() const {
 
	return "xmpp";
 
	return "slack";
 
}
 

	
 
Frontend *SlackFrontendPlugin::createFrontend() {
 
	return new SlackFrontend();
 
}
 

	
spectrum/src/frontends/slack/SlackSession.cpp
Show inline comments
 
@@ -88,12 +88,22 @@ void SlackSession::sendOnlineBuddies() {
 
			setPurpose(onlineBuddies, m_jid2channel[to]);
 
		}
 
	}
 
	m_onlineBuddiesTimer->start();
 
}
 

	
 
void SlackSession::sendMessageToAll(const std::string &msg) {
 
	std::vector<std::string> channels;
 
	for (std::map<std::string, std::string>::const_iterator it = m_jid2channel.begin(); it != m_jid2channel.end(); it++) {
 
		if (std::find(channels.begin(), channels.end(), it->second) == channels.end()) {
 
			channels.push_back(it->second);
 
			m_rtm->getAPI()->sendMessage("Soectrum 2", it->second, msg);
 
		}
 
	}
 
}
 

	
 
void SlackSession::sendMessage(boost::shared_ptr<Swift::Message> message) {
 
	if (m_user) {
 
		std::map<std::string, Conversation *> convs = m_user->getConversationManager()->getConversations();
 
		for (std::map<std::string, Conversation *> ::const_iterator it = convs.begin(); it != convs.end(); it++) {
 
			Conversation *conv = it->second;
 
			if (!conv) {
spectrum/src/frontends/slack/SlackSession.h
Show inline comments
 
@@ -48,12 +48,14 @@ class SlackSession {
 
		virtual ~SlackSession();
 

	
 
		boost::signal<void (const std::string &user)> onInstallationDone;
 

	
 
		void sendMessage(boost::shared_ptr<Swift::Message> message);
 

	
 
		void sendMessageToAll(const std::string &msg);
 

	
 
		void setPurpose(const std::string &purpose, const std::string &channel = "");
 

	
 
		void setUser(User *user) {
 
			m_user = user;
 
		}
 

	
spectrum/src/frontends/slack/SlackUser.cpp
Show inline comments
 
@@ -52,11 +52,23 @@ SlackUser::~SlackUser(){
 
	if (m_session) {
 
		delete m_session;
 
	}
 
}
 

	
 
void SlackUser::disconnectUser(const std::string &error, Swift::SpectrumErrorPayload::Error e) {
 
	if (!m_session) {
 
		return;
 
	}
 

	
 
	if (!error.empty()) {
 
		m_session->sendMessageToAll(error);
 
	}
 
	else {
 
		m_session->sendMessageToAll("Disconnected from 3rd-party network for unknown reason.");
 
	}
 
	m_session->sendMessageToAll("Try using ```.spectrum2 reconnect``` to reconnect.");
 
	static_cast<SlackUserManager *>(m_userManager)->moveTempSession(m_jid.toString(), m_session);
 
	m_session = NULL;
 
}
 

	
 

	
 
}
spectrum/src/frontends/slack/SlackUserManager.cpp
Show inline comments
 
@@ -74,12 +74,16 @@ SlackSession *SlackUserManager::moveTempSession(const std::string &user) {
 
		m_tempSessions.erase(user);
 
		return session;
 
	}
 
	return NULL;
 
}
 

	
 
void SlackUserManager::moveTempSession(const std::string &user, SlackSession *session) {
 
	m_tempSessions[user] = session;
 
}
 

	
 

	
 
UserRegistration *SlackUserManager::getUserRegistration() {
 
	return m_userRegistration;
 
}
 

	
 
std::string SlackUserManager::handleOAuth2Code(const std::string &code, const std::string &state) {
spectrum/src/frontends/slack/SlackUserManager.h
Show inline comments
 
@@ -57,12 +57,13 @@ class SlackUserManager : public UserManager {
 

	
 
		std::string getOAuth2URL(const std::vector<std::string> &args);
 

	
 
		void sendMessage(boost::shared_ptr<Swift::Message> message);
 

	
 
		SlackSession *moveTempSession(const std::string &user);
 
		void moveTempSession(const std::string &user, SlackSession *session);
 

	
 
	private:
 
		Component *m_component;
 
		UserRegistration *m_userRegistration;
 
		StorageBackend *m_storageBackend;
 
		std::map<std::string, SlackSession *> m_tempSessions;
src/User.cpp
Show inline comments
 
@@ -394,13 +394,13 @@ void User::handleDisconnected(const std::string &error, Swift::SpectrumErrorPayl
 
	if (m_ignoreDisconnect) {
 
		LOG4CXX_INFO(logger, m_jid.toString() << ": Disconnecting from legacy network ignored (probably moving between backends)");
 
		return;
 
	}
 

	
 
	if (e == Swift::SpectrumErrorPayload::CONNECTION_ERROR_OTHER_ERROR || e == Swift::SpectrumErrorPayload::CONNECTION_ERROR_NETWORK_ERROR) {
 
		if (m_reconnectCounter < 3) {
 
		if (m_reconnectLimit < 0 || m_reconnectCounter < m_reconnectLimit) {
 
			m_reconnectCounter++;
 
			LOG4CXX_INFO(logger, m_jid.toString() << ": Disconnecting from legacy network " << error << ", trying to reconnect automatically.");
 
			// Simulate destruction/resurrection :)
 
			// TODO: If this stops working, create onReconnect signal
 
			m_userManager->onUserDestroyed(this);
 
			m_userManager->onUserCreated(this);
0 comments (0 inline, 0 general)