diff options
| author | Remko Tronçon <git@el-tramo.be> | 2011-12-22 15:46:23 (GMT) | 
|---|---|---|
| committer | Remko Tronçon <git@el-tramo.be> | 2011-12-22 16:53:18 (GMT) | 
| commit | 732253a9b3e88b99b36dd3298157cf502f743294 (patch) | |
| tree | 3471f2d999dfe3cb2ae8f5aec5bdd7b3d1465078 /Swiften/Network/BOSHConnectionPool.cpp | |
| parent | 6c1e7d8a8339a849896566464bd9f1db33e3b3da (diff) | |
| download | swift-contrib-732253a9b3e88b99b36dd3298157cf502f743294.zip swift-contrib-732253a9b3e88b99b36dd3298157cf502f743294.tar.bz2  | |
BOSH Refactoring.
Diffstat (limited to 'Swiften/Network/BOSHConnectionPool.cpp')
| -rw-r--r-- | Swiften/Network/BOSHConnectionPool.cpp | 13 | 
1 files changed, 8 insertions, 5 deletions
diff --git a/Swiften/Network/BOSHConnectionPool.cpp b/Swiften/Network/BOSHConnectionPool.cpp index 6c3ba7e..4886ede 100644 --- a/Swiften/Network/BOSHConnectionPool.cpp +++ b/Swiften/Network/BOSHConnectionPool.cpp @@ -1,69 +1,72 @@  /*   * Copyright (c) 2011 Kevin Smith   * Licensed under the GNU General Public License v3.   * See Documentation/Licenses/GPLv3.txt for more information.   */  #include <Swiften/Network/BOSHConnectionPool.h>  #include <climits>  #include <boost/bind.hpp>  #include <boost/lexical_cast.hpp>  #include <Swiften/Base/foreach.h>  #include <Swiften/Base/SafeString.h>  #include <Swiften/Network/TLSConnectionFactory.h>  #include <Swiften/Network/HTTPConnectProxiedConnectionFactory.h>  namespace Swift { -BOSHConnectionPool::BOSHConnectionPool(boost::shared_ptr<BOSHConnectionFactory> connectionFactory, const std::string& to, long initialRID, const URL& boshHTTPConnectProxyURL, const SafeString& boshHTTPConnectProxyAuthID, const SafeString& boshHTTPConnectProxyAuthPassword) -	: connectionFactory(connectionFactory), +BOSHConnectionPool::BOSHConnectionPool(const URL& boshURL, ConnectionFactory* connectionFactory, XMLParserFactory* parserFactory, TLSContextFactory* tlsFactory, const std::string& to, long initialRID, const URL& boshHTTPConnectProxyURL, const SafeString& boshHTTPConnectProxyAuthID, const SafeString& boshHTTPConnectProxyAuthPassword) : +		boshURL(boshURL), +		connectionFactory(connectionFactory), +		xmlParserFactory(parserFactory), +		tlsFactory(tlsFactory),  		rid(initialRID),  		pendingTerminate(false),  		to(to),  		requestLimit(2),  		restartCount(0),  		pendingRestart(false) {  	tlsConnectionFactory = NULL;  	if (boshHTTPConnectProxyURL.empty()) {  		connectProxyFactory = NULL;  	}  	else { -		ConnectionFactory* rawFactory = connectionFactory->getRawConnectionFactory(); +		ConnectionFactory* rawFactory = connectionFactory;  		if (boshHTTPConnectProxyURL.getScheme() == "https") { -			tlsConnectionFactory = new TLSConnectionFactory(connectionFactory->getTLSContextFactory(), rawFactory); +			tlsConnectionFactory = new TLSConnectionFactory(tlsFactory, rawFactory);  			rawFactory = tlsConnectionFactory;  		}  		connectProxyFactory = new HTTPConnectProxiedConnectionFactory(rawFactory, HostAddressPort(HostAddress(boshHTTPConnectProxyURL.getHost()), boshHTTPConnectProxyURL.getPort()), boshHTTPConnectProxyAuthID, boshHTTPConnectProxyAuthPassword);  	}  	createConnection();  }  BOSHConnectionPool::~BOSHConnectionPool() {  	close();  	delete connectProxyFactory;  	delete tlsConnectionFactory;  }  void BOSHConnectionPool::write(const SafeByteArray& data) {  	dataQueue.push_back(data);  	tryToSendQueuedData();  }  void BOSHConnectionPool::handleDataRead(const SafeByteArray& data) {  	onXMPPDataRead(data);  	tryToSendQueuedData(); /* Will rebalance the connections */  }  void BOSHConnectionPool::restartStream() {  	BOSHConnection::ref connection = getSuitableConnection();  	if (connection) {  		pendingRestart = false;  		rid++;  		connection->setRID(rid);  		connection->restartStream();  		restartCount++;  	}  	else {  		pendingRestart = true;  	} @@ -174,71 +177,71 @@ void BOSHConnectionPool::tryToSendQueuedData() {  					suitableConnection->write(createSafeByteArray(""));  				}  				else {  				/* My thought process I went through when writing this, to aid anyone else confused why this can happen...  				 *  				 * What to do here? I think this isn't possible.  				   If you didn't have two connections, suitable would have made one.  				   If you have two connections and neither is suitable, pending would be true.  				   If you have a non-pending connection, it's suitable.  				   If I decide to do something here, remove assert above.  				   Ah! Yes, because there's a period between creating the connection and it being connected. */  				}  			}  		}  	}  }  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) {  	destroyConnection(connection);  	if (false && error) {  		handleSessionTerminated(boost::make_shared<BOSHError>(BOSHError::UndefinedCondition));  	}  	else {  		/* We might have just freed up a connection slot to send with */  		tryToSendQueuedData();  	}  }  boost::shared_ptr<BOSHConnection> BOSHConnectionPool::createConnection() { -	BOSHConnection::ref connection = boost::dynamic_pointer_cast<BOSHConnection>(connectionFactory->createConnection(connectProxyFactory)); +	BOSHConnection::ref connection = BOSHConnection::create(boshURL, connectProxyFactory ? connectProxyFactory : connectionFactory, xmlParserFactory, tlsFactory);  	connection->onXMPPDataRead.connect(boost::bind(&BOSHConnectionPool::handleDataRead, this, _1));  	connection->onSessionStarted.connect(boost::bind(&BOSHConnectionPool::handleSessionStarted, this, _1, _2));  	connection->onBOSHDataRead.connect(boost::bind(&BOSHConnectionPool::handleBOSHDataRead, this, _1));  	connection->onBOSHDataWritten.connect(boost::bind(&BOSHConnectionPool::handleBOSHDataWritten, this, _1));  	connection->onDisconnected.connect(boost::bind(&BOSHConnectionPool::handleConnectionDisconnected, this, _1, connection));  	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));  	connections.push_back(connection);  	return connection;  }  void BOSHConnectionPool::destroyConnection(boost::shared_ptr<BOSHConnection> connection) {  	connections.erase(std::remove(connections.begin(), connections.end(), connection), connections.end());  	connection->onXMPPDataRead.disconnect(boost::bind(&BOSHConnectionPool::handleDataRead, this, _1));  	connection->onSessionStarted.disconnect(boost::bind(&BOSHConnectionPool::handleSessionStarted, this, _1, _2));  	connection->onBOSHDataRead.disconnect(boost::bind(&BOSHConnectionPool::handleBOSHDataRead, this, _1));  	connection->onBOSHDataWritten.disconnect(boost::bind(&BOSHConnectionPool::handleBOSHDataWritten, this, _1));  	connection->onDisconnected.disconnect(boost::bind(&BOSHConnectionPool::handleConnectionDisconnected, this, _1, connection));  	connection->onConnectFinished.disconnect(boost::bind(&BOSHConnectionPool::handleConnectFinished, this, _1, connection));  	connection->onSessionTerminated.disconnect(boost::bind(&BOSHConnectionPool::handleSessionTerminated, this, _1));  	connection->onHTTPError.disconnect(boost::bind(&BOSHConnectionPool::handleHTTPError, this, _1));  }  void BOSHConnectionPool::handleSessionTerminated(BOSHError::ref error) {  	onSessionTerminated(error);  }  void BOSHConnectionPool::handleBOSHDataRead(const SafeByteArray& data) {  	onBOSHDataRead(data);  }  void BOSHConnectionPool::handleBOSHDataWritten(const SafeByteArray& data) {  	onBOSHDataWritten(data);  | 
 Swift