Slack: Handle disconnection from 3rd-party network

This commit is contained in:
Jan Kaluza 2015-12-21 13:41:30 +01:00
parent b44919ed9b
commit 004f941038
9 changed files with 39 additions and 3 deletions

View file

@ -134,6 +134,10 @@ class User {
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;
@ -165,6 +169,7 @@ class User {
std::list<Swift::Presence::ref> m_joinedRooms;
std::map<std::string, std::string> m_settings;
bool m_cacheMessages;
int m_reconnectLimit;
};
}

View file

@ -98,7 +98,9 @@ RosterManager *SlackFrontend::createRosterManager(User *user, Component *compone
}
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) {

View file

@ -34,7 +34,7 @@ SlackFrontendPlugin::~SlackFrontendPlugin() {
}
std::string SlackFrontendPlugin::name() const {
return "xmpp";
return "slack";
}
Frontend *SlackFrontendPlugin::createFrontend() {

View file

@ -91,6 +91,16 @@ void SlackSession::sendOnlineBuddies() {
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();

View file

@ -51,6 +51,8 @@ class SlackSession {
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) {

View file

@ -55,7 +55,19 @@ SlackUser::~SlackUser(){
}
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;
}

View file

@ -77,6 +77,10 @@ SlackSession *SlackUserManager::moveTempSession(const std::string &user) {
return NULL;
}
void SlackUserManager::moveTempSession(const std::string &user, SlackSession *session) {
m_tempSessions[user] = session;
}
UserRegistration *SlackUserManager::getUserRegistration() {
return m_userRegistration;

View file

@ -60,6 +60,7 @@ class SlackUserManager : public UserManager {
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;

View file

@ -397,7 +397,7 @@ void User::handleDisconnected(const std::string &error, Swift::SpectrumErrorPayl
}
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 :)