From 3a0309d2d897bf4f399610e52ad439c002e16f9c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Thu, 23 Jul 2009 21:03:11 +0200
Subject: Add DNS service factory.


diff --git a/Makefile.config.in b/Makefile.config.in
index 848f223..010075a 100644
--- a/Makefile.config.in
+++ b/Makefile.config.in
@@ -22,6 +22,7 @@ HAVE_EXPAT=@HAVE_EXPAT@
 USE_BUNDLED_EXPAT=@USE_BUNDLED_EXPAT@
 HAVE_LIBXML=@CONFIG_HAVE_LIBXML@
 HAVE_OPENSSL=@CONFIG_HAVE_OPENSSL@
+HAVE_BONJOUR=@HAVE_BONJOUR@
 HAVE_AVAHI=@HAVE_AVAHI@
 WIN32=@CONFIG_WIN32@
 MACOSX=@CONFIG_MACOSX@
diff --git a/Swiften/LinkLocal/AvahiDNSSDService.cpp b/Swiften/LinkLocal/AvahiDNSSDService.cpp
index 72039b7..fe9ac83 100644
--- a/Swiften/LinkLocal/AvahiDNSSDService.cpp
+++ b/Swiften/LinkLocal/AvahiDNSSDService.cpp
@@ -82,7 +82,7 @@ void AvahiDNSSDService::startResolvingService(const LinkLocalServiceID& service)
 	avahi_threaded_poll_unlock(threadedPoll);
 }
 
