summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-11-10 19:29:39 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-11-10 19:29:39 (GMT)
commit3914231a023e36881c2a760a3d8973ffdd2a18ad (patch)
tree7d1c2d61f1cf74f9369092f8473386dc5e9649a3
parentc682941cd230ad8caed3f3d457de3dc0cd7172d4 (diff)
downloadswift-3914231a023e36881c2a760a3d8973ffdd2a18ad.zip
swift-3914231a023e36881c2a760a3d8973ffdd2a18ad.tar.bz2
More Client refactoring.
-rw-r--r--Swiften/Client/Client.cpp14
-rw-r--r--Swiften/Client/Client.h4
-rw-r--r--Swiften/Client/ClientSession.h6
-rw-r--r--Swiften/Session/BasicSessionStream.cpp20
-rw-r--r--Swiften/Session/BasicSessionStream.h2
-rw-r--r--Swiften/Session/SessionStream.h13
6 files changed, 40 insertions, 19 deletions
diff --git a/Swiften/Client/Client.cpp b/Swiften/Client/Client.cpp
index adda6af..9fb4ca0 100644
--- a/Swiften/Client/Client.cpp
+++ b/Swiften/Client/Client.cpp
@@ -46,18 +46,18 @@ void Client::handleConnectionConnectFinished(bool error) {
if (error) {
onError(ClientError::ConnectionError);
}
else {
assert(!sessionStream_);
sessionStream_ = boost::shared_ptr<BasicSessionStream>(new BasicSessionStream(connection_, &payloadParserFactories_, &payloadSerializers_, tlsLayerFactory_));
- sessionStream_->initialize();
if (!certificate_.isEmpty()) {
sessionStream_->setTLSCertificate(PKCS12Certificate(certificate_, password_));
}
- //sessionStream_->onDataRead.connect(boost::bind(&Client::handleDataRead, this, _1));
- //sessionStream_->onDataWritten.connect(boost::bind(&Client::handleDataWritten, this, _1));
+ sessionStream_->onDataRead.connect(boost::bind(&Client::handleDataRead, shared_from_this(), _1));
+ sessionStream_->onDataWritten.connect(boost::bind(&Client::handleDataWritten, shared_from_this(), _1));
+ sessionStream_->initialize();
session_ = boost::shared_ptr<ClientSession>(new ClientSession(jid_, sessionStream_));
session_->onInitialized.connect(boost::bind(boost::ref(onConnected)));
session_->onFinished.connect(boost::bind(&Client::handleSessionFinished, shared_from_this(), _1));
session_->onNeedCredentials.connect(boost::bind(&Client::handleNeedCredentials, shared_from_this()));
session_->onElementReceived.connect(boost::bind(&Client::handleElement, shared_from_this(), _1));
@@ -161,15 +161,15 @@ void Client::handleSessionFinished(boost::shared_ptr<Error> error) {
}
void Client::handleNeedCredentials() {
session_->sendCredentials(password_);
}
-void Client::handleDataRead(const ByteArray& data) {
- onDataRead(String(data.getData(), data.getSize()));
+void Client::handleDataRead(const String& data) {
+ onDataRead(data);
}
-void Client::handleDataWritten(const ByteArray& data) {
- onDataWritten(String(data.getData(), data.getSize()));
+void Client::handleDataWritten(const String& data) {
+ onDataWritten(data);
}
}
diff --git a/Swiften/Client/Client.h b/Swiften/Client/Client.h
index 27c2458..16127dd 100644
--- a/Swiften/Client/Client.h
+++ b/Swiften/Client/Client.h
@@ -50,14 +50,14 @@ namespace Swift {
void handleConnectionConnectFinished(bool error);
void send(boost::shared_ptr<Stanza>);
virtual String getNewIQID();
void handleElement(boost::shared_ptr<Element>);
void handleSessionFinished(boost::shared_ptr<Error>);
void handleNeedCredentials();
- void handleDataRead(const ByteArray&);
- void handleDataWritten(const ByteArray&);
+ void handleDataRead(const String&);
+ void handleDataWritten(const String&);
void reset();
private:
JID jid_;
String password_;
diff --git a/Swiften/Client/ClientSession.h b/Swiften/Client/ClientSession.h
index 1b01a66..e09861b 100644
--- a/Swiften/Client/ClientSession.h
+++ b/Swiften/Client/ClientSession.h
@@ -63,15 +63,15 @@ namespace Swift {
return JID("", localJID.getDomain());
}
void sendStreamHeader();
void sendSessionStart();
- virtual void handleElement(boost::shared_ptr<Element>);
- virtual void handleStreamStart(const ProtocolHeader&);
- virtual void handleStreamError(boost::shared_ptr<Swift::Error>);
+ void handleElement(boost::shared_ptr<Element>);
+ void handleStreamStart(const ProtocolHeader&);
+ void handleStreamError(boost::shared_ptr<Swift::Error>);
void handleTLSEncrypted();
bool checkState(State);
public:
diff --git a/Swiften/Session/BasicSessionStream.cpp b/Swiften/Session/BasicSessionStream.cpp
index 115dc7c..8b14367 100644
--- a/Swiften/Session/BasicSessionStream.cpp
+++ b/Swiften/Session/BasicSessionStream.cpp
@@ -1,8 +1,6 @@
-// TODO: Send out better errors
-
#include "Swiften/Session/BasicSessionStream.h"
#include <boost/bind.hpp>
#include "Swiften/StreamStack/XMPPLayer.h"
#include "Swiften/StreamStack/StreamStack.h"
@@ -20,12 +18,14 @@ void BasicSessionStream::initialize() {
xmppLayer = boost::shared_ptr<XMPPLayer>(
new XMPPLayer(payloadParserFactories, payloadSerializers));
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, 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);
@@ -61,13 +61,13 @@ bool BasicSessionStream::supportsTLSEncryption() {
}
void BasicSessionStream::addTLSEncryption() {
assert(available);
tlsLayer = tlsLayerFactory->createTLSLayer();
if (hasTLSCertificate() && !tlsLayer->setClientCertificate(getTLSCertificate())) {
- onError(boost::shared_ptr<Error>(new Error()));
+ 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();
@@ -98,24 +98,32 @@ void BasicSessionStream::handleStreamStartReceived(const ProtocolHeader& header)
void BasicSessionStream::handleElementReceived(boost::shared_ptr<Element> element) {
onElementReceived(element);
}
void BasicSessionStream::handleXMPPError() {
available = false;
- onError(boost::shared_ptr<Error>(new Error()));
+ onError(boost::shared_ptr<Error>(new Error(Error::ParseError)));
}
void BasicSessionStream::handleTLSConnected() {
onTLSEncrypted();
}
void BasicSessionStream::handleTLSError() {
available = false;
- onError(boost::shared_ptr<Error>(new Error()));
+ 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()));
+ 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 5fe0b4c..0cb50eb 100644
--- a/Swiften/Session/BasicSessionStream.h
+++ b/Swiften/Session/BasicSessionStream.h
@@ -47,12 +47,14 @@ namespace Swift {
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;
diff --git a/Swiften/Session/SessionStream.h b/Swiften/Session/SessionStream.h
index b2444f5..6bba237 100644
--- a/Swiften/Session/SessionStream.h
+++ b/Swiften/Session/SessionStream.h
@@ -10,13 +10,22 @@
namespace Swift {
class SessionStream {
public:
class Error : public Swift::Error {
public:
- Error() {}
+ enum Type {
+ ParseError,
+ TLSError,
+ InvalidTLSCertificateError,
+ ConnectionError
+ };
+
+ Error(Type type) : type(type) {}
+
+ Type type;
};
virtual ~SessionStream();
virtual bool isAvailable() = 0;
@@ -40,12 +49,14 @@ namespace Swift {
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;
}