From 42e410208ca9aa8af5f27c85a6b33af488a0b6cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Fri, 24 Jul 2009 08:30:33 +0200
Subject: More DNSSD framework.


diff --git a/Slimber/CLI/main.cpp b/Slimber/CLI/main.cpp
index 40f41c8..65af3bc 100644
--- a/Slimber/CLI/main.cpp
+++ b/Slimber/CLI/main.cpp
@@ -10,6 +10,7 @@
 #include "Slimber/Server.h"
 #include "Slimber/FileVCardCollection.h"
 #include "Swiften/LinkLocal/LinkLocalRoster.h"
+#include "Swiften/LinkLocal/BonjourQuerier.h"
 #include "Swiften/EventLoop/SimpleEventLoop.h"
 #include "Swiften/Application/Platform/PlatformApplication.h"
 
@@ -17,7 +18,14 @@ using namespace Swift;
 
 int main() {
 	SimpleEventLoop eventLoop;
+	boost::shared_ptr<BonjourQuerier> querier(new BonjourQuerier());
+	querier->start();
+	boost::shared_ptr<DNSSDBrowseQuery> query = querier->createBrowseQuery();
+	query->startBrowsing();
+	boost::shared_ptr<DNSSDPublishQuery> query2 = querier->createPublishQuery("remko", 1234, LinkLocalServiceInfo());
+	query2->publish();
 
+/*
 	boost::shared_ptr<DNSSDService> dnsSDService;
 #if defined(SWIFTEN_PLATFORM_MACOSX) || defined(SWIFTEN_PLATFORM_WINDOWS)
 	dnsSDService = boost::shared_ptr<AppleDNSSDService>(
@@ -32,6 +40,8 @@ int main() {
 	FileVCardCollection vCardCollection(PlatformApplication("Slimber").getSettingsDir());
 
 	Server server(5222, 5562, linkLocalRoster, dnsSDService, &vCardCollection);
+	*/
+	
 	eventLoop.run();
 	return 0;
 }
diff --git a/Swiften/LinkLocal/BonjourBrowseQuery.h b/Swiften/LinkLocal/BonjourBrowseQuery.h
index 5b14d30..6a50a61 100644
--- a/Swiften/LinkLocal/BonjourBrowseQuery.h
+++ b/Swiften/LinkLocal/BonjourBrowseQuery.h
@@ -11,8 +11,9 @@ namespace Swift {
 			BonjourBrowseQuery(boost::shared_ptr<BonjourQuerier> q) : BonjourQuery(q) {
 				DNSServiceErrorType result = DNSServiceBrowse(
 						&sdRef, 0, 0, "_presence._tcp", 0, 
-						&BonjourBrowseQuery::handleServiceDiscoveredGlobal , this);
+						&BonjourBrowseQuery::handleServiceDiscovered, this);
 				if (result != kDNSServiceErr_NoError) {
+					std::cout << "Error" << std::endl;
 					// TODO
 				}
 			}
@@ -27,29 +28,21 @@ namespace Swift {
 			}
 
 		private:
-			static void handleServiceDiscoveredGlobal(DNSServiceRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *serviceName, const char *regtype, const char *replyDomain, void *context) {
-				static_cast<BonjourBrowseQuery*>(context)->handleServiceDiscovered(flags, interfaceIndex, errorCode, serviceName, regtype, replyDomain);
-			}
-
-			void handleServiceDiscovered(
-					DNSServiceFlags flags, 
-					uint32_t interfaceIndex, 
-					DNSServiceErrorType errorCode, 
-					const char *serviceName, 
-					const char *type, 
-					const char *domain) {
+			static void handleServiceDiscovered(DNSServiceRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *name, const char *type, const char *domain, void *context) {
 				if (errorCode != kDNSServiceErr_NoError) {
 					return;
 				}
 				else {
-					LinkLocalServiceID service(serviceName, type, domain, interfaceIndex);
+					BonjourBrowseQuery* query = static_cast<BonjourBrowseQuery*>(context);
+					LinkLocalServiceID service(name, type, domain, interfaceIndex);
+					std::cout << "Service discovered: " << name << std::endl;
 					if (flags & kDNSServiceFlagsAdd) {
-						onServiceAdded(service);
+						query->onServiceAdded(service);
 					}
 					else {
-						onServiceRemoved(service);
+						query->onServiceRemoved(service);
 					}
 				}
-		}
+			}
 	};
 }