-void AvahiDNSSDService::stopResolvingService(const Service& service) {
+void AvahiDNSSDService::stopResolvingService(const LinkLocalServiceID& service) {
 	avahi_threaded_poll_lock(threadedPoll);
 
 	ServiceResolverMap::iterator i = serviceResolvers.find(service);
diff --git a/Swiften/LinkLocal/AvahiDNSSDService.h b/Swiften/LinkLocal/AvahiDNSSDService.h
index a5be943..99a1d23 100644
--- a/Swiften/LinkLocal/AvahiDNSSDService.h
+++ b/Swiften/LinkLocal/AvahiDNSSDService.h
@@ -47,7 +47,7 @@ namespace Swift {
 			AvahiClient* client;
 			AvahiThreadedPoll* threadedPoll;
 			AvahiServiceBrowser* serviceBrowser;
-			typedef std::map<Service, AvahiServiceResolver*> ServiceResolverMap;
+			typedef std::map<LinkLocalServiceID, AvahiServiceResolver*> ServiceResolverMap;
 			ServiceResolverMap serviceResolvers;
 			typedef std::map<String, HostAddress> HostnameAddressMap;
 			HostnameAddressMap hostnameAddresses;
diff --git a/Swiften/LinkLocal/DNSSDServiceFactory.cpp b/Swiften/LinkLocal/DNSSDServiceFactory.cpp
new file mode 100644
index 0000000..f53660a
--- /dev/null
+++ b/Swiften/LinkLocal/DNSSDServiceFactory.cpp
@@ -0,0 +1,8 @@
+#include "Swiften/LinkLocal/DNSSDServiceFactory.h"
+
+namespace Swift {
+
+DNSSDServiceFactory::~DNSSDServiceFactory() {
+}
+
+}
diff --git a/Swiften/LinkLocal/DNSSDServiceFactory.h b/Swiften/LinkLocal/DNSSDServiceFactory.h
new file mode 100644
index 0000000..6296165
--- /dev/null
+++ b/Swiften/LinkLocal/DNSSDServiceFactory.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#include <boost/shared_ptr.hpp>
+
+namespace Swift {
+	class DNSSDService;
+
+	class DNSSDServiceFactory {
+		public:
+			virtual ~DNSSDServiceFactory();
+			virtual bool canCreate() const = 0;
+
+			virtual boost::shared_ptr<DNSSDService> createDNSSDService() = 0;
+	};
+}
diff --git a/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp b/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp
index fbc3022..904f8f0 100644
--- a/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp
+++ b/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp
@@ -6,7 +6,8 @@
 
 namespace Swift {
 
-LinkLocalServiceBrowser::LinkLocalServiceBrowser(boost::shared_ptr<DNSSDService> service) : dnsSDService(service) {
+LinkLocalServiceBrowser::LinkLocalServiceBrowser(DNSSDServiceFactory* factory) {
+  dnsSDService = factory->createDNSSDService();
 	dnsSDService->onServiceAdded.connect(
 			boost::bind(&LinkLocalServiceBrowser::handleServiceAdded, this, _1));
 	dnsSDService->onServiceRemoved.connect(
diff --git a/Swiften/LinkLocal/LinkLocalServiceBrowser.h b/Swiften/LinkLocal/LinkLocalServiceBrowser.h
index 6107557..879b286 100644
--- a/Swiften/LinkLocal/LinkLocalServiceBrowser.h
+++ b/Swiften/LinkLocal/LinkLocalServiceBrowser.h
@@ -7,6 +7,7 @@
 
 #include "Swiften/Base/String.h"
 #include "Swiften/LinkLocal/DNSSDService.h"
+#include "Swiften/LinkLocal/DNSSDServiceFactory.h"
 #include "Swiften/LinkLocal/LinkLocalService.h"
 
 namespace Swift {
@@ -14,7 +15,7 @@ namespace Swift {
 
 	class LinkLocalServiceBrowser {
 		public:
-			LinkLocalServiceBrowser(boost::shared_ptr<DNSSDService> service);
+			LinkLocalServiceBrowser(DNSSDServiceFactory*);
 
 			std::vector<LinkLocalService> getServices() const;
 
diff --git a/Swiften/LinkLocal/Makefile.inc b/Swiften/LinkLocal/Makefile.inc
index a890d0a..4bd1b0e 100644
--- a/Swiften/LinkLocal/Makefile.inc
+++ b/Swiften/LinkLocal/Makefile.inc
@@ -1,4 +1,6 @@
 SWIFTEN_SOURCES += \
+	Swiften/LinkLocal/DNSSDServiceFactory.cpp \
+	Swiften/LinkLocal/PlatformDNSSDServiceFactory.cpp \
 	Swiften/LinkLocal/DNSSDService.cpp \
 	Swiften/LinkLocal/LinkLocalRoster.cpp \
 	Swiften/LinkLocal/LinkLocalServiceID.cpp \
@@ -8,7 +10,7 @@ SWIFTEN_SOURCES += \
 	Swiften/LinkLocal/OutgoingLinkLocalSession.cpp \
 	Swiften/LinkLocal/LinkLocalConnector.cpp
 
-ifeq ($(MACOSX),1)
+ifeq ($(HAVE_BONJOUR),yes)
 SWIFTEN_SOURCES += \
 	Swiften/LinkLocal/AppleDNSSDService.cpp
 endif
diff --git a/Swiften/LinkLocal/PlatformDNSSDServiceFactory.cpp b/Swiften/LinkLocal/PlatformDNSSDServiceFactory.cpp
new file mode 100644
index 0000000..df70506
--- /dev/null
+++ b/Swiften/LinkLocal/PlatformDNSSDServiceFactory.cpp
@@ -0,0 +1,38 @@
+#include "Swiften/LinkLocal/PlatformDNSSDServiceFactory.h"
+
+#include <cassert>
+
+#ifdef HAVE_SWIFTEN_CONFIG_H
+#include "Swiften/config.h"
+#endif
+#if defined(HAVE_AVAHI)
+#include "Swiften/LinkLocal/AvahiDNSSDService.h"
+#elif defined(HAVE_BONJOUR)
+#include "Swiften/LinkLocal/AppleDNSSDService.h"
+#endif
+
+namespace Swift {
+
+PlatformDNSSDServiceFactory::PlatformDNSSDServiceFactory() {
+}
+
+bool PlatformDNSSDServiceFactory::canCreate() const {
+#if defined(HAVE_AVAHI) || defined(HAVE_BONJOUR)
+	return true;
+#else
+	return false;
+#endif
+}
+
+boost::shared_ptr<DNSSDService> PlatformDNSSDServiceFactory::createDNSSDService() {
+#if defined(HAVE_AVAHI)
+	return boost::shared_ptr<DNSSDService>(new AvahiDNSSDService());
+#elif defined(HAVE_BONJOUR)
+  return boost::shared_ptr<DNSSDService>(new AppleDNSSDService());
+#else
+	assert(false);
+	return boost::shared_ptr<DNSSDService>();
+#endif
+}
+
+}
diff --git a/Swiften/LinkLocal/PlatformDNSSDServiceFactory.h b/Swiften/LinkLocal/PlatformDNSSDServiceFactory.h
new file mode 100644
index 0000000..9c4e4e6
--- /dev/null
+++ b/Swiften/LinkLocal/PlatformDNSSDServiceFactory.h
@@ -0,0 +1,13 @@
+#pragma once
+
+#include "Swiften/LinkLocal/DNSSDServiceFactory.h"
+
+namespace Swift {
+	class PlatformDNSSDServiceFactory : public DNSSDServiceFactory {
+		public:
+			PlatformDNSSDServiceFactory();
+
+			bool canCreate() const;
+			virtual boost::shared_ptr<DNSSDService> createDNSSDService();
+	};
+}
diff --git a/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp b/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
index 59a113d..34f797c 100644
--- a/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
+++ b/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
@@ -9,7 +9,7 @@
 #include "Swiften/LinkLocal/DNSSDService.h"
 #include "Swiften/EventLoop/DummyEventLoop.h"
 
-// Test IP address change
+// Test canCreate() == false
 
 using namespace Swift;
 
@@ -26,7 +26,7 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 
 		void setUp() {
 			eventLoop = new DummyEventLoop();
-			dnsSDService = boost::shared_ptr<MockDNSSDService>(new MockDNSSDService());
+			dnsSDServiceFactory = new MockDNSSDServiceFactory();
 			testServiceID = new LinkLocalServiceID("foo", "bar.local");
 			testServiceInfo = new DNSSDService::ResolveResult("xmpp.bar.local", 1234, LinkLocalServiceInfo());
 			testServiceInfo2 = new DNSSDService::ResolveResult("xmpp.foo.local", 2345, LinkLocalServiceInfo());
@@ -40,14 +40,15 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 			delete testServiceInfo2;
 			delete testServiceInfo;
 			delete testServiceID;
+      delete dnsSDServiceFactory;
 			delete eventLoop;
 		}
 
 		void testServiceAdded() {
 			boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
 
-			dnsSDService->setServiceInfo(*testServiceID,*testServiceInfo);
-			dnsSDService->addService(*testServiceID);
+			dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo);
+			dnsSDService()->addService(*testServiceID);
 			eventLoop->processEvents();
 
 			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addedServices.size()));
@@ -64,7 +65,7 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 		void testServiceAdded_NoServiceInfo() {
 			boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
 
-			dnsSDService->addService(*testServiceID);
+			dnsSDService()->addService(*testServiceID);
 			eventLoop->processEvents();
 
 			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(addedServices.size()));
@@ -74,11 +75,11 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 
 		void testServiceChanged() {
 			boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
-			dnsSDService->setServiceInfo(*testServiceID,*testServiceInfo);
-			dnsSDService->addService(*testServiceID);
+			dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo);
+			dnsSDService()->addService(*testServiceID);
 			eventLoop->processEvents();
 
-			dnsSDService->setServiceInfo(*testServiceID,*testServiceInfo2);
+			dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo2);
 			eventLoop->processEvents();
 
 			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addedServices.size()));
@@ -94,13 +95,13 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 
 		void testServiceRemoved() {
 			boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
-			dnsSDService->setServiceInfo(*testServiceID,*testServiceInfo);
-			dnsSDService->addService(*testServiceID);
+			dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo);
+			dnsSDService()->addService(*testServiceID);
 			eventLoop->processEvents();
 
-			dnsSDService->removeService(*testServiceID);
+			dnsSDService()->removeService(*testServiceID);
 			eventLoop->processEvents();
-			dnsSDService->setServiceInfo(*testServiceID,*testServiceInfo2);
+			dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo2);
 			eventLoop->processEvents();
 
 			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addedServices.size()));
@@ -114,7 +115,7 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 	private:
 		boost::shared_ptr<LinkLocalServiceBrowser> createTestling() {
 			boost::shared_ptr<LinkLocalServiceBrowser> testling(
-					new LinkLocalServiceBrowser(dnsSDService));
+					new LinkLocalServiceBrowser(dnsSDServiceFactory));
 			testling->onServiceAdded.connect(boost::bind(
 					&LinkLocalServiceBrowserTest::handleServiceAdded, this, _1));
 			testling->onServiceChanged.connect(boost::bind(
@@ -136,9 +137,14 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 			changedServices.push_back(service);
 		}
 
+    boost::shared_ptr<MockDNSSDService> dnsSDService() const {
+      CPPUNIT_ASSERT(dnsSDServiceFactory->services.size() > 0);
+      return dnsSDServiceFactory->services[0];
+    }
+
 	private:
 		DummyEventLoop* eventLoop;
-		boost::shared_ptr<MockDNSSDService> dnsSDService;
+		MockDNSSDServiceFactory* dnsSDServiceFactory;
 		std::vector<LinkLocalServiceID> addedServices;
 		std::vector<LinkLocalServiceID> changedServices;
 		std::vector<LinkLocalServiceID> removedServices;
diff --git a/Swiften/LinkLocal/UnitTest/MockDNSSDService.h b/Swiften/LinkLocal/UnitTest/MockDNSSDService.h
index 56661ee..69ac06d 100644
--- a/Swiften/LinkLocal/UnitTest/MockDNSSDService.h
+++ b/Swiften/LinkLocal/UnitTest/MockDNSSDService.h
@@ -6,6 +6,7 @@
 
 #include "Swiften/EventLoop/MainEventLoop.h"
 #include "Swiften/LinkLocal/DNSSDService.h"
+#include "Swiften/LinkLocal/DNSSDServiceFactory.h"
 
 namespace Swift {
 	class MockDNSSDService : public DNSSDService {
@@ -78,5 +79,19 @@ namespace Swift {
 			std::vector<LinkLocalServiceID> resolvingServices;
 	};
 
+  class MockDNSSDServiceFactory : public DNSSDServiceFactory {
+    public:
+      boost::shared_ptr<DNSSDService> createDNSSDService() {
+        boost::shared_ptr<MockDNSSDService> result(new MockDNSSDService());   
+        services.push_back(result);
+        return result;
+      }
+
+      bool canCreate() const {
+        return true;
+      }
+
+      std::vector<boost::shared_ptr<MockDNSSDService> > services;
+  };
 
 }
diff --git a/configure.in b/configure.in
index 9025855..3a10e2c 100644
--- a/configure.in
+++ b/configure.in
@@ -13,6 +13,7 @@ AC_CANONICAL_HOST
 AH_TEMPLATE(HAVE_OPENSSL, [OpenSSL library available])
 AH_TEMPLATE(HAVE_LIBXML, [LibXML library available])
 AH_TEMPLATE(HAVE_EXPAT, [Expat library available])
+AH_TEMPLATE(HAVE_BONJOUR, [Apple Bonjour SDK available])
 AH_TEMPLATE(HAVE_AVAHI, [Avahi library available])
 
 ################################################################################
@@ -194,11 +195,13 @@ fi
 # Resolv
 AC_CHECK_LIB(resolv, inet_aton)
 
-# Avahi
+# DNSSD Service
 case $host in
 	*-*-cygwin*)
+    AC_DEFINE(HAVE_BONJOUR)
 		;;
 	*-mingw32*)
+    AC_DEFINE(HAVE_BONJOUR)
 		;;
 	*-*-darwin*)
 		;;
@@ -239,6 +242,7 @@ echo
 AC_SUBST(SET_MAKE)
 AC_SUBST(HAVE_EXPAT)
 AC_SUBST(HAVE_AVAHI)
+AC_SUBST(HAVE_BONJOUR)
 AC_SUBST(CONFIG_CXX)
 AC_SUBST(CONFIG_CXXFLAGS)
 AC_SUBST(CONFIG_CC)
-- 
cgit v0.10.2-6-g49f6