From 387739c84f5f4860df6e3f69bb89b839669c4888 Mon Sep 17 00:00:00 2001 From: lotodore Date: Thu, 10 Feb 2011 22:16:36 +0000 Subject: [PATCH] Implementing ticket #47 Automatic notifications before server restart. --- src/net/common/serveradminbot.cpp | 80 ++++++++++++++++++++++++++++++- src/net/common/serverlobbybot.cpp | 2 +- src/net/common/servermanager.cpp | 2 +- src/net/serveradminbot.h | 8 +++- src/net/serverlobbybot.h | 2 +- 5 files changed, 89 insertions(+), 5 deletions(-) diff --git a/src/net/common/serveradminbot.cpp b/src/net/common/serveradminbot.cpp index f28e9fdc..60568fa8 100644 --- a/src/net/common/serveradminbot.cpp +++ b/src/net/common/serveradminbot.cpp @@ -34,7 +34,9 @@ using namespace std; ServerAdminBot::ServerAdminBot() -: m_ircRestartTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::auto_start) +: m_notifyTimeoutMinutes(0), m_notifyIntervalMinutes(0), m_notifyCounter(0), + m_ircRestartTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::second_timer::auto_start), + m_notifyTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::second_timer::manual_start) { } @@ -231,6 +233,30 @@ ServerAdminBot::SignalIrcChatMsg(const std::string &nickName, const std::string else m_ircAdminThread->SendChatMessage(nickName + ": Invalid message."); } + else if (command == "notify-restart") + { + int timeoutMinutes = 0; + int intervalMinutes = 0; + msgStream >> timeoutMinutes; + msgStream >> intervalMinutes; + if (timeoutMinutes && intervalMinutes) + { + ostringstream ack; + ack << nickName << ": Restart planned in " << timeoutMinutes << " minutes, notifying every " << intervalMinutes << " minutes."; + m_ircAdminThread->SendChatMessage(ack.str()); + boost::mutex::scoped_lock lock(m_notifyMutex); + m_notifyTimeoutMinutes = timeoutMinutes; + m_notifyIntervalMinutes = intervalMinutes; + m_notifyCounter = 0; + m_notifyTimer.reset(); + m_notifyTimer.start(); + } + else + { + m_ircAdminThread->SendChatMessage(nickName + ": Invalid parameters."); + } + + } else m_ircAdminThread->SendChatMessage(nickName + ": Invalid command \"" + command + "\"."); } @@ -278,6 +304,58 @@ ServerAdminBot::Process() m_ircRestartTimer.reset(); m_ircRestartTimer.start(); } + { + boost::mutex::scoped_lock lock(m_notifyMutex); + + if (m_notifyTimeoutMinutes && m_notifyTimer.elapsed().total_seconds() >= m_notifyCounter * m_notifyIntervalMinutes * 60) + { + int remainingMinutes = m_notifyTimeoutMinutes - m_notifyCounter * m_notifyIntervalMinutes; + ++m_notifyCounter; + if (remainingMinutes > 1) + { + ostringstream notifyStream; + notifyStream << "The server will be restarted in " << remainingMinutes << " minutes."; + GetLobbyThread().SendGlobalChat(notifyStream.str()); + notifyStream.str(""); + notifyStream << "Der Server wird in " << remainingMinutes << " Minuten neu gestartet werden."; + GetLobbyThread().SendGlobalChat(notifyStream.str()); + notifyStream.str(""); + notifyStream << "Le serveur va être redémarré dans " << remainingMinutes << " minutes."; + GetLobbyThread().SendGlobalChat(notifyStream.str()); + notifyStream.str(""); + notifyStream << "Il server verrà riavviato in " << remainingMinutes << " minuti."; + GetLobbyThread().SendGlobalChat(notifyStream.str()); + notifyStream.str(""); + notifyStream << "El servidor se reiniciará en " << remainingMinutes << " minutos."; + GetLobbyThread().SendGlobalChat(notifyStream.str()); + } + else if (remainingMinutes == 1) + { + ostringstream notifyStream; + notifyStream << "The server will be restarted in " << remainingMinutes << " minute."; + GetLobbyThread().SendGlobalChat(notifyStream.str()); + notifyStream.str(""); + notifyStream << "Der Server wird in " << remainingMinutes << " Minute neu gestartet werden."; + GetLobbyThread().SendGlobalChat(notifyStream.str()); + notifyStream.str(""); + notifyStream << "Le serveur va être redémarré dans " << remainingMinutes << " minute."; + GetLobbyThread().SendGlobalChat(notifyStream.str()); + notifyStream.str(""); + notifyStream << "Il server verrà riavviato in " << remainingMinutes << " minuto."; + GetLobbyThread().SendGlobalChat(notifyStream.str()); + notifyStream.str(""); + notifyStream << "El servidor se reiniciará en " << remainingMinutes << " minuto."; + GetLobbyThread().SendGlobalChat(notifyStream.str()); + } + else + { + GetLobbyThread().SendGlobalChat("The server will be restarted NOW."); + GetLobbyThread().SendGlobalMsgBox("The server will be restarted NOW."); + m_notifyTimeoutMinutes = m_notifyCounter = m_notifyIntervalMinutes = 0; + m_notifyTimer.reset(); + } + } + } } void diff --git a/src/net/common/serverlobbybot.cpp b/src/net/common/serverlobbybot.cpp index 92e291b6..71107d38 100644 --- a/src/net/common/serverlobbybot.cpp +++ b/src/net/common/serverlobbybot.cpp @@ -34,7 +34,7 @@ using namespace std; ServerLobbyBot::ServerLobbyBot() -: m_ircRestartTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::auto_start) +: m_ircRestartTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::second_timer::auto_start) { } diff --git a/src/net/common/servermanager.cpp b/src/net/common/servermanager.cpp index 63f91a77..07254c3d 100644 --- a/src/net/common/servermanager.cpp +++ b/src/net/common/servermanager.cpp @@ -119,7 +119,7 @@ ServerManager::JoinAll(bool wait) ServerLobbyThread & ServerManager::GetLobbyThread() { - assert(m_lobbyThread.get()); + assert(m_lobbyThread); return *m_lobbyThread; } diff --git a/src/net/serveradminbot.h b/src/net/serveradminbot.h index dd1af229..40436694 100644 --- a/src/net/serveradminbot.h +++ b/src/net/serveradminbot.h @@ -22,6 +22,7 @@ #define _SERVERADMINBOT_H_ #include +#include #include #include #include @@ -69,10 +70,15 @@ protected: private: std::string m_ircNick; + mutable boost::mutex m_notifyMutex; + int m_notifyTimeoutMinutes; + int m_notifyIntervalMinutes; + int m_notifyCounter; boost::shared_ptr m_lobbyThread; boost::shared_ptr m_ircAdminThread; - boost::timers::portable::microsec_timer m_ircRestartTimer; + boost::timers::portable::second_timer m_ircRestartTimer; + boost::timers::portable::second_timer m_notifyTimer; }; #endif diff --git a/src/net/serverlobbybot.h b/src/net/serverlobbybot.h index dbb99ca1..e418b4a9 100644 --- a/src/net/serverlobbybot.h +++ b/src/net/serverlobbybot.h @@ -74,7 +74,7 @@ private: boost::shared_ptr m_lobbyThread; boost::shared_ptr m_ircLobbyThread; - boost::timers::portable::microsec_timer m_ircRestartTimer; + boost::timers::portable::second_timer m_ircRestartTimer; }; #endif