diff --git a/Swiften/LinkLocal/BonjourPublishQuery.h b/Swiften/LinkLocal/BonjourPublishQuery.h
new file mode 100644
index 0000000..4e7fd2f
--- /dev/null
+++ b/Swiften/LinkLocal/BonjourPublishQuery.h
@@ -0,0 +1,41 @@
+#pragma once
+
+#include "Swiften/LinkLocal/BonjourQuery.h"
+#include "Swiften/LinkLocal/DNSSDPublishQuery.h"
+#include "Swiften/LinkLocal/LinkLocalServiceInfo.h"
+#include "Swiften/Base/ByteArray.h"
+
+namespace Swift {
+	class BonjourQuerier;
+
+	class BonjourPublishQuery : public DNSSDPublishQuery, public BonjourQuery {
+		public:	
+			BonjourPublishQuery(const String& name, int port, const LinkLocalServiceInfo& info, boost::shared_ptr<BonjourQuerier> querier) : BonjourQuery(querier) {
+				ByteArray txtRecord = info.toTXTRecord();
+				DNSServiceErrorType result = DNSServiceRegister(
+						&sdRef, 0, 0, name.getUTF8Data(), "_presence._tcp", NULL, NULL, port, 
+						txtRecord.getSize(), txtRecord.getData(), 
+						&BonjourPublishQuery::handleServiceRegistered, this);
+				if (result != kDNSServiceErr_NoError) {
+					// TODO
+					std::cerr << "Error creating service registration" << std::endl;
+				}
+			}
+
+			void publish() {
+				run();
+			}
+
+		private:
+			static void handleServiceRegistered(DNSServiceRef, DNSServiceFlags, DNSServiceErrorType errorCode, const char *name, const char *regtype, const char *domain, void *context) {
+				std::cout << "Publish finished " << name << std::endl;
+				BonjourPublishQuery* query = static_cast<BonjourPublishQuery*>(context);
+				if (errorCode != kDNSServiceErr_NoError) {
+					query->onPublishFinished(boost::optional<LinkLocalServiceID>());
+				}
+				else {
+					query->onPublishFinished(boost::optional<LinkLocalServiceID>(LinkLocalServiceID(name, regtype, domain, 0)));
+				}
+			}
+	};
+}
diff --git a/Swiften/LinkLocal/BonjourQuerier.cpp b/Swiften/LinkLocal/BonjourQuerier.cpp
index 1ddbd9d..93259c1 100644
--- a/Swiften/LinkLocal/BonjourQuerier.cpp
+++ b/Swiften/LinkLocal/BonjourQuerier.cpp
@@ -7,6 +7,7 @@
 
 #include "Swiften/EventLoop/MainEventLoop.h"
 #include "Swiften/LinkLocal/BonjourBrowseQuery.h"
