368 lines
11 KiB
C++
368 lines
11 KiB
C++
/**
|
|
* @file source/ActivMiddleware/ActivFramework/PacketServerHelper.h
|
|
*
|
|
* @brief Header file for Packet Server Messages, Subscriber and Publisher.
|
|
*/
|
|
|
|
#if (!defined ACTIV_FRAMEWORK_PACKET_SERVER_HELPER_H)
|
|
#define ACTIV_FRAMEWORK_PACKET_SERVER_HELPER_H
|
|
|
|
#include "ActivMiddleware/ActivBase/ISerializableMessage.h"
|
|
#include "ActivMiddleware/ActivBase/MessageBuilder.h"
|
|
#include "ActivMiddleware/ActivBase/MessageValidator.h"
|
|
|
|
#include "ActivMiddleware/ActivBase/Ip/IpAddress.h"
|
|
|
|
#include "ActivMiddleware/ActivFramework/ManagedEndPoint.h"
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
namespace PacketServerHelper
|
|
{
|
|
|
|
/**
|
|
* @brief The list of possible packet server request types and their strings.
|
|
*
|
|
* Note this list is converted to an enum using the ACTIV_DECLARE_ELEMENT_1 macro in ActivMiddleware.h
|
|
*/
|
|
#define ACTIV_FRAMEWORK_PACKET_SERVER_REQUEST_LIST(d) \
|
|
d(PACKET_SERVER_SUBSCRIBER, "subscriber") \
|
|
d(PACKET_SERVER_PUBLISHER, "publisher") \
|
|
d(PACKET_SERVER_UNKNOWN, "unknown")
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief The list of possible packet server tcp connection states and their strings.
|
|
*
|
|
* Note this list is converted to an enum using the ACTIV_DECLARE_ELEMENT_1 macro in ActivMiddleware.h
|
|
*/
|
|
#define PACKET_SERVER_CONNECTION_STATES(d) \
|
|
d(STATE_UNKNOWN, "Unknown") \
|
|
d(STATE_INITIALIZED, "Initialized") \
|
|
d(STATE_DISCONNECTED, "Disconnected") \
|
|
d(STATE_W4_CONNECT, "W4 Connect") \
|
|
d(STATE_W4_BREAK, "W4 Break") \
|
|
d(STATE_W4_DISCONNECT, "W4 Disconnect") \
|
|
d(STATE_CONNECTED, "Connected")
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! The list of possible packet server tcp connection states and their strings.
|
|
enum State
|
|
{
|
|
PACKET_SERVER_CONNECTION_STATES(ACTIV_DECLARE_ELEMENT_1_WITH_COMMA)
|
|
NUM_STATES
|
|
};
|
|
|
|
/**
|
|
* @brief Return tcp state name.
|
|
*
|
|
* @param state the state.
|
|
*
|
|
* @return tcp connection state description.
|
|
*/
|
|
ACTIV_FRAMEWORK_API const String &StateToString(State state);
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Subscriber or publisher settings class.
|
|
*/
|
|
class Settings
|
|
{
|
|
public:
|
|
static const uint32_t defaultSwitchTimeout = 5;
|
|
static const uint32_t defaultConnectFailSleep = 5;
|
|
|
|
/**
|
|
* @brief Default constructor.
|
|
*/
|
|
ACTIV_FRAMEWORK_API Settings();
|
|
|
|
/**
|
|
* @brief Return true if is initialized.
|
|
*
|
|
* @retval true if packet server subscriber setting is initialized.
|
|
* @retval false if packet server subscriber setting is not initialized.
|
|
*/
|
|
ACTIV_FRAMEWORK_API bool IsInitialized() const;
|
|
|
|
/**
|
|
* @brief Read settings from ini file.
|
|
*
|
|
* @param section Ini file section to read.
|
|
* @param prefix the prefix for INI key.
|
|
*
|
|
* @throw Exception
|
|
*/
|
|
ACTIV_FRAMEWORK_API void Read(const IniFileParser::ConstSectionIterator §ion, const String &prefix = String());
|
|
|
|
String m_name; ///< Name of connection.
|
|
std::vector<String> m_tcpUrls; ///< Input url of tcp server to connect to.
|
|
uint32_t m_switchTimeout; ///< Time to wait before switching when no data received.
|
|
uint32_t m_connectFailSleep; ///< How long to sleep when we fail to connect to all urls.
|
|
uint32_t m_ttl; ///< Default: 30. Time to live.
|
|
IHeap::Settings m_heapSettings; ///< The heap settings.
|
|
};
|
|
|
|
/**
|
|
* @brief Packet Server request message from publisher or subscriber to Packet Server.
|
|
*/
|
|
class PacketServerRequest : public ISerializableMessage
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Enumeration of all packet server request types.
|
|
*/
|
|
enum Type
|
|
{
|
|
ACTIV_FRAMEWORK_PACKET_SERVER_REQUEST_LIST(ACTIV_DECLARE_ELEMENT_1_WITH_COMMA)
|
|
NUM_TYPES
|
|
};
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param requestType the request type (subscriber / publisher).
|
|
* @param clientIpAddress the ip address of requestor (subscriber / publisher).
|
|
* @param name the name. For same publisher name there is same udp output port.
|
|
*/
|
|
ACTIV_FRAMEWORK_API PacketServerRequest(const Type requestType = PACKET_SERVER_UNKNOWN,
|
|
const IpAddress &clientIpAddress = IpAddress(), const String &name = String());
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*/
|
|
ACTIV_FRAMEWORK_API virtual ~PacketServerRequest() ACTIV_OVERRIDE;
|
|
|
|
/**
|
|
* @brief Serialize.
|
|
*
|
|
* @param messageBuilder the message builder.
|
|
*/
|
|
ACTIV_FRAMEWORK_API virtual StatusCode Serialize(MessageBuilder &messageBuilder) const ACTIV_OVERRIDE;
|
|
|
|
/**
|
|
* @brief Deserialize.
|
|
*
|
|
* @param messageValidator the message validator.
|
|
*/
|
|
ACTIV_FRAMEWORK_API virtual StatusCode Deserialize(MessageValidator &messageValidator) ACTIV_OVERRIDE;
|
|
|
|
/**
|
|
* @brief Return request type name.
|
|
*
|
|
* @param type the request type.
|
|
*
|
|
* @return request type description.
|
|
*/
|
|
ACTIV_FRAMEWORK_API static const String &TypeToString(Type type);
|
|
|
|
Type m_requestType; ///< The request type.
|
|
IpAddress m_clientIpAddress; ///< The ip address of client.
|
|
String m_name; ///< The name.
|
|
|
|
private:
|
|
class Message;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Packet Server response message from Packet Server to subscriber or publisher.
|
|
*/
|
|
class PacketServerResponse : public ISerializableMessage
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param serverIpAddress the packet server ip address.
|
|
*/
|
|
ACTIV_FRAMEWORK_API PacketServerResponse(const IpAddress &serverIpAddress = IpAddress());
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param message the message.
|
|
*/
|
|
ACTIV_FRAMEWORK_API PacketServerResponse(const HeapMessage &message);
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*/
|
|
ACTIV_FRAMEWORK_API virtual ~PacketServerResponse() ACTIV_OVERRIDE;
|
|
|
|
/**
|
|
* @brief Serialize.
|
|
*
|
|
* @param messageBuilder the message builder.
|
|
*/
|
|
ACTIV_FRAMEWORK_API virtual StatusCode Serialize(MessageBuilder &messageBuilder) const ACTIV_OVERRIDE;
|
|
|
|
/**
|
|
* @brief Deserialize.
|
|
*
|
|
* @param messageValidator the message validator.
|
|
*/
|
|
ACTIV_FRAMEWORK_API virtual StatusCode Deserialize(MessageValidator &messageValidator) ACTIV_OVERRIDE;
|
|
|
|
IpAddress m_serverIpAddress; ///< The ip address of the packet server.
|
|
private:
|
|
class Message;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
class PacketServerCommonImpl;
|
|
|
|
/**
|
|
* @brief Packet server publisher class. Connect to udp Packet Server application.
|
|
*/
|
|
class Publisher
|
|
{
|
|
public:
|
|
typedef CxxYZ::function<void(const IpAddress&)> OnRemoteUdpIpAddress;
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param name the name. For same publisher name there is same udp output port.
|
|
* @param managedEndPoint endpoint to receive messages on.
|
|
* @param settings the packet server settings.
|
|
* @param timeout if nonzero timeout initiate a synchronous connect. If timeout zero then initiate an asynchronous connect.
|
|
* @param onRemoteUdpIpAddress event functor called when remote udp packet server ip address provided.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_FRAMEWORK_API Publisher(const String &name, ManagedEndPoint &managedEndPoint,
|
|
const PacketServerHelper::Settings &settings, const TimeoutPeriod timeout = 0,
|
|
OnRemoteUdpIpAddress onRemoteUdpIpAddress = OnRemoteUdpIpAddress());
|
|
|
|
//! Destructor.
|
|
ACTIV_FRAMEWORK_API virtual ~Publisher();
|
|
|
|
//! Perform shutdown work.
|
|
ACTIV_FRAMEWORK_API void Shutdown();
|
|
|
|
//! Get settings.
|
|
ACTIV_FRAMEWORK_API const PacketServerHelper::Settings &GetSettings() const;
|
|
|
|
//! Get remote udp packet server ip address.
|
|
ACTIV_FRAMEWORK_API const IpAddress &GetRemoteUdpIpAddress();
|
|
|
|
/**
|
|
* @brief Returns current tcp connecntion url.
|
|
*
|
|
* @return Current tcp connection url.
|
|
*
|
|
* @throw StatusCodeException.
|
|
*/
|
|
ACTIV_FRAMEWORK_API const String &GetCurrentUrl() const;
|
|
|
|
/**
|
|
* @brief Returns current tcp connecntion url index.
|
|
*
|
|
* @return Current tcp connection url index.
|
|
*/
|
|
ACTIV_FRAMEWORK_API size_t GetCurrentUrlIndex() const;
|
|
|
|
//! Return current state.
|
|
ACTIV_FRAMEWORK_API State GetState() const;
|
|
|
|
/**
|
|
* @brief Break existing tcp connection end connect to new url index.
|
|
*
|
|
* @param urlIndex new url index.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval ...
|
|
*/
|
|
ACTIV_FRAMEWORK_API StatusCode SwitchUrl(const size_t urlIndex);
|
|
|
|
private:
|
|
CxxYZ::unique_ptr<PacketServerCommonImpl> m_pImpl; ///< Implementation details.
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Packet server subscriber class. Connect to udp Packet Server application.
|
|
*/
|
|
class Subscriber
|
|
{
|
|
public:
|
|
typedef CxxYZ::function<StatusCode(const void * const, const size_t, const IpAddress &)> UdpPostFunction;
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param managedEndPoint endpoint to receive messages on.
|
|
* @param settings the settings for the packet server subscriber.
|
|
* @param interfaceAddress the interface address.
|
|
* @param portNumber the port number.
|
|
* @param timeout On nonzero timeout initiate a synchronous connect. If timeout zero then initiate an asynchronous connect.
|
|
* @param udpPostFunction the udp post function.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_FRAMEWORK_API Subscriber(ManagedEndPoint &managedEndPoint, const PacketServerHelper::Settings &settings,
|
|
const String &interfaceAddress, const uint16_t portNumber, const TimeoutPeriod timeout = 0,
|
|
UdpPostFunction udpPostFunction = UdpPostFunction());
|
|
|
|
//! Destructor.
|
|
ACTIV_FRAMEWORK_API virtual ~Subscriber();
|
|
|
|
//! Perform shutdown work.
|
|
ACTIV_FRAMEWORK_API void Shutdown();
|
|
|
|
//! Get settings.
|
|
ACTIV_FRAMEWORK_API const PacketServerHelper::Settings &GetSettings() const;
|
|
|
|
/**
|
|
* @brief Returns current tcp connecntion url.
|
|
*
|
|
* @return Current tcp connection url.
|
|
*
|
|
* @throw StatusCodeException.
|
|
*/
|
|
ACTIV_FRAMEWORK_API const String &GetCurrentUrl() const;
|
|
|
|
/**
|
|
* @brief Returns current tcp connecntion url index.
|
|
*
|
|
* @return Current tcp connection url index.
|
|
*/
|
|
ACTIV_FRAMEWORK_API size_t GetCurrentUrlIndex() const;
|
|
|
|
//! Return current state.
|
|
ACTIV_FRAMEWORK_API State GetState() const;
|
|
|
|
/**
|
|
* @brief Break existing tcp connection end connect to new url index.
|
|
*
|
|
* @param urlIndex new url index.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval ...
|
|
*/
|
|
ACTIV_FRAMEWORK_API StatusCode SwitchUrl(const size_t urlIndex);
|
|
|
|
private:
|
|
CxxYZ::unique_ptr<PacketServerCommonImpl> m_pImpl; ///< Implementation details.
|
|
};
|
|
|
|
} // namespace PacketServerHelper
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_FRAMEWORK_PACKET_SERVER_HELPER_H)
|