summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2018-08-02 09:00:25 (GMT)
committerTobias Markmann <tm@ayena.de>2018-08-02 09:00:25 (GMT)
commit091f6e520694360a0407ab0cf3bb036fb461e6e3 (patch)
tree717ff863e8cd799842e5c82fc2d833ae026c9b20
parent80f74201f0a35718642e434c58b631b238fd85df (diff)
downloadswift-091f6e520694360a0407ab0cf3bb036fb461e6e3.zip
swift-091f6e520694360a0407ab0cf3bb036fb461e6e3.tar.bz2
Have StreamStack own the top and bottom layer
Test-Information: Builds, unit tests and integration tests pass on macOS with clang 7.0 master. Change-Id: I0db411e49339ccb2301edd1a16612cb1ad2c927c
-rw-r--r--Swiften/Session/BasicSessionStream.cpp15
-rw-r--r--Swiften/Session/BasicSessionStream.h4
-rw-r--r--Swiften/Session/Session.cpp28
-rw-r--r--Swiften/Session/Session.h16
-rw-r--r--Swiften/StreamStack/StreamStack.cpp12
-rw-r--r--Swiften/StreamStack/StreamStack.h14
-rw-r--r--Swiften/StreamStack/UnitTest/StreamStackTest.cpp29
7 files changed, 56 insertions, 62 deletions
diff --git a/Swiften/Session/BasicSessionStream.cpp b/Swiften/Session/BasicSessionStream.cpp
index 3e65640..c44961d 100644
--- a/Swiften/Session/BasicSessionStream.cpp
+++ b/Swiften/Session/BasicSessionStream.cpp
@@ -32,65 +32,65 @@ BasicSessionStream::BasicSessionStream(
const TLSOptions& tlsOptions) :
available(false),
connection(connection),
tlsContextFactory(tlsContextFactory),
timerFactory(timerFactory),
tlsOptions_(tlsOptions) {
- xmppLayer = new XMPPLayer(payloadParserFactories, payloadSerializers, xmlParserFactory, streamType);
+ auto xmppLayer = std::make_unique<XMPPLayer>(payloadParserFactories, payloadSerializers, xmlParserFactory, streamType);
xmppLayer->onStreamStart.connect(boost::bind(&BasicSessionStream::handleStreamStartReceived, this, _1));
xmppLayer->onStreamEnd.connect(boost::bind(&BasicSessionStream::handleStreamEndReceived, this));
xmppLayer->onElement.connect(boost::bind(&BasicSessionStream::handleElementReceived, this, _1));
xmppLayer->onError.connect(boost::bind(&BasicSessionStream::handleXMPPError, this));
xmppLayer->onDataRead.connect(boost::bind(&BasicSessionStream::handleDataRead, this, _1));
xmppLayer->onWriteData.connect(boost::bind(&BasicSessionStream::handleDataWritten, this, _1));
connection->onDisconnected.connect(boost::bind(&BasicSessionStream::handleConnectionFinished, this, _1));
- connectionLayer = new ConnectionLayer(connection);
-
- streamStack = new StreamStack(xmppLayer, connectionLayer);
+ streamStack = std::make_unique<StreamStack>(std::move(xmppLayer), std::unique_ptr<ConnectionLayer>(new ConnectionLayer(connection)));
available = true;
}
BasicSessionStream::~BasicSessionStream() {
if (auto tlsLayer = streamStack->getLayer<TLSLayer>()) {
tlsLayer->onError.disconnect(boost::bind(&BasicSessionStream::handleTLSError, this, _1));
tlsLayer->onConnected.disconnect(boost::bind(&BasicSessionStream::handleTLSConnected, this));
}
- delete streamStack;
connection->onDisconnected.disconnect(boost::bind(&BasicSessionStream::handleConnectionFinished, this, _1));
- delete connectionLayer;
+ auto xmppLayer = streamStack->getLayer<XMPPLayer>();
xmppLayer->onStreamStart.disconnect(boost::bind(&BasicSessionStream::handleStreamStartReceived, this, _1));
xmppLayer->onStreamEnd.disconnect(boost::bind(&BasicSessionStream::handleStreamEndReceived, this));
xmppLayer->onElement.disconnect(boost::bind(&BasicSessionStream::handleElementReceived, this, _1));
xmppLayer->onError.disconnect(boost::bind(&BasicSessionStream::handleXMPPError, this));
xmppLayer->onDataRead.disconnect(boost::bind(&BasicSessionStream::handleDataRead, this, _1));
xmppLayer->onWriteData.disconnect(boost::bind(&BasicSessionStream::handleDataWritten, this, _1));
- delete xmppLayer;
}
void BasicSessionStream::writeHeader(const ProtocolHeader& header) {
assert(available);
+ auto* xmppLayer = streamStack->getLayer<XMPPLayer>();
xmppLayer->writeHeader(header);
}
void BasicSessionStream::writeElement(std::shared_ptr<ToplevelElement> element) {
assert(available);
+ auto* xmppLayer = streamStack->getLayer<XMPPLayer>();
xmppLayer->writeElement(element);
}
void BasicSessionStream::writeFooter() {
assert(available);
+ auto* xmppLayer = streamStack->getLayer<XMPPLayer>();
xmppLayer->writeFooter();
}
void BasicSessionStream::writeData(const std::string& data) {
assert(available);
+ auto* xmppLayer = streamStack->getLayer<XMPPLayer>();
xmppLayer->writeData(data);
}
void BasicSessionStream::close() {
connection->disconnect();
}
@@ -159,12 +159,13 @@ void BasicSessionStream::setWhitespacePingEnabled(bool enabled) {
else if (whitespacePingLayer) {
whitespacePingLayer->setInactive();
}
}
void BasicSessionStream::resetXMPPParser() {
+ auto* xmppLayer = streamStack->getLayer<XMPPLayer>();
xmppLayer->resetParser();
}
void BasicSessionStream::handleStreamStartReceived(const ProtocolHeader& header) {
onStreamStartReceived(header);
}
diff --git a/Swiften/Session/BasicSessionStream.h b/Swiften/Session/BasicSessionStream.h
index 472b5cc..30a7e3b 100644
--- a/Swiften/Session/BasicSessionStream.h
+++ b/Swiften/Session/BasicSessionStream.h
@@ -80,13 +80,11 @@ namespace Swift {
private:
bool available;
std::shared_ptr<Connection> connection;
TLSContextFactory* tlsContextFactory;
TimerFactory* timerFactory;
- XMPPLayer* xmppLayer;
- ConnectionLayer* connectionLayer;
- StreamStack* streamStack;
+ std::unique_ptr<StreamStack> streamStack;
TLSOptions tlsOptions_;
};
}
diff --git a/Swiften/Session/Session.cpp b/Swiften/Session/Session.cpp
index ebdb5d1..b1525b8 100644
--- a/Swiften/Session/Session.cpp
+++ b/Swiften/Session/Session.cpp
@@ -1,8 +1,8 @@
/*
- * Copyright (c) 2010-2016 Isode Limited.
+ * Copyright (c) 2010-2018 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swiften/Session/Session.h>
@@ -19,68 +19,70 @@ Session::Session(
PayloadSerializerCollection* payloadSerializers,
XMLParserFactory* xmlParserFactory) :
connection(connection),
payloadParserFactories(payloadParserFactories),
payloadSerializers(payloadSerializers),
xmlParserFactory(xmlParserFactory),
- xmppLayer(nullptr),
- connectionLayer(nullptr),
- streamStack(nullptr),
finishing(false) {
}
Session::~Session() {
- delete streamStack;
- delete connectionLayer;
- delete xmppLayer;
}
void Session::startSession() {
initializeStreamStack();
handleSessionStarted();
}
void Session::finishSession() {
if (finishing) {
return;
}
finishing = true;
- if (xmppLayer) {
+ if (auto xmppLayer = getXMPPLayer()) {
xmppLayer->writeFooter();
}
connection->disconnect();
}
void Session::finishSession(const SessionError& /*error*/) {
if (finishing) {
return;
}
finishing = true;
- if (xmppLayer) {
+ if (auto xmppLayer = getXMPPLayer()) {
xmppLayer->writeFooter();
}
connection->disconnect();
}
void Session::initializeStreamStack() {
- xmppLayer = new XMPPLayer(payloadParserFactories, payloadSerializers, xmlParserFactory, ClientStreamType);
+ auto xmppLayer = std::unique_ptr<XMPPLayer>(new XMPPLayer(payloadParserFactories, payloadSerializers, xmlParserFactory, ClientStreamType));
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));
xmppLayer->onDataRead.connect(boost::bind(boost::ref(onDataRead), _1));
xmppLayer->onWriteData.connect(boost::bind(boost::ref(onDataWritten), _1));
connection->onDisconnected.connect(
boost::bind(&Session::handleDisconnected, this, _1));
- connectionLayer = new ConnectionLayer(connection);
- streamStack = new StreamStack(xmppLayer, connectionLayer);
+ streamStack = std::unique_ptr<StreamStack>(new StreamStack(std::move(xmppLayer), std::unique_ptr<ConnectionLayer>(new ConnectionLayer(connection))));
}
+XMPPLayer* Session::getXMPPLayer() const {
+ return dynamic_cast<XMPPLayer*>(streamStack->getTopLayer());
+}
+
+StreamStack* Session::getStreamStack() const {
+ return streamStack.get();
+}
+
+
void Session::sendElement(std::shared_ptr<ToplevelElement> stanza) {
- xmppLayer->writeElement(stanza);
+ getXMPPLayer()->writeElement(stanza);
}
void Session::handleDisconnected(const boost::optional<Connection::Error>& connectionError) {
connection->onDisconnected.disconnect(
boost::bind(&Session::handleDisconnected, this, _1));
if (connectionError) {
diff --git a/Swiften/Session/Session.h b/Swiften/Session/Session.h
index 04153ec..e6a0d53 100644
--- a/Swiften/Session/Session.h
+++ b/Swiften/Session/Session.h
@@ -1,8 +1,8 @@
/*
- * Copyright (c) 2010-2017 Isode Limited.
+ * Copyright (c) 2010-2018 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
@@ -82,19 +82,14 @@ namespace Swift {
virtual void handleSessionFinished(const boost::optional<SessionError>&) {}
virtual void handleElement(std::shared_ptr<ToplevelElement>) = 0;
virtual void handleStreamStart(const ProtocolHeader&) = 0;
void initializeStreamStack();
- XMPPLayer* getXMPPLayer() const {
- return xmppLayer;
- }
-
- StreamStack* getStreamStack() const {
- return streamStack;
- }
+ XMPPLayer* getXMPPLayer() const;
+ StreamStack* getStreamStack() const;
void setFinished();
private:
void handleDisconnected(const boost::optional<Connection::Error>& error);
@@ -102,12 +97,11 @@ namespace Swift {
JID localJID;
JID remoteJID;
std::shared_ptr<Connection> connection;
PayloadParserFactoryCollection* payloadParserFactories;
PayloadSerializerCollection* payloadSerializers;
XMLParserFactory* xmlParserFactory;
- XMPPLayer* xmppLayer;
- ConnectionLayer* connectionLayer;
- StreamStack* streamStack;
+
+ std::unique_ptr<StreamStack> streamStack;
bool finishing;
};
}
diff --git a/Swiften/StreamStack/StreamStack.cpp b/Swiften/StreamStack/StreamStack.cpp
index 75fb1ce..cf80fb1 100644
--- a/Swiften/StreamStack/StreamStack.cpp
+++ b/Swiften/StreamStack/StreamStack.cpp
@@ -5,31 +5,31 @@
*/
#include <Swiften/StreamStack/StreamStack.h>
#include <boost/bind.hpp>
+#include <Swiften/StreamStack/HighLayer.h>
#include <Swiften/StreamStack/LowLayer.h>
#include <Swiften/StreamStack/StreamLayer.h>
-#include <Swiften/StreamStack/HighLayer.h>
namespace Swift {
-StreamStack::StreamStack(HighLayer* topLayer, LowLayer* bottomLayer) : topLayer_(topLayer), bottomLayer_(bottomLayer) {
- bottomLayer_->setParentLayer(topLayer_);
- topLayer_->setChildLayer(bottomLayer_);
+StreamStack::StreamStack(std::unique_ptr<HighLayer> topLayer, std::unique_ptr<LowLayer> bottomLayer) : topLayer_(std::move(topLayer)), bottomLayer_(std::move(bottomLayer)) {
+ bottomLayer_->setParentLayer(topLayer_.get());
+ topLayer_->setChildLayer(bottomLayer_.get());
}
StreamStack::~StreamStack() {
}
void StreamStack::addLayer(std::unique_ptr<StreamLayer> streamLayer) {
- LowLayer* lowLayer = layers_.empty() ? bottomLayer_ : layers_.rbegin()->get();
+ auto* lowLayer = layers_.empty() ? bottomLayer_.get() : layers_.rbegin()->get();
topLayer_->setChildLayer(streamLayer.get());
- streamLayer->setParentLayer(topLayer_);
+ streamLayer->setParentLayer(topLayer_.get());
lowLayer->setParentLayer(streamLayer.get());
streamLayer->setChildLayer(lowLayer);
layers_.emplace_back(std::move(streamLayer));
}
diff --git a/Swiften/StreamStack/StreamStack.h b/Swiften/StreamStack/StreamStack.h
index bd95811..263b1f5 100644
--- a/Swiften/StreamStack/StreamStack.h
+++ b/Swiften/StreamStack/StreamStack.h
@@ -18,31 +18,37 @@ namespace Swift {
class HighLayer;
class LowLayer;
class StreamLayer;
class SWIFTEN_API StreamStack {
public:
- StreamStack(HighLayer* topLayer, LowLayer* bottomLayer);
+ StreamStack(std::unique_ptr<HighLayer> topLayer, std::unique_ptr<LowLayer> bottomLayer);
~StreamStack();
void addLayer(std::unique_ptr<StreamLayer> /* streamLayer */);
HighLayer* getTopLayer() const {
- return topLayer_;
+ return topLayer_.get();
}
template<typename T> T* getLayer() const {
for (const auto& i : layers_) {
T* layer = dynamic_cast<T*>(i.get());
if (layer) {
return layer;
}
}
+ if (T* layer = dynamic_cast<T*>(topLayer_.get())) {
+ return layer;
+ }
+ if (T* layer = dynamic_cast<T*>(bottomLayer_.get())) {
+ return layer;
+ }
return nullptr;
}
private:
- HighLayer* topLayer_;
- LowLayer* bottomLayer_;
+ std::unique_ptr<HighLayer> topLayer_;
+ std::unique_ptr<LowLayer> bottomLayer_;
std::vector<std::unique_ptr<StreamLayer>> layers_;
};
}
diff --git a/Swiften/StreamStack/UnitTest/StreamStackTest.cpp b/Swiften/StreamStack/UnitTest/StreamStackTest.cpp
index 0b520f1..b074736 100644
--- a/Swiften/StreamStack/UnitTest/StreamStackTest.cpp
+++ b/Swiften/StreamStack/UnitTest/StreamStackTest.cpp
@@ -36,94 +36,86 @@ class StreamStackTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testReadData_TwoIntermediateStreamStack);
CPPUNIT_TEST(testAddLayer_ExistingOnWriteDataSlot);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {
- physicalStream_ = new TestLowLayer();
- xmppStream_ = new XMPPLayer(&parserFactories_, &serializers_, &xmlParserFactory_, ClientStreamType);
+ testling_ = std::make_unique<StreamStack>(std::make_unique<XMPPLayer>(&parserFactories_, &serializers_, &xmlParserFactory_, ClientStreamType), std::make_unique<TestLowLayer>());
+ physicalStream_ = testling_->getLayer<TestLowLayer>();
+ xmppStream_ = testling_->getLayer<XMPPLayer>();
elementsReceived_ = 0;
dataWriteReceived_ = 0;
}
void tearDown() {
- delete physicalStream_;
- delete xmppStream_;
}
void testWriteData_NoIntermediateStreamStack() {
- StreamStack testling(xmppStream_, physicalStream_);
xmppStream_->writeData("foo");
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), physicalStream_->data_.size());
CPPUNIT_ASSERT_EQUAL(createSafeByteArray("foo"), physicalStream_->data_[0]);
}
void testWriteData_OneIntermediateStream() {
- StreamStack testling(xmppStream_, physicalStream_);
std::unique_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
- testling.addLayer(std::move(xStream));
+ testling_->addLayer(std::move(xStream));
xmppStream_->writeData("foo");
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), physicalStream_->data_.size());
CPPUNIT_ASSERT_EQUAL(createSafeByteArray("Xfoo"), physicalStream_->data_[0]);
}
void testWriteData_TwoIntermediateStreamStack() {
- StreamStack testling(xmppStream_, physicalStream_);
std::unique_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
std::unique_ptr<MyStreamLayer> yStream(new MyStreamLayer("Y"));
- testling.addLayer(std::move(xStream));
- testling.addLayer(std::move(yStream));
+ testling_->addLayer(std::move(xStream));
+ testling_->addLayer(std::move(yStream));
xmppStream_->writeData("foo");
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), physicalStream_->data_.size());
CPPUNIT_ASSERT_EQUAL(createSafeByteArray("XYfoo"), physicalStream_->data_[0]);
}
void testReadData_NoIntermediateStreamStack() {
- StreamStack testling(xmppStream_, physicalStream_);
xmppStream_->onElement.connect(boost::bind(&StreamStackTest::handleElement, this, _1));
physicalStream_->onDataRead(createSafeByteArray("<stream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>"));
CPPUNIT_ASSERT_EQUAL(1, elementsReceived_);
}
void testReadData_OneIntermediateStream() {
- StreamStack testling(xmppStream_, physicalStream_);
xmppStream_->onElement.connect(boost::bind(&StreamStackTest::handleElement, this, _1));
std::unique_ptr<MyStreamLayer> xStream(new MyStreamLayer("<"));
- testling.addLayer(std::move(xStream));
+ testling_->addLayer(std::move(xStream));
physicalStream_->onDataRead(createSafeByteArray("stream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>"));
CPPUNIT_ASSERT_EQUAL(1, elementsReceived_);
}
void testReadData_TwoIntermediateStreamStack() {
- StreamStack testling(xmppStream_, physicalStream_);
xmppStream_->onElement.connect(boost::bind(&StreamStackTest::handleElement, this, _1));
std::unique_ptr<MyStreamLayer> xStream(new MyStreamLayer("s"));
std::unique_ptr<MyStreamLayer> yStream(new MyStreamLayer("<"));
- testling.addLayer(std::move(xStream));
- testling.addLayer(std::move(yStream));
+ testling_->addLayer(std::move(xStream));
+ testling_->addLayer(std::move(yStream));
physicalStream_->onDataRead(createSafeByteArray("tream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>"));
CPPUNIT_ASSERT_EQUAL(1, elementsReceived_);
}
void testAddLayer_ExistingOnWriteDataSlot() {
- StreamStack testling(xmppStream_, physicalStream_);
xmppStream_->onWriteData.connect(boost::bind(&StreamStackTest::handleWriteData, this, _1));
std::unique_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
- testling.addLayer(std::move(xStream));
+ testling_->addLayer(std::move(xStream));
xmppStream_->writeData("foo");
CPPUNIT_ASSERT_EQUAL(1, dataWriteReceived_);
}
@@ -173,11 +165,12 @@ class StreamStackTest : public CppUnit::TestFixture {
private:
FullPayloadParserFactoryCollection parserFactories_;
FullPayloadSerializerCollection serializers_;
TestLowLayer* physicalStream_;
PlatformXMLParserFactory xmlParserFactory_;
XMPPLayer* xmppStream_;
+ std::unique_ptr<StreamStack> testling_;
int elementsReceived_;
int dataWriteReceived_;
};
CPPUNIT_TEST_SUITE_REGISTRATION(StreamStackTest);