From b6003bea740e8898127ec135e230eed421924370 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Fri, 6 Nov 2009 21:26:48 +0100
Subject: Added SessionStream.


diff --git a/Swiften/Session/BasicSessionStream.cpp b/Swiften/Session/BasicSessionStream.cpp
new file mode 100644
index 0000000..46d4e16
--- /dev/null
+++ b/Swiften/Session/BasicSessionStream.cpp
@@ -0,0 +1,72 @@
+// TODO: whitespacePingLayer_->setInactive();
+
+#include "Swiften/Session/BasicSessionStream.h"
+
+#include <boost/bind.hpp>
+
+#include "Swiften/StreamStack/XMPPLayer.h"
+#include "Swiften/StreamStack/StreamStack.h"
+#include "Swiften/StreamStack/ConnectionLayer.h"
+#include "Swiften/StreamStack/WhitespacePingLayer.h"
+#include "Swiften/StreamStack/TLSLayer.h"
+#include "Swiften/StreamStack/TLSLayerFactory.h"
+
+namespace Swift {
+
+BasicSessionStream::BasicSessionStream(boost::shared_ptr<Connection> connection, PayloadParserFactoryCollection* payloadParserFactories, PayloadSerializerCollection* payloadSerializers, TLSLayerFactory* tlsLayerFactory) : tlsLayerFactory(tlsLayerFactory) {
+	xmppLayer = boost::shared_ptr<XMPPLayer>(
+			new XMPPLayer(payloadParserFactories, payloadSerializers));
+	xmppLayer->onStreamStart.connect(boost::ref(onStreamStartReceived));
+	xmppLayer->onElement.connect(boost::ref(onElementReceived));
+	xmppLayer->onError.connect(boost::bind(
+      &BasicSessionStream::handleXMPPError, this));
+
+	connectionLayer = boost::shared_ptr<ConnectionLayer>(
+      new ConnectionLayer(connection));
+
+	streamStack = new StreamStack(xmppLayer, connectionLayer);
+}
+
+BasicSessionStream::~BasicSessionStream() {
+  delete streamStack;
+}
+
+void BasicSessionStream::writeHeader(const ProtocolHeader& header) {
+	xmppLayer->writeHeader(header);
+}
+
+void BasicSessionStream::writeElement(boost::shared_ptr<Element> element) {
+	xmppLayer->writeElement(element);
+}
+
+bool BasicSessionStream::supportsTLSEncryption() {
+  return tlsLayerFactory && tlsLayerFactory->canCreate();
+}
+
+void BasicSessionStream::addTLSEncryption() {
+	tlsLayer = tlsLayerFactory->createTLSLayer();
+  streamStack->addLayer(tlsLayer);
+  // TODO: Add tls layer certificate if needed
+  tlsLayer->onError.connect(boost::bind(&BasicSessionStream::handleTLSError, this));
+  tlsLayer->connect();
+}
+
+void BasicSessionStream::addWhitespacePing() {
+  whitespacePingLayer = boost::shared_ptr<WhitespacePingLayer>(new WhitespacePingLayer());
+  streamStack->addLayer(whitespacePingLayer);
+  whitespacePingLayer->setActive();
+}
+
+void BasicSessionStream::resetXMPPParser() {
+  xmppLayer->resetParser();
+}
+
+void BasicSessionStream::handleXMPPError() {
+  // TODO
+}
+
+void BasicSessionStream::handleTLSError() {
+  // TODO
+}
+
+};
diff --git a/Swiften/Session/BasicSessionStream.h b/Swiften/Session/BasicSessionStream.h
new file mode 100644
index 0000000..bf92bbb
--- /dev/null
+++ b/Swiften/Session/BasicSessionStream.h
@@ -0,0 +1,52 @@
+#pragma once
+
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Network/Connection.h"
+#include "Swiften/Session/SessionStream.h"
+
+namespace Swift {
+  class TLSLayerFactory;
+  class TLSLayer;
+  class WhitespacePingLayer;
+	class PayloadParserFactoryCollection;
+	class PayloadSerializerCollection;
+  class StreamStack;
+	class XMPPLayer;
+  class ConnectionLayer;
+
+  class BasicSessionStream : 
+      public SessionStream, 
+      public boost::BOOST_SIGNALS_NAMESPACE::trackable {
+    public:
+      BasicSessionStream(
+		    boost::shared_ptr<Connection> connection,
+        PayloadParserFactoryCollection* payloadParserFactories, 
+        PayloadSerializerCollection* payloadSerializers,
+		    TLSLayerFactory* tlsLayerFactory
+      );
+      ~BasicSessionStream();
+
+			virtual void writeHeader(const ProtocolHeader& header);
+			virtual void writeElement(boost::shared_ptr<Element>);
+
+			virtual bool supportsTLSEncryption();
+			virtual void addTLSEncryption();
+
+			virtual void addWhitespacePing();
+
+			virtual void resetXMPPParser();
+
+    private:
+      void handleXMPPError();
+      void handleTLSError();
+
+    private:
+			boost::shared_ptr<XMPPLayer> xmppLayer;
+			boost::shared_ptr<ConnectionLayer> connectionLayer;
+			StreamStack* streamStack;
+      TLSLayerFactory* tlsLayerFactory;
+      boost::shared_ptr<TLSLayer> tlsLayer;
+      boost::shared_ptr<WhitespacePingLayer> whitespacePingLayer;
+  };
+}
diff --git a/Swiften/Session/SessionStream.cpp b/Swiften/Session/SessionStream.cpp
new file mode 100644
index 0000000..1d73d0f
--- /dev/null
+++ b/Swiften/Session/SessionStream.cpp
@@ -0,0 +1,8 @@
+#include "Swiften/Session/SessionStream.h"
+
+namespace Swift {
+
+SessionStream::~SessionStream() {
+}
+
+};
diff --git a/Swiften/Session/SessionStream.h b/Swiften/Session/SessionStream.h
new file mode 100644
index 0000000..17d9a24
--- /dev/null
+++ b/Swiften/Session/SessionStream.h
@@ -0,0 +1,27 @@
+#pragma once
+
+#include <boost/signal.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Elements/ProtocolHeader.h"
+#include "Swiften/Elements/Element.h"
+
+namespace Swift {
+	class SessionStream {
+		public:
+			virtual ~SessionStream();
+
+			virtual void writeHeader(const ProtocolHeader& header) = 0;
+			virtual void writeElement(boost::shared_ptr<Element>) = 0;
+
+			virtual bool supportsTLSEncryption() = 0;
+			virtual void addTLSEncryption() = 0;
+
+			virtual void addWhitespacePing() = 0;
+
+			virtual void resetXMPPParser() = 0;
+
+			boost::signal<void (const ProtocolHeader&)> onStreamStartReceived;
+			boost::signal<void (boost::shared_ptr<Element>)> onElementReceived;
+	};
+}
-- 
cgit v0.10.2-6-g49f6