summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Session')
-rw-r--r--Swiften/Session/Makefile.inc2
-rw-r--r--Swiften/Session/Session.cpp72
-rw-r--r--Swiften/Session/Session.h76
3 files changed, 150 insertions, 0 deletions
diff --git a/Swiften/Session/Makefile.inc b/Swiften/Session/Makefile.inc
new file mode 100644
index 0000000..faa73c8
--- /dev/null
+++ b/Swiften/Session/Makefile.inc
@@ -0,0 +1,2 @@
+SWIFTEN_SOURCES += \
+ Swiften/Session/Session.cpp
diff --git a/Swiften/Session/Session.cpp b/Swiften/Session/Session.cpp
new file mode 100644
index 0000000..9ab8e4d
--- /dev/null
+++ b/Swiften/Session/Session.cpp
@@ -0,0 +1,72 @@
+#include "Swiften/Session/Session.h"
+
+#include <boost/bind.hpp>
+
+#include "Swiften/StreamStack/XMPPLayer.h"
+#include "Swiften/StreamStack/StreamStack.h"
+
+namespace Swift {
+
+Session::Session(
+ boost::shared_ptr<Connection> connection,
+ PayloadParserFactoryCollection* payloadParserFactories,
+ PayloadSerializerCollection* payloadSerializers) :
+ connection(connection),
+ payloadParserFactories(payloadParserFactories),
+ payloadSerializers(payloadSerializers),
+ initialized(false) {
+}
+
+Session::~Session() {
+ delete streamStack;
+}
+
+void Session::startSession() {
+ initializeStreamStack();
+ handleSessionStarted();
+}
+
+void Session::finishSession() {
+ connection->disconnect();
+ onSessionFinished(boost::optional<Error>());
+}
+
+void Session::finishSession(const Error& error) {
+ connection->disconnect();
+ onSessionFinished(boost::optional<Error>(error));
+}
+
+void Session::initializeStreamStack() {
+ xmppLayer = boost::shared_ptr<XMPPLayer>(
+ new XMPPLayer(payloadParserFactories, payloadSerializers));
+ xmppLayer->onStreamStart.connect(
+ boost::bind(&Session::handleStreamStart, this, _1));
+ xmppLayer->onElement.connect(
+ boost::bind(&Session::handleElement, this, _1));
+ xmppLayer->onError.connect(
+ boost::bind(&Session::finishSession, this, XMLError));
+ connection->onDisconnected.connect(
+ boost::bind(&Session::handleDisconnected, shared_from_this(), _1));
+ connectionLayer = boost::shared_ptr<ConnectionLayer>(new ConnectionLayer(connection));
+ streamStack = new StreamStack(xmppLayer, connectionLayer);
+}
+
+void Session::sendStanza(boost::shared_ptr<Stanza> stanza) {
+ xmppLayer->writeElement(stanza);
+}
+
+void Session::handleDisconnected(const boost::optional<Connection::Error>& connectionError) {
+ if (connectionError) {
+ finishSession(ConnectionError);
+ }
+ else {
+ finishSession();
+ }
+}
+
+void Session::setInitialized() {
+ initialized = true;
+ onSessionStarted();
+}
+
+}
diff --git a/Swiften/Session/Session.h b/Swiften/Session/Session.h
new file mode 100644
index 0000000..bf8049a
--- /dev/null
+++ b/Swiften/Session/Session.h
@@ -0,0 +1,76 @@
+#pragma once
+
+#include <boost/shared_ptr.hpp>
+#include <boost/signal.hpp>
+#include <boost/optional.hpp>
+#include <boost/enable_shared_from_this.hpp>
+
+#include "Swiften/JID/JID.h"
+#include "Swiften/Elements/Element.h"
+#include "Swiften/Network/Connection.h"
+#include "Swiften/StreamStack/ConnectionLayer.h"
+
+namespace Swift {
+ class ProtocolHeader;
+ class StreamStack;
+ class JID;
+ class Stanza;
+ class ByteArray;
+ class PayloadParserFactoryCollection;
+ class PayloadSerializerCollection;
+ class XMPPLayer;
+
+ class Session : public boost::enable_shared_from_this<Session> {
+ public:
+ enum Error {
+ ConnectionError,
+ XMLError
+ };
+
+ Session(
+ boost::shared_ptr<Connection> connection,
+ PayloadParserFactoryCollection* payloadParserFactories,
+ PayloadSerializerCollection* payloadSerializers);
+ virtual ~Session();
+
+ void startSession();
+ void finishSession();
+ void sendStanza(boost::shared_ptr<Stanza>);
+
+ boost::signal<void (boost::shared_ptr<Stanza>)> onStanzaReceived;
+ boost::signal<void ()> onSessionStarted;
+ boost::signal<void (const boost::optional<Error>&)> onSessionFinished;
+ boost::signal<void (const ByteArray&)> onDataWritten;
+ boost::signal<void (const ByteArray&)> onDataRead;
+
+ protected:
+ void finishSession(const Error&);
+
+ virtual void handleSessionStarted() {}
+ virtual void handleElement(boost::shared_ptr<Element>) = 0;
+ virtual void handleStreamStart(const ProtocolHeader&) = 0;
+
+ void initializeStreamStack();
+
+ boost::shared_ptr<XMPPLayer> getXMPPLayer() const {
+ return xmppLayer;
+ }
+
+ void setInitialized();
+ bool isInitialized() const {
+ return initialized;
+ }
+
+ private:
+ void handleDisconnected(const boost::optional<Connection::Error>& error);
+
+ private:
+ boost::shared_ptr<Connection> connection;
+ PayloadParserFactoryCollection* payloadParserFactories;
+ PayloadSerializerCollection* payloadSerializers;
+ boost::shared_ptr<XMPPLayer> xmppLayer;
+ boost::shared_ptr<ConnectionLayer> connectionLayer;
+ StreamStack* streamStack;
+ bool initialized;
+ };
+}