summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Sluift/Examples/Login.lua4
-rw-r--r--Sluift/SConscript1
-rw-r--r--Sluift/SluiftGlobals.h6
-rw-r--r--Sluift/Watchdog.cpp50
-rw-r--r--Sluift/Watchdog.h28
-rw-r--r--Sluift/client.cpp1
-rw-r--r--Sluift/main.cpp10
7 files changed, 74 insertions, 26 deletions
diff --git a/Sluift/Examples/Login.lua b/Sluift/Examples/Login.lua
index c43b72a..fadb651 100644
--- a/Sluift/Examples/Login.lua
+++ b/Sluift/Examples/Login.lua
@@ -1,4 +1,4 @@
--[[
- Copyright (c) 2010-2013 Remko Tronçon
+ Copyright (c) 2010-2014 Remko Tronçon
Licensed under the GNU General Public License v3.
See Documentation/Licenses/GPLv3.txt for more information.
@@ -22,5 +22,5 @@ sluift.debug = os.getenv("SLUIFT_DEBUG") or false
print("Connecting " .. os.getenv("SLUIFT_JID") .. " ...")
c = sluift.new_client(os.getenv("SLUIFT_JID"), os.getenv("SLUIFT_PASS"))
-c:set_options({compress = false, tls = false})
+c:set_options{compress = false, tls = false}
c:connect()
c:send_presence("")
diff --git a/Sluift/SConscript b/Sluift/SConscript
index d88e948..38baef1 100644
--- a/Sluift/SConscript
+++ b/Sluift/SConscript
@@ -33,4 +33,5 @@ elif env["SCONS_STAGE"] == "build" :
"ClientHelpers.cpp",
"SluiftClient.cpp",
+ "Watchdog.cpp",
"core.c",
"client.cpp",
diff --git a/Sluift/SluiftGlobals.h b/Sluift/SluiftGlobals.h
index 18a90c2..0d8e637 100644
--- a/Sluift/SluiftGlobals.h
+++ b/Sluift/SluiftGlobals.h
@@ -1,4 +1,4 @@
/*
- * Copyright (c) 2013 Remko Tronçon
+ * Copyright (c) 2013-2014 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
@@ -10,8 +10,9 @@
#include <Swiften/EventLoop/SimpleEventLoop.h>
#include <Swiften/Network/BoostNetworkFactories.h>
+#include <signal.h>
namespace Swift {
struct SluiftGlobals {
- SluiftGlobals() : networkFactories(&eventLoop) {}
+ SluiftGlobals() : networkFactories(&eventLoop), interruptRequested(0) {}
int timeout;
@@ -21,4 +22,5 @@ namespace Swift {
BoostNetworkFactories networkFactories;
int coreLibIndex;
+ sig_atomic_t interruptRequested;
};
}
diff --git a/Sluift/Watchdog.cpp b/Sluift/Watchdog.cpp
new file mode 100644
index 0000000..8b1c9ab
--- /dev/null
+++ b/Sluift/Watchdog.cpp
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2014 Remko Tronçon
+ * Licensed under the GNU General Public License.
+ * See the COPYING file for more information.
+ */
+
+#include <Sluift/Watchdog.h>
+
+#include <Sluift/globals.h>
+
+static const int INTERVAL_MS = 500;
+
+
+using namespace Swift;
+
+Watchdog::Watchdog(int timeout, TimerFactory* timerFactory) :
+ remainingTime(timeout),
+ timerFactory(timerFactory),
+ timedOut(false) {
+ Sluift::globals.interruptRequested = 0;
+
+ int nextTimeout = remainingTime >= 0 ? std::min(remainingTime, INTERVAL_MS) : INTERVAL_MS;
+
+ timer = timerFactory->createTimer(nextTimeout);
+ timer->onTick.connect(boost::bind(&Watchdog::handleTimerTick, this));
+ remainingTime -= nextTimeout;
+ timer->start();
+}
+
+Watchdog::~Watchdog() {
+ if (timer) {
+ timer->stop();
+ }
+}
+
+void Watchdog::handleTimerTick() {
+ if (Sluift::globals.interruptRequested || remainingTime == 0) {
+ timedOut = true;
+ }
+ else {
+ int nextTimeout = remainingTime >= 0 ? std::min(remainingTime, INTERVAL_MS) : INTERVAL_MS;
+ if (nextTimeout != INTERVAL_MS) {
+ timer->onTick.disconnect(boost::bind(&Watchdog::handleTimerTick, this));
+ timer = timerFactory->createTimer(nextTimeout);
+ timer->onTick.connect(boost::bind(&Watchdog::handleTimerTick, this));
+ }
+ remainingTime -= nextTimeout;
+ timer->start();
+ }
+}
diff --git a/Sluift/Watchdog.h b/Sluift/Watchdog.h
index 95b6971..868621e 100644
--- a/Sluift/Watchdog.h
+++ b/Sluift/Watchdog.h
@@ -1,4 +1,4 @@
/*
- * Copyright (c) 2011 Remko Tronçon
+ * Copyright (c) 2011-2014 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
@@ -7,4 +7,6 @@
#pragma once
+#include <algorithm>
+
#include <Swiften/Network/TimerFactory.h>
@@ -12,20 +14,6 @@ namespace Swift {
class Watchdog {
public:
- Watchdog(int timeout, TimerFactory* timerFactory) : timedOut(false) {
- if (timeout > 0) {
- timer = timerFactory->createTimer(timeout);
- timer->start();
- timer->onTick.connect(boost::bind(&Watchdog::handleTimerTick, this));
- }
- else if (timeout == 0) {
- timedOut = true;
- }
- }
-
- ~Watchdog() {
- if (timer) {
- timer->stop();
- }
- }
+ Watchdog(int timeout, TimerFactory* timerFactory);
+ ~Watchdog();
bool getTimedOut() const {
@@ -34,10 +22,10 @@ namespace Swift {
private:
- void handleTimerTick() {
- timedOut = true;
- }
+ void handleTimerTick();
private:
Timer::ref timer;
+ int remainingTime;
+ TimerFactory* timerFactory;
bool timedOut;
};
diff --git a/Sluift/client.cpp b/Sluift/client.cpp
index 16f1281..9cb5090 100644
--- a/Sluift/client.cpp
+++ b/Sluift/client.cpp
@@ -26,5 +26,4 @@
#include <Swiften/Presence/SubscriptionManager.h>
#include <Swiften/Roster/XMPPRosterItem.h>
-#include <Sluift/Watchdog.h>
#include <Swiften/Queries/Requests/GetSoftwareVersionRequest.h>
#include <Sluift/Lua/FunctionRegistration.h>
diff --git a/Sluift/main.cpp b/Sluift/main.cpp
index 76ba572..505cc5c 100644
--- a/Sluift/main.cpp
+++ b/Sluift/main.cpp
@@ -1,4 +1,4 @@
/*
- * Copyright (c) 2013 Remko Tronçon
+ * Copyright (c) 2013-2014 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
@@ -16,4 +16,5 @@
#include <boost/numeric/conversion/cast.hpp>
#include <boost/assign/list_of.hpp>
+#include <Sluift/globals.h>
#include <Sluift/Console.h>
#include <Sluift/StandardTerminal.h>
@@ -49,4 +50,8 @@ static const luaL_Reg defaultLibraries[] = {
};
+static void handleInterruptSignal(int) {
+ Sluift::globals.interruptRequested = 1;
+}
+
static void checkResult(lua_State* L, int result) {
if (result && !lua_isnil(L, -1)) {
@@ -151,4 +156,7 @@ int main(int argc, char* argv[]) {
// Run console
if (arguments.count("interactive") || arguments.count("script") == 0) {
+ // Set up signal handler
+ signal(SIGINT, handleInterruptSignal);
+
// Import some useful functions into the global namespace
lua_getglobal(L, "sluift");