summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-07-15 18:07:44 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-07-15 18:07:44 (GMT)
commitf061668e3c1d5eac01b85303e2c81df2bc560e9a (patch)
treede56afe6d93cb36425f7c2e458575d27bd9105a4 /Swiften/StreamStack
parent0930cd940963be0edfe7c80b4925babca0e01443 (diff)
downloadswift-contrib-f061668e3c1d5eac01b85303e2c81df2bc560e9a.zip
swift-contrib-f061668e3c1d5eac01b85303e2c81df2bc560e9a.tar.bz2
Make stream stack layers reference counted.
Diffstat (limited to 'Swiften/StreamStack')
-rw-r--r--Swiften/StreamStack/PlatformTLSLayerFactory.cpp6
-rw-r--r--Swiften/StreamStack/PlatformTLSLayerFactory.h7
-rw-r--r--Swiften/StreamStack/StreamStack.cpp19
-rw-r--r--Swiften/StreamStack/StreamStack.h27
-rw-r--r--Swiften/StreamStack/TLSLayerFactory.h9
-rw-r--r--Swiften/StreamStack/UnitTest/StreamStackTest.cpp38
6 files changed, 55 insertions, 51 deletions
diff --git a/Swiften/StreamStack/PlatformTLSLayerFactory.cpp b/Swiften/StreamStack/PlatformTLSLayerFactory.cpp
index bb1abf1..40021ee 100644
--- a/Swiften/StreamStack/PlatformTLSLayerFactory.cpp
+++ b/Swiften/StreamStack/PlatformTLSLayerFactory.cpp
@@ -22,12 +22,12 @@ bool PlatformTLSLayerFactory::canCreate() const {
#endif
}
-TLSLayer* PlatformTLSLayerFactory::createTLSLayer() {
+boost::shared_ptr<TLSLayer> PlatformTLSLayerFactory::createTLSLayer() {
#ifdef HAVE_OPENSSL
- return new OpenSSLLayer();
+ return boost::shared_ptr<TLSLayer>(new OpenSSLLayer());
#else
assert(false);
- return 0;
+ return boost::shared_ptr<TLSLayer>();
#endif
}
diff --git a/Swiften/StreamStack/PlatformTLSLayerFactory.h b/Swiften/StreamStack/PlatformTLSLayerFactory.h
index 6ebee32..6c343b0 100644
--- a/Swiften/StreamStack/PlatformTLSLayerFactory.h
+++ b/Swiften/StreamStack/PlatformTLSLayerFactory.h
@@ -1,5 +1,4 @@
-#ifndef SWIFTEN_OpenSSLLayerFactory_H
-#define SWIFTEN_OpenSSLLayerFactory_H
+#pragma once
#include "Swiften/StreamStack/TLSLayerFactory.h"
@@ -9,8 +8,6 @@ namespace Swift {
PlatformTLSLayerFactory();
bool canCreate() const;
- virtual TLSLayer* createTLSLayer();
+ virtual boost::shared_ptr<TLSLayer> createTLSLayer();
};
}
-
-#endif
diff --git a/Swiften/StreamStack/StreamStack.cpp b/Swiften/StreamStack/StreamStack.cpp
index 7dde858..9eb9b4e 100644
--- a/Swiften/StreamStack/StreamStack.cpp
+++ b/Swiften/StreamStack/StreamStack.cpp
@@ -2,25 +2,36 @@
#include <boost/bind.hpp>
+#include "Swiften/Base/foreach.h"
#include "Swiften/StreamStack/XMPPLayer.h"
#include "Swiften/StreamStack/LowLayer.h"
#include "Swiften/StreamStack/StreamLayer.h"
namespace Swift {
-StreamStack::StreamStack(XMPPLayer* xmppLayer, LowLayer* physicalLayer) : xmppLayer_(xmppLayer), physicalLayer_(physicalLayer) {
+StreamStack::StreamStack(boost::shared_ptr<XMPPLayer> xmppLayer, boost::shared_ptr<LowLayer> physicalLayer) : xmppLayer_(xmppLayer), physicalLayer_(physicalLayer) {
xmppReadSlotConnection_ = physicalLayer_->onDataRead.connect(boost::bind(&XMPPLayer::parseData, xmppLayer_, _1));
xmppWriteSignalConnection_ = xmppLayer_->onWriteData.connect(boost::bind(&LowLayer::writeData, physicalLayer_, _1));
}
-void StreamStack::addLayer(StreamLayer* newLayer) {
+StreamStack::~StreamStack() {
+ // Disconnect the write signal connections to break cyclic signal
+ // dependencies. The read signal connections have
+ // to remain, since these can be reached from the main event loop.
+ xmppWriteSignalConnection_.disconnect();
+ foreach(const boost::bsignals::connection& connection, writeSignalConnections_) {
+ connection.disconnect();
+ }
+}
+
+void StreamStack::addLayer(boost::shared_ptr<StreamLayer> newLayer) {
xmppReadSlotConnection_.disconnect();
xmppWriteSignalConnection_.disconnect();
- LowLayer* lowLayer = (layers_.empty() ? physicalLayer_ : *layers_.rbegin());
+ boost::shared_ptr<LowLayer> lowLayer = (layers_.empty() ? physicalLayer_ : *layers_.rbegin());
lowLayer->onDataRead.connect(boost::bind(&HighLayer::handleDataRead, newLayer, _1), boost::bsignals::at_front);
- newLayer->onWriteData.connect(boost::bind(&LowLayer::writeData, lowLayer, _1), boost::bsignals::at_front);
+ writeSignalConnections_.push_back(newLayer->onWriteData.connect(boost::bind(&LowLayer::writeData, lowLayer, _1), boost::bsignals::at_front));
xmppWriteSignalConnection_ = xmppLayer_->onWriteData.connect(boost::bind(&LowLayer::writeData, newLayer, _1), boost::bsignals::at_front);
xmppReadSlotConnection_ = newLayer->onDataRead.connect(boost::bind(&XMPPLayer::parseData, xmppLayer_, _1), boost::bsignals::at_front);
layers_.push_back(newLayer);
diff --git a/Swiften/StreamStack/StreamStack.h b/Swiften/StreamStack/StreamStack.h
index bc2e89b..87c522d 100644
--- a/Swiften/StreamStack/StreamStack.h
+++ b/Swiften/StreamStack/StreamStack.h
@@ -1,5 +1,4 @@
-#ifndef SWIFTEN_STREAMSTACK_H
-#define SWIFTEN_STREAMSTACK_H
+#pragma once
#include <boost/shared_ptr.hpp>
#include <boost/signal.hpp>
@@ -15,31 +14,31 @@ namespace Swift {
class StreamStack {
public:
- StreamStack(XMPPLayer* xmppLayer, LowLayer* physicalLayer);
+ StreamStack(boost::shared_ptr<XMPPLayer> xmppLayer, boost::shared_ptr<LowLayer> physicalLayer);
+ ~StreamStack();
- void addLayer(StreamLayer*);
+ void addLayer(boost::shared_ptr<StreamLayer>);
- XMPPLayer* getXMPPLayer() const {
+ boost::shared_ptr<XMPPLayer> getXMPPLayer() const {
return xmppLayer_;
}
- template<typename T> T* getLayer() {
- foreach(StreamLayer* streamLayer, layers_) {
- T* layer = dynamic_cast<T*>(streamLayer);
+ template<typename T> boost::shared_ptr<T> getLayer() {
+ foreach(const boost::shared_ptr<StreamLayer>& streamLayer, layers_) {
+ boost::shared_ptr<T> layer = boost::dynamic_pointer_cast<T>(streamLayer);
if (layer) {
return layer;
}
}
- return 0;
+ return boost::shared_ptr<T>();
}
private:
- XMPPLayer* xmppLayer_;
- LowLayer* physicalLayer_;
- std::vector<StreamLayer*> layers_;
+ boost::shared_ptr<XMPPLayer> xmppLayer_;
+ boost::shared_ptr<LowLayer> physicalLayer_;
+ std::vector< boost::shared_ptr<StreamLayer> > layers_;
boost::bsignals::connection xmppReadSlotConnection_;
boost::bsignals::connection xmppWriteSignalConnection_;
+ std::vector< boost::bsignals::connection > writeSignalConnections_;
};
}
-
-#endif
diff --git a/Swiften/StreamStack/TLSLayerFactory.h b/Swiften/StreamStack/TLSLayerFactory.h
index b4218a5..1b0bfc1 100644
--- a/Swiften/StreamStack/TLSLayerFactory.h
+++ b/Swiften/StreamStack/TLSLayerFactory.h
@@ -1,5 +1,6 @@
-#ifndef SWIFTEN_TLSLayerFactory_H
-#define SWIFTEN_TLSLayerFactory_H
+#pragma once
+
+#include <boost/shared_ptr.hpp>
namespace Swift {
class TLSLayer;
@@ -9,8 +10,6 @@ namespace Swift {
virtual ~TLSLayerFactory();
virtual bool canCreate() const = 0;
- virtual TLSLayer* createTLSLayer() = 0;
+ virtual boost::shared_ptr<TLSLayer> createTLSLayer() = 0;
};
}
-
-#endif
diff --git a/Swiften/StreamStack/UnitTest/StreamStackTest.cpp b/Swiften/StreamStack/UnitTest/StreamStackTest.cpp
index 1f6aad7..6b97c0d 100644
--- a/Swiften/StreamStack/UnitTest/StreamStackTest.cpp
+++ b/Swiften/StreamStack/UnitTest/StreamStackTest.cpp
@@ -29,15 +29,13 @@ class StreamStackTest : public CppUnit::TestFixture
StreamStackTest() {}
void setUp() {
- physicalStream_ = new TestLowLayer();
- xmppStream_ = new XMPPLayer(&parserFactories_, &serializers_);
+ physicalStream_ = boost::shared_ptr<TestLowLayer>(new TestLowLayer());
+ xmppStream_ = boost::shared_ptr<XMPPLayer>(new XMPPLayer(&parserFactories_, &serializers_));
elementsReceived_ = 0;
dataWriteReceived_ = 0;
}
void tearDown() {
- delete physicalStream_;
- delete xmppStream_;
}
void testWriteData_NoIntermediateStreamStack() {
@@ -51,8 +49,8 @@ class StreamStackTest : public CppUnit::TestFixture
void testWriteData_OneIntermediateStream() {
StreamStack testling(xmppStream_, physicalStream_);
- std::auto_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
- testling.addLayer(xStream.get());
+ boost::shared_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
+ testling.addLayer(xStream);
xmppStream_->writeData("foo");
@@ -62,10 +60,10 @@ class StreamStackTest : public CppUnit::TestFixture
void testWriteData_TwoIntermediateStreamStack() {
StreamStack testling(xmppStream_, physicalStream_);
- std::auto_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
- std::auto_ptr<MyStreamLayer> yStream(new MyStreamLayer("Y"));
- testling.addLayer(xStream.get());
- testling.addLayer(yStream.get());
+ boost::shared_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
+ boost::shared_ptr<MyStreamLayer> yStream(new MyStreamLayer("Y"));
+ testling.addLayer(xStream);
+ testling.addLayer(yStream);
xmppStream_->writeData("foo");
@@ -85,8 +83,8 @@ class StreamStackTest : public CppUnit::TestFixture
void testReadData_OneIntermediateStream() {
StreamStack testling(xmppStream_, physicalStream_);
xmppStream_->onElement.connect(boost::bind(&StreamStackTest::handleElement, this, _1));
- std::auto_ptr<MyStreamLayer> xStream(new MyStreamLayer("<"));
- testling.addLayer(xStream.get());
+ boost::shared_ptr<MyStreamLayer> xStream(new MyStreamLayer("<"));
+ testling.addLayer(xStream);
physicalStream_->onDataRead(ByteArray("stream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>"));
@@ -96,10 +94,10 @@ class StreamStackTest : public CppUnit::TestFixture
void testReadData_TwoIntermediateStreamStack() {
StreamStack testling(xmppStream_, physicalStream_);
xmppStream_->onElement.connect(boost::bind(&StreamStackTest::handleElement, this, _1));
- std::auto_ptr<MyStreamLayer> xStream(new MyStreamLayer("s"));
- std::auto_ptr<MyStreamLayer> yStream(new MyStreamLayer("<"));
- testling.addLayer(xStream.get());
- testling.addLayer(yStream.get());
+ boost::shared_ptr<MyStreamLayer> xStream(new MyStreamLayer("s"));
+ boost::shared_ptr<MyStreamLayer> yStream(new MyStreamLayer("<"));
+ testling.addLayer(xStream);
+ testling.addLayer(yStream);
physicalStream_->onDataRead(ByteArray("tream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>"));
@@ -109,8 +107,8 @@ class StreamStackTest : public CppUnit::TestFixture
void testAddLayer_ExistingOnWriteDataSlot() {
StreamStack testling(xmppStream_, physicalStream_);
xmppStream_->onWriteData.connect(boost::bind(&StreamStackTest::handleWriteData, this, _1));
- std::auto_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
- testling.addLayer(xStream.get());
+ boost::shared_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
+ testling.addLayer(xStream);
xmppStream_->writeData("foo");
@@ -158,8 +156,8 @@ class StreamStackTest : public CppUnit::TestFixture
private:
FullPayloadParserFactoryCollection parserFactories_;
FullPayloadSerializerCollection serializers_;
- TestLowLayer* physicalStream_;
- XMPPLayer* xmppStream_;
+ boost::shared_ptr<TestLowLayer> physicalStream_;
+ boost::shared_ptr<XMPPLayer> xmppStream_;
int elementsReceived_;
int dataWriteReceived_;
};