}
/**
+ * @brief Retrieve 1 bit from the payload buffer.
+ *
+ * @param byte_index The index of the byte.
+ * @param bit_index The index of bit within the byte. The indexes are as follow:
+ * [0|1|2|3|4|5|6|7], with the index zero being the left most bit within the byte.
+ *
+ * @return The bit value stored at @a bit_index.
+ */
+ bool MessagePayload::decode1(
+ const int byte_index,
+ const int bit_index
+ ) const
+ {
+ const int max_bit_index = 7;
+ BOOST_ASSERT( ( 0 <= byte_index ) && ( byte_index < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( ( 0 <= bit_index ) && ( bit_index <= max_bit_index ) );
+
+ int shift_amount = max_bit_index - bit_index;
+ uint8_t bit_mask = static_cast<uint8_t>( 0x01U << shift_amount );
+ bool value = static_cast<bool>( Payload[ byte_index ] & bit_mask );
+
+ return value;
+ }
+
+ /**
+ * @brief Store 1 bit in the payload buffer.
+ *
+ * @param byte_index The index of the byte.
+ * @param bit_index The index of bit within the byte.
+ * @param value The bit value to store at @a bit_index.
+ *
+ * @return void
+ */
+ void MessagePayload::encode1(
+ const int byte_index,
+ const int bit_index,
+ const bool value
+ )
+ {
+ const int max_bit_index = 7;
+ BOOST_ASSERT( ( 0 <= byte_index ) && ( byte_index < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( ( 0 <= bit_index ) && ( bit_index <= max_bit_index ) );
+
+ int shift_amount = max_bit_index - bit_index;
+ uint8_t bit_mask = static_cast<uint8_t>( 0x01U << shift_amount );
+
+ if ( value )
+ {
+ Payload[ byte_index ] = Payload[ byte_index ] | bit_mask;
+ }
+ else
+ {
+ bit_mask = static_cast<uint8_t>( ~bit_mask );
+ Payload[ byte_index ] = Payload[ byte_index ] & bit_mask;
+ }
+ }
+
+ /**
* @brief Retrieve 16 bits from the payload buffer.
*
- * @param left_byte the index of the left byte
- * @param right_byte the index of the right byte
+ * @param left_byte_index The index of the left byte.
+ * @param right_byte_index The index of the right byte.
*
- * @return a concatenation of the byte indexed by left_byte with the byte
- * indexed by right_byte.
+ * @return A concatenation of the byte indexed by @a left_byte_index with the byte
+ * indexed by @a right_byte_index.
*/
uint16_t MessagePayload::decode16(
- const int left_byte,
- const int right_byte
+ const int left_byte_index,
+ const int right_byte_index
) const
{
- BOOST_ASSERT( ( 0 <= left_byte ) && ( left_byte < static_cast<int>(PayloadSizeInBytes) ) );
- BOOST_ASSERT( ( 0 <= right_byte ) && ( right_byte < static_cast<int>(PayloadSizeInBytes) ) );
- BOOST_ASSERT( left_byte < right_byte );
- BOOST_ASSERT( ( static_cast<size_t>( right_byte - left_byte ) + 1 ) == sizeof(uint16_t) );
+ BOOST_ASSERT( ( 0 <= left_byte_index ) && ( left_byte_index < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( ( 0 <= right_byte_index ) && ( right_byte_index < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( left_byte_index < right_byte_index );
- BOOST_ASSERT( ( ( right_byte_index - left_byte_index ) + 1 ) == sizeof(uint16_t) );
++ BOOST_ASSERT( ( static_cast<size_t>( right_byte_index - left_byte_index ) + 1 ) == sizeof(uint16_t) );
- uint32_t value = static_cast<uint16_t>( Payload[ left_byte ] << 8 );
- value += static_cast<uint16_t>( Payload[ right_byte ] );
+ uint32_t value = static_cast<uint16_t>( Payload[ left_byte_index ] << 8 );
+ value += static_cast<uint16_t>( Payload[ right_byte_index ] );
BOOST_ASSERT( value <= numeric_limits<uint16_t>::max() );
const uint16_t value
)
{
- BOOST_ASSERT( ( 0 <= left_byte ) && ( left_byte < static_cast<int>(PayloadSizeInBytes) ) );
- BOOST_ASSERT( ( 0 <= right_byte ) && ( right_byte < static_cast<int>(PayloadSizeInBytes) ) );
- BOOST_ASSERT( left_byte < right_byte );
- BOOST_ASSERT( ( static_cast<size_t>( right_byte - left_byte ) + 1 ) == sizeof(uint16_t) );
+ BOOST_ASSERT( ( 0 <= left_byte_index ) && ( left_byte_index < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( ( 0 <= right_byte_index ) && ( right_byte_index < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( left_byte_index < right_byte_index );
- BOOST_ASSERT( ( ( right_byte_index - left_byte_index ) + 1 ) == sizeof(uint16_t) );
++ BOOST_ASSERT( ( static_cast<size_t>( right_byte_index - left_byte_index ) + 1 ) == sizeof(uint16_t) );
- Payload[ left_byte ] = static_cast<uint8_t>( value >> 8 );
- Payload[ right_byte ] = static_cast<uint8_t>( value & 0xFF );
+ Payload[ left_byte_index ] = static_cast<uint8_t>( value >> 8 );
+ Payload[ right_byte_index ] = static_cast<uint8_t>( value & 0xFF );
-}
+} //lint !e1762
/**
* @brief Retrieve 32 bits from the payload buffer.
*
- * @param first_byte The index of the first byte out of 4
- * @param last_byte The index of the last byte out of 4
+ * @param first_byte_index The index of the first byte out of 4.
+ * @param last_byte_index The index of the last byte out of 4.
*
- * @return a concatenation of 4 bytes, from the byte indexed by first_byte to
- * the byte indexed by last_byte.
+ * @return a concatenation of 4 bytes, from the byte indexed by
+ * @a first_byte_index to the byte indexed by @a last_byte_index.
*/
uint32_t MessagePayload::decode32(
- const int first_byte,
- const int last_byte
+ const int first_byte_index,
+ const int last_byte_index
) const
{
- BOOST_ASSERT( ( 0 <= first_byte ) && ( first_byte < static_cast<int>(PayloadSizeInBytes) ) );
- BOOST_ASSERT( ( 0 <= last_byte ) && ( last_byte < static_cast<int>(PayloadSizeInBytes) ) );
- BOOST_ASSERT( first_byte < last_byte );
- BOOST_ASSERT( ( static_cast<size_t>( last_byte - first_byte ) + 1 ) == sizeof(uint32_t) );
+ BOOST_ASSERT( ( 0 <= first_byte_index ) && ( first_byte_index < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( ( 0 <= last_byte_index ) && ( last_byte_index < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( first_byte_index < last_byte_index );
- BOOST_ASSERT( ( ( last_byte_index - first_byte_index ) + 1 ) == sizeof(uint32_t) );
++ BOOST_ASSERT( ( static_cast<size_t>( last_byte_index - first_byte_index ) + 1 ) == sizeof(uint32_t) );
- int current_byte = first_byte;
+ int current_byte_index = first_byte_index;
- uint64_t value = static_cast<uint32_t>( Payload[ current_byte ] ) << 24;
- value += static_cast<uint32_t>( Payload[ ++current_byte ] << 16 );
- value += static_cast<uint32_t>( Payload[ ++current_byte ] << 8 );
- value += static_cast<uint32_t>( Payload[ ++current_byte ] );
- uint64_t value = static_cast<uint32_t>( Payload[ current_byte_index ] << 24 );
++ uint64_t value = static_cast<uint32_t>( Payload[ current_byte_index ] ) << 24;
+ value += static_cast<uint32_t>( Payload[ ++current_byte_index ] << 16 );
+ value += static_cast<uint32_t>( Payload[ ++current_byte_index ] << 8 );
+ value += static_cast<uint32_t>( Payload[ ++current_byte_index ] );
BOOST_ASSERT( value <= numeric_limits<uint32_t>::max() );
- BOOST_ASSERT( current_byte == last_byte );
+ BOOST_ASSERT( current_byte_index == last_byte_index );
return static_cast<uint32_t>( value );
}
const uint32_t value
)
{
- BOOST_ASSERT( ( 0 < first_byte ) && ( first_byte < static_cast<int>(PayloadSizeInBytes) ) );
- BOOST_ASSERT( ( 0 < last_byte ) && ( last_byte < static_cast<int>(PayloadSizeInBytes) ) );
- BOOST_ASSERT( first_byte < last_byte );
- BOOST_ASSERT( ( static_cast<size_t>( last_byte - first_byte ) + 1 ) == sizeof(uint32_t) );
+ BOOST_ASSERT( ( 0 <= first_byte_index ) && ( first_byte_index < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( ( 0 <= last_byte_index ) && ( last_byte_index < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( first_byte_index < last_byte_index );
- BOOST_ASSERT( ( ( last_byte_index - first_byte_index ) + 1 ) == sizeof(uint32_t) );
++ BOOST_ASSERT( ( static_cast<size_t>( last_byte_index - first_byte_index ) + 1 ) == sizeof(uint32_t) );
- int current_byte = first_byte;
+ int current_byte_index = first_byte_index;
- Payload[ current_byte ] = static_cast<uint8_t>( ( value >> 24 ) & 0xFF );
- Payload[ ++current_byte ] = static_cast<uint8_t>( ( value >> 16 ) & 0xFF );
- Payload[ ++current_byte ] = static_cast<uint8_t>( ( value >> 8 ) & 0xFF );
- Payload[ ++current_byte ] = static_cast<uint8_t>( value & 0xFF );
+ Payload[ current_byte_index ] = static_cast<uint8_t>( ( value >> 24 ) & 0xFF );
+ Payload[ ++current_byte_index ] = static_cast<uint8_t>( ( value >> 16 ) & 0xFF );
+ Payload[ ++current_byte_index ] = static_cast<uint8_t>( ( value >> 8 ) & 0xFF );
+ Payload[ ++current_byte_index ] = static_cast<uint8_t>( value & 0xFF );
- BOOST_ASSERT( current_byte == last_byte );
+ BOOST_ASSERT( current_byte_index == last_byte_index );
-}
+} //lint !e1762
/**
* @brief Read/Extract all the data from the input stream @a is and stores it
void notify_host_down( const std::string &host_address );
private:
+ //
+ // Types
+ //
+
- enum LinkStatus
+ enum Status
{
- LinkStatus_Up,
- LinkStatus_Down
+ Status_Up,
+ Status_Down
};
enum NotificationStatus
NotificationStatus_Reported
};
+ #ifdef UNDER_TEST
+ public:
-#else
-private:
+ #endif
+ //
+ // Methods
+ //
+
void add_host_up( const std::string &host_address );
void add_host_down( const std::string &host_address );
bool can_report_link_status() const;
void set_link_status(
- const LinkStatusAnalyzer::LinkStatus new_link_status
+ const LinkStatus::Status new_link_status
);
-private:
+ //
+ // Attributes
+ //
+
- /// the maximum amount of hosts which can be down before sound the alarm
+ /// The maximum amount of hosts which can be down before sound the alarm
const int HostsDownLimit;
- /// list of hosts down (obvious isn't it?)
+ /// List of hosts down (obvious isn't it?)
std::set<std::string> HostsDownList;
- /// interval the link have to be stable in order to consider it is functional
+ /// Interval the link have to be stable in order to consider it is functional
const int LinkUpIntervalInMin;
- /// interval the link have to be down in order to consider it is non-functional
+ /// Interval the link have to be down in order to consider it is non-functional
const int LinkDownIntervalInMin;
- /// keep track of the actual link status
- LinkStatusAnalyzer::LinkStatus CurrentLinkStatus;
- /// indicates if the last link status change was notified
- LinkStatusAnalyzer::NotificationStatus CurrentNotificationStatus;
- /// when was the last time the status changed
+ /// Keep track of the actual link status
+ LinkStatus::Status CurrentLinkStatus;
+ /// Indicates if the last link status change was notified
+ LinkStatus::NotificationStatus CurrentNotificationStatus;
+ /// When was the last time the status changed
boost::posix_time::ptime TimeLinkStatusChanged;
- /// command used to notify the status of the link
- StatusNotifierCommand StatusNotifierCmd;
- /// mutual exclusion variable to avoid data races
+ /// Command used to notify the status of the link
+ StatusNotifierCommandItem StatusNotifierCmd;
+ /// Mutual exclusion variable to avoid data races
boost::mutex Mutex;
};