Files

342 lines
10 KiB
C++

/**
* @file include/ActivMiddleware/ActivFramework/Service.h
*
* @brief Header file for service class.
*/
#if (!defined ACTIV_FRAMEWORK_SERVICE_H)
#define ACTIV_FRAMEWORK_SERVICE_H
#include "ActivMiddleware/ActivFramework/External.h"
#include "ActivMiddleware/ActivFramework/Component.h"
#include "ActivMiddleware/ActivFramework/Messages.h"
#include "ActivMiddleware/ActivFramework/ServiceTypes.h"
#include "ActivMiddleware/CxxYZ/list.h"
//! Activ namespace.
namespace Activ
{
namespace ServiceSettingsKeywords
{
using namespace Activ::ComponentSettingsKeywords;
namespace tag
{
using namespace Activ::ComponentSettingsKeywords::tag;
} // namespace tag
// Service::Settings fields.
ACTIV_BOOST_PARAMETER_NAME(serviceId)
ACTIV_BOOST_PARAMETER_NAME(serviceAccessPointList)
ACTIV_BOOST_PARAMETER_NAME(serviceAttributes)
} // namespace ServiceSettingsKeywords
template<typename T>
class ChildService;
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Service class.
*/
class Service : public Component
{
public:
class SettingsImpl : public Component::SettingsImpl
{
public:
//! Copy constructor.
SettingsImpl(const SettingsImpl &settings);
//! Boost parameter constructor.
template<typename ARGS>
SettingsImpl(const ARGS &args);
ServiceInfo m_serviceInfo;
ACTIV_DECLARE_PARAMETER_ACCESSOR(ServiceSettingsKeywords, serviceId)
ACTIV_DECLARE_PARAMETER_ACCESSOR(ServiceSettingsKeywords, serviceAccessPointList)
ACTIV_DECLARE_PARAMETER_ACCESSOR(ServiceSettingsKeywords, serviceAttributes)
};
/**
* @brief Optional settings for a Service.
*
* name name of the service.
* pHeap heap to use for messaging, or 0 (the default) for the calling application's heap.
* licenseId the service's license id.
*
* This uses Boost's parameter library and can be used as follows:
*
* Service::Settings settings(Service::Settings::name() = "Test", Service::Settings::serviceId() = "My.Service");
*/
class Settings : public SettingsImpl
{
public:
//! Copy constructor.
Settings(const Settings &settings);
// Boost parameter constructor.
ACTIV_BOOST_PARAMETER_CONSTRUCTOR(
Settings, (SettingsImpl), ServiceSettingsKeywords::tag,
(required
(name, *)
(serviceId, *)
(serviceAccessPointList, *)
)
(optional
(serviceAttributes, *)
(portId, *)
(heap, *)
(licenseId, *)
(callbackInfoList, *)
)
)
};
//! An empty access point list for a service.
ACTIV_FRAMEWORK_API static const ServiceAccessPointList emptyServiceAccessPointList;
/**
* @brief Constructor.
*
* Attaches this object to a managed endpoint.
*
* @param managedEndPoint reference to a ManagedEndPoint object to which this component is to be attached.
* @param settings
*
* @throw Exception.
*/
ACTIV_FRAMEWORK_API Service(ManagedEndPoint &managedEndPoint, const SettingsImpl &settings);
/**
* @brief Constructor.
*
* Attaches this service to a managed endpoint at the next available slot. The service will get allocated
* a uuid.
*
* @param managedEndPoint reference to a ManagedEndPoint object to which this component is to be attached.
* @param name name of the service.
* @param serviceInfo service id and access points for this service.
* @param pHeap heap to use for messaging, or 0 for the system heap.
* @param componentId the component id.
*
* @throw Exception.
*/
ACTIV_FRAMEWORK_API Service(ManagedEndPoint &managedEndPoint, const String &name, const ServiceInfo &serviceInfo,
IHeap * const pHeap = 0, const String &componentId = defaultComponentId);
//! Destructor.
ACTIV_FRAMEWORK_API ~Service();
//! Get the service id.
ACTIV_FRAMEWORK_API const ServiceId &GetServiceId() const;
//! Get the service access point list.
ACTIV_FRAMEWORK_API const ServiceAccessPointList &GetServiceAccessPointList() const;
//! Get the ServiceInstance.
ACTIV_FRAMEWORK_API const ServiceInstance &GetServiceInstance() const;
/**
* @brief Register the service with any configured directory services.
*
* @retval STATUS_CODE_SUCCESS.
* @retval ...
*/
ACTIV_FRAMEWORK_API virtual StatusCode Register();
/**
* @brief Deregister the service from any directory services.
*
* @retval STATUS_CODE_SUCCESS.
* @retval ...
*/
ACTIV_FRAMEWORK_API virtual StatusCode Deregister();
/**
* @brief Update the service's attributes.
*
* Note as with all other Service methods, this function is not thread-aware. It is up to the user to ensure that
* concurrent calls to this method are not made.
*
* @param serviceAttributes
*
* @return STATUS_CODE_SUCCESS or an error status if unable to notify the DirectoryService of the change.
*/
ACTIV_FRAMEWORK_API StatusCode UpdateAttributes(const ServiceAttributes &serviceAttributes);
//! Close all access points and accepted connections.
ACTIV_FRAMEWORK_API void Close();
//! Break accepted connections across all servers.
ACTIV_FRAMEWORK_API void BreakAll();
//! Get the set of RouterService endpoint ids that will be handling all of this service's access points.
ACTIV_FRAMEWORK_API void GetRouterServiceEndPointIdSet(EndPointIdSet &endPointIdSet) const;
protected:
/**
* @brief A client is attempting to connect.
*
* The default behaviour is to call ProcessOpenServiceRequest(). See Component::OnAccept() for more information.
*
* @param acceptMessage
*
* @return Unused by the framework.
*/
ACTIV_FRAMEWORK_API virtual StatusCode OnAccept(HeapMessage &acceptMessage) ACTIV_OVERRIDE;
/**
* @brief Process an open service request.
*
* This method is protected to allow processing of extended open service requests. By default it validates
* again the default open service request and builds a default open service response.
*
* @param acceptMessage as passed to OnAccept().
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_FRAMEWORK_API virtual StatusCode ProcessOpenServiceRequest(HeapMessage &acceptMessage);
/**
* @brief Validate a received open service message.
*
* An open service message is contained in the body of the message passed to OnAccept().
*
* @param acceptMessage as passed to OnAccept().
* @param openServiceRequest message to deserialize into.
*
* @retval STATUS_CODE_SUCCESS.
* @retval ...
*/
ACTIV_FRAMEWORK_API StatusCode ValidateOpenServiceRequest(const HeapMessage &acceptMessage,
OpenServiceRequest &openServiceRequest) const;
/**
* @brief Build a response to an open service request.
*
* @param acceptMessage as passed to OnAccept() and validated by ValidateOpenServiceRequest().
* @param openServiceResponse message to serialize as the response.
* @param openServiceVersion version of the open service message.
*
* @retval STATUS_CODE_SUCCESS.
* @retval ...
*/
ACTIV_FRAMEWORK_API StatusCode BuildOpenServiceResponse(HeapMessage &acceptMessage, OpenServiceResponse &openServiceResponse,
const byte_t openServiceVersion) const;
private:
//! List of servers.
class AccessList
{
public:
/**
* @brief Constructor.
*
* @param service
*/
AccessList(Service &service);
//! Destructor.
~AccessList();
/**
* @brief Add a server address to the list.
*
* @param serverAddress address of server to add.
*/
void Add(const Address &serverAddress);
//! Clear access list. Stop listening and break accepted connections.
void Clear();
//! Break accepted connections across all servers.
void BreakAll();
//! Get the set of RouterService endpoint ids that will be handling all of this service's access points.
void GetRouterServiceEndPointIdSet(EndPointIdSet &endPointIdSet) const;
private:
typedef CxxYZ::list<Address> ServerList;
Service & m_service;
ServerList m_serverList;
};
/**
* @brief Constructor for ChildService.
*
* @param parentService reference to the parent service.
* @param managedEndPoint reference to a ManagedEndPoint object to which this port is to be attached.
* @param pHeap heap to use for messaging, or 0 for the parent service heap.
* @param name name of the Port, local to the endpoint.
*
* @throw Exception.
*/
ACTIV_FRAMEWORK_API Service(Service &service, ManagedEndPoint &managedEndPoint, IHeap * const pHeap, const String &name);
/**
* @brief Make ServiceInstance object with normalized access points.
*
* @param serviceInfo
* @param uuid
* @param address
*
* @return Normalized ServiceInstance.
*/
const ServiceInstance MakeServiceInstance(const ServiceInfo &serviceInfo, const Uuid &uuid, const Address &address);
/**
* @brief Make ServiceInstance object with no access points or attributes for a ChildService.
*
* @param parentServiceInstance
*
* @return ServiceInstance.
*/
static const ServiceInstance MakeChildServiceInstance(const ServiceInstance &parentServiceInstance);
AccessList m_accessList; ///< List of servers this service is available on.
ServiceInstance m_serviceInstance; ///< Service id, access points.
template<typename T>
friend class ChildService;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline Service::SettingsImpl::SettingsImpl(const SettingsImpl &settings) :
Component::SettingsImpl(static_cast<const Component::SettingsImpl &>(settings)),
m_serviceInfo(settings.m_serviceInfo)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename ARGS>
inline Service::SettingsImpl::SettingsImpl(const ARGS &args) :
Component::SettingsImpl(args),
m_serviceInfo(args[ServiceSettingsKeywords::_serviceId], args[ServiceSettingsKeywords::_serviceAccessPointList],
args[ServiceSettingsKeywords::_serviceAttributes || Boost::value_factory<ServiceAttributes>()])
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline Service::Settings::Settings(const Settings &settings) :
Service::SettingsImpl(static_cast<const SettingsImpl &>(settings))
{
}
} // namespace Activ
#endif // (!defined ACTIV_FRAMEWORK_SERVICE_H)