summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2011-12-24 14:27:33 (GMT)
committerRemko Tronçon <git@el-tramo.be>2011-12-24 14:44:44 (GMT)
commitc060049e759571ae02a3a970c6a3088e099e5c9f (patch)
tree8512e2ca0ae0fba16238651b9d0edbb74e085b16
parentb453b3bf94dcd40d71c30fd0053f7110cb52b211 (diff)
downloadswift-contrib-c060049e759571ae02a3a970c6a3088e099e5c9f.zip
swift-contrib-c060049e759571ae02a3a970c6a3088e099e5c9f.tar.bz2
Some BOSH refactoring.
-rw-r--r--Swiften/Network/BOSHConnection.cpp34
-rw-r--r--Swiften/Network/BOSHConnection.h22
-rw-r--r--Swiften/Network/BOSHConnectionPool.cpp4
-rw-r--r--Swiften/Network/BOSHConnectionPool.h2
-rw-r--r--Swiften/Network/UnitTest/BOSHConnectionTest.cpp21
5 files changed, 42 insertions, 41 deletions
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<TLSConnection>(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<SafeByteArray> 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<SafeByteArray> 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<BOSHError>() : boost::make_shared<BOSHError>(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<size_t>(requestsString);
+ try {
+ requests = boost::lexical_cast<size_t>(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>& error) {
+void BOSHConnection::handleDisconnected(const boost::optional<Connection::Error>& 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<BOSHConnection> {
+ class BOSHConnection : public boost::enable_shared_from_this<BOSHConnection> {
public:
typedef boost::shared_ptr<BOSHConnection> 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<SafeByteArray, size_t> createHTTPRequest(const SafeByteArray& data, bool streamRestart, bool terminate, long rid, const std::string& sid, const URL& boshURL);
+ boost::signal<void (bool /* error */)> onConnectFinished;
+ boost::signal<void (bool /* error */)> onDisconnected;
boost::signal<void (BOSHError::ref)> onSessionTerminated;
boost::signal<void (const std::string& /*sid*/, size_t /*requests*/)> onSessionStarted;
boost::signal<void (const SafeByteArray&)> onXMPPDataRead;
boost::signal<void (const SafeByteArray&)> onBOSHDataRead;
boost::signal<void (const SafeByteArray&)> onBOSHDataWritten;
boost::signal<void (const std::string&)> 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<SafeByteArray, size_t> 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<SafeByteArray> data);
- void handleDisconnected(const boost::optional<Error>& error);
+ void handleDisconnected(const boost::optional<Connection::Error>& 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> 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>(BOSHError::UndefinedCondition));
}
-void BOSHConnectionPool::handleConnectionDisconnected(const boost::optional<Connection::Error>& error, BOSHConnection::ref connection) {
+void BOSHConnectionPool::handleConnectionDisconnected(bool error, BOSHConnection::ref connection) {
destroyConnection(connection);
if (false && error) {
handleSessionTerminated(boost::make_shared<BOSHError>(BOSHError::UndefinedCondition));
@@ -218,7 +218,7 @@ boost::shared_ptr<BOSHConnection> 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<Connection::Error>& 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("<body wait='60' "
@@ -76,7 +77,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture {
void testReadiness_ok() {
BOSHConnection::ref testling = createTestling();
- testling->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("<mypayload/>"));
@@ -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("<mypayload/>"));
@@ -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<size_t>(1), connectionFactory->connections.size());
boost::shared_ptr<MockConnection> connection = connectionFactory->connections[0];
@@ -198,7 +199,7 @@ class BOSHConnectionTest : public CppUnit::TestFixture {
connectFinishedWithError = error;
}
- void handleDisconnected(const boost::optional<Connection::Error>& 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<Connection::Error> disconnectedError;
+ bool disconnectedError;
ByteArray dataRead;
PlatformXMLParserFactory parserFactory;
std::string sid;