From 6810a2896f27e7ee07aee847f5e8dbccd1f6ec89 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Wed, 27 Oct 2010 21:06:56 +0200
Subject: Remove MainEventLoop singleton.

The event loop now needs to be explicitly passed to clients
using it.

diff --git a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot1.cpp b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot1.cpp
index df1da12..ec9c583 100644
--- a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot1.cpp
+++ b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot1.cpp
@@ -11,7 +11,7 @@ using namespace Swift;
 int main(int, char**) {
 	SimpleEventLoop eventLoop;
 
-	Client client(JID("echobot@wonderland.lit"), "mypass");
+	Client client(&eventLoop, JID("echobot@wonderland.lit"), "mypass");
 	client.connect();
 
 	eventLoop.run();
diff --git a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot2.cpp b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot2.cpp
index dcecbcb..fb24e46 100644
--- a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot2.cpp
+++ b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot2.cpp
@@ -20,7 +20,7 @@ void handleMessageReceived(Message::ref message);
 int main(int, char**) {
 	SimpleEventLoop eventLoop;
 
-	client = new Client(JID("echobot@wonderland.lit"), "mypass");
+	client = new Client(&eventLoop, JID("echobot@wonderland.lit"), "mypass");
 	client->onConnected.connect(&handleConnected);
 	client->onMessageReceived.connect(bind(&handleMessageReceived, _1));
 	client->connect();
diff --git a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot3.cpp b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot3.cpp
index b5e6972..d3e3d97 100644
--- a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot3.cpp
+++ b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot3.cpp
@@ -14,8 +14,8 @@ using namespace boost;
 
 class EchoBot {
 	public:
-		EchoBot() {
-			client = new Client(JID("echobot@wonderland.lit"), "mypass");
+		EchoBot(EventLoop* eventLoop) {
+			client = new Client(eventLoop, JID("echobot@wonderland.lit"), "mypass");
 			client->onConnected.connect(bind(&EchoBot::handleConnected, this));
 			client->onMessageReceived.connect(
 					bind(&EchoBot::handleMessageReceived, this, _1));
@@ -47,7 +47,7 @@ class EchoBot {
 
 int main(int, char**) {
 	SimpleEventLoop eventLoop;
-	EchoBot bot;
+	EchoBot bot(&eventLoop);
 	eventLoop.run();
 	return 0;
 }
diff --git a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot4.cpp b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot4.cpp
index 9cedc43..e54346d 100644
--- a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot4.cpp
+++ b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot4.cpp
@@ -15,9 +15,9 @@ using namespace boost;
 //...
 class EchoBot {
 	public:
-		EchoBot() {
+		EchoBot(EventLoop* eventLoop) {
 			//...
-			client = new Client(JID("echobot@wonderland.lit"), "mypass");
+			client = new Client(eventLoop, JID("echobot@wonderland.lit"), "mypass");
 			client->onConnected.connect(bind(&EchoBot::handleConnected, this));
 			client->onMessageReceived.connect(
 					bind(&EchoBot::handleMessageReceived, this, _1));
@@ -82,7 +82,7 @@ class EchoBot {
 
 int main(int, char**) {
 	SimpleEventLoop eventLoop;
-	EchoBot bot;
+	EchoBot bot(&eventLoop);
 	eventLoop.run();
 	return 0;
 }
diff --git a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot5.cpp b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot5.cpp
index d675062..000b2ce 100644
--- a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot5.cpp
+++ b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoBot5.cpp
@@ -15,9 +15,9 @@ using namespace boost;
 //...
 class EchoBot {
 	public:
-		EchoBot() {
+		EchoBot(EventLoop* eventLoop) {
 			//...
-			client = new Client(JID("echobot@wonderland.lit"), "mypass");
+			client = new Client(eventLoop, JID("echobot@wonderland.lit"), "mypass");
 			client->onConnected.connect(bind(&EchoBot::handleConnected, this));
 			client->onMessageReceived.connect(
 					bind(&EchoBot::handleMessageReceived, this, _1));
@@ -90,7 +90,7 @@ class EchoBot {
 
 int main(int, char**) {
 	SimpleEventLoop eventLoop;
-	EchoBot bot;
+	EchoBot bot(&eventLoop);
 	eventLoop.run();
 	return 0;
 }
diff --git a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoComponent.cpp b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoComponent.cpp
index 67f469d..b99aec5 100644
--- a/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoComponent.cpp
+++ b/Documentation/SwiftenDevelopersGuide/Examples/EchoBot/EchoComponent.cpp
@@ -14,8 +14,8 @@ using namespace boost;
 
 class EchoComponent {
 	public:
-		EchoComponent() {
-			component = new Component(JID("echo.wonderland.lit"), "EchoSecret");
+		EchoComponent(EventLoop* eventLoop) {
+			component = new Component(eventLoop, JID("echo.wonderland.lit"), "EchoSecret");
 			component->onConnected.connect(bind(&EchoComponent::handleConnected, this));
 			component->onMessageReceived.connect(
 					bind(&EchoComponent::handleMessageReceived, this, _1));
@@ -58,7 +58,7 @@ class EchoComponent {
 
 int main(int, char**) {
 	SimpleEventLoop eventLoop;
-	EchoComponent bot;
+	EchoComponent bot(&eventLoop);
 	eventLoop.run();
 	return 0;
 }
diff --git a/Limber/main.cpp b/Limber/main.cpp
index 45c6931..0ab4710 100644
--- a/Limber/main.cpp
+++ b/Limber/main.cpp
@@ -13,7 +13,7 @@
 #include "Swiften/Elements/VCard.h"
 #include "Swiften/Server/SimpleUserRegistry.h"
 #include "Swiften/Base/IDGenerator.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 #include "Swiften/EventLoop/SimpleEventLoop.h"
 #include "Swiften/EventLoop/EventOwner.h"
 #include "Swiften/Elements/Stanza.h"
@@ -29,8 +29,8 @@ using namespace Swift;
 
 class Server {
 	public:
-		Server(UserRegistry* userRegistry) : userRegistry_(userRegistry) {
-			serverFromClientConnectionServer_ = BoostConnectionServer::create(5222, &boostIOServiceThread_.getIOService());
+		Server(UserRegistry* userRegistry, EventLoop* eventLoop) : userRegistry_(userRegistry) {
+			serverFromClientConnectionServer_ = BoostConnectionServer::create(5222, &boostIOServiceThread_.getIOService(), eventLoop);
 			serverFromClientConnectionServer_->onNewConnection.connect(boost::bind(&Server::handleNewConnection, this, _1));
 			serverFromClientConnectionServer_->start();
 		}
@@ -96,7 +96,7 @@ int main() {
 	userRegistry.addUser(JID("kevin@localhost"), "kevin");
 	userRegistry.addUser(JID("remko@limber.swift.im"), "remko");
 	userRegistry.addUser(JID("kevin@limber.swift.im"), "kevin");
-	Server server(&userRegistry);
+	Server server(&userRegistry, &eventLoop);
 	eventLoop.run();
 	return 0;
 }
diff --git a/Slimber/CLI/main.cpp b/Slimber/CLI/main.cpp
index d254cb1..5252373 100644
--- a/Slimber/CLI/main.cpp
+++ b/Slimber/CLI/main.cpp
@@ -14,7 +14,7 @@ int main() {
 	SimpleEventLoop eventLoop;
 	
 	DummyMenulet menulet;
-	MainController controller(&menulet);
+	MainController controller(&menulet, &eventLoop);
 
 	eventLoop.run();
 	return 0;
diff --git a/Slimber/Cocoa/CocoaController.mm b/Slimber/Cocoa/CocoaController.mm
index 437d85a..a0c4ef4 100644
--- a/Slimber/Cocoa/CocoaController.mm
+++ b/Slimber/Cocoa/CocoaController.mm
@@ -2,6 +2,7 @@
 
 #include "Slimber/MainController.h"
 #include "Slimber/Cocoa/CocoaMenulet.h"
+#include "Slimber/Cocoa/main.h"
 
 @implementation CocoaController
 
@@ -13,7 +14,7 @@
 
 - (void) awakeFromNib {
 	menulet = new CocoaMenulet();
-	main = new MainController(menulet);
+	main = new MainController(menulet, eventLoop);
 }
 
 @end
diff --git a/Slimber/Cocoa/main.h b/Slimber/Cocoa/main.h
new file mode 100644
index 0000000..681139d
--- /dev/null
+++ b/Slimber/Cocoa/main.h
@@ -0,0 +1,11 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/EventLoop/Cocoa/CocoaEventLoop.h"
+
+extern Swift::CocoaEventLoop* eventLoop;
diff --git a/Slimber/Cocoa/main.mm b/Slimber/Cocoa/main.mm
index e267477..fc051e0 100644
--- a/Slimber/Cocoa/main.mm
+++ b/Slimber/Cocoa/main.mm
@@ -1,8 +1,13 @@
 #include <Cocoa/Cocoa.h>
 
+#include "Slimber/Cocoa/main.h"
 #include "Swiften/EventLoop/Cocoa/CocoaEventLoop.h"
 
+Swift::CocoaEventLoop* eventLoop;
+
 int main(int argc, char *argv[]) {
-	Swift::CocoaEventLoop eventLoop;
-	return NSApplicationMain(argc,  const_cast<const char **>(argv));
+	eventLoop = new Swift::CocoaEventLoop();
+	int result = NSApplicationMain(argc,  const_cast<const char **>(argv));
+	delete eventLoop;
+	return result;
 }
diff --git a/Slimber/MainController.cpp b/Slimber/MainController.cpp
index 38c1a7c..e6c2ab5 100644
--- a/Slimber/MainController.cpp
+++ b/Slimber/MainController.cpp
@@ -22,8 +22,8 @@
 
 using namespace Swift;
 
-MainController::MainController(Menulet* menulet) : menulet(menulet) {
-	dnsSDQuerier = PlatformDNSSDQuerierFactory().createQuerier();
+MainController::MainController(Menulet* menulet, EventLoop* eventLoop) : menulet(menulet) {
+	dnsSDQuerier = PlatformDNSSDQuerierFactory(eventLoop).createQuerier();
 	assert(dnsSDQuerier);
 
 	linkLocalServiceBrowser = new LinkLocalServiceBrowser(dnsSDQuerier);
@@ -37,7 +37,7 @@ MainController::MainController(Menulet* menulet) : menulet(menulet) {
 	vCardCollection = new FileVCardCollection(
 			PlatformApplicationPathProvider("Slimber").getDataDir());
 
-	server = new Server(5222, 5562, linkLocalServiceBrowser, vCardCollection);
+	server = new Server(5222, 5562, linkLocalServiceBrowser, vCardCollection, eventLoop);
 	server->onStopped.connect(
 			boost::bind(&MainController::handleServerStopped, this, _1));
 	server->onSelfConnected.connect(
diff --git a/Slimber/MainController.h b/Slimber/MainController.h
index de90932..4b341d9 100644
--- a/Slimber/MainController.h
+++ b/Slimber/MainController.h
@@ -16,6 +16,7 @@ namespace Swift {
 	class LinkLocalServiceBrowser;
 	class VCardCollection;
 	class Server;
+	class EventLoop;
 }
 
 class MenuletController;
@@ -23,7 +24,7 @@ class Menulet;
 
 class MainController {
 	public:
-		MainController(Menulet* menulet);
+		MainController(Menulet* menulet, Swift::EventLoop* eventLoop);
 		virtual ~MainController();
 
 	private:
diff --git a/Slimber/Server.cpp b/Slimber/Server.cpp
index 734f3b8..c6e700e 100644
--- a/Slimber/Server.cpp
+++ b/Slimber/Server.cpp
@@ -37,13 +37,15 @@ Server::Server(
 		int clientConnectionPort, 
 		int linkLocalConnectionPort, 
 		LinkLocalServiceBrowser* linkLocalServiceBrowser,
-		VCardCollection* vCardCollection) : 
+		VCardCollection* vCardCollection,
+		EventLoop* eventLoop) :
 			linkLocalServiceRegistered(false), 
 			rosterRequested(false), 
 			clientConnectionPort(clientConnectionPort), 
 			linkLocalConnectionPort(linkLocalConnectionPort),
 			linkLocalServiceBrowser(linkLocalServiceBrowser),
 			vCardCollection(vCardCollection),
+			eventLoop(eventLoop),
 			presenceManager(NULL),
 			stopping(false) {
 	linkLocalServiceBrowser->onServiceRegistered.connect(
@@ -57,7 +59,7 @@ Server::~Server() {
 void Server::start() {
 	assert(!serverFromClientConnectionServer);
 	serverFromClientConnectionServer = BoostConnectionServer::create(
-					clientConnectionPort, &boostIOServiceThread.getIOService());
+					clientConnectionPort, &boostIOServiceThread.getIOService(), eventLoop);
 	serverFromClientConnectionServerSignalConnections.push_back(
 		serverFromClientConnectionServer->onNewConnection.connect(
 				boost::bind(&Server::handleNewClientConnection, this, _1)));
@@ -67,7 +69,7 @@ void Server::start() {
 
 	assert(!serverFromNetworkConnectionServer);
 	serverFromNetworkConnectionServer = BoostConnectionServer::create(
-			linkLocalConnectionPort, &boostIOServiceThread.getIOService());
+			linkLocalConnectionPort, &boostIOServiceThread.getIOService(), eventLoop);
 	serverFromNetworkConnectionServerSignalConnections.push_back(
 		serverFromNetworkConnectionServer->onNewConnection.connect(
 				boost::bind(&Server::handleNewLinkLocalConnection, this, _1)));
@@ -254,7 +256,7 @@ void Server::handleElementReceived(boost::shared_ptr<Element> element, boost::sh
 							new LinkLocalConnector(
 								*service,
 								linkLocalServiceBrowser->getQuerier(),
-								BoostConnection::create(&boostIOServiceThread.getIOService())));
+								BoostConnection::create(&boostIOServiceThread.getIOService(), eventLoop)));
 					connector->onConnectFinished.connect(
 							boost::bind(&Server::handleConnectFinished, this, connector, _1));
 					connectors.push_back(connector);
diff --git a/Slimber/Server.h b/Slimber/Server.h
index 864f705..039f351 100644
--- a/Slimber/Server.h
+++ b/Slimber/Server.h
@@ -32,6 +32,7 @@ namespace Swift {
 	class SessionTracer;
 	class RosterPayload;
 	class Presence;
+	class EventLoop;
 
 	class Server {
 		public:
@@ -39,7 +40,8 @@ namespace Swift {
 					int clientConnectionPort, 
 					int linkLocalConnectionPort, 
 					LinkLocalServiceBrowser* browser, 
-					VCardCollection* vCardCollection);
+					VCardCollection* vCardCollection,
+					EventLoop* eventLoop);
 			~Server();
 
 			void start();
@@ -102,6 +104,7 @@ namespace Swift {
 			int linkLocalConnectionPort;
 			LinkLocalServiceBrowser* linkLocalServiceBrowser;
 			VCardCollection* vCardCollection;
+			EventLoop* eventLoop;
 			LinkLocalPresenceManager* presenceManager;
 			bool stopping;
 			boost::shared_ptr<BoostConnectionServer> serverFromClientConnectionServer;
diff --git a/Slimber/UnitTest/LinkLocalPresenceManagerTest.cpp b/Slimber/UnitTest/LinkLocalPresenceManagerTest.cpp
index f8ee755..8c6710a 100644
--- a/Slimber/UnitTest/LinkLocalPresenceManagerTest.cpp
+++ b/Slimber/UnitTest/LinkLocalPresenceManagerTest.cpp
@@ -42,7 +42,7 @@ class LinkLocalPresenceManagerTest : public CppUnit::TestFixture {
 	public:
 		void setUp() {
 			eventLoop = new DummyEventLoop();
-			querier = boost::shared_ptr<FakeDNSSDQuerier>(new FakeDNSSDQuerier("wonderland.lit"));
+			querier = boost::shared_ptr<FakeDNSSDQuerier>(new FakeDNSSDQuerier("wonderland.lit", eventLoop));
 			browser = new LinkLocalServiceBrowser(querier);
 			browser->start();
 		}
diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index b02c78b..c9e6230 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -65,6 +65,7 @@ static const String CLIENT_NODE = "http://swift.im";
 static const String SHOW_NOTIFICATIONS = "showNotifications";
 
 MainController::MainController(
+		EventLoop* eventLoop,
 		ChatWindowFactory* chatWindowFactory,
 		MainWindowFactory *mainWindowFactory,
 		LoginWindowFactory *loginWindowFactory,
@@ -79,7 +80,8 @@ MainController::MainController(
 		Dock* dock,
 		Notifier* notifier,
 		bool useDelayForLatency) :
-			timerFactory_(&boostIOServiceThread_.getIOService()),
+			eventLoop_(eventLoop),
+			timerFactory_(&boostIOServiceThread_.getIOService(), eventLoop),
 			idleDetector_(&idleQuerier_, &timerFactory_, 100),
 			storagesFactory_(storagesFactory),
 			chatWindowFactory_(chatWindowFactory),
@@ -361,7 +363,7 @@ void MainController::performLoginFromCachedCredentials() {
 	}
 	if (!client_) {
 		storages_ = storagesFactory_->createStorages(jid_);
-		client_ = new Swift::Client(jid_, password_, storages_);
+		client_ = new Swift::Client(eventLoop_, jid_, password_, storages_);
 		client_->onDataRead.connect(boost::bind(&XMLConsoleController::handleDataRead, xmlConsoleController_, _1));
 		client_->onDataWritten.connect(boost::bind(&XMLConsoleController::handleDataWritten, xmlConsoleController_, _1));
 		client_->onError.connect(boost::bind(&MainController::handleError, this, _1));
diff --git a/Swift/Controllers/MainController.h b/Swift/Controllers/MainController.h
index 22d2559..709bacf 100644
--- a/Swift/Controllers/MainController.h
+++ b/Swift/Controllers/MainController.h
@@ -28,6 +28,7 @@
 #include "Swift/Controllers/UIEvents/UIEvent.h"
 
 namespace Swift {
+	class EventLoop;
 	class Client;
 	class ChatWindowFactory;
 	class ChatController;
@@ -65,6 +66,7 @@ namespace Swift {
 	class MainController {
 		public:
 			MainController(
+					EventLoop* eventLoop,
 					ChatWindowFactory* chatWindowFactory,
 					MainWindowFactory *mainWindowFactory,
 					LoginWindowFactory *loginWindowFactory,
@@ -107,6 +109,7 @@ namespace Swift {
 			void handleNotificationClicked(const JID& jid);
 
 		private:
+			EventLoop* eventLoop_;
 			BoostIOServiceThread boostIOServiceThread_;
 			BoostTimerFactory timerFactory_;
 			PlatformIdleQuerier idleQuerier_;
diff --git a/Swift/QtUI/QtSwift.cpp b/Swift/QtUI/QtSwift.cpp
index d61f94c..77860d5 100644
--- a/Swift/QtUI/QtSwift.cpp
+++ b/Swift/QtUI/QtSwift.cpp
@@ -135,6 +135,7 @@ QtSwift::QtSwift(po::variables_map options) : autoUpdater_(NULL) {
 		QtMUCSearchWindowFactory* mucSearchWindowFactory = new QtMUCSearchWindowFactory();
 		mucSearchWindowFactories_.push_back(mucSearchWindowFactory);
 		MainController* mainController = new MainController(
+				&clientMainThreadCaller_,
 				chatWindowFactory_,
 				rosterWindowFactory,
 				loginWindowFactory,
diff --git a/Swift/QtUI/QtSwift.h b/Swift/QtUI/QtSwift.h
index 59fa746..6817734 100644
--- a/Swift/QtUI/QtSwift.h
+++ b/Swift/QtUI/QtSwift.h
@@ -45,6 +45,7 @@ namespace Swift {
 	class QtEventWindowFactory;
 	class QtChatListWindowFactory;
 	class QtMUCSearchWindowFactory;
+	class EventLoop;
 		
 	class QtSwift : public QObject {
 		Q_OBJECT
diff --git a/Swiften/Client/Client.cpp b/Swiften/Client/Client.cpp
index 67e2051..45eeeff 100644
--- a/Swiften/Client/Client.cpp
+++ b/Swiften/Client/Client.cpp
@@ -22,7 +22,7 @@
 
 namespace Swift {
 
-Client::Client(const JID& jid, const String& password, Storages* storages) : CoreClient(jid, password), storages(storages) {
+Client::Client(EventLoop* eventLoop, const JID& jid, const String& password, Storages* storages) : CoreClient(eventLoop, jid, password), storages(storages) {
 	memoryStorages = new MemoryStorages();
 
 	softwareVersionResponder = new SoftwareVersionResponder(getIQRouter());
diff --git a/Swiften/Client/Client.h b/Swiften/Client/Client.h
index 47a276a..7e11df9 100644
--- a/Swiften/Client/Client.h
+++ b/Swiften/Client/Client.h
@@ -39,7 +39,7 @@ namespace Swift {
 			 *	this is NULL,
 			 *	all data will be stored in memory (and be lost on shutdown)
 			 */
-			Client(const JID& jid, const String& password, Storages* storages = NULL);
+			Client(EventLoop* eventLoop, const JID& jid, const String& password, Storages* storages = NULL);
 			~Client();
 
 
diff --git a/Swiften/Client/CoreClient.cpp b/Swiften/Client/CoreClient.cpp
index 272a2a7..fa9dca0 100644
--- a/Swiften/Client/CoreClient.cpp
+++ b/Swiften/Client/CoreClient.cpp
@@ -23,7 +23,7 @@
 
 namespace Swift {
 
-CoreClient::CoreClient(const JID& jid, const String& password) : jid_(jid), password_(password), disconnectRequested_(false) {
+CoreClient::CoreClient(EventLoop* eventLoop, const JID& jid, const String& password) : resolver_(eventLoop), jid_(jid), password_(password), eventLoop(eventLoop), disconnectRequested_(false) {
 	stanzaChannel_ = new ClientSessionStanzaChannel();
 	stanzaChannel_->onMessageReceived.connect(boost::ref(onMessageReceived));
 	stanzaChannel_->onPresenceReceived.connect(boost::ref(onPresenceReceived));
@@ -31,8 +31,8 @@ CoreClient::CoreClient(const JID& jid, const String& password) : jid_(jid), pass
 	stanzaChannel_->onAvailableChanged.connect(boost::bind(&CoreClient::handleStanzaChannelAvailableChanged, this, _1));
 
 	iqRouter_ = new IQRouter(stanzaChannel_);
-	connectionFactory_ = new BoostConnectionFactory(&MainBoostIOServiceThread::getInstance().getIOService());
-	timerFactory_ = new BoostTimerFactory(&MainBoostIOServiceThread::getInstance().getIOService());
+	connectionFactory_ = new BoostConnectionFactory(&MainBoostIOServiceThread::getInstance().getIOService(), eventLoop);
+	timerFactory_ = new BoostTimerFactory(&MainBoostIOServiceThread::getInstance().getIOService(), eventLoop);
 	tlsLayerFactory_ = new PlatformTLSLayerFactory();
 }
 
diff --git a/Swiften/Client/CoreClient.h b/Swiften/Client/CoreClient.h
index f440dfe..f6e0b6d 100644
--- a/Swiften/Client/CoreClient.h
+++ b/Swiften/Client/CoreClient.h
@@ -31,6 +31,7 @@ namespace Swift {
 	class TimerFactory;
 	class ClientSession;
 	class BasicSessionStream;
+	class EventLoop;
 
 	/** 
 	 * The central class for communicating with an XMPP server.
@@ -46,8 +47,9 @@ namespace Swift {
 		public: 
 			/**
 			 * Constructs a client for the given JID with the given password.
+			 * The given eventLoop will be used to post events to.
 			 */
-			CoreClient(const JID& jid, const String& password); 
+			CoreClient(EventLoop* eventLoop, const JID& jid, const String& password);
 			~CoreClient();
 
 			void setCertificate(const String& certificate);
@@ -180,6 +182,7 @@ namespace Swift {
 			PlatformDomainNameResolver resolver_;
 			JID jid_;
 			String password_;
+			EventLoop* eventLoop;
 			ClientSessionStanzaChannel* stanzaChannel_;
 			IQRouter* iqRouter_;
 			Connector::ref connector_;
diff --git a/Swiften/Component/Component.cpp b/Swiften/Component/Component.cpp
index af3802d..579bca9 100644
--- a/Swiften/Component/Component.cpp
+++ b/Swiften/Component/Component.cpp
@@ -10,7 +10,7 @@
 
 namespace Swift {
 
-Component::Component(const JID& jid, const String& secret) : CoreComponent(jid, secret) {
+Component::Component(EventLoop* eventLoop, const JID& jid, const String& secret) : CoreComponent(eventLoop, jid, secret) {
 	softwareVersionResponder = new SoftwareVersionResponder(getIQRouter());
 	softwareVersionResponder->start();
 }
diff --git a/Swiften/Component/Component.h b/Swiften/Component/Component.h
index a89deb3..b880725 100644
--- a/Swiften/Component/Component.h
+++ b/Swiften/Component/Component.h
@@ -19,7 +19,7 @@ namespace Swift {
 	 */
 	class Component : public CoreComponent {
 		public:
-			Component(const JID& jid, const String& secret);
+			Component(EventLoop* eventLoop, const JID& jid, const String& secret);
 			~Component();
 
 			/**
diff --git a/Swiften/Component/CoreComponent.cpp b/Swiften/Component/CoreComponent.cpp
index c9d9051..af6ebe5 100644
--- a/Swiften/Component/CoreComponent.cpp
+++ b/Swiften/Component/CoreComponent.cpp
@@ -23,7 +23,7 @@
 
 namespace Swift {
 
-CoreComponent::CoreComponent(const JID& jid, const String& secret) : jid_(jid), secret_(secret), disconnectRequested_(false) {
+CoreComponent::CoreComponent(EventLoop* eventLoop, const JID& jid, const String& secret) : eventLoop(eventLoop), resolver_(eventLoop), jid_(jid), secret_(secret), disconnectRequested_(false) {
 	stanzaChannel_ = new ComponentSessionStanzaChannel();
 	stanzaChannel_->onMessageReceived.connect(boost::ref(onMessageReceived));
 	stanzaChannel_->onPresenceReceived.connect(boost::ref(onPresenceReceived));
@@ -31,8 +31,8 @@ CoreComponent::CoreComponent(const JID& jid, const String& secret) : jid_(jid),
 
 	iqRouter_ = new IQRouter(stanzaChannel_);
 	iqRouter_->setFrom(jid);
-	connectionFactory_ = new BoostConnectionFactory(&MainBoostIOServiceThread::getInstance().getIOService());
-	timerFactory_ = new BoostTimerFactory(&MainBoostIOServiceThread::getInstance().getIOService());
+	connectionFactory_ = new BoostConnectionFactory(&MainBoostIOServiceThread::getInstance().getIOService(), eventLoop);
+	timerFactory_ = new BoostTimerFactory(&MainBoostIOServiceThread::getInstance().getIOService(), eventLoop);
 	tlsLayerFactory_ = new NullTLSLayerFactory();
 }
 
diff --git a/Swiften/Component/CoreComponent.h b/Swiften/Component/CoreComponent.h
index 67f87ee..75e6bda 100644
--- a/Swiften/Component/CoreComponent.h
+++ b/Swiften/Component/CoreComponent.h
@@ -43,7 +43,7 @@ namespace Swift {
 	 */
 	class CoreComponent  {
 		public:
-			CoreComponent(const JID& jid, const String& secret);
+			CoreComponent(EventLoop* eventLoop, const JID& jid, const String& secret);
 			~CoreComponent();
 
 			void connect(const String& host, int port);
@@ -88,6 +88,7 @@ namespace Swift {
 			void handleDataWritten(const String&);
 
 		private:
+			EventLoop* eventLoop;
 			PlatformDomainNameResolver resolver_;
 			JID jid_;
 			String secret_;
diff --git a/Swiften/Component/UnitTest/ComponentConnectorTest.cpp b/Swiften/Component/UnitTest/ComponentConnectorTest.cpp
index 4648365..7d00b09 100644
--- a/Swiften/Component/UnitTest/ComponentConnectorTest.cpp
+++ b/Swiften/Component/UnitTest/ComponentConnectorTest.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;
@@ -38,8 +37,8 @@ class ComponentConnectorTest : 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();
 		}
 
@@ -164,14 +163,14 @@ class ComponentConnectorTest : 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));
 					}
 				}
 
@@ -179,19 +178,21 @@ class ComponentConnectorTest : public CppUnit::TestFixture {
 				void write(const ByteArray&) { assert(false); }
 				HostAddressPort getLocalAddress() const { return HostAddressPort(); }
 
+				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;
 		};
diff --git a/Swiften/EventLoop/EventLoop.cpp b/Swiften/EventLoop/EventLoop.cpp
index 2e9e021..f787e73 100644
--- a/Swiften/EventLoop/EventLoop.cpp
+++ b/Swiften/EventLoop/EventLoop.cpp
@@ -10,16 +10,12 @@
 #include <boost/bind.hpp>
 #include <iostream>
 
-#include "Swiften/EventLoop/MainEventLoop.h"
-
 namespace Swift {
 
 EventLoop::EventLoop() : nextEventID_(0), handlingEvents_(false) {
-	MainEventLoop::setInstance(this);
 }
 
 EventLoop::~EventLoop() {
-	MainEventLoop::resetInstance();
 }
 
 void EventLoop::handleEvent(const Event& event) {
diff --git a/Swiften/EventLoop/MainEventLoop.cpp b/Swiften/EventLoop/MainEventLoop.cpp
deleted file mode 100644
index dae3261..0000000
--- a/Swiften/EventLoop/MainEventLoop.cpp
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (c) 2010 Remko Tronçon
- * Licensed under the GNU General Public License v3.
- * See Documentation/Licenses/GPLv3.txt for more information.
- */
-
-#include "Swiften/EventLoop/MainEventLoop.h"
-
-#include <iostream>
-#include <typeinfo>
-
-namespace Swift {
-
-EventLoop* MainEventLoop::getInstance() {
-	if (!instance_) {
-		std::cerr << "No main event loop instantiated. Please instantiate the appropriate subclass of EventLoop (e.g. SimpleEventLoop, QtEventLoop) at the start of your application." << std::endl;
-		exit(-1);
-	}
-	return instance_;
-}
-
-void MainEventLoop::setInstance(EventLoop* loop) {
-	assert(!instance_);
-	instance_ = loop;
-}
-
-void MainEventLoop::resetInstance() {
-	assert(instance_);
-	instance_ = 0;
-}
-
-void MainEventLoop::postEvent(boost::function<void ()> event, boost::shared_ptr<EventOwner> owner) {
-	getInstance()->postEvent(event, owner);
-}
-
-void MainEventLoop::removeEventsFromOwner(boost::shared_ptr<EventOwner> owner) {
-	getInstance()->removeEventsFromOwner(owner);
-}
-
-EventLoop* MainEventLoop::instance_ = 0;
-
-}
diff --git a/Swiften/EventLoop/MainEventLoop.h b/Swiften/EventLoop/MainEventLoop.h
deleted file mode 100644
index 5789db2..0000000
--- a/Swiften/EventLoop/MainEventLoop.h
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (c) 2010 Remko Tronçon
- * Licensed under the GNU General Public License v3.
- * See Documentation/Licenses/GPLv3.txt for more information.
- */
-
-#ifndef SWIFTEN_MainEventLoop_H
-#define SWIFTEN_MainEventLoop_H
-
-#include <boost/function.hpp>
-
-#include "Swiften/EventLoop/Deleter.h"
-#include "Swiften/EventLoop/EventLoop.h"
-
-namespace Swift {
-	class EventLoop;
-	class EventOwner;
-
-	class MainEventLoop {
-			friend class EventLoop;
-
-		public:
-			/**
-			 * Post an event from the given owner to the event loop.
-			 * If the owner is destroyed, all events should be removed from the
-			 * loop using removeEventsFromOwner().
-			 */
-			static void postEvent(boost::function<void ()> event, boost::shared_ptr<EventOwner> owner = boost::shared_ptr<EventOwner>());
-
-			static void removeEventsFromOwner(boost::shared_ptr<EventOwner> owner);
-
-			template<typename T>
-			static void deleteLater(T* t) {
-				getInstance()->postEvent(Deleter<T>(t), 0);
-			}
-
-		private:
-			static void setInstance(EventLoop*);
-			static void resetInstance();
-			static EventLoop* getInstance();
-		
-		private:
-			static EventLoop* instance_;
-	};
-}
-
-#endif
diff --git a/Swiften/EventLoop/SConscript b/Swiften/EventLoop/SConscript
index 248d451..a98971f 100644
--- a/Swiften/EventLoop/SConscript
+++ b/Swiften/EventLoop/SConscript
@@ -3,7 +3,6 @@ Import("swiften_env")
 sources = [
 		"EventLoop.cpp",
 		"EventOwner.cpp",
-		"MainEventLoop.cpp",
 		"SimpleEventLoop.cpp",
 	]
 
diff --git a/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp b/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp
index e796e66..e870d83 100644
--- a/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp
+++ b/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp
@@ -9,7 +9,7 @@
 
 #include "Swiften/Client/Client.h"
 #include "Swiften/Network/BoostTimer.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 #include "Swiften/Client/ClientXMLTracer.h"
 #include "Swiften/EventLoop/SimpleEventLoop.h"
 #include "Swiften/Network/BoostIOServiceThread.h"
@@ -67,7 +67,7 @@ int main(int argc, char* argv[]) {
 		connectHost = argv[argi++];
 	}
 
-	client = new Swift::Client(JID(jid), String(argv[argi++]));
+	client = new Swift::Client(&eventLoop, JID(jid), String(argv[argi++]));
 	char* timeoutChar = argv[argi++];
 	int timeout = atoi(timeoutChar);
 	timeout = (timeout ? timeout : 30) * 1000;
@@ -84,7 +84,7 @@ int main(int argc, char* argv[]) {
 	}
 
 	{
-		BoostTimer::ref timer(BoostTimer::create(timeout, &MainBoostIOServiceThread::getInstance().getIOService()));
+		BoostTimer::ref timer(BoostTimer::create(timeout, &MainBoostIOServiceThread::getInstance().getIOService(), &eventLoop));
 		timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop));
 		timer->start();
 
diff --git a/Swiften/Examples/LinkLocalTool/main.cpp b/Swiften/Examples/LinkLocalTool/main.cpp
index ccecd1a..65fc9bc 100644
--- a/Swiften/Examples/LinkLocalTool/main.cpp
+++ b/Swiften/Examples/LinkLocalTool/main.cpp
@@ -23,7 +23,7 @@ int main(int argc, char* argv[]) {
 	}
 
 	SimpleEventLoop eventLoop;
-	PlatformDNSSDQuerierFactory factory;
+	PlatformDNSSDQuerierFactory factory(&eventLoop);
 	boost::shared_ptr<DNSSDQuerier> querier = factory.createQuerier();
 	querier->start();
 
diff --git a/Swiften/Examples/SendFile/SendFile.cpp b/Swiften/Examples/SendFile/SendFile.cpp
index da0b2fe..6f72480 100644
--- a/Swiften/Examples/SendFile/SendFile.cpp
+++ b/Swiften/Examples/SendFile/SendFile.cpp
@@ -9,7 +9,7 @@
 
 #include "Swiften/Client/Client.h"
 #include "Swiften/Network/BoostTimer.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 #include "Swiften/Client/ClientXMLTracer.h"
 #include "Swiften/EventLoop/SimpleEventLoop.h"
 #include "Swiften/Network/MainBoostIOServiceThread.h"
@@ -27,10 +27,10 @@ int exitCode = 2;
 class FileSender {
 	public:
 		FileSender(const JID& jid, const String& password, const JID& recipient, const boost::filesystem::path& file, int port) : jid(jid), password(password), recipient(recipient), file(file), transfer(NULL) {
-			connectionServer = BoostConnectionServer::create(port, &MainBoostIOServiceThread::getInstance().getIOService());
+			connectionServer = BoostConnectionServer::create(port, &MainBoostIOServiceThread::getInstance().getIOService(), &eventLoop);
 			socksBytestreamServer = new SOCKS5BytestreamServer(connectionServer);
 
-			client = new Swift::Client(jid, password);
+			client = new Swift::Client(&eventLoop, jid, password);
 			client->onConnected.connect(boost::bind(&FileSender::handleConnected, this));
 			client->onError.connect(boost::bind(&FileSender::handleError, this, _1));
 			//tracer = new ClientXMLTracer(client);
diff --git a/Swiften/Examples/SendMessage/SendMessage.cpp b/Swiften/Examples/SendMessage/SendMessage.cpp
index 2ba5f3b..d9ed923 100644
--- a/Swiften/Examples/SendMessage/SendMessage.cpp
+++ b/Swiften/Examples/SendMessage/SendMessage.cpp
@@ -9,7 +9,7 @@
 
 #include "Swiften/Client/Client.h"
 #include "Swiften/Network/BoostTimer.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 #include "Swiften/Client/ClientXMLTracer.h"
 #include "Swiften/EventLoop/SimpleEventLoop.h"
 #include "Swiften/Network/BoostIOServiceThread.h"
@@ -57,7 +57,7 @@ int main(int argc, char* argv[]) {
 		connectHost = argv[argi++];
 	}
 
-	client = new Swift::Client(JID(jid), String(argv[argi++]));
+	client = new Swift::Client(&eventLoop, JID(jid), String(argv[argi++]));
 
 	recipient = JID(argv[argi++]);
 	messageBody = std::string(argv[argi++]);
@@ -72,7 +72,7 @@ int main(int argc, char* argv[]) {
 	}
 
 	{
-		BoostTimer::ref timer(BoostTimer::create(30000, &MainBoostIOServiceThread::getInstance().getIOService()));
+		BoostTimer::ref timer(BoostTimer::create(30000, &MainBoostIOServiceThread::getInstance().getIOService(), &eventLoop));
 		timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop));
 		timer->start();
 
diff --git a/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamServerSessionTest.cpp b/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamServerSessionTest.cpp
index 574b35b..b3b93ac 100644
--- a/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamServerSessionTest.cpp
+++ b/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamServerSessionTest.cpp
@@ -33,7 +33,7 @@ class SOCKS5BytestreamServerSessionTest : public CppUnit::TestFixture {
 		void setUp() {
 			receivedDataChunks = 0;
 			eventLoop = new DummyEventLoop();
-			connection = boost::shared_ptr<DummyConnection>(new DummyConnection());
+			connection = boost::shared_ptr<DummyConnection>(new DummyConnection(eventLoop));
 			connection->onDataSent.connect(boost::bind(&SOCKS5BytestreamServerSessionTest::handleDataWritten, this, _1));
 			stream1 = boost::shared_ptr<ByteArrayReadBytestream>(new ByteArrayReadBytestream(ByteArray("abcdefg")));
 		}
diff --git a/Swiften/LinkLocal/DNSSD/Avahi/AvahiBrowseQuery.h b/Swiften/LinkLocal/DNSSD/Avahi/AvahiBrowseQuery.h
index f8ec795..3bee907 100644
--- a/Swiften/LinkLocal/DNSSD/Avahi/AvahiBrowseQuery.h
+++ b/Swiften/LinkLocal/DNSSD/Avahi/AvahiBrowseQuery.h
@@ -10,7 +10,7 @@
 
 #include "Swiften/LinkLocal/DNSSD/Avahi/AvahiQuery.h"
 #include "Swiften/LinkLocal/DNSSD/DNSSDBrowseQuery.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 
 namespace Swift {
 	class AvahiQuerier;
@@ -27,7 +27,7 @@ namespace Swift {
 				browser = avahi_service_browser_new(querier->getClient(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_presence._tcp", NULL, static_cast<AvahiLookupFlags>(0), &handleServiceDiscoveredStatic, this);
 				if (!browser) {
 					std::cout << "Error" << std::endl;
-					MainEventLoop::postEvent(boost::bind(boost::ref(onError)), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onError)), shared_from_this());
 				}
 				avahi_threaded_poll_unlock(querier->getThreadedPoll());
 			}
@@ -49,18 +49,18 @@ namespace Swift {
 				switch (event) {
 					case AVAHI_BROWSER_FAILURE:	
 						std::cout << "Service browse error" << std::endl;
-						MainEventLoop::postEvent(boost::bind(boost::ref(onError)), shared_from_this());
+						eventLoop->postEvent(boost::bind(boost::ref(onError)), shared_from_this());
 						break;
 					case AVAHI_BROWSER_NEW: {
 						DNSSDServiceID service(name, domain, type, interfaceIndex);
 						std::cout << "Service discovered " << name << " " << domain << " " << type << " " << interfaceIndex << std::endl;
-						MainEventLoop::postEvent(boost::bind(boost::ref(onServiceAdded), service), shared_from_this());
+						eventLoop->postEvent(boost::bind(boost::ref(onServiceAdded), service), shared_from_this());
 						break;
 					}
 					case AVAHI_BROWSER_REMOVE: {
 						std::cout << "Service went away " << name << " " << domain << " " << type << " " << interfaceIndex << std::endl;
 						DNSSDServiceID service(name, domain, type, interfaceIndex);
-						MainEventLoop::postEvent(boost::bind(boost::ref(onServiceRemoved), service), shared_from_this());
+						eventLoop->postEvent(boost::bind(boost::ref(onServiceRemoved), service), shared_from_this());
 						break;
 					}
 					case AVAHI_BROWSER_ALL_FOR_NOW:
diff --git a/Swiften/LinkLocal/DNSSD/Avahi/AvahiRegisterQuery.h b/Swiften/LinkLocal/DNSSD/Avahi/AvahiRegisterQuery.h
index 2045328..3e5a758 100644
--- a/Swiften/LinkLocal/DNSSD/Avahi/AvahiRegisterQuery.h
+++ b/Swiften/LinkLocal/DNSSD/Avahi/AvahiRegisterQuery.h
@@ -11,7 +11,7 @@
 #include "Swiften/LinkLocal/DNSSD/Avahi/AvahiQuery.h"
 #include "Swiften/LinkLocal/DNSSD/DNSSDRegisterQuery.h"
 #include "Swiften/Base/ByteArray.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 
 namespace Swift {
 	class AvahiQuerier;
@@ -29,7 +29,7 @@ namespace Swift {
 					group = avahi_entry_group_new(querier->getClient(), handleEntryGroupChange, this);
 					if (!group) {
 						std::cout << "Error ceating entry group" << std::endl;
-						MainEventLoop::postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
+						eventLoop->postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
 					}
 				}
 
@@ -61,7 +61,7 @@ namespace Swift {
 				int result = avahi_entry_group_add_service_strlst(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, static_cast<AvahiPublishFlags>(0), name.getUTF8Data(), "_presence._tcp", NULL, NULL, port, txtList);
 				if (result < 0) {
 					std::cout << "Error registering service: " << avahi_strerror(result) << std::endl;
-					MainEventLoop::postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
 				}
 				result = avahi_entry_group_commit(group);
 				if (result < 0) {
@@ -78,7 +78,7 @@ namespace Swift {
 				switch (state) {
 					case AVAHI_ENTRY_GROUP_ESTABLISHED :
 						// Domain is a hack!
-						MainEventLoop::postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>(DNSSDServiceID(name, "local", "_presence._tcp", 0))), shared_from_this());
+						eventLoop->postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>(DNSSDServiceID(name, "local", "_presence._tcp", 0))), shared_from_this());
 						std::cout << "Entry group established" << std::endl;
 						break;
 				case AVAHI_ENTRY_GROUP_COLLISION : {
@@ -106,7 +106,7 @@ namespace Swift {
 				if (result != kDNSServiceErr_NoError) {
 					sdRef = NULL;
 				}*/
-				//MainEventLoop::postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
+				//eventLoop->postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
 			}
 		}
 
@@ -117,7 +117,7 @@ namespace Swift {
 
 			void handleServiceRegistered(DNSServiceErrorType errorCode, const char *name, const char *regtype, const char *domain) {
 				if (errorCode != kDNSServiceErr_NoError) {
-					MainEventLoop::postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
 				}
 				else {
 				}
diff --git a/Swiften/LinkLocal/DNSSD/Avahi/AvahiResolveHostnameQuery.h b/Swiften/LinkLocal/DNSSD/Avahi/AvahiResolveHostnameQuery.h
index e069629..6803fd0 100644
--- a/Swiften/LinkLocal/DNSSD/Avahi/AvahiResolveHostnameQuery.h
+++ b/Swiften/LinkLocal/DNSSD/Avahi/AvahiResolveHostnameQuery.h
@@ -9,7 +9,7 @@
 #include "Swiften/Base/String.h"
 #include "Swiften/LinkLocal/DNSSD/Avahi/AvahiQuery.h"
 #include "Swiften/LinkLocal/DNSSD/DNSSDResolveHostnameQuery.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 #include "Swiften/Network/HostAddress.h"
 
 #include <netinet/in.h>
@@ -24,7 +24,7 @@ namespace Swift {
 			}
 
 			void run() {
-					MainEventLoop::postEvent(boost::bind(boost::ref(onHostnameResolved), boost::optional<HostAddress>(HostAddress(hostname))), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onHostnameResolved), boost::optional<HostAddress>(HostAddress(hostname))), shared_from_this());
 			}
 
 			void finish() {
diff --git a/Swiften/LinkLocal/DNSSD/Avahi/AvahiResolveServiceQuery.h b/Swiften/LinkLocal/DNSSD/Avahi/AvahiResolveServiceQuery.h
index 4709677..402f84f 100644
--- a/Swiften/LinkLocal/DNSSD/Avahi/AvahiResolveServiceQuery.h
+++ b/Swiften/LinkLocal/DNSSD/Avahi/AvahiResolveServiceQuery.h
@@ -10,7 +10,7 @@
 #include "Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h"
 #include "Swiften/LinkLocal/LinkLocalServiceInfo.h"
 #include "Swiften/Base/ByteArray.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 
 namespace Swift {
 	class AvahiQuerier;
@@ -27,7 +27,7 @@ namespace Swift {
 				resolver = avahi_service_resolver_new(querier->getClient(), service.getNetworkInterfaceID(), AVAHI_PROTO_UNSPEC, service.getName().getUTF8Data(), service.getType().getUTF8Data(), service.getDomain().getUTF8Data(), AVAHI_PROTO_UNSPEC, static_cast<AvahiLookupFlags>(0), handleServiceResolvedStatic, this);
 				if (!resolver) {
 					std::cout << "Error starting resolver" << std::endl;
-					MainEventLoop::postEvent(boost::bind(boost::ref(onServiceResolved), boost::optional<Result>()), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onServiceResolved), boost::optional<Result>()), shared_from_this());
 				}
 				avahi_threaded_poll_unlock(querier->getThreadedPoll());
 			}
@@ -50,7 +50,7 @@ namespace Swift {
 				switch(event) {
 					case AVAHI_RESOLVER_FAILURE:
 						std::cout << "Resolve error " << avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(resolver))) << std::endl;
-						MainEventLoop::postEvent(boost::bind(boost::ref(onServiceResolved), boost::optional<Result>()), shared_from_this());
+						eventLoop->postEvent(boost::bind(boost::ref(onServiceResolved), boost::optional<Result>()), shared_from_this());
 						break;
 					case AVAHI_RESOLVER_FOUND: {
 						std::cout << "Success" << std::endl;
@@ -64,7 +64,7 @@ namespace Swift {
 						// FIXME: Probably not accurate
 						String fullname = String(name) + "." + String(type) + "." + String(domain) + ".";
 						std::cout << "Result: " << fullname << "->" << String(a) << ":" << port << std::endl;
-						MainEventLoop::postEvent(
+						eventLoop->postEvent(
 								boost::bind(
 									boost::ref(onServiceResolved), 
 									Result(fullname, String(a), port, txtRecord)),
diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourBrowseQuery.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourBrowseQuery.h
index bfecea9..52c47d7 100644
--- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourBrowseQuery.h
+++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourBrowseQuery.h
@@ -8,14 +8,14 @@
 
 #include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h"
 #include "Swiften/LinkLocal/DNSSD/DNSSDBrowseQuery.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 
 namespace Swift {
 	class BonjourQuerier;
 
 	class BonjourBrowseQuery : public DNSSDBrowseQuery, public BonjourQuery {
 		public:	
-			BonjourBrowseQuery(boost::shared_ptr<BonjourQuerier> q) : BonjourQuery(q) {
+			BonjourBrowseQuery(boost::shared_ptr<BonjourQuerier> q, EventLoop* eventLoop) : BonjourQuery(q, eventLoop) {
 				DNSServiceErrorType result = DNSServiceBrowse(
 						&sdRef, 0, 0, "_presence._tcp", 0, 
 						&BonjourBrowseQuery::handleServiceDiscoveredStatic, this);
@@ -26,7 +26,7 @@ namespace Swift {
 
 			void startBrowsing() {
 				if (!sdRef) {
-					MainEventLoop::postEvent(boost::bind(boost::ref(onError)), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onError)), shared_from_this());
 				}
 				else {
 					run();
@@ -44,16 +44,16 @@ namespace Swift {
 
 			void handleServiceDiscovered(DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *name, const char *type, const char *domain) {
 				if (errorCode != kDNSServiceErr_NoError) {
-					MainEventLoop::postEvent(boost::bind(boost::ref(onError)), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onError)), shared_from_this());
 				}
 				else {
 					//std::cout << "Discovered service: name:" << name << " domain:" << domain << " type: " << type << std::endl;
 					DNSSDServiceID service(name, domain, type, interfaceIndex);
 					if (flags & kDNSServiceFlagsAdd) {
-						MainEventLoop::postEvent(boost::bind(boost::ref(onServiceAdded), service), shared_from_this());
+						eventLoop->postEvent(boost::bind(boost::ref(onServiceAdded), service), shared_from_this());
 					}
 					else {
-						MainEventLoop::postEvent(boost::bind(boost::ref(onServiceRemoved), service), shared_from_this());
+						eventLoop->postEvent(boost::bind(boost::ref(onServiceRemoved), service), shared_from_this());
 					}
 				}
 			}
diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp
index 55c64c4..03271d6 100644
--- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp
+++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp
@@ -18,7 +18,7 @@
 
 namespace Swift {
 
-BonjourQuerier::BonjourQuerier() : stopRequested(false), thread(0) {
+BonjourQuerier::BonjourQuerier(EventLoop* eventLoop) : eventLoop(eventLoop), stopRequested(false), thread(0) {
 	int fds[2];
 	int result = pipe(fds);
 	assert(result == 0);
@@ -32,19 +32,19 @@ BonjourQuerier::~BonjourQuerier() {
 }
 
 boost::shared_ptr<DNSSDBrowseQuery> BonjourQuerier::createBrowseQuery() {
-	return boost::shared_ptr<DNSSDBrowseQuery>(new BonjourBrowseQuery(shared_from_this()));
+	return boost::shared_ptr<DNSSDBrowseQuery>(new BonjourBrowseQuery(shared_from_this(), eventLoop));
 }
 
 boost::shared_ptr<DNSSDRegisterQuery> BonjourQuerier::createRegisterQuery(const String& name, int port, const ByteArray& info) {
-	return boost::shared_ptr<DNSSDRegisterQuery>(new BonjourRegisterQuery(name, port, info, shared_from_this()));
+	return boost::shared_ptr<DNSSDRegisterQuery>(new BonjourRegisterQuery(name, port, info, shared_from_this(), eventLoop));
 }
 
 boost::shared_ptr<DNSSDResolveServiceQuery> BonjourQuerier::createResolveServiceQuery(const DNSSDServiceID& service) {
-	return boost::shared_ptr<DNSSDResolveServiceQuery>(new BonjourResolveServiceQuery(service, shared_from_this()));
+	return boost::shared_ptr<DNSSDResolveServiceQuery>(new BonjourResolveServiceQuery(service, shared_from_this(), eventLoop));
 }
 
 boost::shared_ptr<DNSSDResolveHostnameQuery> BonjourQuerier::createResolveHostnameQuery(const String& hostname, int interfaceIndex) {
-	return boost::shared_ptr<DNSSDResolveHostnameQuery>(new BonjourResolveHostnameQuery(hostname, interfaceIndex, shared_from_this()));
+	return boost::shared_ptr<DNSSDResolveHostnameQuery>(new BonjourResolveHostnameQuery(hostname, interfaceIndex, shared_from_this(), eventLoop));
 }
 
 void BonjourQuerier::addRunningQuery(boost::shared_ptr<BonjourQuery> query) {
diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h
index a6c7d4d..916acc3 100644
--- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h
+++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h
@@ -22,7 +22,7 @@ namespace Swift {
 			public DNSSDQuerier, 
 			public boost::enable_shared_from_this<BonjourQuerier> {
 		public:
-			BonjourQuerier();
+			BonjourQuerier(EventLoop* eventLoop);
 			~BonjourQuerier();
 
 			boost::shared_ptr<DNSSDBrowseQuery> createBrowseQuery();
@@ -45,6 +45,7 @@ namespace Swift {
 			void run();
 
 		private:
+			EventLoop* eventLoop;
 			bool stopRequested;
 			boost::thread* thread;
 			boost::mutex runningQueriesMutex;
diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.cpp b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.cpp
index c496072..d7ff5d5 100644
--- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.cpp
+++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.cpp
@@ -9,7 +9,7 @@
 
 namespace Swift {
 
-BonjourQuery::BonjourQuery(boost::shared_ptr<BonjourQuerier> q) : querier(q), sdRef(0) {
+BonjourQuery::BonjourQuery(boost::shared_ptr<BonjourQuerier> q, EventLoop* eventLoop) : eventLoop(eventLoop), querier(q), sdRef(0) {
 }
 
 BonjourQuery::~BonjourQuery() {
diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h
index e13883e..e3cd9a5 100644
--- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h
+++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h
@@ -15,12 +15,13 @@
 
 namespace Swift {
 	class BonjourQuerier;
+	class EventLoop;
 
 	class BonjourQuery : 
 			public EventOwner,
 			public boost::enable_shared_from_this<BonjourQuery> {
 		public:
-			BonjourQuery(boost::shared_ptr<BonjourQuerier>);
+			BonjourQuery(boost::shared_ptr<BonjourQuerier>, EventLoop* eventLoop);
 			virtual ~BonjourQuery();
 			
 			void processResult();
@@ -31,6 +32,7 @@ namespace Swift {
 			void finish();
 		
 		protected:
+			EventLoop* eventLoop;
 			boost::shared_ptr<BonjourQuerier> querier;
 			mutable boost::mutex sdRefMutex;
 			DNSServiceRef sdRef;
diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h
index 8155a33..9d8516b 100644
--- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h
+++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h
@@ -9,14 +9,14 @@
 #include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h"
 #include "Swiften/LinkLocal/DNSSD/DNSSDRegisterQuery.h"
 #include "Swiften/Base/ByteArray.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 
 namespace Swift {
 	class BonjourQuerier;
 
 	class BonjourRegisterQuery : public DNSSDRegisterQuery, public BonjourQuery {
 		public:	
-			BonjourRegisterQuery(const String& name, int port, const ByteArray& txtRecord, boost::shared_ptr<BonjourQuerier> querier) : BonjourQuery(querier) {
+			BonjourRegisterQuery(const String& name, int port, const ByteArray& txtRecord, boost::shared_ptr<BonjourQuerier> querier, EventLoop* eventLoop) : BonjourQuery(querier, eventLoop) {
 				DNSServiceErrorType result = DNSServiceRegister(
 						&sdRef, 0, 0, name.getUTF8Data(), "_presence._tcp", NULL, NULL, port, 
 						txtRecord.getSize(), txtRecord.getData(), 
@@ -31,7 +31,7 @@ namespace Swift {
 					run();
 				}
 				else {
-					MainEventLoop::postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
 				}
 			}
 
@@ -51,10 +51,10 @@ namespace Swift {
 
 			void handleServiceRegistered(DNSServiceErrorType errorCode, const char *name, const char *regtype, const char *domain) {
 				if (errorCode != kDNSServiceErr_NoError) {
-					MainEventLoop::postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
 				}
 				else {
-					MainEventLoop::postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>(DNSSDServiceID(name, domain, regtype, 0))), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>(DNSSDServiceID(name, domain, regtype, 0))), shared_from_this());
 				}
 			}
 	};
diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveHostnameQuery.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveHostnameQuery.h
index de52321..16e9be6 100644
--- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveHostnameQuery.h
+++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveHostnameQuery.h
@@ -11,7 +11,7 @@
 #include "Swiften/Base/String.h"
 #include "Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h"
 #include "Swiften/LinkLocal/DNSSD/DNSSDResolveHostnameQuery.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 #include "Swiften/Network/HostAddress.h"
 
 #include <netinet/in.h>
@@ -21,7 +21,7 @@ namespace Swift {
 
 	class BonjourResolveHostnameQuery : public DNSSDResolveHostnameQuery, public BonjourQuery {
 		public: 
-			BonjourResolveHostnameQuery(const String& hostname, int interfaceIndex, boost::shared_ptr<BonjourQuerier> querier) : BonjourQuery(querier) {
+			BonjourResolveHostnameQuery(const String& hostname, int interfaceIndex, boost::shared_ptr<BonjourQuerier> querier, EventLoop* eventLoop) : BonjourQuery(querier, eventLoop) {
 				DNSServiceErrorType result = DNSServiceGetAddrInfo(
 						&sdRef, 0, interfaceIndex, kDNSServiceProtocol_IPv4, 
 						hostname.getUTF8Data(), 
@@ -37,7 +37,7 @@ namespace Swift {
 					BonjourQuery::run();
 				}
 				else {
-					MainEventLoop::postEvent(boost::bind(boost::ref(onHostnameResolved), boost::optional<HostAddress>()), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onHostnameResolved), boost::optional<HostAddress>()), shared_from_this());
 				}
 			}
 
@@ -52,7 +52,7 @@ namespace Swift {
 
 			void handleHostnameResolved(DNSServiceErrorType errorCode, const struct sockaddr *rawAddress) {
 				if (errorCode) {
-					MainEventLoop::postEvent(
+					eventLoop->postEvent(
 								boost::bind(boost::ref(onHostnameResolved), 
 								boost::optional<HostAddress>()), 
 							shared_from_this());
@@ -61,7 +61,7 @@ namespace Swift {
 					assert(rawAddress->sa_family == AF_INET);
 					const sockaddr_in* sa = reinterpret_cast<const sockaddr_in*>(rawAddress);
 					uint32_t address = ntohl(sa->sin_addr.s_addr);
-					MainEventLoop::postEvent(boost::bind(
+					eventLoop->postEvent(boost::bind(
 								boost::ref(onHostnameResolved), 
 								HostAddress(reinterpret_cast<unsigned char*>(&address), 4)), 
 							shared_from_this());
diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveServiceQuery.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveServiceQuery.h
index 38121ca..136b366 100644
--- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveServiceQuery.h
+++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveServiceQuery.h
@@ -10,14 +10,14 @@
 #include "Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h"
 #include "Swiften/LinkLocal/LinkLocalServiceInfo.h"
 #include "Swiften/Base/ByteArray.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 
 namespace Swift {
 	class BonjourQuerier;
 
 	class BonjourResolveServiceQuery : public DNSSDResolveServiceQuery, public BonjourQuery {
 		public:	
-			BonjourResolveServiceQuery(const DNSSDServiceID& service, boost::shared_ptr<BonjourQuerier> querier) : BonjourQuery(querier) {
+			BonjourResolveServiceQuery(const DNSSDServiceID& service, boost::shared_ptr<BonjourQuerier> querier, EventLoop* eventLoop) : BonjourQuery(querier, eventLoop) {
 				DNSServiceErrorType result = DNSServiceResolve(
 						&sdRef, 0, service.getNetworkInterfaceID(), 
 						service.getName().getUTF8Data(), service.getType().getUTF8Data(), 
@@ -33,7 +33,7 @@ namespace Swift {
 					run();
 				}
 				else {
-					MainEventLoop::postEvent(boost::bind(boost::ref(onServiceResolved), boost::optional<Result>()), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onServiceResolved), boost::optional<Result>()), shared_from_this());
 				}
 			}
 
@@ -48,11 +48,11 @@ namespace Swift {
 
 			void handleServiceResolved(DNSServiceErrorType errorCode, const char* fullName, const char* host, uint16_t port, uint16_t txtLen, const unsigned char *txtRecord) {
 				if (errorCode != kDNSServiceErr_NoError) {
-					MainEventLoop::postEvent(boost::bind(boost::ref(onServiceResolved), boost::optional<Result>()), shared_from_this());
+					eventLoop->postEvent(boost::bind(boost::ref(onServiceResolved), boost::optional<Result>()), shared_from_this());
 				}
 				else {
 					//std::cout << "Service resolved: name:" << fullName << " host:" << host << " port:" << port << std::endl;
-					MainEventLoop::postEvent(
+					eventLoop->postEvent(
 							boost::bind(
 								boost::ref(onServiceResolved), 
 								Result(String(fullName), String(host), port, 
diff --git a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp
index d8e7acf..0bcdba1 100644
--- a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp
+++ b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp
@@ -12,11 +12,11 @@
 #include "Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDRegisterQuery.h"
 #include "Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDResolveServiceQuery.h"
 #include "Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDResolveHostnameQuery.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 
 namespace Swift {
 
-FakeDNSSDQuerier::FakeDNSSDQuerier(const String& domain) : domain(domain) {
+FakeDNSSDQuerier::FakeDNSSDQuerier(const String& domain, EventLoop* eventLoop) : domain(domain), eventLoop(eventLoop) {
 }
 
 FakeDNSSDQuerier::~FakeDNSSDQuerier() {
@@ -46,24 +46,24 @@ void FakeDNSSDQuerier::addRunningQuery(boost::shared_ptr<FakeDNSSDQuery> query)
 	allQueriesEverRun.push_back(query);
 	if (boost::shared_ptr<FakeDNSSDBrowseQuery> browseQuery = boost::dynamic_pointer_cast<FakeDNSSDBrowseQuery>(query)) {
 		foreach(const DNSSDServiceID& service, services) {
-			MainEventLoop::postEvent(boost::bind(boost::ref(browseQuery->onServiceAdded), service), shared_from_this());
+			eventLoop->postEvent(boost::bind(boost::ref(browseQuery->onServiceAdded), service), shared_from_this());
 		}
 	}
 	else if (boost::shared_ptr<FakeDNSSDResolveServiceQuery> resolveQuery = boost::dynamic_pointer_cast<FakeDNSSDResolveServiceQuery>(query)) {
 		for(ServiceInfoMap::const_iterator i = serviceInfo.begin(); i != serviceInfo.end(); ++i) {
 			if (i->first == resolveQuery->service) {
-				MainEventLoop::postEvent(boost::bind(boost::ref(resolveQuery->onServiceResolved), i->second), shared_from_this());
+				eventLoop->postEvent(boost::bind(boost::ref(resolveQuery->onServiceResolved), i->second), shared_from_this());
 			}
 		}
 	}
 	else if (boost::shared_ptr<FakeDNSSDRegisterQuery> registerQuery = boost::dynamic_pointer_cast<FakeDNSSDRegisterQuery>(query)) {
 		DNSSDServiceID service(registerQuery->name, domain);
-		MainEventLoop::postEvent(boost::bind(boost::ref(registerQuery->onRegisterFinished), service), shared_from_this());
+		eventLoop->postEvent(boost::bind(boost::ref(registerQuery->onRegisterFinished), service), shared_from_this());
 	}
 	else if (boost::shared_ptr<FakeDNSSDResolveHostnameQuery> resolveHostnameQuery = boost::dynamic_pointer_cast<FakeDNSSDResolveHostnameQuery>(query)) {
 		std::map<String,boost::optional<HostAddress> >::const_iterator i = addresses.find(resolveHostnameQuery->hostname);
 		if (i != addresses.end()) {
-			MainEventLoop::postEvent(
+			eventLoop->postEvent(
 					boost::bind(
 						boost::ref(resolveHostnameQuery->onHostnameResolved), i->second), 
 					shared_from_this());
@@ -79,7 +79,7 @@ void FakeDNSSDQuerier::removeRunningQuery(boost::shared_ptr<FakeDNSSDQuery> quer
 void FakeDNSSDQuerier::addService(const DNSSDServiceID& id) {
 	services.insert(id);
 	foreach(const boost::shared_ptr<FakeDNSSDBrowseQuery>& query, getQueries<FakeDNSSDBrowseQuery>()) {
-		MainEventLoop::postEvent(boost::bind(boost::ref(query->onServiceAdded), id), shared_from_this());
+		eventLoop->postEvent(boost::bind(boost::ref(query->onServiceAdded), id), shared_from_this());
 	}
 }
 
@@ -87,7 +87,7 @@ void FakeDNSSDQuerier::removeService(const DNSSDServiceID& id) {
 	services.erase(id);
 	serviceInfo.erase(id);
 	foreach(const boost::shared_ptr<FakeDNSSDBrowseQuery>& query, getQueries<FakeDNSSDBrowseQuery>()) {
-		MainEventLoop::postEvent(boost::bind(boost::ref(query->onServiceRemoved), id), shared_from_this());
+		eventLoop->postEvent(boost::bind(boost::ref(query->onServiceRemoved), id), shared_from_this());
 	}
 }
 
@@ -98,7 +98,7 @@ void FakeDNSSDQuerier::setServiceInfo(const DNSSDServiceID& id, const DNSSDResol
 	}
 	foreach(const boost::shared_ptr<FakeDNSSDResolveServiceQuery>& query, getQueries<FakeDNSSDResolveServiceQuery>()) {
 		if (query->service == id) {
-			MainEventLoop::postEvent(boost::bind(boost::ref(query->onServiceResolved), info), shared_from_this());
+			eventLoop->postEvent(boost::bind(boost::ref(query->onServiceResolved), info), shared_from_this());
 		}
 	}
 }
@@ -114,13 +114,13 @@ bool FakeDNSSDQuerier::isServiceRegistered(const String& name, int port, const B
 
 void FakeDNSSDQuerier::setBrowseError() {
 	foreach(const boost::shared_ptr<FakeDNSSDBrowseQuery>& query, getQueries<FakeDNSSDBrowseQuery>()) {
-		MainEventLoop::postEvent(boost::ref(query->onError), shared_from_this());
+		eventLoop->postEvent(boost::ref(query->onError), shared_from_this());
 	}
 }
 
 void FakeDNSSDQuerier::setRegisterError() {
 	foreach(const boost::shared_ptr<FakeDNSSDRegisterQuery>& query, getQueries<FakeDNSSDRegisterQuery>()) {
-		MainEventLoop::postEvent(boost::bind(boost::ref(query->onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
+		eventLoop->postEvent(boost::bind(boost::ref(query->onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this());
 	}
 }
 
@@ -128,7 +128,7 @@ void FakeDNSSDQuerier::setAddress(const String& hostname, boost::optional<HostAd
 	addresses[hostname] = address;
 	foreach(const boost::shared_ptr<FakeDNSSDResolveHostnameQuery>& query, getQueries<FakeDNSSDResolveHostnameQuery>()) {
 		if (query->hostname == hostname) {
-			MainEventLoop::postEvent(boost::bind(
+			eventLoop->postEvent(boost::bind(
 					boost::ref(query->onHostnameResolved), address), shared_from_this());
 		}
 	}
diff --git a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h
index f08ab68..9338dd4 100644
--- a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h
+++ b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h
@@ -22,13 +22,14 @@ namespace Swift {
 	class ByteArray;
 	class FakeDNSSDQuery;
 	class FakeDNSSDBrowseQuery;
+	class EventLoop;
 
 	class FakeDNSSDQuerier : 
 			public DNSSDQuerier, 
 			public EventOwner,
 			public boost::enable_shared_from_this<FakeDNSSDQuerier> {
 		public:
-			FakeDNSSDQuerier(const String& domain);
+			FakeDNSSDQuerier(const String& domain, EventLoop* eventLoop);
 			~FakeDNSSDQuerier();
 
 			void start() {}
@@ -84,6 +85,7 @@ namespace Swift {
 
 		private:
 			String domain;
+			EventLoop* eventLoop;
 			std::list< boost::shared_ptr<FakeDNSSDQuery> > runningQueries;
 			std::list< boost::shared_ptr<FakeDNSSDQuery> > allQueriesEverRun;
 			std::set<DNSSDServiceID> services;
diff --git a/Swiften/LinkLocal/DNSSD/PlatformDNSSDQuerierFactory.cpp b/Swiften/LinkLocal/DNSSD/PlatformDNSSDQuerierFactory.cpp
index 56128f0..7a42129 100644
--- a/Swiften/LinkLocal/DNSSD/PlatformDNSSDQuerierFactory.cpp
+++ b/Swiften/LinkLocal/DNSSD/PlatformDNSSDQuerierFactory.cpp
@@ -15,11 +15,14 @@
 
 namespace Swift {
 
+PlatformDNSSDQuerierFactory::PlatformDNSSDQuerierFactory(EventLoop* eventLoop) : eventLoop(eventLoop) {
+}
+
 boost::shared_ptr<DNSSDQuerier> PlatformDNSSDQuerierFactory::createQuerier() {
 #if defined(HAVE_BONJOUR)
-	return boost::shared_ptr<DNSSDQuerier>(new BonjourQuerier());
+	return boost::shared_ptr<DNSSDQuerier>(new BonjourQuerier(eventLoop));
 #elif defined(HAVE_AVAHI)
-	return boost::shared_ptr<DNSSDQuerier>(new AvahiQuerier());
+	return boost::shared_ptr<DNSSDQuerier>(new AvahiQuerier(eventLoop));
 #else
 	return boost::shared_ptr<DNSSDQuerier>();
 #endif
diff --git a/Swiften/LinkLocal/DNSSD/PlatformDNSSDQuerierFactory.h b/Swiften/LinkLocal/DNSSD/PlatformDNSSDQuerierFactory.h
index 674c13a..ca5570f 100644
--- a/Swiften/LinkLocal/DNSSD/PlatformDNSSDQuerierFactory.h
+++ b/Swiften/LinkLocal/DNSSD/PlatformDNSSDQuerierFactory.h
@@ -10,11 +10,17 @@
 
 namespace Swift {
 	class DNSSDQuerier;
+	class EventLoop;
 
 	class PlatformDNSSDQuerierFactory {
 		public:
-			 boost::shared_ptr<DNSSDQuerier> createQuerier();
+			PlatformDNSSDQuerierFactory(EventLoop* eventLoop);
 
-			 bool canCreate();
+			boost::shared_ptr<DNSSDQuerier> createQuerier();
+
+			bool canCreate();
+
+		private:
+			EventLoop* eventLoop;
 	};
 }
diff --git a/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp b/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp
index f6fc131..65b8a67 100644
--- a/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp
+++ b/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp
@@ -30,8 +30,8 @@ class LinkLocalConnectorTest : public CppUnit::TestFixture {
 		void setUp() {
 			eventLoop = new DummyEventLoop();
 			querier = boost::shared_ptr<FakeDNSSDQuerier>(
-					new FakeDNSSDQuerier("rabbithole.local"));
-			connection = boost::shared_ptr<FakeConnection>(new FakeConnection());
+					new FakeDNSSDQuerier("rabbithole.local", eventLoop));
+			connection = boost::shared_ptr<FakeConnection>(new FakeConnection(eventLoop));
 			connectFinished = false;
 		}
 
diff --git a/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp b/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
index f4b0034..d55603c 100644
--- a/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
+++ b/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
@@ -44,7 +44,7 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 
 		void setUp() {
 			eventLoop = new DummyEventLoop();
-			querier = boost::shared_ptr<FakeDNSSDQuerier>(new FakeDNSSDQuerier("wonderland.lit"));
+			querier = boost::shared_ptr<FakeDNSSDQuerier>(new FakeDNSSDQuerier("wonderland.lit", eventLoop));
 			aliceServiceID = new DNSSDServiceID("alice", "wonderland.lit");
 			aliceServiceInfo = new DNSSDResolveServiceQuery::Result("_presence._tcp.wonderland.lit", "xmpp.wonderland.lit", 1234, LinkLocalServiceInfo().toTXTRecord());
 			testServiceID = new DNSSDServiceID("foo", "bar.local");
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;
 		};
diff --git a/Swiften/QA/ClientTest/ClientTest.cpp b/Swiften/QA/ClientTest/ClientTest.cpp
index f504d2c..4e48339 100644
--- a/Swiften/QA/ClientTest/ClientTest.cpp
+++ b/Swiften/QA/ClientTest/ClientTest.cpp
@@ -9,7 +9,7 @@
 
 #include "Swiften/Client/Client.h"
 #include "Swiften/Network/BoostTimer.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 #include "Swiften/EventLoop/SimpleEventLoop.h"
 #include "Swiften/Roster/GetRosterRequest.h"
 #include "Swiften/Client/ClientXMLTracer.h"
@@ -55,13 +55,13 @@ int main(int, char**) {
 		return -1;
 	}
 
-	client = new Swift::Client(JID(jid), String(pass));
+	client = new Swift::Client(&eventLoop, JID(jid), String(pass));
 	ClientXMLTracer* tracer = new ClientXMLTracer(client);
 	client->onConnected.connect(&handleConnected);
 	client->connect();
 
 	{
-		boost::shared_ptr<BoostTimer> timer(BoostTimer::create(30000, &MainBoostIOServiceThread::getInstance().getIOService()));
+		boost::shared_ptr<BoostTimer> timer(BoostTimer::create(30000, &MainBoostIOServiceThread::getInstance().getIOService(), &eventLoop));
 		timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop));
 		timer->start();
 
diff --git a/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp b/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp
index 543e085..4c38576 100644
--- a/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp
+++ b/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp
@@ -39,15 +39,15 @@ class BoostConnectionServerTest : public CppUnit::TestFixture {
 		}
 
 		void testConstructor_TwoServersOnSamePort() {
-			BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService()));
-			BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService()));
+			BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService(), eventLoop_));
+			BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService(), eventLoop_));
 		}
 
 		void testStart_Conflict() {
-			BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService()));
+			BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService(), eventLoop_));
 			testling->start();
 
-			BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService()));
+			BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService(), eventLoop_));
 			testling2->onStopped.connect(
 					boost::bind(&BoostConnectionServerTest::handleStopped, this, _1));
 
@@ -55,12 +55,12 @@ class BoostConnectionServerTest : public CppUnit::TestFixture {
 		}
 
 		void testStop() {
-			BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService()));
+			BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService(), eventLoop_));
 			testling->start();
 
 			testling->stop();
 
-			BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService()));
+			BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, &boostIOServiceThread_->getIOService(), eventLoop_));
 			testling2->start();
 
 			testling2->stop();
diff --git a/Swiften/QA/NetworkTest/BoostConnectionTest.cpp b/Swiften/QA/NetworkTest/BoostConnectionTest.cpp
index 913f7c0..6d6fddf 100644
--- a/Swiften/QA/NetworkTest/BoostConnectionTest.cpp
+++ b/Swiften/QA/NetworkTest/BoostConnectionTest.cpp
@@ -43,14 +43,14 @@ class BoostConnectionTest : public CppUnit::TestFixture {
 
 		void testDestructor() {
 			{
-				BoostConnection::ref testling(BoostConnection::create(&boostIOServiceThread_->getIOService()));
+				BoostConnection::ref testling(BoostConnection::create(&boostIOServiceThread_->getIOService(), eventLoop_));
 				testling->connect(HostAddressPort(HostAddress(address, 4), 5222));
 			}
 		}
 
 		void testDestructor_PendingEvents() {
 			{
-				BoostConnection::ref testling(BoostConnection::create(&boostIOServiceThread_->getIOService()));
+				BoostConnection::ref testling(BoostConnection::create(&boostIOServiceThread_->getIOService(), eventLoop_));
 				testling->connect(HostAddressPort(HostAddress(address, 4), 5222));
 				while (!eventLoop_->hasEvents()) {
 					Swift::sleep(10);
@@ -60,7 +60,7 @@ class BoostConnectionTest : public CppUnit::TestFixture {
 		}
 
 		void testWrite() {
-			BoostConnection::ref testling(BoostConnection::create(&boostIOServiceThread_->getIOService()));
+			BoostConnection::ref testling(BoostConnection::create(&boostIOServiceThread_->getIOService(), eventLoop_));
 			testling->onConnectFinished.connect(boost::bind(&BoostConnectionTest::doWrite, this, testling.get()));
 			testling->onDataRead.connect(boost::bind(&BoostConnectionTest::handleDataRead, this, _1));
 			testling->onDisconnected.connect(boost::bind(&BoostConnectionTest::handleDisconnected, this));
diff --git a/Swiften/QA/NetworkTest/DomainNameResolverTest.cpp b/Swiften/QA/NetworkTest/DomainNameResolverTest.cpp
index 309e617..c853d68 100644
--- a/Swiften/QA/NetworkTest/DomainNameResolverTest.cpp
+++ b/Swiften/QA/NetworkTest/DomainNameResolverTest.cpp
@@ -43,7 +43,7 @@ class DomainNameResolverTest : public CppUnit::TestFixture {
 
 		void setUp() {
 			eventLoop = new DummyEventLoop();
-			resolver = new PlatformDomainNameResolver();
+			resolver = new PlatformDomainNameResolver(eventLoop);
 			resultsAvailable = false;
 		}
 
diff --git a/Swiften/QA/ReconnectTest/ReconnectTest.cpp b/Swiften/QA/ReconnectTest/ReconnectTest.cpp
index c4e34ac..e74ae27 100644
--- a/Swiften/QA/ReconnectTest/ReconnectTest.cpp
+++ b/Swiften/QA/ReconnectTest/ReconnectTest.cpp
@@ -9,7 +9,7 @@
 
 #include "Swiften/Client/Client.h"
 #include "Swiften/Network/BoostTimer.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/EventLoop/EventLoop.h"
 #include "Swiften/EventLoop/SimpleEventLoop.h"
 #include "Swiften/Roster/GetRosterRequest.h"
 #include "Swiften/Client/ClientXMLTracer.h"
diff --git a/Swiften/Queries/Request.cpp b/Swiften/Queries/Request.cpp
index db7b1f4..45ece8a 100644
--- a/Swiften/Queries/Request.cpp
+++ b/Swiften/Queries/Request.cpp
@@ -6,7 +6,6 @@
 
 #include "Swiften/Queries/Request.h"
 #include "Swiften/Queries/IQRouter.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
 
 namespace Swift {
 
diff --git a/Swiften/Roster/XMPPRosterController.cpp b/Swiften/Roster/XMPPRosterController.cpp
index 75ab494..62bebc3 100644
--- a/Swiften/Roster/XMPPRosterController.cpp
+++ b/Swiften/Roster/XMPPRosterController.cpp
@@ -12,7 +12,6 @@
 #include "Swiften/Elements/RosterItemPayload.h"
 #include "Swiften/Queries/IQRouter.h"
 #include "Swiften/Roster/GetRosterRequest.h"
-#include "Swiften/EventLoop/MainEventLoop.h"
 #include "Swiften/Roster/Roster.h"
 #include "Swiften/Roster/SetPresence.h"
 #include "Swiften/Roster/OfflineRosterFilter.h"
-- 
cgit v0.10.2-6-g49f6