diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-07-19 12:27:15 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-07-19 12:27:15 (GMT) |
commit | 9ccf1973ec3e23e4ba061b774c3f3e3bde4f1040 (patch) | |
tree | 4f760b6223c1ca5ef95ee3a261a0f2ec13b963a5 | |
parent | 1de12ed346fc0018527afe082886129088e27a95 (diff) | |
download | swift-contrib-9ccf1973ec3e23e4ba061b774c3f3e3bde4f1040.zip swift-contrib-9ccf1973ec3e23e4ba061b774c3f3e3bde4f1040.tar.bz2 |
Rename Session to ClientSession.
-rw-r--r-- | Swiften/Client/Client.cpp | 30 | ||||
-rw-r--r-- | Swiften/Client/Client.h | 8 | ||||
-rw-r--r-- | Swiften/Client/ClientSession.cpp (renamed from Swiften/Client/Session.cpp) | 48 | ||||
-rw-r--r-- | Swiften/Client/ClientSession.h (renamed from Swiften/Client/Session.h) | 12 | ||||
-rw-r--r-- | Swiften/Client/Makefile.inc | 2 | ||||
-rw-r--r-- | Swiften/Client/UnitTest/ClientSessionTest.cpp (renamed from Swiften/Client/UnitTest/SessionTest.cpp) | 90 | ||||
-rw-r--r-- | Swiften/Client/UnitTest/Makefile.inc | 2 | ||||
-rw-r--r-- | Swiften/Session/Makefile.inc | 2 | ||||
-rw-r--r-- | Swiften/Session/Session.cpp | 72 | ||||
-rw-r--r-- | Swiften/Session/Session.h | 76 |
10 files changed, 244 insertions, 98 deletions
diff --git a/Swiften/Client/Client.cpp b/Swiften/Client/Client.cpp index 04a24bf..a38416a 100644 --- a/Swiften/Client/Client.cpp +++ b/Swiften/Client/Client.cpp @@ -3,7 +3,7 @@ #include <boost/bind.hpp> #include "Swiften/Network/DomainNameResolver.h" -#include "Swiften/Client/Session.h" +#include "Swiften/Client/ClientSession.h" #include "Swiften/StreamStack/PlatformTLSLayerFactory.h" #include "Swiften/Network/BoostConnectionFactory.h" #include "Swiften/Network/DomainNameResolveException.h" @@ -44,7 +44,7 @@ void Client::handleConnectionConnectFinished(bool error) { onError(ClientError::ConnectionError); } else { - session_ = new Session(jid_, connection_, tlsLayerFactory_, &payloadParserFactories_, &payloadSerializers_); + session_ = new ClientSession(jid_, connection_, tlsLayerFactory_, &payloadParserFactories_, &payloadSerializers_); if (!certificate_.isEmpty()) { session_->setCertificate(PKCS12Certificate(certificate_, password_)); } @@ -108,43 +108,43 @@ void Client::setCertificate(const String& certificate) { certificate_ = certificate; } -void Client::handleSessionError(Session::SessionError error) { +void Client::handleSessionError(ClientSession::SessionError error) { ClientError clientError; switch (error) { - case Session::NoError: + case ClientSession::NoError: assert(false); break; - case Session::ConnectionReadError: + case ClientSession::ConnectionReadError: clientError = ClientError(ClientError::ConnectionReadError); break; - case Session::ConnectionWriteError: + case ClientSession::ConnectionWriteError: clientError = ClientError(ClientError::ConnectionWriteError); break; - case Session::XMLError: + case ClientSession::XMLError: clientError = ClientError(ClientError::XMLError); break; - case Session::AuthenticationFailedError: + case ClientSession::AuthenticationFailedError: clientError = ClientError(ClientError::AuthenticationFailedError); break; - case Session::NoSupportedAuthMechanismsError: + case ClientSession::NoSupportedAuthMechanismsError: clientError = ClientError(ClientError::NoSupportedAuthMechanismsError); break; - case Session::UnexpectedElementError: + case ClientSession::UnexpectedElementError: clientError = ClientError(ClientError::UnexpectedElementError); break; - case Session::ResourceBindError: + case ClientSession::ResourceBindError: clientError = ClientError(ClientError::ResourceBindError); break; - case Session::SessionStartError: + case ClientSession::SessionStartError: clientError = ClientError(ClientError::SessionStartError); break; - case Session::TLSError: + case ClientSession::TLSError: clientError = ClientError(ClientError::TLSError); break; - case Session::ClientCertificateLoadError: + case ClientSession::ClientCertificateLoadError: clientError = ClientError(ClientError::ClientCertificateLoadError); break; - case Session::ClientCertificateError: + case ClientSession::ClientCertificateError: clientError = ClientError(ClientError::ClientCertificateError); break; } diff --git a/Swiften/Client/Client.h b/Swiften/Client/Client.h index 66f9b01..48b76d9 100644 --- a/Swiften/Client/Client.h +++ b/Swiften/Client/Client.h @@ -4,7 +4,7 @@ #include <boost/signals.hpp> #include <boost/shared_ptr.hpp> -#include "Swiften/Client/Session.h" +#include "Swiften/Client/ClientSession.h" #include "Swiften/Client/ClientError.h" #include "Swiften/Elements/Presence.h" #include "Swiften/Elements/Message.h" @@ -20,7 +20,7 @@ namespace Swift { class TLSLayerFactory; class ConnectionFactory; - class Session; + class ClientSession; class Client : public StanzaChannel, public IQRouter { public: @@ -47,7 +47,7 @@ namespace Swift { void send(boost::shared_ptr<Stanza>); virtual String getNewIQID(); void handleElement(boost::shared_ptr<Element>); - void handleSessionError(Session::SessionError error); + void handleSessionError(ClientSession::SessionError error); void handleNeedCredentials(); void handleDataRead(const ByteArray&); void handleDataWritten(const ByteArray&); @@ -61,7 +61,7 @@ namespace Swift { TLSLayerFactory* tlsLayerFactory_; FullPayloadParserFactoryCollection payloadParserFactories_; FullPayloadSerializerCollection payloadSerializers_; - Session* session_; + ClientSession* session_; boost::shared_ptr<Connection> connection_; String certificate_; }; diff --git a/Swiften/Client/Session.cpp b/Swiften/Client/ClientSession.cpp index 1bd2b22..11317e8 100644 --- a/Swiften/Client/Session.cpp +++ b/Swiften/Client/ClientSession.cpp @@ -1,4 +1,4 @@ -#include "Swiften/Client/Session.h" +#include "Swiften/Client/ClientSession.h" #include <boost/bind.hpp> @@ -24,7 +24,7 @@ namespace Swift { -Session::Session( +ClientSession::ClientSession( const JID& jid, boost::shared_ptr<Connection> connection, TLSLayerFactory* tlsLayerFactory, @@ -41,42 +41,42 @@ Session::Session( needSessionStart_(false) { } -Session::~Session() { +ClientSession::~ClientSession() { delete streamStack_; } -void Session::start() { +void ClientSession::start() { assert(state_ == Initial); - connection_->onDisconnected.connect(boost::bind(&Session::handleDisconnected, this, _1)); + connection_->onDisconnected.connect(boost::bind(&ClientSession::handleDisconnected, this, _1)); initializeStreamStack(); state_ = WaitingForStreamStart; sendStreamHeader(); } -void Session::stop() { +void ClientSession::stop() { // TODO: Send end stream header if applicable connection_->disconnect(); } -void Session::sendStreamHeader() { +void ClientSession::sendStreamHeader() { ProtocolHeader header; header.setTo(jid_.getDomain()); xmppLayer_->writeHeader(header); } -void Session::initializeStreamStack() { +void ClientSession::initializeStreamStack() { xmppLayer_ = boost::shared_ptr<XMPPLayer>(new XMPPLayer(payloadParserFactories_, payloadSerializers_)); - xmppLayer_->onStreamStart.connect(boost::bind(&Session::handleStreamStart, this)); - xmppLayer_->onElement.connect(boost::bind(&Session::handleElement, this, _1)); - xmppLayer_->onError.connect(boost::bind(&Session::setError, this, XMLError)); + xmppLayer_->onStreamStart.connect(boost::bind(&ClientSession::handleStreamStart, this)); + xmppLayer_->onElement.connect(boost::bind(&ClientSession::handleElement, this, _1)); + xmppLayer_->onError.connect(boost::bind(&ClientSession::setError, this, XMLError)); xmppLayer_->onDataRead.connect(boost::bind(boost::ref(onDataRead), _1)); xmppLayer_->onWriteData.connect(boost::bind(boost::ref(onDataWritten), _1)); connectionLayer_ = boost::shared_ptr<ConnectionLayer>(new ConnectionLayer(connection_)); streamStack_ = new StreamStack(xmppLayer_, connectionLayer_); } -void Session::handleDisconnected(const boost::optional<Connection::Error>& error) { +void ClientSession::handleDisconnected(const boost::optional<Connection::Error>& error) { if (error) { switch (*error) { case Connection::ReadError: @@ -89,16 +89,16 @@ void Session::handleDisconnected(const boost::optional<Connection::Error>& error } } -void Session::setCertificate(const PKCS12Certificate& certificate) { +void ClientSession::setCertificate(const PKCS12Certificate& certificate) { certificate_ = certificate; } -void Session::handleStreamStart() { +void ClientSession::handleStreamStart() { checkState(WaitingForStreamStart); state_ = Negotiating; } -void Session::handleElement(boost::shared_ptr<Element> element) { +void ClientSession::handleElement(boost::shared_ptr<Element> element) { if (getState() == SessionStarted) { onElementReceived(element); } @@ -173,8 +173,8 @@ void Session::handleElement(boost::shared_ptr<Element> element) { setError(ClientCertificateLoadError); } else { - tlsLayer_->onConnected.connect(boost::bind(&Session::handleTLSConnected, this)); - tlsLayer_->onError.connect(boost::bind(&Session::handleTLSError, this)); + tlsLayer_->onConnected.connect(boost::bind(&ClientSession::handleTLSConnected, this)); + tlsLayer_->onError.connect(boost::bind(&ClientSession::handleTLSError, this)); tlsLayer_->connect(); } } @@ -229,19 +229,19 @@ void Session::handleElement(boost::shared_ptr<Element> element) { } } -void Session::sendSessionStart() { +void ClientSession::sendSessionStart() { state_ = StartingSession; xmppLayer_->writeElement(IQ::createRequest(IQ::Set, JID(), "session-start", boost::shared_ptr<StartSession>(new StartSession()))); } -void Session::setError(SessionError error) { +void ClientSession::setError(SessionError error) { assert(error != NoError); state_ = Error; error_ = error; onError(error); } -bool Session::checkState(State state) { +bool ClientSession::checkState(State state) { if (state_ != state) { setError(UnexpectedElementError); return false; @@ -249,24 +249,24 @@ bool Session::checkState(State state) { return true; } -void Session::sendCredentials(const String& password) { +void ClientSession::sendCredentials(const String& password) { assert(WaitingForCredentials); state_ = Authenticating; xmppLayer_->writeElement(boost::shared_ptr<Element>(new AuthRequest("PLAIN", PLAINMessage(jid_.getNode(), password).getValue()))); } -void Session::sendElement(boost::shared_ptr<Element> element) { +void ClientSession::sendElement(boost::shared_ptr<Element> element) { assert(SessionStarted); xmppLayer_->writeElement(element); } -void Session::handleTLSConnected() { +void ClientSession::handleTLSConnected() { state_ = WaitingForStreamStart; xmppLayer_->resetParser(); sendStreamHeader(); } -void Session::handleTLSError() { +void ClientSession::handleTLSError() { setError(TLSError); } diff --git a/Swiften/Client/Session.h b/Swiften/Client/ClientSession.h index 58531b3..50dae24 100644 --- a/Swiften/Client/Session.h +++ b/Swiften/Client/ClientSession.h @@ -1,5 +1,4 @@ -#ifndef SWIFTEN_Session_H -#define SWIFTEN_Session_H +#pragma once #include <boost/signal.hpp> #include <boost/shared_ptr.hpp> @@ -22,7 +21,7 @@ namespace Swift { class TLSLayer; class WhitespacePingLayer; - class Session { + class ClientSession { public: enum State { Initial, @@ -52,13 +51,13 @@ namespace Swift { ClientCertificateError }; - Session( + ClientSession( const JID& jid, boost::shared_ptr<Connection>, TLSLayerFactory*, PayloadParserFactoryCollection*, PayloadSerializerCollection*); - ~Session(); + ~ClientSession(); State getState() const { return state_; @@ -122,7 +121,4 @@ namespace Swift { bool needSessionStart_; PKCS12Certificate certificate_; }; - } - -#endif diff --git a/Swiften/Client/Makefile.inc b/Swiften/Client/Makefile.inc index 75eb08f..8171ed1 100644 --- a/Swiften/Client/Makefile.inc +++ b/Swiften/Client/Makefile.inc @@ -1,5 +1,5 @@ SWIFTEN_SOURCES += \ Swiften/Client/Client.cpp \ - Swiften/Client/Session.cpp + Swiften/Client/ClientSession.cpp include Swiften/Client/UnitTest/Makefile.inc diff --git a/Swiften/Client/UnitTest/SessionTest.cpp b/Swiften/Client/UnitTest/ClientSessionTest.cpp index eb7281c..1e66019 100644 --- a/Swiften/Client/UnitTest/SessionTest.cpp +++ b/Swiften/Client/UnitTest/ClientSessionTest.cpp @@ -29,15 +29,15 @@ #include "Swiften/EventLoop/DummyEventLoop.h" #include "Swiften/Network/Connection.h" #include "Swiften/Network/ConnectionFactory.h" -#include "Swiften/Client/Session.h" +#include "Swiften/Client/ClientSession.h" #include "Swiften/TLS/PKCS12Certificate.h" #include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h" #include "Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h" using namespace Swift; -class SessionTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(SessionTest); +class ClientSessionTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(ClientSessionTest); CPPUNIT_TEST(testConstructor); CPPUNIT_TEST(testStart_Error); CPPUNIT_TEST(testStart_XMLError); @@ -62,7 +62,7 @@ class SessionTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE_END(); public: - SessionTest() {} + ClientSessionTest() {} void setUp() { eventLoop_ = new DummyEventLoop(); @@ -79,7 +79,7 @@ class SessionTest : public CppUnit::TestFixture { void testConstructor() { std::auto_ptr<MockSession> session(createSession("me@foo.com/Bar")); - CPPUNIT_ASSERT_EQUAL(Session::Initial, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Initial, session->getState()); } void testStart_Error() { @@ -88,13 +88,13 @@ class SessionTest : public CppUnit::TestFixture { getMockServer()->expectStreamStart(); session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::WaitingForStreamStart, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::WaitingForStreamStart, session->getState()); getMockServer()->setError(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(Session::ConnectionReadError, session->getError()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::ConnectionReadError, session->getError()); } void testStart_XMLError() { @@ -103,13 +103,13 @@ class SessionTest : public CppUnit::TestFixture { getMockServer()->expectStreamStart(); session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::WaitingForStreamStart, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::WaitingForStreamStart, session->getState()); getMockServer()->sendInvalidXML(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(Session::XMLError, session->getError()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::XMLError, session->getError()); } void testStartTLS_NoTLSSupport() { @@ -120,7 +120,7 @@ class SessionTest : public CppUnit::TestFixture { getMockServer()->sendStreamFeaturesWithStartTLS(); session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::SessionStarted, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); } void testStartTLS() { @@ -133,7 +133,7 @@ class SessionTest : public CppUnit::TestFixture { getMockServer()->sendTLSProceed(); session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::Encrypting, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Encrypting, session->getState()); CPPUNIT_ASSERT(session->getTLSLayer()); CPPUNIT_ASSERT(session->getTLSLayer()->isConnecting()); @@ -143,7 +143,7 @@ class SessionTest : public CppUnit::TestFixture { session->getTLSLayer()->setConnected(); // FIXME: Test 'WatingForStreamStart' state processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::Negotiating, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Negotiating, session->getState()); } void testStartTLS_ServerError() { @@ -156,8 +156,8 @@ class SessionTest : public CppUnit::TestFixture { session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(Session::TLSError, session->getError()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::TLSError, session->getError()); } void testStartTLS_ConnectError() { @@ -171,8 +171,8 @@ class SessionTest : public CppUnit::TestFixture { processEvents(); session->getTLSLayer()->setError(); - CPPUNIT_ASSERT_EQUAL(Session::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(Session::TLSError, session->getError()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::TLSError, session->getError()); } void testStartTLS_ErrorAfterConnect() { @@ -192,19 +192,19 @@ class SessionTest : public CppUnit::TestFixture { session->getTLSLayer()->setError(); - CPPUNIT_ASSERT_EQUAL(Session::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(Session::TLSError, session->getError()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::TLSError, session->getError()); } void testAuthenticate() { std::auto_ptr<MockSession> session(createSession("me@foo.com/Bar")); - session->onNeedCredentials.connect(boost::bind(&SessionTest::setNeedCredentials, this)); + session->onNeedCredentials.connect(boost::bind(&ClientSessionTest::setNeedCredentials, this)); getMockServer()->expectStreamStart(); getMockServer()->sendStreamStart(); getMockServer()->sendStreamFeaturesWithAuthentication(); session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::WaitingForCredentials, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::WaitingForCredentials, session->getState()); CPPUNIT_ASSERT(needCredentials_); getMockServer()->expectAuth("me", "mypass"); @@ -212,9 +212,9 @@ class SessionTest : public CppUnit::TestFixture { getMockServer()->expectStreamStart(); getMockServer()->sendStreamStart(); session->sendCredentials("mypass"); - CPPUNIT_ASSERT_EQUAL(Session::Authenticating, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Authenticating, session->getState()); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::Negotiating, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Negotiating, session->getState()); } void testAuthenticate_Unauthorized() { @@ -230,8 +230,8 @@ class SessionTest : public CppUnit::TestFixture { session->sendCredentials("mypass"); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(Session::AuthenticationFailedError, session->getError()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::AuthenticationFailedError, session->getError()); } void testAuthenticate_NoValidAuthMechanisms() { @@ -242,8 +242,8 @@ class SessionTest : public CppUnit::TestFixture { session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(Session::NoSupportedAuthMechanismsError, session->getError()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::NoSupportedAuthMechanismsError, session->getError()); } void testResourceBind() { @@ -252,13 +252,13 @@ class SessionTest : public CppUnit::TestFixture { getMockServer()->sendStreamStart(); getMockServer()->sendStreamFeaturesWithResourceBind(); getMockServer()->expectResourceBind("Bar", "session-bind"); - // FIXME: Check CPPUNIT_ASSERT_EQUAL(Session::BindingResource, session->getState()); + // FIXME: Check CPPUNIT_ASSERT_EQUAL(ClientSession::BindingResource, session->getState()); getMockServer()->sendResourceBindResponse("me@foo.com/Bar", "session-bind"); session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::SessionStarted, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar"), session->getJID()); } @@ -272,7 +272,7 @@ class SessionTest : public CppUnit::TestFixture { session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::SessionStarted, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar123"), session->getJID()); } @@ -286,7 +286,7 @@ class SessionTest : public CppUnit::TestFixture { session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::SessionStarted, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/NewResource"), session->getJID()); } @@ -300,23 +300,23 @@ class SessionTest : public CppUnit::TestFixture { session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(Session::ResourceBindError, session->getError()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::ResourceBindError, session->getError()); } void testSessionStart() { std::auto_ptr<MockSession> session(createSession("me@foo.com/Bar")); - session->onSessionStarted.connect(boost::bind(&SessionTest::setSessionStarted, this)); + session->onSessionStarted.connect(boost::bind(&ClientSessionTest::setSessionStarted, this)); getMockServer()->expectStreamStart(); getMockServer()->sendStreamStart(); getMockServer()->sendStreamFeaturesWithSession(); getMockServer()->expectSessionStart("session-start"); - // FIXME: Check CPPUNIT_ASSERT_EQUAL(Session::StartingSession, session->getState()); + // FIXME: Check CPPUNIT_ASSERT_EQUAL(ClientSession::StartingSession, session->getState()); getMockServer()->sendSessionStartResponse("session-start"); session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::SessionStarted, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); CPPUNIT_ASSERT(sessionStarted_); } @@ -330,13 +330,13 @@ class SessionTest : public CppUnit::TestFixture { session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::Error, session->getState()); - CPPUNIT_ASSERT_EQUAL(Session::SessionStartError, session->getError()); + CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStartError, session->getError()); } void testSessionStart_AfterResourceBind() { std::auto_ptr<MockSession> session(createSession("me@foo.com/Bar")); - session->onSessionStarted.connect(boost::bind(&SessionTest::setSessionStarted, this)); + session->onSessionStarted.connect(boost::bind(&ClientSessionTest::setSessionStarted, this)); getMockServer()->expectStreamStart(); getMockServer()->sendStreamStart(); getMockServer()->sendStreamFeaturesWithResourceBindAndSession(); @@ -347,7 +347,7 @@ class SessionTest : public CppUnit::TestFixture { session->start(); processEvents(); - CPPUNIT_ASSERT_EQUAL(Session::SessionStarted, session->getState()); + CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState()); CPPUNIT_ASSERT(sessionStarted_); } @@ -375,7 +375,7 @@ class SessionTest : public CppUnit::TestFixture { void testSendElement() { std::auto_ptr<MockSession> session(createSession("me@foo.com/Bar")); - session->onElementReceived.connect(boost::bind(&SessionTest::addReceivedElement, this, _1)); + session->onElementReceived.connect(boost::bind(&ClientSessionTest::addReceivedElement, this, _1)); getMockServer()->expectStreamStart(); getMockServer()->sendStreamStart(); getMockServer()->sendStreamFeatures(); @@ -673,8 +673,8 @@ class SessionTest : public CppUnit::TestFixture { bool haveTLS_; }; - struct MockSession : public Session { - MockSession(const JID& jid, boost::shared_ptr<Connection> connection, TLSLayerFactory* tlsLayerFactory, PayloadParserFactoryCollection* payloadParserFactories, PayloadSerializerCollection* payloadSerializers) : Session(jid, connection, tlsLayerFactory, payloadParserFactories, payloadSerializers) {} + struct MockSession : public ClientSession { + MockSession(const JID& jid, boost::shared_ptr<Connection> connection, TLSLayerFactory* tlsLayerFactory, PayloadParserFactoryCollection* payloadParserFactories, PayloadSerializerCollection* payloadSerializers) : ClientSession(jid, connection, tlsLayerFactory, payloadParserFactories, payloadSerializers) {} boost::shared_ptr<MockTLSLayer> getTLSLayer() const { return getStreamStack()->getLayer<MockTLSLayer>(); @@ -701,4 +701,4 @@ class SessionTest : public CppUnit::TestFixture { EventQueue events_; }; -CPPUNIT_TEST_SUITE_REGISTRATION(SessionTest); +CPPUNIT_TEST_SUITE_REGISTRATION(ClientSessionTest); diff --git a/Swiften/Client/UnitTest/Makefile.inc b/Swiften/Client/UnitTest/Makefile.inc index 3ef87e5..14dac57 100644 --- a/Swiften/Client/UnitTest/Makefile.inc +++ b/Swiften/Client/UnitTest/Makefile.inc @@ -1,2 +1,2 @@ UNITTEST_SOURCES += \ - Swiften/Client/UnitTest/SessionTest.cpp + Swiften/Client/UnitTest/ClientSessionTest.cpp diff --git a/Swiften/Session/Makefile.inc b/Swiften/Session/Makefile.inc new file mode 100644 index 0000000..faa73c8 --- /dev/null +++ b/Swiften/Session/Makefile.inc @@ -0,0 +1,2 @@ +SWIFTEN_SOURCES += \ + Swiften/Session/Session.cpp diff --git a/Swiften/Session/Session.cpp b/Swiften/Session/Session.cpp new file mode 100644 index 0000000..9ab8e4d --- /dev/null +++ b/Swiften/Session/Session.cpp @@ -0,0 +1,72 @@ +#include "Swiften/Session/Session.h" + +#include <boost/bind.hpp> + +#include "Swiften/StreamStack/XMPPLayer.h" +#include "Swiften/StreamStack/StreamStack.h" + +namespace Swift { + +Session::Session( + boost::shared_ptr<Connection> connection, + PayloadParserFactoryCollection* payloadParserFactories, + PayloadSerializerCollection* payloadSerializers) : + connection(connection), + payloadParserFactories(payloadParserFactories), + payloadSerializers(payloadSerializers), + initialized(false) { +} + +Session::~Session() { + delete streamStack; +} + +void Session::startSession() { + initializeStreamStack(); + handleSessionStarted(); +} + +void Session::finishSession() { + connection->disconnect(); + onSessionFinished(boost::optional<Error>()); +} + +void Session::finishSession(const Error& error) { + connection->disconnect(); + onSessionFinished(boost::optional<Error>(error)); +} + +void Session::initializeStreamStack() { + xmppLayer = boost::shared_ptr<XMPPLayer>( + new XMPPLayer(payloadParserFactories, payloadSerializers)); + 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)); + connection->onDisconnected.connect( + boost::bind(&Session::handleDisconnected, shared_from_this(), _1)); + connectionLayer = boost::shared_ptr<ConnectionLayer>(new ConnectionLayer(connection)); + streamStack = new StreamStack(xmppLayer, connectionLayer); +} + +void Session::sendStanza(boost::shared_ptr<Stanza> stanza) { + xmppLayer->writeElement(stanza); +} + +void Session::handleDisconnected(const boost::optional<Connection::Error>& connectionError) { + if (connectionError) { + finishSession(ConnectionError); + } + else { + finishSession(); + } +} + +void Session::setInitialized() { + initialized = true; + onSessionStarted(); +} + +} diff --git a/Swiften/Session/Session.h b/Swiften/Session/Session.h new file mode 100644 index 0000000..bf8049a --- /dev/null +++ b/Swiften/Session/Session.h @@ -0,0 +1,76 @@ +#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 Session : public boost::enable_shared_from_this<Session> { + public: + enum Error { + ConnectionError, + XMLError + }; + + Session( + boost::shared_ptr<Connection> connection, + PayloadParserFactoryCollection* payloadParserFactories, + PayloadSerializerCollection* payloadSerializers); + virtual ~Session(); + + void startSession(); + void finishSession(); + void sendStanza(boost::shared_ptr<Stanza>); + + boost::signal<void (boost::shared_ptr<Stanza>)> onStanzaReceived; + boost::signal<void ()> onSessionStarted; + boost::signal<void (const boost::optional<Error>&)> onSessionFinished; + boost::signal<void (const ByteArray&)> onDataWritten; + boost::signal<void (const ByteArray&)> onDataRead; + + protected: + void finishSession(const Error&); + + virtual void handleSessionStarted() {} + virtual void handleElement(boost::shared_ptr<Element>) = 0; + virtual void handleStreamStart(const ProtocolHeader&) = 0; + + void initializeStreamStack(); + + boost::shared_ptr<XMPPLayer> getXMPPLayer() const { + return xmppLayer; + } + + void setInitialized(); + bool isInitialized() const { + return initialized; + } + + private: + void handleDisconnected(const boost::optional<Connection::Error>& error); + + private: + boost::shared_ptr<Connection> connection; + PayloadParserFactoryCollection* payloadParserFactories; + PayloadSerializerCollection* payloadSerializers; + boost::shared_ptr<XMPPLayer> xmppLayer; + boost::shared_ptr<ConnectionLayer> connectionLayer; + StreamStack* streamStack; + bool initialized; + }; +} |