/** * @file include/ActivMiddleware/ActivFramework/RequestId.h * * @brief Header file for request id class. */ #if (!defined ACTIV_FRAMEWORK_REQUEST_ID_H) #define ACTIV_FRAMEWORK_REQUEST_ID_H #include "ActivMiddleware/ActivFramework/External.h" #include "ActivMiddleware/ActivBase/ActivBase.h" #include "ActivMiddleware/ActivBase/StatusCodeException.h" #include "ActivMiddleware/ActivFramework/Address.h" #include "ActivMiddleware/Misc/MemoryHelper.h" #include ACTIV_BOOST_INCLUDE(typeof/typeof.hpp) #include //! Activ namespace. namespace Activ { class SyncRequestId; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Request id class. */ class RequestId { public: typedef uint32_t SendId; ///< Request id type for synchronous calls. typedef byte_t Attribute; ///< Attribute type. static const Attribute ATTRIBUTE_NONE = 0x00; static const Attribute ATTRIBUTE_ON = 0x01; static const Attribute ATTRIBUTE_RESPONSE = 0x02; static const Attribute ATTRIBUTE_USE_RESPONSE_ADDRESS = 0x04; static const byte_t MAX_LENGTH = 16; ///< Large enough for a UUID. // Default constructor. RequestId(); /** * @brief Constructor. * * @param pData data to set * @param dataSize size of data in pData. */ RequestId(const void *pData, const size_t dataSize); /** * @brief Constructor. * * @param pData data to set. * @param dataSize size of data in pData. * @param responseAddress address to route response to request. */ RequestId(const void *pData, const size_t dataSize, const Address &responseAddress); /** * @brief Constructor. * * @param value to set. */ template RequestId(const T value, typename Boost::enable_if >::type* dummy = 0); /** * @brief Constructor. * * @param value to set. * @param responseAddress address to route response to request. */ template RequestId(const T value, const Address &responseAddress, typename Boost::enable_if >::type* dummy = 0); /** * @brief Copy constructor. * * @param requestId to copy from. */ RequestId(const RequestId &requestId); //! Clear the id. void Clear(); //! Get the length of request id data. size_t GetLength() const; /** * @brief Set the length of data. Truncated if over maxLength. * * @param dataLength length to set to. */ void SetLength(const size_t dataLength); //! Get a pointer to request id data. const byte_t *Get() const; /** * @brief Get integral value. * * @param value * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_LENGTH */ template typename Boost::enable_if, StatusCode>::type Get(T &value) const; /** * @brief Get integral value. * * @return value * * @throw StatusCodeException */ template typename Boost::enable_if, T>::type Get() const; /** * @brief Set the data. * * @param pData data to set * @param dataLength size of data in pData. */ void Set(const void *pData, const size_t dataLength); /** * @brief Set the data. * * @param pData data to set * @param dataLength size of data in pData. * @param responseAddress address to route response to request. */ void Set(const void *pData, const size_t dataLength, const Address &responseAddress); /** * @brief Set an integral value. * * @param value */ template typename Boost::enable_if >::type Set(const T value); /** * @brief Set an integral value. * * @param value * @param responseAddress address to route response to request. */ template typename Boost::enable_if >::type Set(const T value, const Address &responseAddress); /** * @brief Equality operator. * * @return true if this is equal to rhs. */ bool operator==(const RequestId &rhs) const; /** * @brief Inequality operator. * * @return true if this is not equal to rhs. */ bool operator!=(const RequestId &rhs) const; /** * @brief Less than operator. * * @param rhs RequestId to compare with. * * @retval true if this is less than rhs. * @retval false if this is greater than or equal to rhs. */ bool operator<(const RequestId &rhs) const; /** * @brief Assignment operator. * * @param rhs to assign from. */ RequestId &operator=(const RequestId &rhs); //! Get the attribute bits. Attribute GetAttributes() const; /** * @brief Set the attribute bits. * * @param attributes the attributes. */ void SetAttributes(Attribute attributes); //! Get the response address. const Address &GetResponseAddress() const; //! Set the response address. void SetResponseAddress(const Address &responseAddress); //! Get the serialized length of the RequestId. size_t GetSerializedLength() const; /** * @brief Serialize the request id. * * @param messageBuilder reference to MessageBuilder to serialize with. * * @retval STATUS_CODE_SUCCESS * @retval ... */ StatusCode Serialize(MessageBuilder &messageBuilder) const; /** * @brief Deserialize the request id. * * @param messageValidator reference to MessageValidator to deserialize with. * * @retval STATUS_CODE_SUCCESS * @retval ... */ StatusCode Deserialize(MessageValidator &messageValidator); //! Convert the request id to a printable string. String ToString() const; //! Is the message model request/response? bool IsRequestOrResponse() const; //! Get a string representation of the message model (req/resp or other). String MessageModelToString() const; /** * @brief Get a string representation of the message model (req/resp or other). * * @param attributes attributes from a request id. * * @return String representation of the message model. */ static String MessageModelToString(const Attribute attributes); ACTIV_FRAMEWORK_API static const RequestId emptyRequestId; ///< An empty request id. private: /** * @brief Set the data. * * @param pData data to set * @param dataLength size of data in pData. */ void CommonSet(const void *pData, const size_t dataSize); /** * @brief Compare this with another RequestId. * * @param rhs RequestId to compare with. * * @retval -ve if this is less than rhs. * @retval 0 if this equal to rhs. * @retval +ve if this is greater than rhs. */ int Compare(const RequestId &rhs) const; Attribute m_attributes; ///< Attribute byte. Address m_responseAddress; ///< Optional response address. byte_t m_length; ///< Bytes used in buffer. byte_t m_buffer[MAX_LENGTH]; ///< The request id buffer. friend class SyncRequestId; }; // --------------------------------------------------------------------------------------------------------------------------------- inline RequestId::RequestId() { Clear(); } // --------------------------------------------------------------------------------------------------------------------------------- inline RequestId::RequestId(const void *pData, const size_t dataSize) { Set(pData, dataSize); } // --------------------------------------------------------------------------------------------------------------------------------- inline RequestId::RequestId(const void *pData, const size_t dataLength, const Address &responseAddress) { Set(pData, dataLength, responseAddress); } // --------------------------------------------------------------------------------------------------------------------------------- template inline RequestId::RequestId(const T t, typename Boost::enable_if >::type* dummy) { Set(t); } // --------------------------------------------------------------------------------------------------------------------------------- template inline RequestId::RequestId(const T t, const Address &responseAddress, typename Boost::enable_if >::type* dummy) { Set(t, responseAddress); } // --------------------------------------------------------------------------------------------------------------------------------- inline RequestId::RequestId(const RequestId &requestId) { *this = requestId; } // --------------------------------------------------------------------------------------------------------------------------------- inline void RequestId::Clear() { m_attributes = 0; m_responseAddress.Clear(); m_length = 0; } // --------------------------------------------------------------------------------------------------------------------------------- inline size_t RequestId::GetLength() const { return m_length; } // --------------------------------------------------------------------------------------------------------------------------------- inline void RequestId::SetLength(const size_t dataLength) { m_length = static_cast(std::min(dataLength, sizeof(m_buffer))); } // --------------------------------------------------------------------------------------------------------------------------------- inline const byte_t *RequestId::Get() const { return m_buffer; } // --------------------------------------------------------------------------------------------------------------------------------- template inline typename Boost::enable_if, StatusCode>::type RequestId::Get(T &value) const { if (ACTIV_UNLIKELY(sizeof(T) != GetLength())) return STATUS_CODE_INVALID_LENGTH; value = GetValueFromUnalignedPointer(Get()); return STATUS_CODE_SUCCESS; } // --------------------------------------------------------------------------------------------------------------------------------- template inline typename Boost::enable_if, T>::type RequestId::Get() const { if (ACTIV_UNLIKELY(sizeof(T) != GetLength())) ACTIV_THROW(StatusCodeException, STATUS_CODE_INVALID_LENGTH); return GetValueFromUnalignedPointer(Get()); } // --------------------------------------------------------------------------------------------------------------------------------- inline void RequestId::Set(const void *pData, const size_t dataLength) { CommonSet(pData, dataLength); m_responseAddress.Clear(); } // --------------------------------------------------------------------------------------------------------------------------------- inline void RequestId::Set(const void *pData, const size_t dataLength, const Address &responseAddress) { CommonSet(pData, dataLength); if (responseAddress.IsValid()) { m_attributes |= ATTRIBUTE_USE_RESPONSE_ADDRESS; m_responseAddress = responseAddress; } else { m_responseAddress.Clear(); } } // --------------------------------------------------------------------------------------------------------------------------------- template inline typename Boost::enable_if >::type RequestId::Set(const T value) { Set(&value, sizeof(value)); } // --------------------------------------------------------------------------------------------------------------------------------- template inline typename Boost::enable_if >::type RequestId::Set(const T value, const Address &responseAddress) { Set(&value, sizeof(value), responseAddress); } // --------------------------------------------------------------------------------------------------------------------------------- inline bool RequestId::operator==(const RequestId &rhs) const { return 0 == Compare(rhs); } // --------------------------------------------------------------------------------------------------------------------------------- inline bool RequestId::operator!=(const RequestId &rhs) const { return 0 != Compare(rhs); } // --------------------------------------------------------------------------------------------------------------------------------- inline bool RequestId::operator<(const RequestId &rhs) const { return Compare(rhs) < 0; } // --------------------------------------------------------------------------------------------------------------------------------- inline RequestId &RequestId::operator=(const RequestId &rhs) { if (this != &rhs) { m_attributes = rhs.m_attributes; m_responseAddress = rhs.m_responseAddress; m_length = rhs.m_length; MemoryCopy(m_buffer, rhs.m_buffer, m_length); } return *this; } // --------------------------------------------------------------------------------------------------------------------------------- inline RequestId::Attribute RequestId::GetAttributes() const { return m_attributes; } // --------------------------------------------------------------------------------------------------------------------------------- inline void RequestId::SetAttributes(Attribute attributes) { m_attributes = attributes; } // --------------------------------------------------------------------------------------------------------------------------------- inline const Address &RequestId::GetResponseAddress() const { return m_responseAddress; } // --------------------------------------------------------------------------------------------------------------------------------- inline void RequestId::SetResponseAddress(const Address &responseAddress) { m_responseAddress = responseAddress; } // --------------------------------------------------------------------------------------------------------------------------------- inline size_t RequestId::GetSerializedLength() const { return sizeof(m_attributes) + m_responseAddress.GetSerializedLength() + sizeof(m_length) + m_length; } // --------------------------------------------------------------------------------------------------------------------------------- inline StatusCode RequestId::Serialize(MessageBuilder &messageBuilder) const { ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageBuilder.Append(m_attributes)); ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, m_responseAddress.Serialize(messageBuilder)); ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageBuilder.Append(m_length)); ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageBuilder.AppendBuffer(m_buffer, m_length)); return STATUS_CODE_SUCCESS; } // --------------------------------------------------------------------------------------------------------------------------------- inline StatusCode RequestId::Deserialize(MessageValidator &messageValidator) { ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (messageValidator.ValidateBinaryIntegral(m_attributes))); ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, m_responseAddress.Deserialize(messageValidator)); ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (messageValidator.ValidateBinaryIntegralRange< sizeof(m_length), MessageValidator::ENDIAN_LITTLE, ACTIV_DECLTYPE(m_length), 0, sizeof(m_buffer)>(m_length))); ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageValidator.ValidateBytesCopy(m_length, &m_buffer)); return STATUS_CODE_SUCCESS; } // --------------------------------------------------------------------------------------------------------------------------------- inline String RequestId::ToString() const { char buff[4]; String output; for (size_t i = 0; i < m_length; ++i) { (void) Snprintf(buff, ACTIV_ARRAY_SIZE(buff), "%s%02x", (i > 0) ? " " : "", m_buffer[i]); output += buff; } return output; } // --------------------------------------------------------------------------------------------------------------------------------- inline bool RequestId::IsRequestOrResponse() const { return (m_attributes & ATTRIBUTE_ON); } // --------------------------------------------------------------------------------------------------------------------------------- inline String RequestId::MessageModelToString() const { return MessageModelToString(m_attributes); } // --------------------------------------------------------------------------------------------------------------------------------- inline String RequestId::MessageModelToString(const Attribute attributes) { if (attributes & ATTRIBUTE_ON) { if (attributes & ATTRIBUTE_RESPONSE) return "response"; else return "request"; } else { return "message"; } } // --------------------------------------------------------------------------------------------------------------------------------- inline void RequestId::CommonSet(const void *pData, const size_t dataLength) { assert(dataLength <= sizeof(m_buffer)); // Clear down attributes. Request / response is set by Component Send/Post methods. m_attributes = ATTRIBUTE_NONE; m_length = static_cast(std::min(dataLength, sizeof(m_buffer))); MemoryCopy(m_buffer, pData, m_length); } // --------------------------------------------------------------------------------------------------------------------------------- inline int RequestId::Compare(const RequestId &rhs) const { // The attribute bits do not take part in the comparison operation (a returned request id may have // different bits set). Same applies for response address. Comparison is only on the body of the request id. if (m_length != rhs.m_length) { const size_t compareLength = std::min<>(m_length, rhs.m_length); const int compareValue = ::memcmp(m_buffer, rhs.m_buffer, compareLength); if (0 == compareValue) return (m_length == compareLength) ? -1 : 1; else return compareValue; } else { return ::memcmp(m_buffer, rhs.m_buffer, m_length); } } } // namespace Activ #endif // (!defined ACTIV_FRAMEWORK_REQUEST_ID_H)