Files
externals/linux/include/ActivMiddleware/ActivFramework/RouterService.h
T

349 lines
12 KiB
C++

/**
* @file include/ActivMiddleware/ActivFramework/RouterService.h
*
* @brief Header file for router service class.
*/
#if (!defined ACTIV_FRAMEWORK_ROUTER_SERVICE_H)
#define ACTIV_FRAMEWORK_ROUTER_SERVICE_H
#include "ActivMiddleware/ActivFramework/External.h"
#include "ActivMiddleware/ActivBase/Ip/INetworkAffinityHelper.h"
#include "ActivMiddleware/ActivBase/Ip/IpManager.h"
#include "ActivMiddleware/ActivBase/Ip/NetworkAffinityFilter.h"
#include "ActivMiddleware/ActivBase/Uuid.h"
#include "ActivMiddleware/ActivFramework/Component.h"
#include "ActivMiddleware/ActivFramework/HeapPool.h"
#include "ActivMiddleware/ActivFramework/ManagedEndPoint.h"
#include "ActivMiddleware/ActivFramework/PostMessageHelper.h"
#include "ActivMiddleware/ActivFramework/UiComponent.h"
#if (defined ACTIV_PLATFORM_LINUX)
#include "ActivMiddleware/ActivBase/Linux/OnloadHelper.h"
#endif
#include "ActivMiddleware/CxxYZ/vector.h"
//! Activ namespace.
namespace Activ
{
class UiIo;
/**
* @brief Router service class.
*/
class RouterService : private ManagedEndPoint, public Component
{
public:
using Component::GetApplication;
using ManagedEndPoint::GetEndPointName;
/**
* @brief Settings.
*/
class Settings : public IpManager::Settings
{
public:
/**
* @brief Constructor.
*
* @param defaultServiceName
*/
ACTIV_FRAMEWORK_API Settings(const String &defaultServiceName);
/**
* @brief Read settings from an ini file section.
*
* @param sectionIterator iterator to an ini file section.
*
* @throw Exception
*/
ACTIV_FRAMEWORK_API void Read(const IniFileParser::ConstSectionIterator &sectionIterator);
/**
* @brief How to distribute sockets across threads.
*/
enum ThreadDistributionMode
{
THREAD_DISTRIBUTION_MODE_BY_HEAP_ID, ///< Distribute sockets across threads by user's heap id.
THREAD_DISTRIBUTION_MODE_BY_ENDPOINT_ID, ///< Distribute sockets across threads by user's endpoint id.
THREAD_DISTRIBUTION_MODE_LEAST_REQUESTS, ///< Distribute to thread that has handled the least number of user requests.
THREAD_DISTRIBUTION_MODE_DEFINED_THREAD, ///< Distribute to the thread defined by url parameter or DefaultDistributionThread if configured in settings.
THREAD_DISTRIBUTION_MODE_DEFAULT = THREAD_DISTRIBUTION_MODE_BY_HEAP_ID
};
String m_serviceName;
ManagedEndPoint::Settings m_managerManagedEndPointSettings; ///< Settings for the main RouterService endpoint.
ManagedEndPoint::Settings m_managedEndPointSettings; ///< Settings for the impl RouterService endpoints.
/**
* @brief Default: true. When true the ProcessorAffinity in @a m_managedEndPointSettings will be used to tie each
* IpManager to a single core. When false all IpManagers will be bound to the same set of cores.
*/
bool m_assignPerCoreAffinity;
IHeap::Settings m_serviceHeapSettings; ///< Properties for main heap.
IHeap::Settings m_notificationHeapSettings; ///< Properties for notification heaps.
IHeap::Settings m_implNotificationHeapSettings; ///< Properties for impl notification heaps.
bool m_useHardwareZlib; ///< Use hardware zlib card if available? (default: true).
uint32_t m_lazyTimerInterval; ///< Interval of the lazy timer.
size_t m_numberOfIpManagers; ///< How many ip managers to use.
ThreadDistributionMode m_threadDistributionMode; ///< How to distribute sockets across threads.
size_t m_defaultDistributionThread; ///< Default: 0. If using THREAD_DISTRIBUTION_MODE_DEFINED_THREAD, use this thread for all connections unless overridden.
PostMessageHelper::Settings m_rxPostMessageHelperSettings; ///< Settings for buffering of received messages.
UInt m_noHeartbeatBreakInterval; ///< Default: uninitialized. Break a connection if a heartbeat isn't received in this number of seconds (on supported protocols).
bool m_shouldAllowLocalCircuitShortcut; ///< Default: true. If two TCP connected components are in the same system use endpoint comms and avoid the IP layer.
size_t m_serverConnectionCircuitLimit; ///< Default: 1000. Maximum number of circuits allowed on a single server connection.
// Proxy configuration.
enum ProxyMode
{
PROXY_MODE_NONE, ///< No proxy for HTTP tunnelling.
PROXY_MODE_AUTO_CONFIGURE_FROM_DEFAULT_BROWSER, ///< Attempt to determine proxy settings from default browser.
PROXY_MODE_AUTO_CONFIGURE_FROM_IE, ///< Attempt to determine proxy settings from Internet Explorer.
PROXY_MODE_MANUAL ///< From m_proxyAddress.
};
ProxyMode m_proxyMode;
String m_proxyAddress; ///< For PROXY_MODE_MANUAL.
String m_proxyUser; ///< Optional for other than PROXY_MODE_NONE.
String m_proxyPassword; ///< Ditto.
bool m_shouldTryHttpConnect; ///< Whether to try HTTP CONNECT in automatic tunnelling protocols. Requires a proxy. Default: false.
String m_clientAccessPolicyFilename; ///< For Silverlight clients.
size_t m_heartbeatInterval; ///< Interval (in ms) between heartbeats for pure connections. Default 10000.
size_t m_heartbeatIntervalHttpT2; ///< Interval (in ms) between heartbeats for HTTP t2 tunnelled connections from the client-side. Default 60000.
size_t m_connectTimeout; ///< Time (in ms) to give client-side connections to establish. 0 to leave as OS default. Default 0.
NetworkAffinityFilter::Settings m_networkAffinityFilterSettings; ///< Default network affinity filter to apply to all RouterService sockets.
};
/**
* @brief Router service ui component.
*/
class UiComponent ACTIV_FINAL : public Activ::UiComponent
{
public:
/**
* @brief Constructor for system-wide RouterService ui.
*
* @param routerService the router service to have the ui.
* @param uiServiceName name of the ui service.
*
* @throw Exception.
*/
ACTIV_FRAMEWORK_API UiComponent(RouterService &routerService, const String &uiServiceName);
/**
* @brief Constructor for process-local RouterService ui.
*
* @param routerService the router service to have the ui.
* @param parentUiComponent UiComponent to contain this component.
* @param uiServiceName name of the ui service.
*
* @throw Exception.
*/
ACTIV_FRAMEWORK_API UiComponent(RouterService &routerService, const Activ::UiComponent &parentUiComponent,
const String &uiServiceName);
private:
virtual void Run(UiUser &uiUser) ACTIV_OVERRIDE;
RouterService & m_routerService;
CxxYZ::vector<CxxYZ::shared_ptr<Activ::UiComponent> > m_implUiComponentList;
// Last member.
Component::Enabler m_componentEnabler;
};
/**
* @brief Constructor.
*
* @param application the application to run in.
* @param settings settings for service.
*
* @throw Exception
*/
ACTIV_FRAMEWORK_API RouterService(Application &application, const Settings &settings);
/**
* @brief Destructor.
*/
ACTIV_FRAMEWORK_API ~RouterService();
/**
* @brief Subscribe all implementation objects to endpoint notification messages.
*/
ACTIV_FRAMEWORK_API void SubscribeToEndPointMessages();
/**
* @brief Unsubscribe all implementation objects from endpoint notification messages.
*/
ACTIV_FRAMEWORK_API void UnsubscribeFromEndPointMessages();
class Impl;
protected:
virtual StatusCode OnMessage(HeapMessage &message) ACTIV_OVERRIDE;
virtual StatusCode OnRequest(HeapMessage &request) ACTIV_OVERRIDE;
virtual void OnThreadStart() ACTIV_OVERRIDE;
private:
/**
* @brief Process endpoint die message.
*
* @param message
* @param type string to display ("shutdown" or "died").
*/
void ProcessSystemEndPointIdSetMessage(HeapMessage &message, const char * const type);
/**
* @brief Process a request and distribute it to a worker thread based on heap id in the request.
*
* @param request
*
* @throw Exception.
*/
template<typename T>
void ProcessRequest(HeapMessage &request);
/**
* @brief Process a start listening request.
*
* @param request
*
* @throw Exception.
*/
void ProcessStartListenRequest(HeapMessage &request);
/**
* @brief Forward a request to an Impl determined by heap or endpoint id.
*
* @param heapMessage
* @param heapId
* @param threadDistributionMode
*/
void ForwardRequest(HeapMessage &heapMessage, const HeapId heapId,
const Settings::ThreadDistributionMode threadDistributionMode);
/**
* @brief Forward a request to a specified Impl index.
*
* @param heapMessage
* @param impleIndex
*/
void ForwardRequest(HeapMessage &heapMessage, const size_t implIndex);
/**
* @brief Process a get endpoint id set request from the user.
*
* @param request
*/
void ProcessUserGetEndPointIdSetRequest(HeapMessage &request) const;
/**
* @brief Process an add network affinity filter request from the user.
*
* @param request
* @param isRequest
*/
void ProcessUserAddNetworkAffinityFilterRequest(HeapMessage &request, const bool isRequest);
/**
* @brief Process a remove network affinity filter request from the user.
*
* @param request
* @param isRequest
*/
void ProcessUserRemoveNetworkAffinityFilterRequest(HeapMessage &request, const bool isRequest);
/**
* @brief Get the Impl to use based on heap or endpoint id.
*
* @param heapId
* @param endPointId
* @param threadDistributionMode
*
* @return Index of the Impl.
*/
size_t GetImplIndex(const HeapId heapId, const EndPointId endPointId,
const Settings::ThreadDistributionMode threadDistributionMode);
//! Display statistics.
void UiDisplayStatistics(const UiIo &uiIo) const;
#if (defined ACTIV_PLATFORM_POSIX)
//! Display servers summary.
void UiDisplayServersSummary(const UiIo &uiIo) const;
//! Display server details.
void UiDisplayServerDetails(const UiIo &uiIo) const;
#endif
//! Display network affinity filters.
void UiDisplayNetworkAffinityFilters(const UiIo &uiIo) const;
//! Display unsupported affinity filters.
void UiDisplayUnsupportedNetworkAffinityFilters(const UiIo &uiIo) const;
class ServerComponent;
/**
* @brief Info about the Impl objects maintained in the main thread.
*/
class ImplInfo
{
public:
/**
* @brief Default constructor.
*/
ImplInfo();
size_t m_numberOfForwardedRequests;
};
typedef CxxYZ::shared_ptr<Impl> ImplPtr;
typedef std::pair<ImplInfo, ImplPtr> ImplPair;
typedef std::vector<ImplPair> ImplList;
typedef std::map<size_t, size_t> ImplIndexMap;
typedef CxxYZ::shared_ptr<ServerComponent> ServerComponentPtr;
typedef std::map<Address, ServerComponentPtr> ServerComponentMap;
const Uuid m_uuid; ///< Unique ID of this router.
const Settings::ThreadDistributionMode m_threadDistributionMode; ///< How to distribute sockets across threads.
const size_t m_defaultDistributionThread; ///< If using THREAD_DISTRIBUTION_MODE_DEFINED_THREAD, use this thread for all connections unless overridden.
ImplList m_implList;
ImplIndexMap m_implIndexMap; ///< Heap/endpoint id to Impl index map.
ServerComponentMap m_serverComponentMap;
typedef std::set<NetworkAffinityFilter> NetworkAffinityFilterSet;
NetworkAffinityFilterSet m_networkAffinityFilterSet; ///< Set of active network affinity filters.
NetworkAffinityFilterSet m_unsupportedNetworkAffinityFilterSet; ///< Set of requested but unsupported network affinity filters.
const CxxYZ::unique_ptr<INetworkAffinityHelper>
m_pNetworkAffinityHelper;
#if (defined ACTIV_PLATFORM_POSIX)
IpManager m_ipManager;
HeapPool m_implNotificationHeapPool; ///< Pool of heaps for critical impl notifications.
#if (defined ACTIV_PLATFORM_LINUX)
OnloadHelper m_onloadHelper;
#endif // (defined ACTIV_PLATFORM_LINUX)
#endif // (defined ACTIV_PLATFORM_POSIX)
// Last member.
Component::Enabler m_componentEnabler;
};
} // namespace Activ
#endif // (!defined ACTIV_FRAMEWORK_ROUTER_SERVICE_H)