diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-07-19 15:25:29 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-07-19 15:25:29 (GMT) |
commit | 74e666956ab3b0150ae63885cdf56efae6ca05b3 (patch) | |
tree | 305639f148ec831c462068c0fc0b936622a38f1a /Swiften/LinkLocal | |
parent | d66658252e70abfc2d4eb7cf5f694ba5dc824291 (diff) | |
download | swift-74e666956ab3b0150ae63885cdf56efae6ca05b3.zip swift-74e666956ab3b0150ae63885cdf56efae6ca05b3.tar.bz2 |
Factored out common link local session code.
Diffstat (limited to 'Swiften/LinkLocal')
-rw-r--r-- | Swiften/LinkLocal/IncomingLinkLocalSession.cpp | 25 | ||||
-rw-r--r-- | Swiften/LinkLocal/IncomingLinkLocalSession.h | 14 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalConnector.cpp | 57 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalConnector.h | 59 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalSession.cpp | 64 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalSession.h | 85 | ||||
-rw-r--r-- | Swiften/LinkLocal/Makefile.inc | 3 | ||||
-rw-r--r-- | Swiften/LinkLocal/OutgoingLinkLocalSession.cpp | 85 | ||||
-rw-r--r-- | Swiften/LinkLocal/OutgoingLinkLocalSession.h | 29 |
9 files changed, 139 insertions, 282 deletions
diff --git a/Swiften/LinkLocal/IncomingLinkLocalSession.cpp b/Swiften/LinkLocal/IncomingLinkLocalSession.cpp index b73e979..a1fb901 100644 --- a/Swiften/LinkLocal/IncomingLinkLocalSession.cpp +++ b/Swiften/LinkLocal/IncomingLinkLocalSession.cpp @@ -18,23 +18,17 @@ IncomingLinkLocalSession::IncomingLinkLocalSession( boost::shared_ptr<Connection> connection, PayloadParserFactoryCollection* payloadParserFactories, PayloadSerializerCollection* payloadSerializers) : - LinkLocalSession( - localJID, - connection, - payloadParserFactories, - payloadSerializers) { -} - -void IncomingLinkLocalSession::start() { - initializeStreamStack(); + Session(connection, payloadParserFactories, payloadSerializers) { + setLocalJID(localJID); } void IncomingLinkLocalSession::handleStreamStart(const ProtocolHeader& incomingHeader) { - remoteJID_ = JID(incomingHeader.getFrom()); - if (!remoteJID_.isValid()) { + setRemoteJID(JID(incomingHeader.getFrom())); + if (!getRemoteJID().isValid()) { finishSession(); return; } + ProtocolHeader header; header.setFrom(getLocalJID()); getXMPPLayer()->writeHeader(header); @@ -55,14 +49,7 @@ void IncomingLinkLocalSession::handleElement(boost::shared_ptr<Element> element) setInitialized(); } - if (isInitialized()) { - if (stanza) { - onElementReceived(stanza); - } - else { - std::cerr << "Received unexpected element" << std::endl; - } - } + onElementReceived(element); } diff --git a/Swiften/LinkLocal/IncomingLinkLocalSession.h b/Swiften/LinkLocal/IncomingLinkLocalSession.h index d4fa91a..2f1a45b 100644 --- a/Swiften/LinkLocal/IncomingLinkLocalSession.h +++ b/Swiften/LinkLocal/IncomingLinkLocalSession.h @@ -3,7 +3,7 @@ #include <boost/shared_ptr.hpp> #include <boost/signal.hpp> -#include "Swiften/LinkLocal/LinkLocalSession.h" +#include "Swiften/Session/Session.h" #include "Swiften/JID/JID.h" #include "Swiften/Network/Connection.h" @@ -14,7 +14,7 @@ namespace Swift { class PayloadParserFactoryCollection; class PayloadSerializerCollection; - class IncomingLinkLocalSession : public LinkLocalSession { + class IncomingLinkLocalSession : public Session { public: IncomingLinkLocalSession( const JID& localJID, @@ -22,18 +22,8 @@ namespace Swift { PayloadParserFactoryCollection* payloadParserFactories, PayloadSerializerCollection* payloadSerializers); - const JID& getRemoteJID() const { - return remoteJID_; - } - - void start(); - private: void handleElement(boost::shared_ptr<Element>); void handleStreamStart(const ProtocolHeader&); - - private: - bool initialized_; - JID remoteJID_; }; } diff --git a/Swiften/LinkLocal/LinkLocalConnector.cpp b/Swiften/LinkLocal/LinkLocalConnector.cpp new file mode 100644 index 0000000..af521b0 --- /dev/null +++ b/Swiften/LinkLocal/LinkLocalConnector.cpp @@ -0,0 +1,57 @@ +#include "Swiften/LinkLocal/LinkLocalConnector.h" + +#include <boost/bind.hpp> + +#include "Swiften/Network/Connection.h" +#include "Swiften/Network/ConnectionFactory.h" +#include "Swiften/Network/HostAddress.h" +#include "Swiften/Network/HostAddressPort.h" +#include "Swiften/LinkLocal/DNSSDService.h" + +namespace Swift { + +LinkLocalConnector::LinkLocalConnector( + const JID& remoteJID, + const String& hostname, + int port, + boost::shared_ptr<DNSSDService> resolver, + boost::shared_ptr<Connection> connection) : + remoteJID_(remoteJID), + hostname_(hostname), + port_(port), + resolver_(resolver), + connection_(connection), + resolving_(false) { +} + +void LinkLocalConnector::connect() { + resolving_ = true; + resolver_->onHostnameResolved.connect(boost::bind(&LinkLocalConnector::handleHostnameResolved, boost::dynamic_pointer_cast<LinkLocalConnector>(shared_from_this()), _1, _2)); + resolver_->resolveHostname(hostname_); +} + +void LinkLocalConnector::handleHostnameResolved(const String& hostname, const boost::optional<HostAddress>& address) { + if (resolving_) { + if (hostname == hostname_) { + resolving_ = false; + if (address) { + connection_->onConnectFinished.connect(boost::bind(boost::ref(onConnectFinished), _1)); + connection_->connect(HostAddressPort(*address, port_)); + } + else { + onConnectFinished(false); + } + } + } +} + +void LinkLocalConnector::handleConnected(bool error) { + onConnectFinished(error); +} + +void LinkLocalConnector::queueElement(boost::shared_ptr<Element> element) { + queuedElements_.push_back(element); +} + + +} diff --git a/Swiften/LinkLocal/LinkLocalConnector.h b/Swiften/LinkLocal/LinkLocalConnector.h new file mode 100644 index 0000000..d296804 --- /dev/null +++ b/Swiften/LinkLocal/LinkLocalConnector.h @@ -0,0 +1,59 @@ +#pragma once + +#include <boost/shared_ptr.hpp> +#include <boost/signal.hpp> +#include <boost/enable_shared_from_this.hpp> +#include <vector> + +#include "Swiften/JID/JID.h" +#include "Swiften/Network/Connection.h" + +namespace Swift { + class ConnectionFactory; + class HostAddress; + class String; + class Element; + class PayloadParserFactoryCollection; + class PayloadSerializerCollection; + class DNSSDService; + + class LinkLocalConnector : public boost::enable_shared_from_this<LinkLocalConnector> { + public: + LinkLocalConnector( + const JID& remoteJID, + const String& hostname, + int port, + boost::shared_ptr<DNSSDService> resolver, + boost::shared_ptr<Connection> connection); + + const JID& getRemoteJID() const { + return remoteJID_; + } + + void connect(); + void queueElement(boost::shared_ptr<Element> element); + + const std::vector<boost::shared_ptr<Element> >& getQueuedElements() const { + return queuedElements_; + } + + boost::shared_ptr<Connection> getConnection() const { + return connection_; + } + + boost::signal<void (bool)> onConnectFinished; + + private: + void handleHostnameResolved(const String& hostname, const boost::optional<HostAddress>& address); + void handleConnected(bool error); + + private: + JID remoteJID_; + String hostname_; + int port_; + boost::shared_ptr<DNSSDService> resolver_; + boost::shared_ptr<Connection> connection_; + bool resolving_; + std::vector<boost::shared_ptr<Element> > queuedElements_; + }; +} diff --git a/Swiften/LinkLocal/LinkLocalSession.cpp b/Swiften/LinkLocal/LinkLocalSession.cpp deleted file mode 100644 index 60227a7..0000000 --- a/Swiften/LinkLocal/LinkLocalSession.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "Swiften/LinkLocal/LinkLocalSession.h" - -#include <boost/bind.hpp> - -#include "Swiften/StreamStack/XMPPLayer.h" -#include "Swiften/StreamStack/StreamStack.h" - -namespace Swift { - -LinkLocalSession::LinkLocalSession( - const JID& localJID, - boost::shared_ptr<Connection> connection, - PayloadParserFactoryCollection* payloadParserFactories, - PayloadSerializerCollection* payloadSerializers) : - localJID(localJID), - payloadParserFactories(payloadParserFactories), - payloadSerializers(payloadSerializers), - connection(connection), - initialized(false) { -} - -LinkLocalSession::~LinkLocalSession() { - delete streamStack; -} - -void LinkLocalSession::initializeStreamStack() { - assert(connection); - xmppLayer = boost::shared_ptr<XMPPLayer>( - new XMPPLayer(payloadParserFactories, payloadSerializers)); - xmppLayer->onStreamStart.connect( - boost::bind(&LinkLocalSession::handleStreamStart, this, _1)); - xmppLayer->onElement.connect( - boost::bind(&LinkLocalSession::handleElement, this, _1)); - //xmppLayer->onError.connect( - // boost::bind(&LinkLocalSession::setError, this, XMLError)); - connection->onDisconnected.connect( - boost::bind(&LinkLocalSession::handleDisconnected, shared_from_this(), _1)); - connectionLayer = boost::shared_ptr<ConnectionLayer>(new ConnectionLayer(connection)); - streamStack = new StreamStack(xmppLayer, connectionLayer); -} - -void LinkLocalSession::finishSession() { - connection->disconnect(); -} - -void LinkLocalSession::sendElement(boost::shared_ptr<Element> stanza) { - xmppLayer->writeElement(stanza); -} - -void LinkLocalSession::handleDisconnected(const boost::optional<Connection::Error>& connectionError) { - if (connectionError) { - onSessionFinished(boost::optional<Error>(ConnectionError)); - } - else { - onSessionFinished(boost::optional<Error>()); - } -} - -void LinkLocalSession::setInitialized() { - initialized = true; - onSessionStarted(); -} - -} diff --git a/Swiften/LinkLocal/LinkLocalSession.h b/Swiften/LinkLocal/LinkLocalSession.h deleted file mode 100644 index 4bec14d..0000000 --- a/Swiften/LinkLocal/LinkLocalSession.h +++ /dev/null @@ -1,85 +0,0 @@ -#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 LinkLocalSession : - public boost::enable_shared_from_this<LinkLocalSession> { - public: - enum Error { ConnectionError, UnknownError }; - - LinkLocalSession( - const JID& localJID, - boost::shared_ptr<Connection> connection, - PayloadParserFactoryCollection* payloadParserFactories, - PayloadSerializerCollection* payloadSerializers); - virtual ~LinkLocalSession(); - - void finishSession(); - - // TODO: Make non-virtual when OutgoingSession is fixed - virtual void sendElement(boost::shared_ptr<Element>); - - virtual const JID& getRemoteJID() const = 0; - - virtual void start() = 0; - - boost::signal<void (boost::shared_ptr<Element>)> onElementReceived; - boost::signal<void (boost::optional<Error>)> onSessionFinished; - boost::signal<void ()> onSessionStarted; - boost::signal<void (const ByteArray&)> onDataWritten; - boost::signal<void (const ByteArray&)> onDataRead; - - protected: - void initializeStreamStack(); - - const JID& getLocalJID() const { - return localJID; - } - - boost::shared_ptr<XMPPLayer> getXMPPLayer() const { - return xmppLayer; - } - - // TODO: Remove later - void setConnection(boost::shared_ptr<Connection> c) { - connection = c; - } - - virtual void handleElement(boost::shared_ptr<Element>) = 0; - virtual void handleStreamStart(const ProtocolHeader&) = 0; - - void setInitialized(); - bool isInitialized() const { return initialized; } - - private: - void handleDisconnected(const boost::optional<Connection::Error>& error); - - private: - JID localJID; - PayloadParserFactoryCollection* payloadParserFactories; - PayloadSerializerCollection* payloadSerializers; - boost::shared_ptr<Connection> connection; - boost::shared_ptr<XMPPLayer> xmppLayer; - boost::shared_ptr<ConnectionLayer> connectionLayer; - StreamStack* streamStack; - bool initialized; - }; -} diff --git a/Swiften/LinkLocal/Makefile.inc b/Swiften/LinkLocal/Makefile.inc index 1d29844..c24f2b8 100644 --- a/Swiften/LinkLocal/Makefile.inc +++ b/Swiften/LinkLocal/Makefile.inc @@ -4,8 +4,7 @@ SWIFTEN_SOURCES += \ Swiften/LinkLocal/LinkLocalServiceInfo.cpp \ Swiften/LinkLocal/IncomingLinkLocalSession.cpp \ Swiften/LinkLocal/OutgoingLinkLocalSession.cpp \ - Swiften/LinkLocal/LinkLocalSession.cpp - + Swiften/LinkLocal/LinkLocalConnector.cpp ifeq ($(MACOSX),1) SWIFTEN_SOURCES += \ diff --git a/Swiften/LinkLocal/OutgoingLinkLocalSession.cpp b/Swiften/LinkLocal/OutgoingLinkLocalSession.cpp index 7415174..4118e34 100644 --- a/Swiften/LinkLocal/OutgoingLinkLocalSession.cpp +++ b/Swiften/LinkLocal/OutgoingLinkLocalSession.cpp @@ -1,105 +1,38 @@ -// TODO: Send back errors if we can't make a connection - #include "Swiften/LinkLocal/OutgoingLinkLocalSession.h" #include <boost/bind.hpp> #include "Swiften/Elements/ProtocolHeader.h" -#include "Swiften/Network/Connection.h" -#include "Swiften/Network/ConnectionFactory.h" -#include "Swiften/Network/HostAddress.h" -#include "Swiften/Network/HostAddressPort.h" -#include "Swiften/StreamStack/StreamStack.h" -#include "Swiften/LinkLocal/DNSSDService.h" -#include "Swiften/StreamStack/ConnectionLayer.h" -#include "Swiften/StreamStack/XMPPLayer.h" #include "Swiften/Elements/StreamFeatures.h" #include "Swiften/Elements/IQ.h" -#include "Swiften/SASL/PLAINMessage.h" namespace Swift { OutgoingLinkLocalSession::OutgoingLinkLocalSession( const JID& localJID, const JID& remoteJID, - const String& hostname, - int port, - boost::shared_ptr<DNSSDService> resolver, + boost::shared_ptr<Connection> connection, PayloadParserFactoryCollection* payloadParserFactories, - PayloadSerializerCollection* payloadSerializers, - ConnectionFactory* connectionFactory) : - LinkLocalSession( - localJID, - boost::shared_ptr<Connection>(), - payloadParserFactories, - payloadSerializers), - resolving_(false), - remoteJID_(remoteJID), - hostname_(hostname), - port_(port), - resolver_(resolver), - connectionFactory_(connectionFactory) { -} - -void OutgoingLinkLocalSession::start() { - resolving_ = true; - resolver_->onHostnameResolved.connect(boost::bind(&OutgoingLinkLocalSession::handleHostnameResolved, boost::dynamic_pointer_cast<OutgoingLinkLocalSession>(shared_from_this()), _1, _2)); - resolver_->resolveHostname(hostname_); -} - -void OutgoingLinkLocalSession::handleHostnameResolved(const String& hostname, const boost::optional<HostAddress>& address) { - if (resolving_) { - if (hostname == hostname_) { - resolving_ = false; - if (address) { - boost::shared_ptr<Connection> connection = connectionFactory_->createConnection(); - setConnection(connection); - initializeStreamStack(); - connection->onConnectFinished.connect(boost::bind(&OutgoingLinkLocalSession::handleConnected, boost::dynamic_pointer_cast<OutgoingLinkLocalSession>(shared_from_this()), _1)); - connection->connect(HostAddressPort(*address, port_)); - } - else { - onSessionFinished(boost::optional<Error>(UnknownError)); - } - } - } -} - -void OutgoingLinkLocalSession::handleConnected(bool error) { - if (!error) { - ProtocolHeader header; - header.setFrom(getLocalJID()); - getXMPPLayer()->writeHeader(header); - } - else { - // TODO: Error - } + PayloadSerializerCollection* payloadSerializers) : + Session(connection, payloadParserFactories, payloadSerializers) { + setLocalJID(localJID); + setRemoteJID(remoteJID); } void OutgoingLinkLocalSession::handleStreamStart(const ProtocolHeader&) { foreach(const boost::shared_ptr<Element>& stanza, queuedElements_) { - LinkLocalSession::sendElement(stanza); + sendElement(stanza); } queuedElements_.clear(); setInitialized(); } void OutgoingLinkLocalSession::handleElement(boost::shared_ptr<Element> element) { - if (isInitialized()) { - boost::shared_ptr<Element> stanza = boost::dynamic_pointer_cast<Element>(element); - if (stanza) { - onElementReceived(stanza); - } - } + onElementReceived(element); } -void OutgoingLinkLocalSession::sendElement(boost::shared_ptr<Element> stanza) { - if (isInitialized()) { - LinkLocalSession::sendElement(stanza); - } - else { - queuedElements_.push_back(stanza); - } +void OutgoingLinkLocalSession::queueElement(boost::shared_ptr<Element> element) { + queuedElements_.push_back(element); } diff --git a/Swiften/LinkLocal/OutgoingLinkLocalSession.h b/Swiften/LinkLocal/OutgoingLinkLocalSession.h index d3fed0b..904a9d3 100644 --- a/Swiften/LinkLocal/OutgoingLinkLocalSession.h +++ b/Swiften/LinkLocal/OutgoingLinkLocalSession.h @@ -5,51 +5,32 @@ #include <boost/enable_shared_from_this.hpp> #include <vector> -#include "Swiften/LinkLocal/LinkLocalSession.h" +#include "Swiften/Session/Session.h" #include "Swiften/JID/JID.h" namespace Swift { class ConnectionFactory; - class HostAddress; class String; class Element; class PayloadParserFactoryCollection; class PayloadSerializerCollection; - class DNSSDService; - class OutgoingLinkLocalSession : public LinkLocalSession { + class OutgoingLinkLocalSession : public Session { public: OutgoingLinkLocalSession( const JID& localJID, const JID& remoteJID, - const String& hostname, - int port, - boost::shared_ptr<DNSSDService> resolver, + boost::shared_ptr<Connection> connection, PayloadParserFactoryCollection* payloadParserFactories, - PayloadSerializerCollection* payloadSerializers, - ConnectionFactory* connectionFactory); + PayloadSerializerCollection* payloadSerializers); - const JID& getRemoteJID() const { - return remoteJID_; - } - - void start(); - - void sendElement(boost::shared_ptr<Element> stanza); + void queueElement(boost::shared_ptr<Element> element); private: void handleElement(boost::shared_ptr<Element>); void handleStreamStart(const ProtocolHeader&); - void handleHostnameResolved(const String& hostname, const boost::optional<HostAddress>& address); - void handleConnected(bool error); private: - bool resolving_; - JID remoteJID_; - String hostname_; - int port_; - boost::shared_ptr<DNSSDService> resolver_; std::vector<boost::shared_ptr<Element> > queuedElements_; - ConnectionFactory* connectionFactory_; }; } |