From c060049e759571ae02a3a970c6a3088e099e5c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Sat, 24 Dec 2011 15:27:33 +0100 Subject: Some BOSH refactoring. diff --git a/Swiften/Network/BOSHConnection.cpp b/Swiften/Network/BOSHConnection.cpp index 09548e9..ea84400 100644 --- a/Swiften/Network/BOSHConnection.cpp +++ b/Swiften/Network/BOSHConnection.cpp @@ -49,8 +49,7 @@ BOSHConnection::~BOSHConnection() { disconnect(); } -void BOSHConnection::connect(const HostAddressPort& server) { - /* FIXME: Redundant parameter */ +void BOSHConnection::connect() { Connection::ref rawConnection = connectionFactory_->createConnection(); connection_ = (boshURL_.getScheme() == "https") ? boost::make_shared(rawConnection, tlsFactory_) : rawConnection; connection_->onConnectFinished.connect(boost::bind(&BOSHConnection::handleConnectionConnectFinished, shared_from_this(), _1)); @@ -59,10 +58,6 @@ void BOSHConnection::connect(const HostAddressPort& server) { connection_->connect(HostAddressPort(HostAddress(boshURL_.getHost()), boshURL_.getPort())); } -void BOSHConnection::listen() { - assert(false); -} - void BOSHConnection::disconnect() { if(connection_) { connection_->disconnect(); @@ -170,8 +165,8 @@ void BOSHConnection::startStream(const std::string& to, unsigned long rid) { } void BOSHConnection::handleDataRead(boost::shared_ptr data) { - onBOSHDataRead(*data.get()); - buffer_ = concat(buffer_, *data.get()); + onBOSHDataRead(*data); + buffer_ = concat(buffer_, *data); std::string response = safeByteArrayToString(buffer_); if (response.find("\r\n\r\n") == std::string::npos) { onBOSHDataRead(createSafeByteArray("[[Previous read incomplete, pending]]")); @@ -186,22 +181,26 @@ void BOSHConnection::handleDataRead(boost::shared_ptr data) { BOSHBodyExtractor parser(parserFactory_, createByteArray(response.substr(response.find("\r\n\r\n") + 4))); if (parser.getBody()) { - if ((*parser.getBody()).attributes.getAttribute("type") == "terminate") { - BOSHError::Type errorType = parseTerminationCondition((*parser.getBody()).attributes.getAttribute("condition")); + if (parser.getBody()->attributes.getAttribute("type") == "terminate") { + BOSHError::Type errorType = parseTerminationCondition(parser.getBody()->attributes.getAttribute("condition")); onSessionTerminated(errorType == BOSHError::NoError ? boost::shared_ptr() : boost::make_shared(errorType)); } buffer_.clear(); if (waitingForStartResponse_) { waitingForStartResponse_ = false; - sid_ = (*parser.getBody()).attributes.getAttribute("sid"); - std::string requestsString = (*parser.getBody()).attributes.getAttribute("requests"); + sid_ = parser.getBody()->attributes.getAttribute("sid"); + std::string requestsString = parser.getBody()->attributes.getAttribute("requests"); int requests = 2; if (!requestsString.empty()) { - requests = boost::lexical_cast(requestsString); + try { + requests = boost::lexical_cast(requestsString); + } + catch (const boost::bad_lexical_cast&) { + } } onSessionStarted(sid_, requests); } - SafeByteArray payload = createSafeByteArray((*parser.getBody()).content); + SafeByteArray payload = createSafeByteArray(parser.getBody()->content); /* Say we're good to go again, so don't add anything after here in the method */ pending_ = false; onXMPPDataRead(payload); @@ -257,7 +256,7 @@ const std::string& BOSHConnection::getSID() { return sid_; } -void BOSHConnection::setRID(unsigned long rid) { +void BOSHConnection::setRID(unsigned long long rid) { rid_ = rid; } @@ -265,15 +264,12 @@ void BOSHConnection::setSID(const std::string& sid) { sid_ = sid; } -void BOSHConnection::handleDisconnected(const boost::optional& error) { +void BOSHConnection::handleDisconnected(const boost::optional& error) { onDisconnected(error); sid_ = ""; connectionReady_ = false; } -HostAddressPort BOSHConnection::getLocalAddress() const { - return connection_->getLocalAddress(); -} bool BOSHConnection::isReadyToSend() { /* Without pipelining you need to not send more without first receiving the response */ diff --git a/Swiften/Network/BOSHConnection.h b/Swiften/Network/BOSHConnection.h index 283ea10..4d53253 100644 --- a/Swiften/Network/BOSHConnection.h +++ b/Swiften/Network/BOSHConnection.h @@ -29,6 +29,8 @@ namespace boost { } } +class BOSHConnectionTest; + namespace Swift { class ConnectionFactory; class XMLParserFactory; @@ -49,42 +51,44 @@ namespace Swift { }; - class BOSHConnection : public Connection, public boost::enable_shared_from_this { + class BOSHConnection : public boost::enable_shared_from_this { public: typedef boost::shared_ptr ref; static ref create(const URL& boshURL, ConnectionFactory* connectionFactory, XMLParserFactory* parserFactory, TLSContextFactory* tlsFactory) { return ref(new BOSHConnection(boshURL, connectionFactory, parserFactory, tlsFactory)); } virtual ~BOSHConnection(); - virtual void listen(); - virtual void connect(const HostAddressPort& address); + virtual void connect(); virtual void disconnect(); virtual void write(const SafeByteArray& data); - virtual HostAddressPort getLocalAddress() const; const std::string& getSID(); - void setRID(unsigned long rid); + void setRID(unsigned long long rid); void setSID(const std::string& sid); void startStream(const std::string& to, unsigned long rid); void terminateStream(); bool isReadyToSend(); void restartStream(); - static std::pair createHTTPRequest(const SafeByteArray& data, bool streamRestart, bool terminate, long rid, const std::string& sid, const URL& boshURL); + boost::signal onConnectFinished; + boost::signal onDisconnected; boost::signal onSessionTerminated; boost::signal onSessionStarted; boost::signal onXMPPDataRead; boost::signal onBOSHDataRead; boost::signal onBOSHDataWritten; boost::signal onHTTPError; + private: - BOSHConnection(const URL& boshURL, ConnectionFactory* connectionFactory, XMLParserFactory* parserFactory, TLSContextFactory* tlsFactory); + friend class ::BOSHConnectionTest; + BOSHConnection(const URL& boshURL, ConnectionFactory* connectionFactory, XMLParserFactory* parserFactory, TLSContextFactory* tlsFactory); + static std::pair createHTTPRequest(const SafeByteArray& data, bool streamRestart, bool terminate, long rid, const std::string& sid, const URL& boshURL); void handleConnectionConnectFinished(bool error); void handleDataRead(boost::shared_ptr data); - void handleDisconnected(const boost::optional& error); + void handleDisconnected(const boost::optional& error); void write(const SafeByteArray& data, bool streamRestart, bool terminate); /* FIXME: refactor */ BOSHError::Type parseTerminationCondition(const std::string& text); @@ -94,7 +98,7 @@ namespace Swift { boost::shared_ptr connection_; std::string sid_; bool waitingForStartResponse_; - unsigned long rid_; + unsigned long long rid_; SafeByteArray buffer_; bool pending_; TLSContextFactory* tlsFactory_; diff --git a/Swiften/Network/BOSHConnectionPool.cpp b/Swiften/Network/BOSHConnectionPool.cpp index 4886ede..a30bf7b 100644 --- a/Swiften/Network/BOSHConnectionPool.cpp +++ b/Swiften/Network/BOSHConnectionPool.cpp @@ -197,7 +197,7 @@ void BOSHConnectionPool::handleHTTPError(const std::string& /*errorCode*/) { handleSessionTerminated(boost::make_shared(BOSHError::UndefinedCondition)); } -void BOSHConnectionPool::handleConnectionDisconnected(const boost::optional& error, BOSHConnection::ref connection) { +void BOSHConnectionPool::handleConnectionDisconnected(bool error, BOSHConnection::ref connection) { destroyConnection(connection); if (false && error) { handleSessionTerminated(boost::make_shared(BOSHError::UndefinedCondition)); @@ -218,7 +218,7 @@ boost::shared_ptr BOSHConnectionPool::createConnection() { connection->onConnectFinished.connect(boost::bind(&BOSHConnectionPool::handleConnectFinished, this, _1, connection)); connection->onSessionTerminated.connect(boost::bind(&BOSHConnectionPool::handleSessionTerminated, this, _1)); connection->onHTTPError.connect(boost::bind(&BOSHConnectionPool::handleHTTPError, this, _1)); - connection->connect(HostAddressPort(HostAddress("0.0.0.0"), 0)); + connection->connect(); connections.push_back(connection); return connection; } diff --git a/Swiften/Network/BOSHConnectionPool.h b/Swiften/Network/BOSHConnectionPool.h index 2264c90..cc354b8 100644 --- a/Swiften/Network/BOSHConnectionPool.h +++ b/Swiften/Network/BOSHConnectionPool.h @@ -38,7 +38,7 @@ namespace Swift { void handleBOSHDataWritten(const SafeByteArray& data); void handleSessionTerminated(BOSHError::ref condition); void handleConnectFinished(bool, BOSHConnection::ref connection); - void handleConnectionDisconnected(const boost::optional& error, BOSHConnection::ref connection); + void handleConnectionDisconnected(bool error, BOSHConnection::ref connection); void handleHTTPError(const std::string& errorCode); private: diff --git a/Swiften/Network/UnitTest/BOSHConnectionTest.cpp b/Swiften/Network/UnitTest/BOSHConnectionTest.cpp index 9215725..8062bea 100644 --- a/Swiften/Network/UnitTest/BOSHConnectionTest.cpp +++ b/Swiften/Network/UnitTest/BOSHConnectionTest.cpp @@ -45,6 +45,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture { connectionFactory = new MockConnectionFactory(eventLoop); connectFinished = false; disconnected = false; + disconnectedError = false; dataRead.clear(); } @@ -56,7 +57,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture { void testHeader() { BOSHConnection::ref testling = createTestling(); - testling->connect(HostAddressPort(HostAddress("127.0.0.1"), 5280)); + testling->connect(); eventLoop->processEvents(); testling->startStream("wonderland.lit", 1); std::string initial("connect(HostAddressPort(HostAddress("127.0.0.1"), 5280)); + testling->connect(); eventLoop->processEvents(); testling->setSID("blahhhhh"); CPPUNIT_ASSERT(testling->isReadyToSend()); @@ -84,7 +85,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture { void testReadiness_pending() { BOSHConnection::ref testling = createTestling(); - testling->connect(HostAddressPort(HostAddress("127.0.0.1"), 5280)); + testling->connect(); eventLoop->processEvents(); testling->setSID("mySID"); CPPUNIT_ASSERT(testling->isReadyToSend()); @@ -96,7 +97,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture { void testReadiness_disconnect() { BOSHConnection::ref testling = createTestling(); - testling->connect(HostAddressPort(HostAddress("127.0.0.1"), 5280)); + testling->connect(); eventLoop->processEvents(); testling->setSID("mySID"); CPPUNIT_ASSERT(testling->isReadyToSend()); @@ -107,14 +108,14 @@ class BOSHConnectionTest : public CppUnit::TestFixture { void testReadiness_noSID() { BOSHConnection::ref testling = createTestling(); - testling->connect(HostAddressPort(HostAddress("127.0.0.1"), 5280)); + testling->connect(); eventLoop->processEvents(); CPPUNIT_ASSERT(!testling->isReadyToSend()); } void testWrite_Receive() { BOSHConnection::ref testling = createTestling(); - testling->connect(HostAddressPort(HostAddress("127.0.0.1"), 5280)); + testling->connect(); eventLoop->processEvents(); testling->setSID("mySID"); testling->write(createSafeByteArray("")); @@ -125,7 +126,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture { void testWrite_ReceiveTwice() { BOSHConnection::ref testling = createTestling(); - testling->connect(HostAddressPort(HostAddress("127.0.0.1"), 5280)); + testling->connect(); eventLoop->processEvents(); testling->setSID("mySID"); testling->write(createSafeByteArray("")); @@ -139,7 +140,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture { void testRead_Fragment() { BOSHConnection::ref testling = createTestling(); - testling->connect(HostAddressPort(HostAddress("127.0.0.1"), 5280)); + testling->connect(); eventLoop->processEvents(); CPPUNIT_ASSERT_EQUAL(static_cast(1), connectionFactory->connections.size()); boost::shared_ptr connection = connectionFactory->connections[0]; @@ -198,7 +199,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture { connectFinishedWithError = error; } - void handleDisconnected(const boost::optional& e) { + void handleDisconnected(bool e) { disconnected = true; disconnectedError = e; } @@ -280,7 +281,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture { bool connectFinished; bool connectFinishedWithError; bool disconnected; - boost::optional disconnectedError; + bool disconnectedError; ByteArray dataRead; PlatformXMLParserFactory parserFactory; std::string sid; -- cgit v0.10.2-6-g49f6