+#include "Swiften/LinkLocal/BonjourPublishQuery.h"
 #include "Swiften/Base/foreach.h"
 
 namespace Swift {
@@ -18,7 +19,6 @@ BonjourQuerier::BonjourQuerier() : stopRequested(false), thread(0) {
 	interruptSelectReadSocket = fds[0];
 	fcntl(interruptSelectReadSocket, F_SETFL, fcntl(interruptSelectReadSocket, F_GETFL)|O_NONBLOCK);
 	interruptSelectWriteSocket = fds[1];
-	// TODO: Schedule thread
 }
 
 BonjourQuerier::~BonjourQuerier() {
@@ -29,6 +29,10 @@ boost::shared_ptr<DNSSDBrowseQuery> BonjourQuerier::createBrowseQuery() {
 	return boost::shared_ptr<DNSSDBrowseQuery>(new BonjourBrowseQuery(shared_from_this()));
 }
 
+boost::shared_ptr<DNSSDPublishQuery> BonjourQuerier::createPublishQuery(const String& name, int port, const LinkLocalServiceInfo& info) {
+	return boost::shared_ptr<DNSSDPublishQuery>(new BonjourPublishQuery(name, port, info, shared_from_this()));
+}
+
 void BonjourQuerier::addRunningQuery(boost::shared_ptr<BonjourQuery> query) {
 	{
 		boost::lock_guard<boost::mutex> lock(runningQueriesMutex);
@@ -74,14 +78,16 @@ void BonjourQuerier::run() {
 		int maxSocket;
 		{
 			boost::unique_lock<boost::mutex> lock(runningQueriesMutex);
-			runningQueriesAvailableEvent.wait(lock);
 			if (runningQueries.empty()) {
-				continue;
+				runningQueriesAvailableEvent.wait(lock);
+				if (runningQueries.empty()) {
+					continue;
+				}
 			}
 
 			// Run all running queries
 			FD_ZERO(&fdSet);
-			int maxSocket = interruptSelectReadSocket;
+			maxSocket = interruptSelectReadSocket;
 			FD_SET(interruptSelectReadSocket, &fdSet);
 
 			foreach(const boost::shared_ptr<BonjourQuery>& query, runningQueries) {
diff --git a/Swiften/LinkLocal/BonjourQuerier.h b/Swiften/LinkLocal/BonjourQuerier.h
index 037d9e6..5f69ad6 100644
--- a/Swiften/LinkLocal/BonjourQuerier.h
+++ b/Swiften/LinkLocal/BonjourQuerier.h
@@ -8,9 +8,12 @@
 
 #include "Swiften/EventLoop/EventOwner.h"
 #include "Swiften/LinkLocal/DNSSDBrowseQuery.h"
+#include "Swiften/LinkLocal/DNSSDPublishQuery.h"
 #include "Swiften/LinkLocal/BonjourQuery.h"
 
 namespace Swift {
+	class LinkLocalServiceInfo;
+
 	class BonjourQuerier : 
 			public boost::enable_shared_from_this<BonjourQuerier>, 
 			public EventOwner {
@@ -19,6 +22,7 @@ namespace Swift {
 			~BonjourQuerier();
 
 			boost::shared_ptr<DNSSDBrowseQuery> createBrowseQuery();
+			boost::shared_ptr<DNSSDPublishQuery> createPublishQuery(const String& name, int port, const LinkLocalServiceInfo& info);
 
 			void start();
 			void stop();
diff --git a/Swiften/LinkLocal/BonjourQuery.cpp b/Swiften/LinkLocal/BonjourQuery.cpp
index a9c13fb..965a845 100644
--- a/Swiften/LinkLocal/BonjourQuery.cpp
+++ b/Swiften/LinkLocal/BonjourQuery.cpp
@@ -11,7 +11,9 @@ BonjourQuery::~BonjourQuery() {
 }
 
 void BonjourQuery::processResult() {
+	std::cout << "Process result" << std::endl;
 	boost::lock_guard<boost::mutex> lock(sdRefMutex);
+	std::cout << "DNSSDServiceProcessResult" << std::endl;
 	DNSServiceProcessResult(sdRef);
 }
 
diff --git a/Swiften/LinkLocal/DNSSDBrowseQuery.h b/Swiften/LinkLocal/DNSSDBrowseQuery.h
index b46d968..e548ca5 100644
--- a/Swiften/LinkLocal/DNSSDBrowseQuery.h
+++ b/Swiften/LinkLocal/DNSSDBrowseQuery.h
@@ -9,6 +9,9 @@ namespace Swift {
 		public:
 			virtual ~DNSSDBrowseQuery();
 
+			virtual void startBrowsing() = 0;
+			virtual void stopBrowsing() = 0;
+
 			boost::signal<void (const LinkLocalServiceID&)> onServiceAdded;
 			boost::signal<void (const LinkLocalServiceID&)> onServiceRemoved;
 	};
diff --git a/Swiften/LinkLocal/DNSSDPublishQuery.cpp b/Swiften/LinkLocal/DNSSDPublishQuery.cpp
new file mode 100644
index 0000000..cf3fd25
--- /dev/null
+++ b/Swiften/LinkLocal/DNSSDPublishQuery.cpp
@@ -0,0 +1,8 @@
+#include "Swiften/LinkLocal/DNSSDPublishQuery.h"
+
+namespace Swift {
+
+DNSSDPublishQuery::~DNSSDPublishQuery() {
+}
+
+}
diff --git a/Swiften/LinkLocal/DNSSDPublishQuery.h b/Swiften/LinkLocal/DNSSDPublishQuery.h
new file mode 100644
index 0000000..48c86dc
--- /dev/null
+++ b/Swiften/LinkLocal/DNSSDPublishQuery.h
@@ -0,0 +1,17 @@
+#pragma once
+
+#include <boost/signal.hpp>
+#include <boost/optional.hpp>
+
+#include "Swiften/LinkLocal/LinkLocalServiceID.h"
+
+namespace Swift {
+	class DNSSDPublishQuery {
+		public:
+			virtual ~DNSSDPublishQuery();
+
+			virtual void publish() = 0;
+
+			boost::signal<void (boost::optional<LinkLocalServiceID>)> onPublishFinished;
+	};
+}
diff --git a/Swiften/LinkLocal/Makefile.inc b/Swiften/LinkLocal/Makefile.inc
index b43a7e4..093ac70 100644
--- a/Swiften/LinkLocal/Makefile.inc
+++ b/Swiften/LinkLocal/Makefile.inc
@@ -1,6 +1,7 @@
 SWIFTEN_SOURCES += \
 	Swiften/LinkLocal/BonjourQuery.cpp \
 	Swiften/LinkLocal/DNSSDBrowseQuery.cpp \
+	Swiften/LinkLocal/DNSSDPublishQuery.cpp \
 	Swiften/LinkLocal/DNSSDServiceFactory.cpp \
 	Swiften/LinkLocal/BonjourQuerier.cpp \
 	Swiften/LinkLocal/PlatformDNSSDServiceFactory.cpp \
-- 
cgit v0.10.2-6-g49f6