diff options
Diffstat (limited to 'Sluift/SluiftClient.cpp')
-rw-r--r-- | Sluift/SluiftClient.cpp | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/Sluift/SluiftClient.cpp b/Sluift/SluiftClient.cpp index 9db8969..d2b1beb 100644 --- a/Sluift/SluiftClient.cpp +++ b/Sluift/SluiftClient.cpp @@ -1,32 +1,32 @@ /* - * Copyright (c) 2013-2014 Isode Limited. + * Copyright (c) 2013-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Sluift/SluiftClient.h> #include <boost/numeric/conversion/cast.hpp> #include <Swiften/Client/ClientXMLTracer.h> #include <Swiften/Client/Client.h> #include <Swiften/Roster/XMPPRoster.h> #include <Sluift/SluiftGlobals.h> #include <Sluift/Lua/Exception.h> #include <Swiften/Elements/Message.h> #include <Swiften/Elements/PubSubEvent.h> #include <Swiften/Queries/RawRequest.h> #include <Sluift/Helpers.h> #include <Swiften/Elements/Presence.h> using namespace Swift; SluiftClient::SluiftClient( const JID& jid, const std::string& password, NetworkFactories* networkFactories, SimpleEventLoop* eventLoop) : networkFactories(networkFactories), eventLoop(eventLoop), tracer(NULL) { client = new Client(jid, password, networkFactories); @@ -98,67 +98,75 @@ void SluiftClient::setSoftwareVersion(const std::string& name, const std::string boost::optional<SluiftClient::Event> SluiftClient::getNextEvent( int timeout, boost::function<bool (const Event&)> condition) { Watchdog watchdog(timeout, networkFactories->getTimerFactory()); size_t currentIndex = 0; while (true) { // Look for pending events in the queue while (currentIndex < pendingEvents.size()) { Event event = pendingEvents[currentIndex]; if (!condition || condition(event)) { pendingEvents.erase( pendingEvents.begin() + boost::numeric_cast<int>(currentIndex)); return event; } ++currentIndex; } // Wait for new events while (!watchdog.getTimedOut() && currentIndex >= pendingEvents.size() && client->isActive()) { eventLoop->runUntilEvents(); } // Finish if we're disconnected or timed out if (watchdog.getTimedOut() || !client->isActive()) { return boost::optional<Event>(); } } } -std::vector<XMPPRosterItem> SluiftClient::getRoster() { +std::vector<XMPPRosterItem> SluiftClient::getRoster(int timeout) { + Watchdog watchdog(timeout, networkFactories->getTimerFactory()); if (!rosterReceived) { // If we haven't requested it yet, request it for the first time client->requestRoster(); - } - while (!rosterReceived) { - eventLoop->runUntilEvents(); + + // Wait for new events + while (!watchdog.getTimedOut() && !rosterReceived) { + eventLoop->runUntilEvents(); + } + + // Throw an error if we're timed out + if (watchdog.getTimedOut()) { + throw Lua::Exception("Timeout while requesting roster"); + } } return client->getRoster()->getItems(); } void SluiftClient::handleIncomingMessage(boost::shared_ptr<Message> stanza) { if (stanza->getPayload<PubSubEvent>()) { // Already handled by pubsub manager return; } pendingEvents.push_back(Event(stanza)); } void SluiftClient::handleIncomingPresence(boost::shared_ptr<Presence> stanza) { pendingEvents.push_back(Event(stanza)); } void SluiftClient::handleIncomingPubSubEvent(const JID& from, boost::shared_ptr<PubSubEventPayload> event) { pendingEvents.push_back(Event(from, event)); } void SluiftClient::handleInitialRosterPopulated() { rosterReceived = true; } void SluiftClient::handleRequestResponse(boost::shared_ptr<Payload> response, boost::shared_ptr<ErrorPayload> error) { requestResponse = response; requestError = error; requestResponseReceived = true; } |