diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-10-27 19:06:56 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-10-27 19:07:55 (GMT) |
commit | 6810a2896f27e7ee07aee847f5e8dbccd1f6ec89 (patch) | |
tree | f7ea87f030e57cb4494a4f897506fb18fc3d2241 /Swiften/Network | |
parent | a7da393cfc807048d320ddba8a1c7d24ef23a46e (diff) | |
download | swift-contrib-6810a2896f27e7ee07aee847f5e8dbccd1f6ec89.zip swift-contrib-6810a2896f27e7ee07aee847f5e8dbccd1f6ec89.tar.bz2 |
Remove MainEventLoop singleton.
The event loop now needs to be explicitly passed to clients
using it.
Diffstat (limited to 'Swiften/Network')
20 files changed, 108 insertions, 82 deletions
diff --git a/Swiften/Network/BoostConnection.cpp b/Swiften/Network/BoostConnection.cpp index 8cd19f2..6ec8460 100644 --- a/Swiften/Network/BoostConnection.cpp +++ b/Swiften/Network/BoostConnection.cpp @@ -10,7 +10,7 @@ #include <boost/bind.hpp> #include <boost/thread.hpp> -#include "Swiften/EventLoop/MainEventLoop.h" +#include "Swiften/EventLoop/EventLoop.h" #include "Swiften/Base/String.h" #include "Swiften/Base/ByteArray.h" #include "Swiften/Network/HostAddressPort.h" @@ -42,8 +42,8 @@ class SharedBuffer { // ----------------------------------------------------------------------------- -BoostConnection::BoostConnection(boost::asio::io_service* ioService) : - socket_(*ioService), readBuffer_(BUFFER_SIZE) { +BoostConnection::BoostConnection(boost::asio::io_service* ioService, EventLoop* eventLoop) : + eventLoop(eventLoop), socket_(*ioService), readBuffer_(BUFFER_SIZE) { } BoostConnection::~BoostConnection() { @@ -73,11 +73,11 @@ void BoostConnection::write(const ByteArray& data) { void BoostConnection::handleConnectFinished(const boost::system::error_code& error) { if (!error) { - MainEventLoop::postEvent(boost::bind(boost::ref(onConnectFinished), false), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onConnectFinished), false), shared_from_this()); doRead(); } else if (error != boost::asio::error::operation_aborted) { - MainEventLoop::postEvent(boost::bind(boost::ref(onConnectFinished), true), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onConnectFinished), true), shared_from_this()); } } @@ -89,26 +89,26 @@ void BoostConnection::doRead() { void BoostConnection::handleSocketRead(const boost::system::error_code& error, size_t bytesTransferred) { if (!error) { - MainEventLoop::postEvent(boost::bind(boost::ref(onDataRead), ByteArray(&readBuffer_[0], bytesTransferred)), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onDataRead), ByteArray(&readBuffer_[0], bytesTransferred)), shared_from_this()); doRead(); } else if (error == boost::asio::error::eof) { - MainEventLoop::postEvent(boost::bind(boost::ref(onDisconnected), boost::optional<Error>()), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onDisconnected), boost::optional<Error>()), shared_from_this()); } else if (error != boost::asio::error::operation_aborted) { - MainEventLoop::postEvent(boost::bind(boost::ref(onDisconnected), ReadError), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onDisconnected), ReadError), shared_from_this()); } } void BoostConnection::handleDataWritten(const boost::system::error_code& error) { if (!error) { - MainEventLoop::postEvent(boost::ref(onDataWritten), shared_from_this()); + eventLoop->postEvent(boost::ref(onDataWritten), shared_from_this()); } if (error == boost::asio::error::eof) { - MainEventLoop::postEvent(boost::bind(boost::ref(onDisconnected), boost::optional<Error>()), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onDisconnected), boost::optional<Error>()), shared_from_this()); } else if (error && error != boost::asio::error::operation_aborted) { - MainEventLoop::postEvent(boost::bind(boost::ref(onDisconnected), WriteError), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onDisconnected), WriteError), shared_from_this()); } } diff --git a/Swiften/Network/BoostConnection.h b/Swiften/Network/BoostConnection.h index 4f5352f..da4f7b8 100644 --- a/Swiften/Network/BoostConnection.h +++ b/Swiften/Network/BoostConnection.h @@ -20,14 +20,16 @@ namespace boost { } namespace Swift { + class EventLoop; + class BoostConnection : public Connection, public EventOwner, public boost::enable_shared_from_this<BoostConnection> { public: typedef boost::shared_ptr<BoostConnection> ref; ~BoostConnection(); - static ref create(boost::asio::io_service* ioService) { - return ref(new BoostConnection(ioService)); + static ref create(boost::asio::io_service* ioService, EventLoop* eventLoop) { + return ref(new BoostConnection(ioService, eventLoop)); } virtual void listen(); @@ -42,7 +44,7 @@ namespace Swift { HostAddressPort getLocalAddress() const; private: - BoostConnection(boost::asio::io_service* ioService); + BoostConnection(boost::asio::io_service* ioService, EventLoop* eventLoop); void handleConnectFinished(const boost::system::error_code& error); void handleSocketRead(const boost::system::error_code& error, size_t bytesTransferred); @@ -50,6 +52,7 @@ namespace Swift { void doRead(); private: + EventLoop* eventLoop; boost::asio::ip::tcp::socket socket_; std::vector<char> readBuffer_; bool disconnecting_; diff --git a/Swiften/Network/BoostConnectionFactory.cpp b/Swiften/Network/BoostConnectionFactory.cpp index 7ba9f48..00b36c6 100644 --- a/Swiften/Network/BoostConnectionFactory.cpp +++ b/Swiften/Network/BoostConnectionFactory.cpp @@ -9,11 +9,11 @@ namespace Swift { -BoostConnectionFactory::BoostConnectionFactory(boost::asio::io_service* ioService) : ioService(ioService) { +BoostConnectionFactory::BoostConnectionFactory(boost::asio::io_service* ioService, EventLoop* eventLoop) : ioService(ioService), eventLoop(eventLoop) { } boost::shared_ptr<Connection> BoostConnectionFactory::createConnection() { - return BoostConnection::create(ioService); + return BoostConnection::create(ioService, eventLoop); } } diff --git a/Swiften/Network/BoostConnectionFactory.h b/Swiften/Network/BoostConnectionFactory.h index 6588498..551defe 100644 --- a/Swiften/Network/BoostConnectionFactory.h +++ b/Swiften/Network/BoostConnectionFactory.h @@ -16,11 +16,12 @@ namespace Swift { class BoostConnectionFactory : public ConnectionFactory { public: - BoostConnectionFactory(boost::asio::io_service*); + BoostConnectionFactory(boost::asio::io_service*, EventLoop* eventLoop); virtual boost::shared_ptr<Connection> createConnection(); private: boost::asio::io_service* ioService; + EventLoop* eventLoop; }; } diff --git a/Swiften/Network/BoostConnectionServer.cpp b/Swiften/Network/BoostConnectionServer.cpp index 03ae19c..839c990 100644 --- a/Swiften/Network/BoostConnectionServer.cpp +++ b/Swiften/Network/BoostConnectionServer.cpp @@ -9,11 +9,11 @@ #include <boost/bind.hpp> #include <boost/system/system_error.hpp> -#include "Swiften/EventLoop/MainEventLoop.h" +#include "Swiften/EventLoop/EventLoop.h" namespace Swift { -BoostConnectionServer::BoostConnectionServer(int port, boost::asio::io_service* ioService) : port_(port), ioService_(ioService), acceptor_(NULL) { +BoostConnectionServer::BoostConnectionServer(int port, boost::asio::io_service* ioService, EventLoop* eventLoop) : port_(port), ioService_(ioService), eventLoop(eventLoop), acceptor_(NULL) { } @@ -27,10 +27,10 @@ void BoostConnectionServer::start() { } catch (const boost::system::system_error& e) { if (e.code() == boost::asio::error::address_in_use) { - MainEventLoop::postEvent(boost::bind(boost::ref(onStopped), Conflict), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onStopped), Conflict), shared_from_this()); } else { - MainEventLoop::postEvent(boost::bind(boost::ref(onStopped), UnknownError), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onStopped), UnknownError), shared_from_this()); } } } @@ -46,24 +46,24 @@ void BoostConnectionServer::stop(boost::optional<Error> e) { delete acceptor_; acceptor_ = NULL; } - MainEventLoop::postEvent(boost::bind(boost::ref(onStopped), e), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onStopped), e), shared_from_this()); } void BoostConnectionServer::acceptNextConnection() { - BoostConnection::ref newConnection(BoostConnection::create(&acceptor_->io_service())); + BoostConnection::ref newConnection(BoostConnection::create(&acceptor_->io_service(), eventLoop)); acceptor_->async_accept(newConnection->getSocket(), boost::bind(&BoostConnectionServer::handleAccept, shared_from_this(), newConnection, boost::asio::placeholders::error)); } void BoostConnectionServer::handleAccept(boost::shared_ptr<BoostConnection> newConnection, const boost::system::error_code& error) { if (error) { - MainEventLoop::postEvent( + eventLoop->postEvent( boost::bind( &BoostConnectionServer::stop, shared_from_this(), UnknownError), shared_from_this()); } else { - MainEventLoop::postEvent( + eventLoop->postEvent( boost::bind(boost::ref(onNewConnection), newConnection), shared_from_this()); newConnection->listen(); diff --git a/Swiften/Network/BoostConnectionServer.h b/Swiften/Network/BoostConnectionServer.h index abcb3af..223f264 100644 --- a/Swiften/Network/BoostConnectionServer.h +++ b/Swiften/Network/BoostConnectionServer.h @@ -25,8 +25,8 @@ namespace Swift { UnknownError }; - static ref create(int port, boost::asio::io_service* ioService) { - return ref(new BoostConnectionServer(port, ioService)); + static ref create(int port, boost::asio::io_service* ioService, EventLoop* eventLoop) { + return ref(new BoostConnectionServer(port, ioService, eventLoop)); } void start(); @@ -37,7 +37,7 @@ namespace Swift { boost::signal<void (boost::optional<Error>)> onStopped; private: - BoostConnectionServer(int port, boost::asio::io_service* ioService); + BoostConnectionServer(int port, boost::asio::io_service* ioService, EventLoop* eventLoop); void stop(boost::optional<Error> e); void acceptNextConnection(); @@ -46,6 +46,7 @@ namespace Swift { private: int port_; boost::asio::io_service* ioService_; + EventLoop* eventLoop; boost::asio::ip::tcp::acceptor* acceptor_; }; } diff --git a/Swiften/Network/BoostTimer.cpp b/Swiften/Network/BoostTimer.cpp index c655860..65e7712 100644 --- a/Swiften/Network/BoostTimer.cpp +++ b/Swiften/Network/BoostTimer.cpp @@ -9,12 +9,12 @@ #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/asio.hpp> -#include "Swiften/EventLoop/MainEventLoop.h" +#include "Swiften/EventLoop/EventLoop.h" namespace Swift { -BoostTimer::BoostTimer(int milliseconds, boost::asio::io_service* service) : - timeout(milliseconds), timer(*service) { +BoostTimer::BoostTimer(int milliseconds, boost::asio::io_service* service, EventLoop* eventLoop) : + timeout(milliseconds), timer(*service), eventLoop(eventLoop) { } void BoostTimer::start() { @@ -31,7 +31,7 @@ void BoostTimer::handleTimerTick(const boost::system::error_code& error) { assert(error == boost::asio::error::operation_aborted); } else { - MainEventLoop::postEvent(boost::bind(boost::ref(onTick)), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onTick)), shared_from_this()); timer.expires_from_now(boost::posix_time::milliseconds(timeout)); timer.async_wait(boost::bind(&BoostTimer::handleTimerTick, shared_from_this(), boost::asio::placeholders::error)); } diff --git a/Swiften/Network/BoostTimer.h b/Swiften/Network/BoostTimer.h index f48cb36..548133f 100644 --- a/Swiften/Network/BoostTimer.h +++ b/Swiften/Network/BoostTimer.h @@ -14,24 +14,27 @@ #include "Swiften/Network/Timer.h" namespace Swift { + class EventLoop; + class BoostTimer : public Timer, public EventOwner, public boost::enable_shared_from_this<BoostTimer> { public: typedef boost::shared_ptr<BoostTimer> ref; - static ref create(int milliseconds, boost::asio::io_service* service) { - return ref(new BoostTimer(milliseconds, service)); + static ref create(int milliseconds, boost::asio::io_service* service, EventLoop* eventLoop) { + return ref(new BoostTimer(milliseconds, service, eventLoop)); } virtual void start(); virtual void stop(); private: - BoostTimer(int milliseconds, boost::asio::io_service* service); + BoostTimer(int milliseconds, boost::asio::io_service* service, EventLoop* eventLoop); void handleTimerTick(const boost::system::error_code& error); private: int timeout; boost::asio::deadline_timer timer; + EventLoop* eventLoop; }; } diff --git a/Swiften/Network/BoostTimerFactory.cpp b/Swiften/Network/BoostTimerFactory.cpp index b22525c..38842f9 100644 --- a/Swiften/Network/BoostTimerFactory.cpp +++ b/Swiften/Network/BoostTimerFactory.cpp @@ -9,11 +9,11 @@ namespace Swift { -BoostTimerFactory::BoostTimerFactory(boost::asio::io_service* ioService) : ioService(ioService) { +BoostTimerFactory::BoostTimerFactory(boost::asio::io_service* ioService, EventLoop* eventLoop) : ioService(ioService), eventLoop(eventLoop) { } boost::shared_ptr<Timer> BoostTimerFactory::createTimer(int milliseconds) { - return BoostTimer::create(milliseconds, ioService); + return BoostTimer::create(milliseconds, ioService, eventLoop); } } diff --git a/Swiften/Network/BoostTimerFactory.h b/Swiften/Network/BoostTimerFactory.h index a22592c..a987763 100644 --- a/Swiften/Network/BoostTimerFactory.h +++ b/Swiften/Network/BoostTimerFactory.h @@ -13,14 +13,16 @@ namespace Swift { class BoostTimer; + class EventLoop; class BoostTimerFactory : public TimerFactory { public: - BoostTimerFactory(boost::asio::io_service*); + BoostTimerFactory(boost::asio::io_service*, EventLoop* eventLoop); virtual boost::shared_ptr<Timer> createTimer(int milliseconds); private: boost::asio::io_service* ioService; + EventLoop* eventLoop; }; } diff --git a/Swiften/Network/CAresDomainNameResolver.cpp b/Swiften/Network/CAresDomainNameResolver.cpp index ba41d03..8462e4f 100644 --- a/Swiften/Network/CAresDomainNameResolver.cpp +++ b/Swiften/Network/CAresDomainNameResolver.cpp @@ -19,7 +19,7 @@ #include "Swiften/Network/DomainNameServiceQuery.h" #include "Swiften/Network/DomainNameAddressQuery.h" #include "Swiften/Base/ByteArray.h" -#include "Swiften/EventLoop/MainEventLoop.h" +#include "Swiften/EventLoop/EventLoop.h" #include "Swiften/Base/foreach.h" namespace Swift { @@ -77,10 +77,10 @@ class CAresDomainNameServiceQuery : public DomainNameServiceQuery, public CAresQ } } std::sort(records.begin(), records.end(), ResultPriorityComparator()); - MainEventLoop::postEvent(boost::bind(boost::ref(onResult), records)); + eventLoop->postEvent(boost::bind(boost::ref(onResult), records)); } else if (status != ARES_EDESTRUCTION) { - MainEventLoop::postEvent(boost::bind(boost::ref(onResult), std::vector<DomainNameServiceQuery::Result>()), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onResult), std::vector<DomainNameServiceQuery::Result>()), shared_from_this()); } } }; @@ -105,15 +105,15 @@ class CAresDomainNameAddressQuery : public DomainNameAddressQuery, public CAresQ std::vector<HostAddress> results; results.push_back(HostAddress(inet_ntoa(addr))); - MainEventLoop::postEvent(boost::bind(boost::ref(onResult), results, boost::optional<DomainNameResolveError>()), boost::dynamic_pointer_cast<CAresDomainNameAddressQuery>(shared_from_this())); + eventLoop->postEvent(boost::bind(boost::ref(onResult), results, boost::optional<DomainNameResolveError>()), boost::dynamic_pointer_cast<CAresDomainNameAddressQuery>(shared_from_this())); ares_free_hostent(hosts); } else { - MainEventLoop::postEvent(boost::bind(boost::ref(onResult), std::vector<HostAddress>(), boost::optional<DomainNameResolveError>(DomainNameResolveError())), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onResult), std::vector<HostAddress>(), boost::optional<DomainNameResolveError>(DomainNameResolveError())), shared_from_this()); } } else if (status != ARES_EDESTRUCTION) { - MainEventLoop::postEvent(boost::bind(boost::ref(onResult), std::vector<HostAddress>(), boost::optional<DomainNameResolveError>(DomainNameResolveError())), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onResult), std::vector<HostAddress>(), boost::optional<DomainNameResolveError>(DomainNameResolveError())), shared_from_this()); } } }; diff --git a/Swiften/Network/DummyConnection.h b/Swiften/Network/DummyConnection.h index 576965f..6b426b1 100644 --- a/Swiften/Network/DummyConnection.h +++ b/Swiften/Network/DummyConnection.h @@ -11,12 +11,14 @@ #include <boost/enable_shared_from_this.hpp> #include "Swiften/Network/Connection.h" -#include "Swiften/EventLoop/MainEventLoop.h" +#include "Swiften/EventLoop/EventLoop.h" #include "Swiften/EventLoop/EventOwner.h" namespace Swift { class DummyConnection : public Connection, public EventOwner, public boost::enable_shared_from_this<DummyConnection> { public: + DummyConnection(EventLoop* eventLoop) : eventLoop(eventLoop) {} + void listen() { assert(false); } @@ -30,12 +32,12 @@ namespace Swift { } void write(const ByteArray& data) { - MainEventLoop::postEvent(boost::ref(onDataWritten), shared_from_this()); + eventLoop->postEvent(boost::ref(onDataWritten), shared_from_this()); onDataSent(data); } void receive(const ByteArray& data) { - MainEventLoop::postEvent(boost::bind(boost::ref(onDataRead), ByteArray(data)), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onDataRead), ByteArray(data)), shared_from_this()); } HostAddressPort getLocalAddress() const { @@ -44,6 +46,7 @@ namespace Swift { boost::signal<void (const ByteArray&)> onDataSent; + EventLoop* eventLoop; HostAddressPort localAddress; }; } diff --git a/Swiften/Network/FakeConnection.h b/Swiften/Network/FakeConnection.h index a89466f..4e2e960 100644 --- a/Swiften/Network/FakeConnection.h +++ b/Swiften/Network/FakeConnection.h @@ -14,7 +14,7 @@ #include "Swiften/Network/Connection.h" #include "Swiften/Network/HostAddressPort.h" #include "Swiften/EventLoop/EventOwner.h" -#include "Swiften/EventLoop/MainEventLoop.h" +#include "Swiften/EventLoop/EventLoop.h" namespace Swift { class FakeConnection : @@ -30,7 +30,7 @@ namespace Swift { DisconnectedWithError }; - FakeConnection() : state(Initial), delayConnect(false) {} + FakeConnection(EventLoop* eventLoop) : eventLoop(eventLoop), state(Initial), delayConnect(false) {} virtual void listen() { assert(false); @@ -44,7 +44,7 @@ namespace Swift { error = boost::optional<Error>(e); state = DisconnectedWithError; if (connectedTo) { - MainEventLoop::postEvent( + eventLoop->postEvent( boost::bind(boost::ref(onDisconnected), error), shared_from_this()); } @@ -62,7 +62,7 @@ namespace Swift { else { state = DisconnectedWithError; } - MainEventLoop::postEvent( + eventLoop->postEvent( boost::bind(boost::ref(onConnectFinished), error), shared_from_this()); } @@ -76,7 +76,7 @@ namespace Swift { state = DisconnectedWithError; } connectedTo.reset(); - MainEventLoop::postEvent( + eventLoop->postEvent( boost::bind(boost::ref(onDisconnected), error), shared_from_this()); } @@ -89,6 +89,7 @@ namespace Swift { delayConnect = true; } + EventLoop* eventLoop; boost::optional<HostAddressPort> connectedTo; std::vector<ByteArray> dataWritten; boost::optional<Error> error; diff --git a/Swiften/Network/PlatformDomainNameResolver.cpp b/Swiften/Network/PlatformDomainNameResolver.cpp index 452783a..44c87e0 100644 --- a/Swiften/Network/PlatformDomainNameResolver.cpp +++ b/Swiften/Network/PlatformDomainNameResolver.cpp @@ -19,7 +19,7 @@ #include "Swiften/Base/String.h" #include "Swiften/Network/HostAddress.h" -#include "Swiften/EventLoop/MainEventLoop.h" +#include "Swiften/EventLoop/EventLoop.h" #include "Swiften/Network/HostAddressPort.h" #include "Swiften/Network/DomainNameAddressQuery.h" @@ -27,7 +27,7 @@ using namespace Swift; namespace { struct AddressQuery : public DomainNameAddressQuery, public boost::enable_shared_from_this<AddressQuery>, public EventOwner { - AddressQuery(const String& host) : hostname(host), thread(NULL), safeToJoin(false) {} + AddressQuery(const String& host, EventLoop* eventLoop) : hostname(host), eventLoop(eventLoop), thread(NULL), safeToJoin(false) {} ~AddressQuery() { if (safeToJoin) { @@ -64,7 +64,7 @@ namespace { } //std::cout << "PlatformDomainNameResolver::doRun(): Success" << std::endl; - MainEventLoop::postEvent( + eventLoop->postEvent( boost::bind(boost::ref(onResult), results, boost::optional<DomainNameResolveError>()), shared_from_this()); } @@ -77,11 +77,12 @@ namespace { } void emitError() { - MainEventLoop::postEvent(boost::bind(boost::ref(onResult), std::vector<HostAddress>(), boost::optional<DomainNameResolveError>(DomainNameResolveError())), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onResult), std::vector<HostAddress>(), boost::optional<DomainNameResolveError>(DomainNameResolveError())), shared_from_this()); } boost::asio::io_service ioService; String hostname; + EventLoop* eventLoop; boost::thread* thread; bool safeToJoin; }; @@ -90,15 +91,15 @@ namespace { namespace Swift { -PlatformDomainNameResolver::PlatformDomainNameResolver() { +PlatformDomainNameResolver::PlatformDomainNameResolver(EventLoop* eventLoop) : eventLoop(eventLoop) { } boost::shared_ptr<DomainNameServiceQuery> PlatformDomainNameResolver::createServiceQuery(const String& name) { - return boost::shared_ptr<DomainNameServiceQuery>(new PlatformDomainNameServiceQuery(getNormalized(name))); + return boost::shared_ptr<DomainNameServiceQuery>(new PlatformDomainNameServiceQuery(getNormalized(name), eventLoop)); } boost::shared_ptr<DomainNameAddressQuery> PlatformDomainNameResolver::createAddressQuery(const String& name) { - return boost::shared_ptr<DomainNameAddressQuery>(new AddressQuery(getNormalized(name))); + return boost::shared_ptr<DomainNameAddressQuery>(new AddressQuery(getNormalized(name), eventLoop)); } } diff --git a/Swiften/Network/PlatformDomainNameResolver.h b/Swiften/Network/PlatformDomainNameResolver.h index a385122..46c209b 100644 --- a/Swiften/Network/PlatformDomainNameResolver.h +++ b/Swiften/Network/PlatformDomainNameResolver.h @@ -10,12 +10,16 @@ namespace Swift { class String; + class EventLoop; class PlatformDomainNameResolver : public DomainNameResolver { public: - PlatformDomainNameResolver(); + PlatformDomainNameResolver(EventLoop* eventLoop); virtual boost::shared_ptr<DomainNameServiceQuery> createServiceQuery(const String& name); virtual boost::shared_ptr<DomainNameAddressQuery> createAddressQuery(const String& name); + + private: + EventLoop* eventLoop; }; } diff --git a/Swiften/Network/PlatformDomainNameServiceQuery.cpp b/Swiften/Network/PlatformDomainNameServiceQuery.cpp index aa0be4e..ed73b64 100644 --- a/Swiften/Network/PlatformDomainNameServiceQuery.cpp +++ b/Swiften/Network/PlatformDomainNameServiceQuery.cpp @@ -25,14 +25,14 @@ #include <boost/bind.hpp> #include "Swiften/Base/ByteArray.h" -#include "Swiften/EventLoop/MainEventLoop.h" +#include "Swiften/EventLoop/EventLoop.h" #include "Swiften/Base/foreach.h" using namespace Swift; namespace Swift { -PlatformDomainNameServiceQuery::PlatformDomainNameServiceQuery(const String& service) : thread(NULL), service(service), safeToJoin(true) { +PlatformDomainNameServiceQuery::PlatformDomainNameServiceQuery(const String& service, EventLoop* eventLoop) : eventLoop(eventLoop), thread(NULL), service(service), safeToJoin(true) { } PlatformDomainNameServiceQuery::~PlatformDomainNameServiceQuery() { @@ -166,12 +166,12 @@ void PlatformDomainNameServiceQuery::doRun() { safeToJoin = true; std::sort(records.begin(), records.end(), ResultPriorityComparator()); //std::cout << "Sending out " << records.size() << " SRV results " << std::endl; - MainEventLoop::postEvent(boost::bind(boost::ref(onResult), records)); + eventLoop->postEvent(boost::bind(boost::ref(onResult), records)); } void PlatformDomainNameServiceQuery::emitError() { safeToJoin = true; - MainEventLoop::postEvent(boost::bind(boost::ref(onResult), std::vector<DomainNameServiceQuery::Result>()), shared_from_this()); + eventLoop->postEvent(boost::bind(boost::ref(onResult), std::vector<DomainNameServiceQuery::Result>()), shared_from_this()); } } diff --git a/Swiften/Network/PlatformDomainNameServiceQuery.h b/Swiften/Network/PlatformDomainNameServiceQuery.h index ff50b31..9808196 100644 --- a/Swiften/Network/PlatformDomainNameServiceQuery.h +++ b/Swiften/Network/PlatformDomainNameServiceQuery.h @@ -14,9 +14,11 @@ #include "Swiften/Base/String.h" namespace Swift { + class EventLoop; + class PlatformDomainNameServiceQuery : public DomainNameServiceQuery, public boost::enable_shared_from_this<PlatformDomainNameServiceQuery>, public EventOwner { public: - PlatformDomainNameServiceQuery(const String& service); + PlatformDomainNameServiceQuery(const String& service, EventLoop* eventLoop); ~PlatformDomainNameServiceQuery(); virtual void run(); @@ -26,6 +28,7 @@ namespace Swift { void emitError(); private: + EventLoop* eventLoop; boost::thread* thread; String service; bool safeToJoin; diff --git a/Swiften/Network/StaticDomainNameResolver.cpp b/Swiften/Network/StaticDomainNameResolver.cpp index 636f310..ccea2b7 100644 --- a/Swiften/Network/StaticDomainNameResolver.cpp +++ b/Swiften/Network/StaticDomainNameResolver.cpp @@ -16,7 +16,7 @@ using namespace Swift; namespace { struct ServiceQuery : public DomainNameServiceQuery, public boost::enable_shared_from_this<ServiceQuery> { - ServiceQuery(const String& service, Swift::StaticDomainNameResolver* resolver) : service(service), resolver(resolver) {} + ServiceQuery(const String& service, Swift::StaticDomainNameResolver* resolver, EventLoop* eventLoop) : eventLoop(eventLoop), service(service), resolver(resolver) {} virtual void run() { if (!resolver->getIsResponsive()) { @@ -28,19 +28,20 @@ namespace { results.push_back(i->second); } } - MainEventLoop::postEvent(boost::bind(&ServiceQuery::emitOnResult, shared_from_this(), results)); + eventLoop->postEvent(boost::bind(&ServiceQuery::emitOnResult, shared_from_this(), results)); } void emitOnResult(std::vector<DomainNameServiceQuery::Result> results) { onResult(results); } + EventLoop* eventLoop; String service; StaticDomainNameResolver* resolver; }; struct AddressQuery : public DomainNameAddressQuery, public boost::enable_shared_from_this<AddressQuery> { - AddressQuery(const String& host, StaticDomainNameResolver* resolver) : host(host), resolver(resolver) {} + AddressQuery(const String& host, StaticDomainNameResolver* resolver, EventLoop* eventLoop) : eventLoop(eventLoop), host(host), resolver(resolver) {} virtual void run() { if (!resolver->getIsResponsive()) { @@ -48,11 +49,11 @@ namespace { } StaticDomainNameResolver::AddressesMap::const_iterator i = resolver->getAddresses().find(host); if (i != resolver->getAddresses().end()) { - MainEventLoop::postEvent( + eventLoop->postEvent( boost::bind(&AddressQuery::emitOnResult, shared_from_this(), i->second, boost::optional<DomainNameResolveError>())); } else { - MainEventLoop::postEvent(boost::bind(&AddressQuery::emitOnResult, shared_from_this(), std::vector<HostAddress>(), boost::optional<DomainNameResolveError>(DomainNameResolveError()))); + eventLoop->postEvent(boost::bind(&AddressQuery::emitOnResult, shared_from_this(), std::vector<HostAddress>(), boost::optional<DomainNameResolveError>(DomainNameResolveError()))); } } @@ -60,6 +61,7 @@ namespace { onResult(results, error); } + EventLoop* eventLoop; String host; StaticDomainNameResolver* resolver; }; @@ -67,7 +69,7 @@ namespace { namespace Swift { -StaticDomainNameResolver::StaticDomainNameResolver() : isResponsive(true) { +StaticDomainNameResolver::StaticDomainNameResolver(EventLoop* eventLoop) : eventLoop(eventLoop), isResponsive(true) { } void StaticDomainNameResolver::addAddress(const String& domain, const HostAddress& address) { @@ -92,11 +94,11 @@ void StaticDomainNameResolver::addXMPPClientService(const String& domain, const } boost::shared_ptr<DomainNameServiceQuery> StaticDomainNameResolver::createServiceQuery(const String& name) { - return boost::shared_ptr<DomainNameServiceQuery>(new ServiceQuery(name, this)); + return boost::shared_ptr<DomainNameServiceQuery>(new ServiceQuery(name, this, eventLoop)); } boost::shared_ptr<DomainNameAddressQuery> StaticDomainNameResolver::createAddressQuery(const String& name) { - return boost::shared_ptr<DomainNameAddressQuery>(new AddressQuery(name, this)); + return boost::shared_ptr<DomainNameAddressQuery>(new AddressQuery(name, this, eventLoop)); } } diff --git a/Swiften/Network/StaticDomainNameResolver.h b/Swiften/Network/StaticDomainNameResolver.h index 69b0d9d..39b2782 100644 --- a/Swiften/Network/StaticDomainNameResolver.h +++ b/Swiften/Network/StaticDomainNameResolver.h @@ -14,7 +14,7 @@ #include "Swiften/Network/DomainNameResolver.h" #include "Swiften/Network/DomainNameServiceQuery.h" #include "Swiften/Network/DomainNameAddressQuery.h" -#include "Swiften/EventLoop/MainEventLoop.h" +#include "Swiften/EventLoop/EventLoop.h" namespace Swift { class String; @@ -25,7 +25,7 @@ namespace Swift { typedef std::vector< std::pair<String, DomainNameServiceQuery::Result> > ServicesCollection; public: - StaticDomainNameResolver(); + StaticDomainNameResolver(EventLoop* eventLoop); void addAddress(const String& domain, const HostAddress& address); void addService(const String& service, const DomainNameServiceQuery::Result& result); @@ -52,6 +52,7 @@ namespace Swift { virtual boost::shared_ptr<DomainNameAddressQuery> createAddressQuery(const String& name); private: + EventLoop* eventLoop; bool isResponsive; AddressesMap addresses; ServicesCollection services; diff --git a/Swiften/Network/UnitTest/ConnectorTest.cpp b/Swiften/Network/UnitTest/ConnectorTest.cpp index 07e520c..a71d4b7 100644 --- a/Swiften/Network/UnitTest/ConnectorTest.cpp +++ b/Swiften/Network/UnitTest/ConnectorTest.cpp @@ -16,7 +16,6 @@ #include "Swiften/Network/HostAddressPort.h" #include "Swiften/Network/StaticDomainNameResolver.h" #include "Swiften/Network/DummyTimerFactory.h" -#include "Swiften/EventLoop/MainEventLoop.h" #include "Swiften/EventLoop/DummyEventLoop.h" using namespace Swift; @@ -44,8 +43,8 @@ class ConnectorTest : public CppUnit::TestFixture { void setUp() { eventLoop = new DummyEventLoop(); - resolver = new StaticDomainNameResolver(); - connectionFactory = new MockConnectionFactory(); + resolver = new StaticDomainNameResolver(eventLoop); + connectionFactory = new MockConnectionFactory(eventLoop); timerFactory = new DummyTimerFactory(); } @@ -257,14 +256,14 @@ class ConnectorTest : public CppUnit::TestFixture { struct MockConnection : public Connection { public: - MockConnection(const std::vector<HostAddressPort>& failingPorts, bool isResponsive) : failingPorts(failingPorts), isResponsive(isResponsive) {} + MockConnection(const std::vector<HostAddressPort>& failingPorts, bool isResponsive, EventLoop* eventLoop) : eventLoop(eventLoop), failingPorts(failingPorts), isResponsive(isResponsive) {} void listen() { assert(false); } void connect(const HostAddressPort& address) { hostAddressPort = address; if (isResponsive) { bool fail = std::find(failingPorts.begin(), failingPorts.end(), address) != failingPorts.end(); - MainEventLoop::postEvent(boost::bind(boost::ref(onConnectFinished), fail)); + eventLoop->postEvent(boost::bind(boost::ref(onConnectFinished), fail)); } } @@ -272,19 +271,21 @@ class ConnectorTest : public CppUnit::TestFixture { void disconnect() { assert(false); } void write(const ByteArray&) { assert(false); } + EventLoop* eventLoop; boost::optional<HostAddressPort> hostAddressPort; std::vector<HostAddressPort> failingPorts; bool isResponsive; }; struct MockConnectionFactory : public ConnectionFactory { - MockConnectionFactory() : isResponsive(true) { + MockConnectionFactory(EventLoop* eventLoop) : eventLoop(eventLoop), isResponsive(true) { } boost::shared_ptr<Connection> createConnection() { - return boost::shared_ptr<Connection>(new MockConnection(failingPorts, isResponsive)); + return boost::shared_ptr<Connection>(new MockConnection(failingPorts, isResponsive, eventLoop)); } + EventLoop* eventLoop; bool isResponsive; std::vector<HostAddressPort> failingPorts; }; |