From 72858ab262a16aa8db209c2898ab0d3c951a9829 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sun, 12 Jul 2009 19:57:48 +0200
Subject: Added ServerFromClientSession.


diff --git a/Limber/main.cpp b/Limber/main.cpp
index 8378c52..6dad151 100644
--- a/Limber/main.cpp
+++ b/Limber/main.cpp
@@ -11,38 +11,14 @@
 #include "Swiften/Base/ByteArray.h"
 #include "Swiften/EventLoop/MainEventLoop.h"
 #include "Swiften/EventLoop/SimpleEventLoop.h"
-#include "Swiften/Network/IncomingConnection.h"
 #include "Swiften/Network/ConnectionServer.h"
 #include "Swiften/Network/BoostIOServiceThread.h"
+#include "Swiften/Server/ServerFromClientSession.h"
 #include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h"
 #include "Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h"
 
 using namespace Swift;
 
-class ServerFromClientSession {
-	public:
-		ServerFromClientSession(
-				boost::shared_ptr<IncomingConnection> connection, 
-				PayloadParserFactoryCollection* payloadParserFactories, 
-				PayloadSerializerCollection* payloadSerializers) : 
-					connection_(connection), 
-					payloadParserFactories_(payloadParserFactories), 
-					payloadSerializers_(payloadSerializers) {
-		}
-
-		void start() {
-			connection_->write("Hello\n");
-			onSessionFinished();
-		}
-	
-		boost::signal<void()> onSessionFinished;
-
-	private:
-		boost::shared_ptr<IncomingConnection> connection_;
-		PayloadParserFactoryCollection* payloadParserFactories_;
-		PayloadSerializerCollection* payloadSerializers_;
-};
-
 // A reference-counted non-modifiable buffer class.
 class SharedBuffer {
 	public:
@@ -134,7 +110,6 @@ class Server {
 			ServerFromClientSession* session = new ServerFromClientSession(c, &payloadParserFactories_, &payloadSerializers_);
 			serverFromClientSessions_.push_back(session);
 			session->onSessionFinished.connect(boost::bind(&Server::handleSessionFinished, this, session));
-			session->start();
 		}
 
 		void handleSessionFinished(ServerFromClientSession* session) {
diff --git a/Swiften/Parser/XMPPParser.cpp b/Swiften/Parser/XMPPParser.cpp
index e05cbca..59cfce7 100644
--- a/Swiften/Parser/XMPPParser.cpp
+++ b/Swiften/Parser/XMPPParser.cpp
@@ -54,7 +54,7 @@ bool XMPPParser::parse(const String& data) {
 void XMPPParser::handleStartElement(const String& element, const String& ns, const AttributeMap& attributes) {
 	if (!inStream()) {
 		if (element == "stream" && ns == "http://etherx.jabber.org/streams") {
-			client_->handleStreamStart();
+			client_->handleStreamStart(attributes.getAttribute("to"));
 		}
 		else {
 			parseErrorOccurred_ = true;
diff --git a/Swiften/Parser/XMPPParserClient.h b/Swiften/Parser/XMPPParserClient.h
index abecc71..d19c7d0 100644
--- a/Swiften/Parser/XMPPParserClient.h
+++ b/Swiften/Parser/XMPPParserClient.h
@@ -6,12 +6,13 @@
 #include "Swiften/Elements/Element.h"
 
 namespace Swift {
-	class XMPPParserClient
-	{
+	class String;
+
+	class XMPPParserClient {
 		public:
 			virtual ~XMPPParserClient();
 
-			virtual void handleStreamStart() = 0;
+			virtual void handleStreamStart(const String& header) = 0;
 			virtual void handleElement(boost::shared_ptr<Element>) = 0;
 			virtual void handleStreamEnd() = 0;
 	};
diff --git a/Swiften/Server/Makefile.inc b/Swiften/Server/Makefile.inc
index 54f09ce..1ab98d7 100644
--- a/Swiften/Server/Makefile.inc
+++ b/Swiften/Server/Makefile.inc
@@ -1,5 +1,6 @@
 SWIFTEN_SOURCES += \
 	Swiften/Server/ServerSession.cpp \
+	Swiften/Server/ServerFromClientSession.cpp \
 	Swiften/Server/ServerStanzaRouter.cpp \
 	Swiften/Server/IncomingConnection.cpp
 
diff --git a/Swiften/Server/ServerFromClientSession.cpp b/Swiften/Server/ServerFromClientSession.cpp
new file mode 100644
index 0000000..e85021e
--- /dev/null
+++ b/Swiften/Server/ServerFromClientSession.cpp
@@ -0,0 +1,47 @@
+#include "Swiften/Server/ServerFromClientSession.h"
+
+#include <boost/bind.hpp>
+
+#include "Swiften/Network/IncomingConnection.h"
+#include "Swiften/StreamStack/StreamStack.h"
+#include "Swiften/StreamStack/IncomingConnectionLayer.h"
+#include "Swiften/StreamStack/XMPPLayer.h"
+
+namespace Swift {
+
+ServerFromClientSession::ServerFromClientSession(
+		boost::shared_ptr<IncomingConnection> connection, 
+		PayloadParserFactoryCollection* payloadParserFactories, 
+		PayloadSerializerCollection* payloadSerializers) : 
+			connection_(connection), 
+			payloadParserFactories_(payloadParserFactories), 
+			payloadSerializers_(payloadSerializers) {
+	xmppLayer_ = new XMPPLayer(payloadParserFactories_, payloadSerializers_);
+	xmppLayer_->onStreamStart.connect(
+			boost::bind(&ServerFromClientSession::handleStreamStart, this, _1));
+	xmppLayer_->onElement.connect(
+			boost::bind(&ServerFromClientSession::handleElement, this, _1));
+	//xmppLayer_->onError.connect(
+	//		boost::bind(&ServerFromClientSession::setError, this, XMLError));
+	xmppLayer_->onDataRead.connect(
+			boost::bind(boost::ref(onDataRead), _1));
+	xmppLayer_->onWriteData.connect(
+			boost::bind(boost::ref(onDataWritten), _1));
+	connectionLayer_ = new IncomingConnectionLayer(connection_.get());
+	streamStack_ = new StreamStack(xmppLayer_, connectionLayer_);
+}
+
+ServerFromClientSession::~ServerFromClientSession() {
+	delete streamStack_;
+	delete connectionLayer_;
+	delete xmppLayer_;
+}
+
+void ServerFromClientSession::handleElement(boost::shared_ptr<Element>) {
+}
+
+void ServerFromClientSession::handleStreamStart(const String& domain) {
+	xmppLayer_->writeHeader(domain);
+}
+
+}
diff --git a/Swiften/Server/ServerFromClientSession.h b/Swiften/Server/ServerFromClientSession.h
new file mode 100644
index 0000000..cedfcdb
--- /dev/null
+++ b/Swiften/Server/ServerFromClientSession.h
@@ -0,0 +1,41 @@
+#pragma once
+
+#include <boost/shared_ptr.hpp>
+#include <boost/signal.hpp>
+
+namespace Swift {
+	class Element;
+	class PayloadParserFactoryCollection;
+	class PayloadSerializerCollection;
+	class StreamStack;
+	class XMPPLayer;
+	class IncomingConnectionLayer;
+	class IncomingConnection;
+	class ByteArray;
+	class String;
+
+	class ServerFromClientSession {
+		public:
+			ServerFromClientSession(
+					boost::shared_ptr<IncomingConnection> connection, 
+					PayloadParserFactoryCollection* payloadParserFactories, 
+					PayloadSerializerCollection* payloadSerializers);
+			~ServerFromClientSession();
+
+			boost::signal<void()> onSessionFinished;
+			boost::signal<void (const ByteArray&)> onDataWritten;
+			boost::signal<void (const ByteArray&)> onDataRead;
+
+		private:
+			void handleElement(boost::shared_ptr<Element>);
+			void handleStreamStart(const String& domain);
+
+		private:
+			boost::shared_ptr<IncomingConnection> connection_;
+			PayloadParserFactoryCollection* payloadParserFactories_;
+			PayloadSerializerCollection* payloadSerializers_;
+			IncomingConnectionLayer* connectionLayer_;
+			StreamStack* streamStack_;
+			XMPPLayer* xmppLayer_;
+	};
+}
diff --git a/Swiften/StreamStack/IncomingConnectionLayer.h b/Swiften/StreamStack/IncomingConnectionLayer.h
new file mode 100644
index 0000000..c1a9eab
--- /dev/null
+++ b/Swiften/StreamStack/IncomingConnectionLayer.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#include <boost/signal.hpp>
+
+#include "Swiften/StreamStack/LowLayer.h"
+#include "Swiften/Network/IncomingConnection.h"
+
+namespace Swift {
+	class IncomingConnectionLayer : public LowLayer {
+		public:
+			IncomingConnectionLayer(IncomingConnection* connection) : connection_(connection) {
+				connection_->onDataRead.connect(onDataRead);
+			}
+
+			void writeData(const ByteArray& data) {
+				connection_->write(data);
+			}
+
+		private:
+			IncomingConnection* connection_;
+	};
+}
diff --git a/Swiften/StreamStack/XMPPLayer.cpp b/Swiften/StreamStack/XMPPLayer.cpp
index 7afa762..464d4b0 100644
--- a/Swiften/StreamStack/XMPPLayer.cpp
+++ b/Swiften/StreamStack/XMPPLayer.cpp
@@ -56,8 +56,8 @@ void XMPPLayer::doResetParser() {
 	resetParserAfterParse_ = false;
 }
 
-void XMPPLayer::handleStreamStart() {
-	onStreamStart();
+void XMPPLayer::handleStreamStart(const String& domain) {
+	onStreamStart(domain);
 }
 
 void XMPPLayer::handleElement(boost::shared_ptr<Element> stanza) {
diff --git a/Swiften/StreamStack/XMPPLayer.h b/Swiften/StreamStack/XMPPLayer.h
index 07b5b1c..e064d94 100644
--- a/Swiften/StreamStack/XMPPLayer.h
+++ b/Swiften/StreamStack/XMPPLayer.h
@@ -31,14 +31,14 @@ namespace Swift {
 			void resetParser();
 
 		public:
-			boost::signal<void ()> onStreamStart;
+			boost::signal<void (const String& domain)> onStreamStart;
 			boost::signal<void (boost::shared_ptr<Element>)> onElement;
 			boost::signal<void (const ByteArray&)> onWriteData;
 			boost::signal<void (const ByteArray&)> onDataRead;
 			boost::signal<void ()> onError;
 
 		private:
-			void handleStreamStart();
+			void handleStreamStart(const String&);
 			void handleElement(boost::shared_ptr<Element>);
 			void handleStreamEnd();
 
-- 
cgit v0.10.2-6-g49f6