diff options
author | Roger Planas <roger.planas@isode.com> | 2017-05-04 13:28:53 (GMT) |
---|---|---|
committer | Edwin Mons <edwin.mons@isode.com> | 2017-05-05 14:15:31 (GMT) |
commit | f033fc54d5cd549822a3462bf0dfcc852e7b4dee (patch) | |
tree | cbc2cf68a11f5fbc25ea0627d78fadd96db49d06 | |
parent | cfea60eda7f3ce5fa10ed92c50c19fc1ee264eb1 (diff) | |
download | swift-f033fc54d5cd549822a3462bf0dfcc852e7b4dee.zip swift-f033fc54d5cd549822a3462bf0dfcc852e7b4dee.tar.bz2 |
Sluift: Enable more options to be passed to connect()
There are many connection options that Sluift enables (bosh_url, tls,
etc..) but only host/port were understood by connect(), the rest
were meant to be set by using set_options().
This can be sometimes annoying, so to make things uniform, now
connect() should accept the same options settable in set_options()
Test-information:
Run test to check you can pass values to connect() rather than
set_options() and things worked.
Change-Id: Ic79aa5c6b232ddc2ffcfc3c14aa6f31233242314
-rw-r--r-- | Sluift/SluiftClient.cpp | 9 | ||||
-rw-r--r-- | Sluift/client.cpp | 81 |
2 files changed, 38 insertions, 52 deletions
diff --git a/Sluift/SluiftClient.cpp b/Sluift/SluiftClient.cpp index 4680d4b..e8c43c9 100644 --- a/Sluift/SluiftClient.cpp +++ b/Sluift/SluiftClient.cpp @@ -27,69 +27,60 @@ SluiftClient::SluiftClient( const JID& jid, const std::string& password, NetworkFactories* networkFactories, SimpleEventLoop* eventLoop) : networkFactories(networkFactories), eventLoop(eventLoop), tracer(nullptr) { client = new Client(jid, password, networkFactories); client->setAlwaysTrustCertificates(); client->onDisconnected.connect(boost::bind(&SluiftClient::handleDisconnected, this, _1)); client->onMessageReceived.connect(boost::bind(&SluiftClient::handleIncomingMessage, this, _1)); client->onPresenceReceived.connect(boost::bind(&SluiftClient::handleIncomingPresence, this, _1)); client->getPubSubManager()->onEvent.connect(boost::bind(&SluiftClient::handleIncomingPubSubEvent, this, _1, _2)); client->getRoster()->onInitialRosterPopulated.connect(boost::bind(&SluiftClient::handleInitialRosterPopulated, this)); client->getClientBlockListManager()->getBlockList()->onItemAdded.connect(boost::bind(&SluiftClient::handleIncomingBlockEvent, this, _1)); client->getClientBlockListManager()->getBlockList()->onItemRemoved.connect(boost::bind(&SluiftClient::handleIncomingUnblockEvent, this, _1)); } SluiftClient::~SluiftClient() { delete tracer; delete client; } void SluiftClient::connect() { rosterReceived = false; blockListReceived = false; disconnectedError = boost::optional<ClientError>(); client->connect(options); } -void SluiftClient::connect(const std::string& host, int port) { - rosterReceived = false; - blockListReceived = false; - options.manualHostname = host; - options.manualPort = port; - disconnectedError = boost::optional<ClientError>(); - client->connect(options); -} - void SluiftClient::setTraceEnabled(bool b) { if (b && !tracer) { tracer = new ClientXMLTracer(client, options.boshURL.isEmpty()? false: true); } else if (!b && tracer) { delete tracer; tracer = nullptr; } } void SluiftClient::waitConnected(int timeout) { Watchdog watchdog(timeout, networkFactories->getTimerFactory()); while (!watchdog.getTimedOut() && client->isActive() && !client->isAvailable()) { eventLoop->runUntilEvents(); } if (watchdog.getTimedOut()) { client->disconnect(); throw Lua::Exception("Timeout while connecting"); } if (disconnectedError) { throw Lua::Exception(getErrorString(*disconnectedError)); } } bool SluiftClient::isConnected() const { return client->isAvailable(); } void SluiftClient::disconnect() { client->disconnect(); diff --git a/Sluift/client.cpp b/Sluift/client.cpp index d2b50fa..0dc7014 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -69,74 +69,99 @@ static void addPayloadsToTable(lua_State* L, const std::vector<std::shared_ptr<P } static std::shared_ptr<Payload> getPayload(lua_State* L, int index) { if (lua_type(L, index) == LUA_TTABLE) { return std::dynamic_pointer_cast<Payload>(Sluift::globals.elementConvertor.convertFromLua(L, index)); } else if (lua_type(L, index) == LUA_TSTRING) { return std::make_shared<RawXMLPayload>(Lua::checkString(L, index)); } else { return std::shared_ptr<Payload>(); } } static std::vector< std::shared_ptr<Payload> > getPayloadsFromTable(lua_State* L, int index) { index = Lua::absoluteOffset(L, index); std::vector< std::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)) { std::shared_ptr<Payload> payload = getPayload(L, -1); if (payload) { result.push_back(payload); } } } lua_pop(L, 1); return result; } +static void setOptions(lua_State* L, SluiftClient* client) { + Lua::checkType(L, 2, LUA_TTABLE); + lua_getfield(L, 2, "host"); + if (!lua_isnil(L, -1)) { + client->getOptions().manualHostname = lua_tostring(L, -1); + } + lua_getfield(L, 2, "port"); + if (!lua_isnil(L, -1)) { + client->getOptions().manualPort = boost::numeric_cast<int>(lua_tointeger(L, -1)); + } + lua_getfield(L, 2, "ack"); + if (!lua_isnil(L, -1)) { + client->getOptions().useAcks = lua_toboolean(L, -1); + } + lua_getfield(L, 2, "compress"); + if (!lua_isnil(L, -1)) { + client->getOptions().useStreamCompression = lua_toboolean(L, -1); + } + lua_getfield(L, 2, "tls"); + if (!lua_isnil(L, -1)) { + bool useTLS = lua_toboolean(L, -1); + client->getOptions().useTLS = (useTLS ? ClientOptions::UseTLSWhenAvailable : ClientOptions::NeverUseTLS); + } + lua_getfield(L, 2, "bosh_url"); + if (!lua_isnil(L, -1)) { + client->getOptions().boshURL = URL::fromString(lua_tostring(L, -1)); + } + lua_getfield(L, 2, "allow_plain_without_tls"); + if (!lua_isnil(L, -1)) { + client->getOptions().allowPLAINWithoutTLS = lua_toboolean(L, -1); + } + lua_pushvalue(L, 1); +} + + 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->connect(host, port); + setOptions(L, client); + client->connect(); 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", "" ) { @@ -459,91 +484,61 @@ SLUIFT_LUA_FUNCTION_WITH_HELP( "Sends a raw string", "self\n" "data the string to send\n", "" ) { Sluift::globals.eventLoop.runOnce(); getClient(L)->getClient()->sendData(std::string(Lua::checkString(L, 2))); lua_pushvalue(L, 1); return 0; } SLUIFT_LUA_FUNCTION_WITH_HELP( Client, set_options, "Sets the connection options of this client.", "self", "host The host to connect to. When omitted, is determined from resolving the JID domain.\n" "port The port to connect to. When omitted, is determined from resolving the JID domain.\n" "ack Request acknowledgements\n" "compress Use stream compression when available\n" "tls Use TLS when available\n" "bosh_url Connect using the specified BOSH URL\n" "allow_plain_without_tls Allow PLAIN authentication without a TLS encrypted connection\n" ) { SluiftClient* client = getClient(L); - Lua::checkType(L, 2, LUA_TTABLE); - lua_getfield(L, 2, "host"); - if (!lua_isnil(L, -1)) { - client->getOptions().manualHostname = lua_tostring(L, -1); - } - lua_getfield(L, 2, "port"); - if (!lua_isnil(L, -1)) { - client->getOptions().manualPort = boost::numeric_cast<int>(lua_tointeger(L, -1)); - } - lua_getfield(L, 2, "ack"); - if (!lua_isnil(L, -1)) { - client->getOptions().useAcks = lua_toboolean(L, -1); - } - lua_getfield(L, 2, "compress"); - if (!lua_isnil(L, -1)) { - client->getOptions().useStreamCompression = lua_toboolean(L, -1); - } - lua_getfield(L, 2, "tls"); - if (!lua_isnil(L, -1)) { - bool useTLS = lua_toboolean(L, -1); - client->getOptions().useTLS = (useTLS ? ClientOptions::UseTLSWhenAvailable : ClientOptions::NeverUseTLS); - } - lua_getfield(L, 2, "bosh_url"); - if (!lua_isnil(L, -1)) { - client->getOptions().boshURL = URL::fromString(lua_tostring(L, -1)); - } - lua_getfield(L, 2, "allow_plain_without_tls"); - if (!lua_isnil(L, -1)) { - client->getOptions().allowPLAINWithoutTLS = lua_toboolean(L, -1); - } - lua_pushvalue(L, 1); + setOptions(L, client); return 0; } SLUIFT_LUA_FUNCTION_WITH_HELP( Client, get_options, "Returns a table with all the connection options of this client.", "self\n", "" ) { Sluift::globals.eventLoop.runOnce(); SluiftClient* client = getClient(L); Lua::Table optionsTable = boost::assign::map_list_of ("host", std::make_shared<Lua::Value>(client->getOptions().manualHostname)) ("port", std::make_shared<Lua::Value>(client->getOptions().manualPort)) ("ack", std::make_shared<Lua::Value>(client->getOptions().useAcks)) ("compress", std::make_shared<Lua::Value>(client->getOptions().useStreamCompression)) ("tls", std::make_shared<Lua::Value>(client->getOptions().useTLS == ClientOptions::NeverUseTLS ? false : true)) ("bosh_url", std::make_shared<Lua::Value>(client->getOptions().boshURL.toString())) ("allow_plain_without_tls", std::make_shared<Lua::Value>(client->getOptions().allowPLAINWithoutTLS)); pushValue(L, optionsTable); Lua::registerTableToString(L, -1); return 1; } static void pushEvent(lua_State* L, const SluiftClient::Event& event) { switch (event.type) { case SluiftClient::Event::MessageType: { Message::ref message = std::dynamic_pointer_cast<Message>(event.stanza); Lua::Table result = boost::assign::map_list_of |