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

186 lines
4.8 KiB
C++

/*
* File: NetworkFailureDetector.cc
* Author: jgaebler
*
* Created on February 24, 2014, 2:28 PM
*/
#include "NetworkFailureDetector.h"
#include "Moversight.h"
#include "Dispatcher.h"
#include "fd/nfd/msg/ControlMessage.h"
#include "fd/nfd/msg/ControlMessageConfirm.h"
namespace ubeeme {
namespace moversight {
#undef DEBUG
#define DEBUG(msg) if (module.isPrintDebugNFD()) MOV_DEBUG << "NFD@" << getLocalID() << " "<<msg<<endl;
/**
* @brief Constructor
* @param d A reference to the dispatcher
*/
NetworkFailureDetector::NetworkFailureDetector(Dispatcher & d) : MoversightService(d, "NetworkFailureDetector"), running(false) {
}
/**
* @brief Copy constructor
* @param orig The instance to copy
*/
NetworkFailureDetector::NetworkFailureDetector(const NetworkFailureDetector& orig) : MoversightService(orig) {
operator=(orig);
}
/**
* @brief Destructor
*/
NetworkFailureDetector::~NetworkFailureDetector() {
};
/**
* @brief Assignment operator
* @param other The instance to assign
* @returns A reference to the local object
*/
NetworkFailureDetector &
NetworkFailureDetector::operator =(const NetworkFailureDetector& other) {
if (this != &other) {
running = other.running;
nfdStateTable = other.nfdStateTable;
detectionTimerQueue = other.detectionTimerQueue;
}
return *this;
}
/**
* @brief Sets the field isRunning.
* @param flag Determines, if the failure detector is running, or
* not.
*/
void
NetworkFailureDetector::setRunning(bool flag) {
running = flag;
}
/**
* @brief Responses, if the failure detector is running, or not.
* @return True, if the failure detector is running, false otherwise.
*/
bool
NetworkFailureDetector::isRunning() const {
return running;
}
/**
* @brief Determines, if the given peer a valid target to monitor
* @param pId The potential peer to monitor.
* @return True, have the given peer to be monitored by the local peer, false otherwise.
*/
bool
NetworkFailureDetector::isTargetPeer(const PeerID & pId) {
if (isLocalPeerMaster()) {
PeerIDList targetPeers = dispatcher.getMembershipService().getClusterAndMasterPeerIDList(getLocalPeer());
if (targetPeers.contains(pId)) {
return true;
}//End if
}//end if
else {
//we got a new master?
if (getLocalMasterID() == pId) {
return true;
}//End if
}
return false;
}
/**
* @brief Starts the failure detection.
*/
void
NetworkFailureDetector::start() {
if (isRunning()) return;
//we start monitoring - init fd
DEBUG("start - start the FD service");
//master
if (isLocalPeerMaster()) {
startMonitorPeersAsMaster();
}//End if
//slave
else {
startMonitorPeersAsSlave();
}//End else
setRunning(true);
if (module.isPrintDebugNFD()) {
DEBUG("start - resulting suspicion table");
nfdStateTable.printTable();
}//End if
}
/**
* @brief Stops the failure detection.
*/
void
NetworkFailureDetector::stop() {
if (isRunning()) {
DEBUG("stop - stop the FD service");
stopMonitorAllPeers();
setRunning(false);
}
}
/**
* @brief Sends a control message to the given peer.
* @param pId The destination peer id.
*/
void
NetworkFailureDetector::sendControlMessage(const PeerID & pId) {
ControlMessage cm;
cm.setSourceID(getLocalID());
cm.setDestID(pId);
DEBUG("sendCM - send CM message to peer " << pId);
sendTo(cm, pId);
}
/**
* @brief Sends a control message confirm to a given peer.
* @param pId The id of the destination peer.
*/
void
NetworkFailureDetector::sendControlMessageConfirm(const PeerID & pId) {
ControlMessageConfirm cmc;
cmc.setSourceID(getLocalID());
cmc.setDestID(pId);
DEBUG("sendCMC - send CMC message to peer " << pId);
sendTo(cmc, pId);
}
}
}