diff options
| author | Remko Tronçon <git@el-tramo.be> | 2011-02-24 19:12:39 (GMT) | 
|---|---|---|
| committer | Remko Tronçon <git@el-tramo.be> | 2011-02-24 19:18:05 (GMT) | 
| commit | 90d6b45adf84976d99911feb3fb09b27cb6a8d1e (patch) | |
| tree | 3e49f71aef78b3d95d5318ca8e1f42305628da9f | |
| parent | 442420335b9533b2a43c6e3f09758a94af045a84 (diff) | |
| download | swift-contrib-90d6b45adf84976d99911feb3fb09b27cb6a8d1e.zip swift-contrib-90d6b45adf84976d99911feb3fb09b27cb6a8d1e.tar.bz2 | |
Added debugging switch to Sluift.
| -rw-r--r-- | Sluift/sluift.cpp | 35 | ||||
| -rw-r--r-- | Swiften/EventLoop/SimpleEventLoop.cpp | 5 | ||||
| -rw-r--r-- | Swiften/EventLoop/SimpleEventLoop.h | 10 | 
3 files changed, 41 insertions, 9 deletions
| diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index cab5361..5a9fbd5 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -15,7 +15,7 @@ extern "C" {  #include <Swiften/Client/Client.h>  #include <Swiften/Client/ClientXMLTracer.h>  #include <Swiften/JID/JID.h> -#include <Swiften/EventLoop/DummyEventLoop.h> +#include <Swiften/EventLoop/SimpleEventLoop.h>  #include <Swiften/Network/BoostNetworkFactories.h>  #include <Swiften/Base/sleep.h>  #include <Swiften/Elements/SoftwareVersion.h> @@ -26,18 +26,34 @@ using namespace Swift;  #define SLUIFT_CLIENT "SluiftClient*"  /******************************************************************************* + * Forward declarations + ******************************************************************************/ + +static int debugRef = 0; + +static bool debug(lua_State* L) { +	lua_rawgeti(L, LUA_REGISTRYINDEX, debugRef); +	lua_getfield(L, -1, "debug"); +	bool b = lua_toboolean(L, -1); +	lua_pop(L, 2); +	return b; +} + +/*******************************************************************************   * Helper classes   ******************************************************************************/ -DummyEventLoop eventLoop; +SimpleEventLoop eventLoop;  BoostNetworkFactories networkFactories(&eventLoop);  class SluiftClient {  	public: -		SluiftClient(const JID& jid, const std::string& password) { +		SluiftClient(const JID& jid, const std::string& password, bool debug = false) : tracer(NULL) {  			client = new Client(jid, password, &networkFactories);  			client->setAlwaysTrustCertificates(); -			tracer = new ClientXMLTracer(client); +			if (debug) { +				tracer = new ClientXMLTracer(client); +			}  		}  		~SluiftClient() { @@ -84,8 +100,7 @@ class SluiftClient {  	private:  		void processEvents() { -			Swift::sleep(100); -			eventLoop.processEvents(); +			eventLoop.runUntilEvents();  		}  		void handleSoftwareVersionResponse(boost::shared_ptr<SoftwareVersion> version, ErrorPayload::ref error) { @@ -180,7 +195,7 @@ static int sluift_connect(lua_State *L) {  	luaL_getmetatable(L, SLUIFT_CLIENT);  	lua_setmetatable(L, -2); -	*client = new SluiftClient(jid, password); +	*client = new SluiftClient(jid, password, debug(L));  	(*client)->connect();  	if (!(*client)->isConnected()) {  		lua_pushnil(L); @@ -201,6 +216,12 @@ static const luaL_reg sluift_functions[] = {  SLUIFT_API int luaopen_sluift(lua_State *L) {  	// Register functions  	luaL_register(L, "sluift", sluift_functions); +	lua_pushboolean(L, false); +	lua_setfield(L, -2, "debug"); +	 +	lua_pushvalue(L, -1); +	debugRef = luaL_ref(L, LUA_REGISTRYINDEX); +  	// Register the client metatable  	luaL_newmetatable(L, SLUIFT_CLIENT); diff --git a/Swiften/EventLoop/SimpleEventLoop.cpp b/Swiften/EventLoop/SimpleEventLoop.cpp index d745e39..2d71544 100644 --- a/Swiften/EventLoop/SimpleEventLoop.cpp +++ b/Swiften/EventLoop/SimpleEventLoop.cpp @@ -25,7 +25,7 @@ SimpleEventLoop::~SimpleEventLoop() {  	}  } -void SimpleEventLoop::run() { +void SimpleEventLoop::doRun(bool breakAfterEvents) {  	while (isRunning_) {  		std::vector<Event> events;  		{ @@ -38,6 +38,9 @@ void SimpleEventLoop::run() {  		foreach(const Event& event, events) {  			handleEvent(event);  		} +		if (breakAfterEvents) { +			return; +		}  	}  } diff --git a/Swiften/EventLoop/SimpleEventLoop.h b/Swiften/EventLoop/SimpleEventLoop.h index 86195e1..bdffa3d 100644 --- a/Swiften/EventLoop/SimpleEventLoop.h +++ b/Swiften/EventLoop/SimpleEventLoop.h @@ -19,12 +19,20 @@ namespace Swift {  			SimpleEventLoop();  			~SimpleEventLoop(); -			void run(); +			void run() { +				doRun(false); +			} + +			void runUntilEvents() { +				doRun(true); +			} +  			void stop();  			virtual void post(const Event& event);  		private: +			void doRun(bool breakAfterEvents);  			void doStop();  		private: | 
 Swift
 Swift