summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-11-10 21:24:03 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-11-10 21:24:03 (GMT)
commit54781ce12f7654f8136e645d4ebc5934d90c6bea (patch)
tree90bad869f9f64d57a3c0af209b83a538a47c7762 /Swiften/Session
parentfcfac59db5cb4503554f2b30854b2e91928296f6 (diff)
parent66ced3654ad295478b33d3e4f1716f66ab4048b5 (diff)
downloadswift-contrib-54781ce12f7654f8136e645d4ebc5934d90c6bea.zip
swift-contrib-54781ce12f7654f8136e645d4ebc5934d90c6bea.tar.bz2
Refactored session management.
Diffstat (limited to 'Swiften/Session')
-rw-r--r--Swiften/Session/BasicSessionStream.cpp89
-rw-r--r--Swiften/Session/BasicSessionStream.h22
-rw-r--r--Swiften/Session/SessionStream.h43
3 files changed, 133 insertions, 21 deletions
diff --git a/Swiften/Session/BasicSessionStream.cpp b/Swiften/Session/BasicSessionStream.cpp
index 46d4e16..8b14367 100644
--- a/Swiften/Session/BasicSessionStream.cpp
+++ b/Swiften/Session/BasicSessionStream.cpp
@@ -1,5 +1,3 @@
-// TODO: whitespacePingLayer_->setInactive();
-
#include "Swiften/Session/BasicSessionStream.h"
#include <boost/bind.hpp>
@@ -13,18 +11,26 @@
namespace Swift {
-BasicSessionStream::BasicSessionStream(boost::shared_ptr<Connection> connection, PayloadParserFactoryCollection* payloadParserFactories, PayloadSerializerCollection* payloadSerializers, TLSLayerFactory* tlsLayerFactory) : tlsLayerFactory(tlsLayerFactory) {
+BasicSessionStream::BasicSessionStream(boost::shared_ptr<Connection> connection, PayloadParserFactoryCollection* payloadParserFactories, PayloadSerializerCollection* payloadSerializers, TLSLayerFactory* tlsLayerFactory) : available(false), connection(connection), payloadParserFactories(payloadParserFactories), payloadSerializers(payloadSerializers), tlsLayerFactory(tlsLayerFactory) {
+}
+
+void BasicSessionStream::initialize() {
xmppLayer = boost::shared_ptr<XMPPLayer>(
new XMPPLayer(payloadParserFactories, payloadSerializers));
- xmppLayer->onStreamStart.connect(boost::ref(onStreamStartReceived));
- xmppLayer->onElement.connect(boost::ref(onElementReceived));
+ xmppLayer->onStreamStart.connect(boost::bind(&BasicSessionStream::handleStreamStartReceived, shared_from_this(), _1));
+ xmppLayer->onElement.connect(boost::bind(&BasicSessionStream::handleElementReceived, shared_from_this(), _1));
xmppLayer->onError.connect(boost::bind(
- &BasicSessionStream::handleXMPPError, this));
+ &BasicSessionStream::handleXMPPError, shared_from_this()));
+ xmppLayer->onDataRead.connect(boost::bind(&BasicSessionStream::handleDataRead, shared_from_this(), _1));
+ xmppLayer->onWriteData.connect(boost::bind(&BasicSessionStream::handleDataWritten, shared_from_this(), _1));
+ connection->onDisconnected.connect(boost::bind(&BasicSessionStream::handleConnectionError, shared_from_this(), _1));
connectionLayer = boost::shared_ptr<ConnectionLayer>(
new ConnectionLayer(connection));
streamStack = new StreamStack(xmppLayer, connectionLayer);
+
+ available = true;
}
BasicSessionStream::~BasicSessionStream() {
@@ -32,41 +38,92 @@ BasicSessionStream::~BasicSessionStream() {
}
void BasicSessionStream::writeHeader(const ProtocolHeader& header) {
+ assert(available);
xmppLayer->writeHeader(header);
}
void BasicSessionStream::writeElement(boost::shared_ptr<Element> element) {
+ assert(available);
xmppLayer->writeElement(element);
}
+void BasicSessionStream::writeFooter() {
+ assert(available);
+ xmppLayer->writeFooter();
+}
+
+bool BasicSessionStream::isAvailable() {
+ return available;
+}
+
bool BasicSessionStream::supportsTLSEncryption() {
return tlsLayerFactory && tlsLayerFactory->canCreate();
}
void BasicSessionStream::addTLSEncryption() {
+ assert(available);
tlsLayer = tlsLayerFactory->createTLSLayer();
- streamStack->addLayer(tlsLayer);
- // TODO: Add tls layer certificate if needed
- tlsLayer->onError.connect(boost::bind(&BasicSessionStream::handleTLSError, this));
- tlsLayer->connect();
+ if (hasTLSCertificate() && !tlsLayer->setClientCertificate(getTLSCertificate())) {
+ onError(boost::shared_ptr<Error>(new Error(Error::InvalidTLSCertificateError)));
+ }
+ else {
+ streamStack->addLayer(tlsLayer);
+ tlsLayer->onError.connect(boost::bind(&BasicSessionStream::handleTLSError, shared_from_this()));
+ tlsLayer->onConnected.connect(boost::bind(&BasicSessionStream::handleTLSConnected, shared_from_this()));
+ tlsLayer->connect();
+ }
}
-void BasicSessionStream::addWhitespacePing() {
- whitespacePingLayer = boost::shared_ptr<WhitespacePingLayer>(new WhitespacePingLayer());
- streamStack->addLayer(whitespacePingLayer);
- whitespacePingLayer->setActive();
+void BasicSessionStream::setWhitespacePingEnabled(bool enabled) {
+ if (enabled && !whitespacePingLayer) {
+ whitespacePingLayer = boost::shared_ptr<WhitespacePingLayer>(new WhitespacePingLayer());
+ streamStack->addLayer(whitespacePingLayer);
+ }
+ if (enabled) {
+ whitespacePingLayer->setActive();
+ }
+ else {
+ whitespacePingLayer->setInactive();
+ }
}
void BasicSessionStream::resetXMPPParser() {
xmppLayer->resetParser();
}
+void BasicSessionStream::handleStreamStartReceived(const ProtocolHeader& header) {
+ onStreamStartReceived(header);
+}
+
+void BasicSessionStream::handleElementReceived(boost::shared_ptr<Element> element) {
+ onElementReceived(element);
+}
+
void BasicSessionStream::handleXMPPError() {
- // TODO
+ available = false;
+ onError(boost::shared_ptr<Error>(new Error(Error::ParseError)));
+}
+
+void BasicSessionStream::handleTLSConnected() {
+ onTLSEncrypted();
}
void BasicSessionStream::handleTLSError() {
- // TODO
+ available = false;
+ onError(boost::shared_ptr<Error>(new Error(Error::TLSError)));
+}
+
+void BasicSessionStream::handleConnectionError(const boost::optional<Connection::Error>&) {
+ available = false;
+ onError(boost::shared_ptr<Error>(new Error(Error::ConnectionError)));
+}
+
+void BasicSessionStream::handleDataRead(const ByteArray& data) {
+ onDataRead(String(data.getData(), data.getSize()));
+}
+
+void BasicSessionStream::handleDataWritten(const ByteArray& data) {
+ onDataWritten(String(data.getData(), data.getSize()));
}
};
diff --git a/Swiften/Session/BasicSessionStream.h b/Swiften/Session/BasicSessionStream.h
index bf92bbb..0cb50eb 100644
--- a/Swiften/Session/BasicSessionStream.h
+++ b/Swiften/Session/BasicSessionStream.h
@@ -1,6 +1,7 @@
#pragma once
#include <boost/shared_ptr.hpp>
+#include <boost/enable_shared_from_this.hpp>
#include "Swiften/Network/Connection.h"
#include "Swiften/Session/SessionStream.h"
@@ -17,7 +18,7 @@ namespace Swift {
class BasicSessionStream :
public SessionStream,
- public boost::BOOST_SIGNALS_NAMESPACE::trackable {
+ public boost::enable_shared_from_this<BasicSessionStream> {
public:
BasicSessionStream(
boost::shared_ptr<Connection> connection,
@@ -27,25 +28,40 @@ namespace Swift {
);
~BasicSessionStream();
+ void initialize();
+
+ virtual bool isAvailable();
+
virtual void writeHeader(const ProtocolHeader& header);
virtual void writeElement(boost::shared_ptr<Element>);
+ virtual void writeFooter();
virtual bool supportsTLSEncryption();
virtual void addTLSEncryption();
- virtual void addWhitespacePing();
+ virtual void setWhitespacePingEnabled(bool);
virtual void resetXMPPParser();
private:
+ void handleConnectionError(const boost::optional<Connection::Error>& error);
void handleXMPPError();
+ void handleTLSConnected();
void handleTLSError();
+ void handleStreamStartReceived(const ProtocolHeader&);
+ void handleElementReceived(boost::shared_ptr<Element>);
+ void handleDataRead(const ByteArray& data);
+ void handleDataWritten(const ByteArray& data);
private:
+ bool available;
+ boost::shared_ptr<Connection> connection;
+ PayloadParserFactoryCollection* payloadParserFactories;
+ PayloadSerializerCollection* payloadSerializers;
+ TLSLayerFactory* tlsLayerFactory;
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.h b/Swiften/Session/SessionStream.h
index 17d9a24..6bba237 100644
--- a/Swiften/Session/SessionStream.h
+++ b/Swiften/Session/SessionStream.h
@@ -5,23 +5,62 @@
#include "Swiften/Elements/ProtocolHeader.h"
#include "Swiften/Elements/Element.h"
+#include "Swiften/Base/Error.h"
+#include "Swiften/TLS/PKCS12Certificate.h"
namespace Swift {
class SessionStream {
public:
+ class Error : public Swift::Error {
+ public:
+ enum Type {
+ ParseError,
+ TLSError,
+ InvalidTLSCertificateError,
+ ConnectionError
+ };
+
+ Error(Type type) : type(type) {}
+
+ Type type;
+ };
+
virtual ~SessionStream();
+ virtual bool isAvailable() = 0;
+
virtual void writeHeader(const ProtocolHeader& header) = 0;
+ virtual void writeFooter() = 0;
virtual void writeElement(boost::shared_ptr<Element>) = 0;
virtual bool supportsTLSEncryption() = 0;
virtual void addTLSEncryption() = 0;
-
- virtual void addWhitespacePing() = 0;
+ virtual void setWhitespacePingEnabled(bool enabled) = 0;
virtual void resetXMPPParser() = 0;
+ void setTLSCertificate(const PKCS12Certificate& cert) {
+ certificate = cert;
+ }
+
+ virtual bool hasTLSCertificate() {
+ return !certificate.isNull();
+ }
+
+
boost::signal<void (const ProtocolHeader&)> onStreamStartReceived;
boost::signal<void (boost::shared_ptr<Element>)> onElementReceived;
+ boost::signal<void (boost::shared_ptr<Error>)> onError;
+ boost::signal<void ()> onTLSEncrypted;
+ boost::signal<void (const String&)> onDataRead;
+ boost::signal<void (const String&)> onDataWritten;
+
+ protected:
+ const PKCS12Certificate& getTLSCertificate() const {
+ return certificate;
+ }
+
+ private:
+ PKCS12Certificate certificate;
};
}