diff options
-rw-r--r-- | Sluift/SluiftClient.cpp | 22 | ||||
-rw-r--r-- | Sluift/SluiftClient.h | 8 | ||||
-rw-r--r-- | Sluift/SluiftGlobals.h | 9 | ||||
-rw-r--r-- | Sluift/client.cpp | 36 | ||||
-rw-r--r-- | Sluift/sluift.cpp | 57 |
5 files changed, 62 insertions, 70 deletions
diff --git a/Sluift/SluiftClient.cpp b/Sluift/SluiftClient.cpp index 257eec1..8c0213e 100644 --- a/Sluift/SluiftClient.cpp +++ b/Sluift/SluiftClient.cpp @@ -24,9 +24,7 @@ SluiftClient::SluiftClient( const std::string& password, NetworkFactories* networkFactories, - SimpleEventLoop* eventLoop, - SluiftGlobals* globals) : + SimpleEventLoop* eventLoop) : networkFactories(networkFactories), eventLoop(eventLoop), - globals(globals), tracer(NULL) { client = new Client(jid, password, networkFactories); @@ -47,7 +45,4 @@ void SluiftClient::connect() { rosterReceived = false; disconnectedError = boost::optional<ClientError>(); - if (globals->debug) { - tracer = new ClientXMLTracer(client, options.boshURL.isEmpty()? false: true); - } client->connect(options); } @@ -58,12 +53,19 @@ void SluiftClient::connect(const std::string& host, int port) { options.manualPort = port; disconnectedError = boost::optional<ClientError>(); - if (globals->debug) { + client->connect(options); +} + +void SluiftClient::setTraceEnabled(bool b) { + if (b && !tracer) { tracer = new ClientXMLTracer(client, options.boshURL.isEmpty()? false: true); } - client->connect(options); + else if (!b && tracer) { + delete tracer; + tracer = NULL; + } } -void SluiftClient::waitConnected() { - Watchdog watchdog(globals->timeout, networkFactories->getTimerFactory()); +void SluiftClient::waitConnected(int timeout) { + Watchdog watchdog(timeout, networkFactories->getTimerFactory()); while (!watchdog.getTimedOut() && client->isActive() && !client->isAvailable()) { eventLoop->runUntilEvents(); diff --git a/Sluift/SluiftClient.h b/Sluift/SluiftClient.h index 60eae16..f0192e4 100644 --- a/Sluift/SluiftClient.h +++ b/Sluift/SluiftClient.h @@ -13,5 +13,4 @@ #include <Swiften/Client/ClientOptions.h> -#include <Sluift/globals.h> #include <Swiften/Elements/IQ.h> #include <Swiften/Elements/Message.h> @@ -63,6 +62,5 @@ namespace Swift { const std::string& password, NetworkFactories* networkFactories, - SimpleEventLoop* eventLoop, - SluiftGlobals* globals); + SimpleEventLoop* eventLoop); ~SluiftClient(); @@ -77,6 +75,7 @@ namespace Swift { void connect(); void connect(const std::string& host, int port); - void waitConnected(); + void waitConnected(int timeout); bool isConnected() const; + void setTraceEnabled(bool b); template<typename T> @@ -120,5 +119,4 @@ namespace Swift { NetworkFactories* networkFactories; SimpleEventLoop* eventLoop; - SluiftGlobals* globals; Client* client; ClientOptions options; diff --git a/Sluift/SluiftGlobals.h b/Sluift/SluiftGlobals.h index 0d8e637..5de7cfe 100644 --- a/Sluift/SluiftGlobals.h +++ b/Sluift/SluiftGlobals.h @@ -14,12 +14,15 @@ namespace Swift { struct SluiftGlobals { - SluiftGlobals() : networkFactories(&eventLoop), interruptRequested(0) {} + SluiftGlobals() : + networkFactories(&eventLoop), + coreLibIndex(-1), + moduleLibIndex(-1), + interruptRequested(0) {} - int timeout; - bool debug; LuaElementConvertors elementConvertor; SimpleEventLoop eventLoop; BoostNetworkFactories networkFactories; int coreLibIndex; + int moduleLibIndex; sig_atomic_t interruptRequested; }; diff --git a/Sluift/client.cpp b/Sluift/client.cpp index 9cb5090..cdbc591 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -42,4 +42,20 @@ static inline SluiftClient* getClient(lua_State* L) { } +static inline int getGlobalTimeout(lua_State* L) { + lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.moduleLibIndex); + lua_getfield(L, -1, "timeout"); + int result = boost::numeric_cast<int>(lua_tointeger(L, -1)); + lua_pop(L, 2); + return result; +} + +static inline bool getGlobalDebug(lua_State* L) { + lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.moduleLibIndex); + lua_getfield(L, -1, "debug"); + int result = lua_toboolean(L, -1); + lua_pop(L, 2); + return result; +} + SLUIFT_LUA_FUNCTION(Client, async_connect) { SluiftClient* client = getClient(L); @@ -55,4 +71,5 @@ SLUIFT_LUA_FUNCTION(Client, async_connect) { } } + client->setTraceEnabled(getGlobalDebug(L)); client->connect(host, port); return 0; @@ -60,4 +77,15 @@ SLUIFT_LUA_FUNCTION(Client, async_connect) { SLUIFT_LUA_FUNCTION_WITH_HELP( + Client, set_trace_enabled, + "Enable/disable tracing of the data sent/received.\n\n.", + "self\n" + "enable a boolean specifying whether to enable/disable tracing", + "" +) { + getClient(L)->setTraceEnabled(lua_toboolean(L, 1)); + return 0; +} + +SLUIFT_LUA_FUNCTION_WITH_HELP( Client, wait_connected, "Block until the client is connected.\n\nThis is useful after an `async_connect`.", @@ -65,5 +93,5 @@ SLUIFT_LUA_FUNCTION_WITH_HELP( "" ) { - getClient(L)->waitConnected(); + getClient(L)->waitConnected(getGlobalTimeout(L)); return 0; } @@ -275,5 +303,5 @@ static int sendQuery(lua_State* L, IQ::Type type) { } - int timeout = Sluift::globals.timeout; + int timeout = getGlobalTimeout(L); if (boost::optional<int> timeoutInt = Lua::getIntField(L, 2, "timeout")) { timeout = *timeoutInt; @@ -307,5 +335,5 @@ SLUIFT_LUA_FUNCTION(Client, query_pubsub) { } - int timeout = Sluift::globals.timeout; + int timeout = getGlobalTimeout(L); if (boost::optional<int> timeoutInt = Lua::getIntField(L, 2, "timeout")) { timeout = *timeoutInt; @@ -521,5 +549,5 @@ SLUIFT_LUA_FUNCTION(Client, get_next_event) { SluiftClient* client = getClient(L); - int timeout = Sluift::globals.timeout; + int timeout = getGlobalTimeout(L); boost::optional<SluiftClient::Event::Type> type; int condition = 0; diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index 39b92fc..08ffd92 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -71,5 +71,5 @@ SLUIFT_LUA_FUNCTION_WITH_HELP( lua_pop(L, 1); - *client = new SluiftClient(jid, password, &Sluift::globals.networkFactories, &Sluift::globals.eventLoop, &Sluift::globals); + *client = new SluiftClient(jid, password, &Sluift::globals.networkFactories, &Sluift::globals.eventLoop); return 1; } @@ -116,39 +116,4 @@ SLUIFT_LUA_FUNCTION_WITH_HELP( } -static int sluift_index(lua_State* L) { - try { - std::string key(Lua::checkString(L, 2)); - if (key == "debug") { - lua_pushboolean(L, Sluift::globals.debug); - return 1; - } - else if (key == "timeout") { - lua_pushnumber(L, Sluift::globals.timeout); - return 1; - } - return 0; - } - catch (const std::exception& e) { - return luaL_error(L, e.what()); - } -} - - -static int sluift_newindex(lua_State* L) { - try { - std::string key(Lua::checkString(L, 2)); - if (key == "debug") { - Sluift::globals.debug = lua_toboolean(L, 3); - } - else if (key == "timeout") { - Sluift::globals.timeout = Lua::checkIntNumber(L, 3); - } - return 0; - } - catch (const std::exception& e) { - return luaL_error(L, e.what()); - } -} - SLUIFT_LUA_FUNCTION_WITH_HELP( Sluift, from_xml, @@ -315,9 +280,13 @@ static const luaL_Reg sluift_functions[] = { {NULL, NULL} }; SLUIFT_API int luaopen_sluift(lua_State* L) { - // Initialize globals - Sluift::globals.debug = false; - Sluift::globals.timeout = -1; - + // Initialize & store the module table luaL_register(L, lua_tostring(L, 1), sluift_functions); + lua_pushinteger(L, -1); + lua_setfield(L, -2, "timeout"); + lua_pushboolean(L, 0); + lua_setfield(L, -2, "debug"); + + lua_pushvalue(L, -1); + Sluift::globals.moduleLibIndex = luaL_ref(L, LUA_REGISTRYINDEX); // Load core lib code @@ -347,12 +316,4 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { lua_pop(L, 1); - // Set read only - lua_createtable(L, 0, 0); - lua_pushcclosure(L, sluift_index, 0); - lua_setfield(L, -2, "__index"); - lua_pushcclosure(L, sluift_newindex, 0); - lua_setfield(L, -2, "__newindex"); - lua_setmetatable(L, -2); - // Load client metatable lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); |