Files
scandocs/uni/masterarbeit/source/moversight/ts/TimeService.cc
2014-06-30 13:58:10 +02:00

102 lines
2.5 KiB
C++

/*
* File: TimeService.cc
* Author: jgaebler
*
* Created on February 14, 2013, 2:12 PM
*/
#include "TimeService.h"
#include "Dispatcher.h"
#include "Moversight.h"
#include "ms/events/LocalPeerUpdatedEvent.h"
namespace ubeeme {
namespace moversight {
#undef DEBUG
#define DEBUG(msg) if (module.isPrintDebugCT()) MOV_DEBUG << "TS@" << getLocalID()<< " "<<msg<<endl;
/**
* @brief Constructor
* @param d A reference to the moversight dispatcher.
*/
TimeService::TimeService(Dispatcher & d) : MoversightService(d, "TimeService") {
}
/**
* @brief Destructor
*/
TimeService::~TimeService() {
}
/**
* @brief Initialize service.
*/
void
TimeService::initialise() {
dispatcher.subscribe<LocalPeerUpdatedEvent>(this);
//init the logical time
logicalTime.setPeerID(getLocalID());
}
/**
* @brief Finish the operation of the service.
*/
void
TimeService::finalise() {
dispatcher.unsubscribeAll(this);
}
/**
* @brief Permits read access to the current local logical time.
* @return A a read-only reference to the current local logical time.
*/
const VirtualLogicalTime &
TimeService::getCurrentLogicalTime() const {
return logicalTime;
}
/**
* @brief Receiver for the LocalPeerUpdated event. Updates the peer id part of the virtual logical time, if the peer changed.
* @param e The event.
*/
void
TimeService::handleEvent(const LocalPeerUpdatedEvent & e){
if(logicalTime.getPeerID() != e.getPeer().getPeerID()){
//setup the new peer ID part of the local time
logicalTime.setPeerID(e.getPeer().getPeerID());
}
}
/**
* @brief Updates the local, virtual logical time to the given time.
* @param newTime The time to set.
* @note The current value of the local, virtual logical time is derived by the MOVS paradigm.
*/
void
TimeService::update(const VirtualLogicalTime & newTime){
logicalTime = newTime;
}
/**
* @brief Increments the local virtual logical time by one.
* @see VirtualLogicalTime#operator++(int)
*/
void
TimeService::incCurrentLogicalTime(){
logicalTime++;
}
}
}