|
OpenTop 1.5 | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||||
| SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD | |||||||
#include "ot/web/HttpClientConnectionManager.h"

The HttpClientConnectionManager is primarily a repository of idle TCP/IP connections to HTTP servers (both origin and proxy servers). It also contains configuration options specifying such things as the proxy server to use and whether or not to make use of persistent HTTP connections.
When an instance of the HttpClient class needs to service a HTTP request, it asks its HttpClientConnectionManager for a connection to the appropriate host, which responds with either a new TCP/IP connection or an existing idle connection if a suitable one already exists.
When the application has completed the HTTP request/response conversation, it returns the connection back to the HttpClientConnectionManager. The HttpClientConnectionManager retains connections returned to it in a cache of idle connections, but only if the returned connections are persistent and the connection manager has been configured to make use of persistent conections.
The application can control how sockets are created by registering custom socket factories.
The HttpClientConnectionManager provides two functions for managing the idle connection pool: manageIdleConnections() and manageClosedConnections(). In a process termed passive connection management, these functions are called automatically whenever a new HTTP connection is requested.
Sometimes it is insufficient for OpenTop to manage idle connections only when a new connection is required. For example, many HTTP servers employ a timeout mechanism where they close connections which haven't been used for a preset amount of time. In order for the network resources associated with these zombie connections to be released, the client side of the socket needs to be closed also. This is what happens when manageClosedConnections() is called, but this may occur some time after the server has closed its side of the connection.
The corollary to the above is when OpenTop decides that a connection has been idle for too long, even though the server appears content to leave it open. Connections that fall into this category are closed by the manageIdleConnections() function.
Applications are free to actively call these two connection management functions themselves (perhaps within an idle processing loop), or they may make use of OpenTop's active connection management by invoking enableActiveConnectionManagement(), which is available within the multi-threaded build of OpenTop.
The following example shows how an application can configure the global HttpClientConnectionManager to use active connection management:-
#include "ot/web/HttpClientConnectionManager.h"
#include "ot/base/SystemMonitor.h"
#include <iostream>
using namespace ot;
using namespace ot::web;
using namespace std;
int main(int argc, char* argv[])
{
SystemMonitor monitor;
try
{
//
// enable active connection management for OpenTop's HTTP implementation
//
HttpClientConnectionManager::GetConnectionManager()->enableActiveConnectionManagement(true);
//
// Now perform a long-running task containing HTTP processing,
// safe in the knowledge that HTTP client connections will be managed effectively
doStuff();
}
catch(Exception& e)
{
cerr << e.toString() << endl;
}
return 0;
}
| Constructor/Destructor Summary | |
HttpClientConnectionManager()Default constructor that configures a new HttpClientConnectionManager using default settings. | |
HttpClientConnectionManager(const HttpClientConnectionManager& rhs)Copy constructor that configures a new HttpClientConnectionManager using the settings (but not the idle connections) from an existing HttpClientConnectionManager. | |
~HttpClientConnectionManager()Destructor. | |
| Method Summary | |
void |
enableActiveConnectionManagement(bool bEnable, size_t sleepInterval)Configures active connection management for this HttpClientConnectionManager. |
virtual RefPtr< HttpClientConnection > |
getConnection(const String& hostName, int port, bool bSecure, HttpProtocolVersion version, size_t timeoutMS)Returns a HttpClientConnection for the specified host and port number. |
static RefPtr< HttpClientConnectionManager > |
GetDefault()Returns a reference to the global default HttpClientConnectionManager object. |
RefPtr< HostnameVerifier > |
getHostnameVerifier() constReturns the HostnameVerifier which will be used to verify SSL connections where the server's certificate does not match the host name of the remote HTTP server. |
size_t |
getIdleConnectionTimeout() constReturns the number of seconds that may elapse before an idle HTTP connection with an origin server is considered stale. |
size_t |
getIdleProxyConnectionTimeout() constReturns the number of seconds that may elapse before an idle HTTP connection with a proxy server is considered stale. |
protected virtual bool |
getProxyForHost(const String& hostName, bool bSecure, String& proxyHost, int& proxyPort)Virtual function called before allocating a TCP/IP connection to process a HTTP request. |
RefPtr< SocketFactory > |
getSocketFactory() constReturns the SocketFactory that will be used by this connection manager when creating standard (insecure) connections with HTTP servers. |
RefPtr< SSLSocketFactory > |
getSSLSocketFactory() constReturns the SSLSocketFactory that will be used by this connection manager to create secure connections with HTTP servers. |
bool |
getUsePersistentConnections() constReturns a flag indicating whether this HttpClientConnectionManager will make use of persistent HTTP connections (also known as keep-alive connections). |
bool |
isActiveConnectionManagementEnabled() constReturns a flag indicating whether active connection management is enabled for this HttpClientConnectionManager. |
protected virtual void |
makeAvailable(HttpClientConnection* pConnection)Called by a client of this HttpClientConnectionManager when it has finished using a HttpClientConnection. |
virtual void |
manageClosedConnections()Removes persistent TCP/IP connections from the free pool which have been closed by the remote HTTP server. |
virtual void |
manageIdleConnections()Manages the pool of persistent TCP/IP connections which are currently idle. |
static void |
SetDefault(HttpClientConnectionManager* pConnectionManager)Sets the global default HttpClientConnectionManager object. |
void |
setHostnameVerifier(HostnameVerifier* pVerifier)Set the HostnameVerifier object which will be used whenever new SSL sessions are established with a HTTP server whose certificate does not match the hostname contained within the URL. |
void |
setIdleConnectionTimeout(size_t secs)Configures the number of seconds that may elapse before an idle HTTP connection with an origin server is considered stale. |
void |
setIdleProxyConnectionTimeout(size_t secs)Configures the number of seconds that may elapse before an idle HTTP connection with a proxy server is considered stale. |
void |
setProxy(const String& proxyHost, int proxyPort)Sets a HTTP proxy server to be used for connections originated by this HttpClientConnectionManager. |
void |
setSocketFactory(SocketFactory* pFactory)Sets the SocketFactory that will be used by this connection manager when creating standard (insecure) connections with HTTP servers. |
void |
setSSLSocketFactory(SSLSocketFactory* pFactory)Sets the SSLSocketFactory that will be used by this connection manager when creating secure (SSL) connections with HTTP servers. |
void |
setUsePersistentConnections(bool bUsePersistentConnections)Configures this HttpClientConnectionManager to use persistent HTTP connections (also known as keep-alive connections) where possible. |
protected virtual bool |
useProxyForHost(const String& hostName)Virtual function called by the default implementation of getPRoxyForHost() when deciding whether to use a proxy server to contact a HTTP origin server. |
| Methods inherited from class ot::ManagedObject |
addRef(), getRefCount(), onFinalRelease(), operator=(const ManagedObject&), release() |
| Methods inherited from class ot::SynchronizedObject |
lock(), unlock() |
| Constructor/Destructor Detail |
HttpClientConnectionManager()
HttpClientConnectionManager(const HttpClientConnectionManager& rhs)
rhs - ~HttpClientConnectionManager()
| Method Detail |
void enableActiveConnectionManagement(bool bEnable,
size_t sleepInterval)
bEnable - sleepInterval - UnsupportedOperationException - virtual RefPtr< HttpClientConnection > getConnection(const String& hostName,
int port,
bool bSecure,
HttpProtocolVersion version,
size_t timeoutMS)
hostName - port - bSecure - timeoutMS - SocketException - SocketTimeoutException - UnknownHostException - static RefPtr< HttpClientConnectionManager > GetDefault()
RefPtr< HostnameVerifier > getHostnameVerifier() const
size_t getIdleConnectionTimeout() const
size_t getIdleProxyConnectionTimeout() const
protected virtual bool getProxyForHost(const String& hostName,
bool bSecure,
String& proxyHost,
int& proxyPort)
hostName - bSecure - proxyHost - proxyPort - RefPtr< SocketFactory > getSocketFactory() const
RefPtr< SSLSocketFactory > getSSLSocketFactory() const
UnsupportedOperationException - bool getUsePersistentConnections() const
bool isActiveConnectionManagementEnabled() const
protected virtual void makeAvailable(HttpClientConnection* pConnection)
virtual void manageClosedConnections()
In the multi-threaded build of OpenTop, this method can be called automatically by a background thread by enabling active connection management.
virtual void manageIdleConnections()
In the multi-threaded build of OpenTop, this method can be called automatically by a background thread by enabling active connection management.
static void SetDefault(HttpClientConnectionManager* pConnectionManager)
To ensure that the passed object exists for as long as the application needs it, the HttpClientConnectionManager object is registered with the system's ObjectManager which holds a (counted) reference to it until system termination.
void setHostnameVerifier(HostnameVerifier* pVerifier)
pVerifier - void setIdleConnectionTimeout(size_t secs)
A value of zero indicates that no timeout will occur.
void setIdleProxyConnectionTimeout(size_t secs)
A value of zero indicates that no timeout will occur.
void setProxy(const String& proxyHost,
int proxyPort)
proxyHost - proxyPort - void setSocketFactory(SocketFactory* pFactory)
pFactory - void setSSLSocketFactory(SSLSocketFactory* pFactory)
pFactory - void setUsePersistentConnections(bool bUsePersistentConnections)
bUsePersistentConnections - protected virtual bool useProxyForHost(const String& hostName)
hostName -
|
OpenTop 1.5 | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||||
| SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD | |||||||