Force reconnect of the irc bot by creating a file in the cache folder.

This commit is contained in:
lotodore
2012-03-03 14:16:45 +00:00
parent 93236dcc8d
commit 06145f30e9
11 changed files with 142 additions and 87 deletions
+86 -18
View File
@@ -25,17 +25,21 @@
#include <net/socket_startup.h>
#include <core/loghelper.h>
#include <boost/filesystem.hpp>
#include <boost/bind.hpp>
#include <boost/algorithm/string/predicate.hpp>
#define SERVER_RESTART_IRC_BOT_INTERVAL_SEC 86400 // 1 day
#define SERVER_NOTIFY_IRC_BOT_INTERVAL_SEC 1
#define SERVER_CHECK_IRC_BOT_INTERVAL_SEC 60
using namespace std;
using namespace boost::filesystem;
ServerAdminBot::ServerAdminBot()
ServerAdminBot::ServerAdminBot(boost::shared_ptr<boost::asio::io_service> ioService)
: 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)
m_notifyTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::second_timer::manual_start),
m_reconnectTimer(*ioService), m_notifyLoopTimer(*ioService), m_checkFileTimer(*ioService)
{
}
@@ -44,10 +48,11 @@ ServerAdminBot::~ServerAdminBot()
}
void
ServerAdminBot::Init(boost::shared_ptr<ServerLobbyThread> lobbyThread, boost::shared_ptr<IrcThread> ircAdminThread)
ServerAdminBot::Init(boost::shared_ptr<ServerLobbyThread> lobbyThread, boost::shared_ptr<IrcThread> ircAdminThread, const std::string &cacheDir)
{
m_lobbyThread = lobbyThread;
m_ircAdminThread = ircAdminThread;
m_cacheDir = cacheDir;
}
void
@@ -251,26 +256,78 @@ ServerAdminBot::SignalIrcServerError(int errorCode)
void
ServerAdminBot::Run()
{
if (m_ircAdminThread)
if (m_ircAdminThread) {
// Initialise the timers.
m_reconnectTimer.expires_from_now(
boost::posix_time::seconds(SERVER_RESTART_IRC_BOT_INTERVAL_SEC));
m_reconnectTimer.async_wait(
boost::bind(
&ServerAdminBot::ReconnectHandler, shared_from_this(), boost::asio::placeholders::error));
m_notifyLoopTimer.expires_from_now(
boost::posix_time::seconds(SERVER_NOTIFY_IRC_BOT_INTERVAL_SEC));
m_notifyLoopTimer.async_wait(
boost::bind(
&ServerAdminBot::NotifyLoop, shared_from_this(), boost::asio::placeholders::error));
m_checkFileTimer.expires_from_now(
boost::posix_time::seconds(SERVER_CHECK_IRC_BOT_INTERVAL_SEC));
m_checkFileTimer.async_wait(
boost::bind(
&ServerAdminBot::CheckFileHandler, shared_from_this(), boost::asio::placeholders::error));
m_ircAdminThread->Run();
}
}
void
ServerAdminBot::Process()
ServerAdminBot::ReconnectHandler(const boost::system::error_code& ec)
{
if (m_ircRestartTimer.elapsed().total_seconds() > SERVER_RESTART_IRC_BOT_INTERVAL_SEC) {
if (m_ircAdminThread) {
m_ircAdminThread->SignalTermination();
if (m_ircAdminThread->Join(NET_ADMIN_IRC_TERMINATE_TIMEOUT_MSEC)) {
boost::shared_ptr<IrcThread> tmpIrcThread(new IrcThread(*m_ircAdminThread));
tmpIrcThread->Run();
m_ircAdminThread = tmpIrcThread;
}
}
m_ircRestartTimer.reset();
m_ircRestartTimer.start();
if (!ec) {
Reconnect();
m_reconnectTimer.expires_from_now(
boost::posix_time::seconds(SERVER_RESTART_IRC_BOT_INTERVAL_SEC));
m_reconnectTimer.async_wait(
boost::bind(
&ServerAdminBot::ReconnectHandler, shared_from_this(), boost::asio::placeholders::error));
}
{
}
void
ServerAdminBot::Reconnect()
{
if (m_ircAdminThread) {
m_ircAdminThread->SignalTermination();
if (m_ircAdminThread->Join(NET_ADMIN_IRC_TERMINATE_TIMEOUT_MSEC)) {
boost::shared_ptr<IrcThread> tmpIrcThread(new IrcThread(*m_ircAdminThread));
tmpIrcThread->Run();
m_ircAdminThread = tmpIrcThread;
}
}
}
void
ServerAdminBot::CheckFileHandler(const boost::system::error_code& ec)
{
if (!ec) {
// Reconnect the irc bot if a signal file exists.
path cachePath(m_cacheDir);
cachePath /= "SignalAdminReconnect";
if (exists(cachePath)) {
remove(cachePath);
Reconnect();
}
m_checkFileTimer.expires_from_now(
boost::posix_time::seconds(SERVER_CHECK_IRC_BOT_INTERVAL_SEC));
m_checkFileTimer.async_wait(
boost::bind(
&ServerAdminBot::CheckFileHandler, shared_from_this(), boost::asio::placeholders::error));
}
}
void
ServerAdminBot::NotifyLoop(const boost::system::error_code& ec)
{
if (!ec) {
boost::mutex::scoped_lock lock(m_notifyMutex);
if (m_notifyTimeoutMinutes && m_notifyTimer.elapsed().total_seconds() >= m_notifyCounter * m_notifyIntervalMinutes * 60) {
@@ -315,6 +372,12 @@ ServerAdminBot::Process()
m_notifyTimer.reset();
}
}
m_notifyLoopTimer.expires_from_now(
boost::posix_time::seconds(SERVER_NOTIFY_IRC_BOT_INTERVAL_SEC));
m_notifyLoopTimer.async_wait(
boost::bind(
&ServerAdminBot::NotifyLoop, shared_from_this(), boost::asio::placeholders::error));
}
}
@@ -323,6 +386,11 @@ ServerAdminBot::SignalTermination()
{
if (m_ircAdminThread)
m_ircAdminThread->SignalTermination();
// Terminated the timers.
m_notifyLoopTimer.cancel();
m_reconnectTimer.cancel();
m_checkFileTimer.cancel();
}
bool
+21 -8
View File
@@ -32,8 +32,8 @@
using namespace std;
ServerLobbyBot::ServerLobbyBot()
: m_ircRestartTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::second_timer::auto_start)
ServerLobbyBot::ServerLobbyBot(boost::shared_ptr<boost::asio::io_service> ioService)
: m_reconnectTimer(*ioService)
{
}
@@ -94,14 +94,22 @@ ServerLobbyBot::SignalLobbyMessage(unsigned playerId, const std::string &playerN
void
ServerLobbyBot::Run()
{
if (m_ircLobbyThread)
if (m_ircLobbyThread) {
// Initialise the reconnect timer.
m_reconnectTimer.expires_from_now(
boost::posix_time::seconds(SERVER_RESTART_IRC_BOT_INTERVAL_SEC));
m_reconnectTimer.async_wait(
boost::bind(
&ServerLobbyBot::Reconnect, shared_from_this(), boost::asio::placeholders::error));
m_ircLobbyThread->Run();
}
}
void
ServerLobbyBot::Process()
ServerLobbyBot::Reconnect(const boost::system::error_code& ec)
{
if (m_ircRestartTimer.elapsed().total_seconds() > SERVER_RESTART_IRC_BOT_INTERVAL_SEC) {
if (!ec) {
if (m_ircLobbyThread) {
m_ircLobbyThread->SignalTermination();
if (m_ircLobbyThread->Join(NET_ADMIN_IRC_TERMINATE_TIMEOUT_MSEC)) {
@@ -110,9 +118,11 @@ ServerLobbyBot::Process()
m_ircLobbyThread = tmpIrcThread;
}
}
m_ircRestartTimer.reset();
m_ircRestartTimer.start();
m_reconnectTimer.expires_from_now(
boost::posix_time::seconds(SERVER_RESTART_IRC_BOT_INTERVAL_SEC));
m_reconnectTimer.async_wait(
boost::bind(
&ServerLobbyBot::Reconnect, shared_from_this(), boost::asio::placeholders::error));
}
}
@@ -121,6 +131,9 @@ ServerLobbyBot::SignalTermination()
{
if (m_ircLobbyThread)
m_ircLobbyThread->SignalTermination();
// Terminated the reconnect timer.
m_reconnectTimer.cancel();
}
bool
-5
View File
@@ -87,11 +87,6 @@ ServerManager::RunAll()
GetLobbyThread().Run();
}
void
ServerManager::Process()
{
}
void
ServerManager::SignalTerminationAll()
{
+3 -11
View File
@@ -29,8 +29,8 @@ using namespace std;
ServerManagerIrc::ServerManagerIrc(ConfigFile &config, GuiInterface &gui, ServerMode mode, AvatarManager &avatarManager)
: ServerManager(config, gui)
{
m_adminBot.reset(new ServerAdminBot);
m_lobbyBot.reset(new ServerLobbyBot);
m_adminBot.reset(new ServerAdminBot(m_ioService));
m_lobbyBot.reset(new ServerLobbyBot(m_ioService));
m_lobbyThread.reset(new ServerLobbyThread(gui, mode, *m_lobbyBot, config, avatarManager, m_ioService));
}
@@ -68,7 +68,7 @@ ServerManagerIrc::Init(unsigned serverPort, bool ipv6, ServerTransportProtocol p
myConfig.readConfigString("LobbyIRCChannelPassword"));
}
m_adminBot->Init(m_lobbyThread, tmpIrcAdminThread);
m_adminBot->Init(m_lobbyThread, tmpIrcAdminThread, myConfig.readConfigString("CacheDir"));
m_lobbyBot->Init(m_lobbyThread, tmpIrcLobbyThread);
ServerManager::Init(serverPort, ipv6, proto, logDir);
}
@@ -81,14 +81,6 @@ ServerManagerIrc::RunAll()
ServerManager::RunAll();
}
void
ServerManagerIrc::Process()
{
m_adminBot->Process();
m_lobbyBot->Process();
ServerManager::Process();
}
void
ServerManagerIrc::SignalTerminationAll()
{