summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2014-03-08 08:27:28 (GMT)
committerSwift Review <review@swift.im>2014-03-20 18:35:05 (GMT)
commita51044193098128f615287308240edd0611a2e4b (patch)
tree6c05d97d57259071b21b389573cbedcc92fcb260
parent2bb017cbe8825d9e4b319ab747e23afe73d106bd (diff)
downloadswift-contrib-a51044193098128f615287308240edd0611a2e4b.zip
swift-contrib-a51044193098128f615287308240edd0611a2e4b.tar.bz2
Sluift: Initialize client tracing at creation time
This avoids all calls to set_trace_enabled prior to connect() being ignored. Change-Id: Ib4f2bc9815aae2bd456f2ececcb2a37ac460eebc
-rw-r--r--Sluift/client.cpp9
-rw-r--r--Sluift/sluift.cpp10
2 files changed, 10 insertions, 9 deletions
diff --git a/Sluift/client.cpp b/Sluift/client.cpp
index e2ba480..63e3bf1 100644
--- a/Sluift/client.cpp
+++ b/Sluift/client.cpp
@@ -17,132 +17,123 @@
#include <Swiften/Elements/RawXMLPayload.h>
#include <Swiften/Elements/RosterItemPayload.h>
#include <Swiften/Elements/RosterPayload.h>
#include <Swiften/Elements/DiscoInfo.h>
#include <Swiften/Disco/ClientDiscoManager.h>
#include <Swiften/Queries/GenericRequest.h>
#include <Swiften/Presence/PresenceSender.h>
#include <Swiften/Roster/XMPPRoster.h>
#include <Swiften/Roster/SetRosterRequest.h>
#include <Swiften/Presence/SubscriptionManager.h>
#include <Swiften/Roster/XMPPRosterItem.h>
#include <Swiften/Queries/Requests/GetSoftwareVersionRequest.h>
#include <Sluift/Lua/FunctionRegistration.h>
#include <Swiften/Base/foreach.h>
#include <Sluift/Lua/Check.h>
#include <Sluift/Lua/Value.h>
#include <Sluift/Lua/Exception.h>
#include <Sluift/Lua/LuaUtils.h>
#include <Sluift/globals.h>
using namespace Swift;
namespace lambda = boost::lambda;
static inline SluiftClient* getClient(lua_State* L) {
return *Lua::checkUserData<SluiftClient>(L, 1);
}
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;
-}
-
static void addPayloadsToTable(lua_State* L, const std::vector<boost::shared_ptr<Payload> >& payloads) {
if (!payloads.empty()) {
lua_createtable(L, boost::numeric_cast<int>(payloads.size()), 0);
for (size_t i = 0; i < payloads.size(); ++i) {
Sluift::globals.elementConvertor.convertToLua(L, payloads[i]);
lua_rawseti(L, -2, boost::numeric_cast<int>(i+1));
}
Lua::registerGetByTypeIndex(L, -1);
lua_setfield(L, -2, "payloads");
}
}
static boost::shared_ptr<Payload> getPayload(lua_State* L, int index) {
if (lua_type(L, index) == LUA_TTABLE) {
return Sluift::globals.elementConvertor.convertFromLua(L, index);
}
else if (lua_type(L, index) == LUA_TSTRING) {
return boost::make_shared<RawXMLPayload>(Lua::checkString(L, index));
}
else {
return boost::shared_ptr<Payload>();
}
}
static std::vector< boost::shared_ptr<Payload> > getPayloadsFromTable(lua_State* L, int index) {
index = Lua::absoluteOffset(L, index);
std::vector< boost::shared_ptr<Payload> > result;
lua_getfield(L, index, "payloads");
if (lua_istable(L, -1)) {
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
boost::shared_ptr<Payload> payload = getPayload(L, -1);
if (payload) {
result.push_back(payload);
}
}
}
lua_pop(L, 1);
return result;
}
SLUIFT_LUA_FUNCTION(Client, async_connect) {
SluiftClient* client = getClient(L);
std::string host = client->getOptions().manualHostname;
int port = client->getOptions().manualPort;
if (lua_istable(L, 2)) {
if (boost::optional<std::string> hostString = Lua::getStringField(L, 2, "host")) {
host = *hostString;
}
if (boost::optional<int> portInt = Lua::getIntField(L, 2, "port")) {
port = *portInt;
}
}
- client->setTraceEnabled(getGlobalDebug(L));
client->connect(host, port);
return 0;
}
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`.",
"self",
""
) {
getClient(L)->waitConnected(getGlobalTimeout(L));
return 0;
}
SLUIFT_LUA_FUNCTION_WITH_HELP(
Client, is_connected,
"Checks whether this client is still connected.\n\nReturns a boolean.",
"self\n",
""
) {
lua_pushboolean(L, getClient(L)->isConnected());
return 1;
}
diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp
index 858e634..3908631 100644
--- a/Sluift/sluift.cpp
+++ b/Sluift/sluift.cpp
@@ -16,96 +16,106 @@
#include "Watchdog.h"
#include <Sluift/Lua/Check.h>
#include <Sluift/SluiftClient.h>
#include <Sluift/globals.h>
#include <Sluift/Lua/Exception.h>
#include <Sluift/Lua/LuaUtils.h>
#include <Sluift/Lua/FunctionRegistration.h>
#include <Swiften/Base/sleep.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Base/IDGenerator.h>
#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h>
#include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h>
#include <Swiften/Serializer/PayloadSerializer.h>
#include <Swiften/TLS/Certificate.h>
#include <Swiften/TLS/CertificateFactory.h>
#include <Sluift/LuaElementConvertor.h>
#include <Sluift/Lua/Debug.h>
#include <Swiften/StringCodecs/Base64.h>
#include <Swiften/StringCodecs/Hexify.h>
#include <Swiften/IDN/IDNConverter.h>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>
#include <Sluift/ITunesInterface.h>
using namespace Swift;
namespace Swift {
namespace Sluift {
SluiftGlobals globals;
}
}
extern "C" const char core_lua[];
extern "C" size_t core_lua_size;
+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;
+}
+
+
/*******************************************************************************
* Module functions
******************************************************************************/
SLUIFT_LUA_FUNCTION_WITH_HELP(
Sluift, new_client,
"Creates a new client.\n\nReturns a @{Client} object.\n",
"jid The JID to connect as\n"
"passphrase The passphrase to use\n",
""
) {
Lua::checkString(L, 1);
JID jid(std::string(Lua::checkString(L, 1)));
std::string password(Lua::checkString(L, 2));
SluiftClient** client = reinterpret_cast<SluiftClient**>(lua_newuserdata(L, sizeof(SluiftClient*)));
lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex);
lua_getfield(L, -1, "Client");
lua_setmetatable(L, -3);
lua_pop(L, 1);
*client = new SluiftClient(jid, password, &Sluift::globals.networkFactories, &Sluift::globals.eventLoop);
+ (*client)->setTraceEnabled(getGlobalDebug(L));
return 1;
}
SLUIFT_LUA_FUNCTION_WITH_HELP(
Sluift, sha1,
"Compute the SHA-1 hash of given data",
"data the data to hash",
""
) {
static boost::shared_ptr<CryptoProvider> crypto(PlatformCryptoProvider::create());
if (!lua_isstring(L, 1)) {
throw Lua::Exception("Expected string");
}
size_t len;
const char* data = lua_tolstring(L, 1, &len);
ByteArray result = crypto->getSHA1Hash(createByteArray(data, len));
lua_pushlstring(L, reinterpret_cast<char*>(vecptr(result)), result.size());
return 1;
}
SLUIFT_LUA_FUNCTION_WITH_HELP(
Sluift, sleep,
"Sleeps for the given time.",
"milliseconds the amount of milliseconds to sleep",
""
) {
Sluift::globals.eventLoop.runOnce();
int timeout = Lua::checkIntNumber(L, 1);
Watchdog watchdog(timeout, Sluift::globals.networkFactories.getTimerFactory());
while (!watchdog.getTimedOut()) {
Swift::sleep(boost::numeric_cast<unsigned int>(std::min(100, timeout)));
Sluift::globals.eventLoop.runOnce();
}
return 0;
}