241 lines
8.2 KiB
C++
241 lines
8.2 KiB
C++
/*
|
|
* File: LogicalTime.cc
|
|
* Author: jgaebler
|
|
*
|
|
* Created on April 15, 2010, 2:31 PM
|
|
*/
|
|
#include "VirtualLogicalTime.h"
|
|
|
|
namespace ubeeme {
|
|
namespace moversight {
|
|
|
|
/**
|
|
* @brief Defines the shift offset, used to differ between to time part and the peerId part of the virtual logical time instance.
|
|
*/
|
|
#define LOGICAL_CLOCK_SHIFT_VALUE 16
|
|
|
|
/**
|
|
* @brief Constructor. Creates a virtual logical time instance, which is set to 0.0.
|
|
*/
|
|
VirtualLogicalTime::VirtualLogicalTime() {
|
|
init(0, 1);
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor. Creates a virtual logical time instance, based on a given logical time value, represanted as int value.
|
|
* @param intValue the logical time value to set, given as integer
|
|
*/
|
|
VirtualLogicalTime::VirtualLogicalTime(unsigned int intValue): value(intValue){
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor. Creates a virtual logical time instance, which is set to startValue.peerId.
|
|
* @param peerId the peerId used to calculate the peer part of the logical time start value
|
|
* @param startValue the startValue used to calculate the time part of the logical time start value
|
|
*/
|
|
VirtualLogicalTime::VirtualLogicalTime(PeerID peerId, unsigned int startValue) {
|
|
init(peerId, startValue);
|
|
}
|
|
|
|
/**
|
|
* @brief Copyconstructor.
|
|
* @param orig the virtual logical time instance to copy.
|
|
*/
|
|
VirtualLogicalTime::VirtualLogicalTime(const VirtualLogicalTime& orig) : value(orig.value) {
|
|
}
|
|
|
|
/**
|
|
* @brief Destructor.
|
|
*/
|
|
VirtualLogicalTime::~VirtualLogicalTime() {
|
|
}
|
|
|
|
/**
|
|
* @brief Inits the virtual logical time instance, based on the given values.
|
|
* @param peerId the peerId used to calculate the peer part of the logical time start value
|
|
* @param startValue the startValue used to calculate the time part of the logical time start value
|
|
*/
|
|
void
|
|
VirtualLogicalTime::init(PeerID peerId, unsigned int startValue) {
|
|
value = (startValue << LOGICAL_CLOCK_SHIFT_VALUE) + peerId.getValue();
|
|
}
|
|
|
|
/**
|
|
* @brief Returns a integer representation of the virtual logical time instance.
|
|
* @returns the integer representation of the virtual logical time instance
|
|
*/
|
|
unsigned int
|
|
VirtualLogicalTime::getValue() const {
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* @brief Recomputes the virtual logical time value based on the given integer value.
|
|
* @param intValue the integer value used to compute the new virtual logical time value
|
|
*/
|
|
void
|
|
VirtualLogicalTime::setAsIntValue(unsigned int intValue) {
|
|
value = intValue;
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the time part of the virtual logical time instance.
|
|
* @return the time part of the current virtual logical time instance
|
|
*/
|
|
unsigned int
|
|
VirtualLogicalTime::getTimePart() const {
|
|
return ((value & 0xFFFF0000) >> LOGICAL_CLOCK_SHIFT_VALUE);
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the peerId part of the virtual logical time instance.
|
|
* @return the peerId part of the current virtual logical time instance
|
|
*/
|
|
PeerID
|
|
VirtualLogicalTime::getPeerID() const {
|
|
return PeerID(value & 0x0000FFFF);
|
|
}
|
|
|
|
/**
|
|
* @brief Sets the time part of the virtual logical time instance and recomputes the virtual logical time.
|
|
* @param time the time part to set
|
|
*/
|
|
void
|
|
VirtualLogicalTime::setTimePart(unsigned int time) {
|
|
init(getPeerID(), time);
|
|
}
|
|
|
|
/**
|
|
* @brief Sets the peerId part of the virtual logical time instance and recomputes the virtual logical time.
|
|
* @param pId the peerId part to set
|
|
*/
|
|
void
|
|
VirtualLogicalTime::setPeerID(const PeerID & pId) {
|
|
value -= getPeerID().getValue();
|
|
value += pId.getValue();
|
|
}
|
|
|
|
/**
|
|
* @brief Adds a integer representation of a virtual logical time to this instance.
|
|
* @param a the integer representation of a virtual logical time to add
|
|
*/
|
|
VirtualLogicalTime
|
|
VirtualLogicalTime::operator+(int a) {
|
|
|
|
VirtualLogicalTime t(*this);
|
|
t.setTimePart(t.getTimePart() + a);
|
|
|
|
return t;
|
|
}
|
|
|
|
/**
|
|
* @brief Increments to time part of the logical time by one (++a).
|
|
*/
|
|
VirtualLogicalTime &
|
|
VirtualLogicalTime::operator++() {
|
|
setTimePart(getTimePart() + 1);
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* @brief Increments to time part of the logical time by one (a++).
|
|
* @param Pseudoparam
|
|
* @return The value of the local instance before the value is increased.
|
|
*/
|
|
VirtualLogicalTime
|
|
VirtualLogicalTime::operator++(int) {
|
|
VirtualLogicalTime result(*this);
|
|
++(*this);
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* @brief Compares a virtual logical time value to a integer representation of another virtual logical time value.
|
|
* @param aLtValue the lt value to compare
|
|
* @return true in case of equality, false otherwise
|
|
*/
|
|
bool
|
|
VirtualLogicalTime::operator==(unsigned int const aLtValue) const {
|
|
return (value == aLtValue);
|
|
}
|
|
|
|
/**
|
|
* @brief Compares two virtual logical time instances.
|
|
* @param aLt the lt value to compare
|
|
* @return true in case of equality, false otherwise
|
|
*/
|
|
bool
|
|
VirtualLogicalTime::operator==(VirtualLogicalTime const& aLt) const {
|
|
return (value == aLt.value);
|
|
}
|
|
|
|
/**
|
|
* @brief Compares two virtual logical time instances.
|
|
* @param aLt the lt value to compare
|
|
* @return true in case of disparity, false otherwise
|
|
*/
|
|
bool
|
|
VirtualLogicalTime::operator!=(VirtualLogicalTime const& aLt) const {
|
|
return (value != aLt.value);
|
|
}
|
|
|
|
/**
|
|
* @brief Compares two virtual logical time instances.
|
|
* @param aLt the lt value to compare
|
|
* @return true is the current time value less than the given time value, false otherwise
|
|
*/
|
|
bool
|
|
VirtualLogicalTime::operator<(VirtualLogicalTime const& aLt) const {
|
|
return (value < aLt.value);
|
|
}
|
|
|
|
/**
|
|
* @brief Compares two virtual logical time instances.
|
|
* @param aLt the lt value to compare
|
|
* @return true is the current time value greater than the given time value, false otherwise
|
|
*/
|
|
bool
|
|
VirtualLogicalTime::operator>(VirtualLogicalTime const& aLt) const {
|
|
return (value > aLt.value);
|
|
}
|
|
|
|
/**
|
|
* @brief Converts a logical time value into an integer.
|
|
* @returns the integer represantation of the logical time value
|
|
*/
|
|
VirtualLogicalTime::operator int() const {
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* @brief Outputoperator. Prints a textual representation of the current logical time instance into the given outputstream.
|
|
* @param o the outputstream to use
|
|
* @param lt the virtual time instance to print
|
|
* @return the modified outputstream object
|
|
*/
|
|
std::ostream &
|
|
operator<<(std::ostream &o, VirtualLogicalTime const& lt) {
|
|
return (o << lt.getTimePart() << "." << lt.getPeerID() << " (" << lt.getValue() << ")");
|
|
}
|
|
|
|
/**
|
|
* @brief Update function. Updates the logical time with a given time by keeping the peer id part unchained.
|
|
* @param newTime The time used to update the current local time.
|
|
*/
|
|
void
|
|
VirtualLogicalTime::update(VirtualLogicalTime newTime) {
|
|
setTimePart(newTime.getTimePart());
|
|
}
|
|
|
|
/**
|
|
* @brief Archive method for the message serialization
|
|
* @param archive The archive to serialize
|
|
*/
|
|
void
|
|
VirtualLogicalTime::set(Archive & archive){
|
|
archive(value);
|
|
}
|
|
|
|
};
|
|
};
|