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

339 lines
9.1 KiB
C++

/*
* File: Peer.cc
* Author: jgaebler
*
* Created on April 27, 2010, 4:57 PM
*/
#include "Peer.h"
#include "Exception.h"
#include "common/time/VirtualLogicalTime.h"
#include "ms/MemberDescription.h"
#include "ms/register/Cluster.h"
#include "ms/register/ClusterReferrer.h"
namespace ubeeme {
namespace moversight {
/**
* @brief Constructor
*/
Peer::Peer() : peerId(UNDEFINED_PEER_ID), role(MASTER), state(DISJOINED), currCluster(NULL) {
}
/**
* @brief Constructor
* @param pId The ID of the new peer.
* @param aTa Its transport address.
* @param aState Its peer state.
* @param desc A description object for the new peer.
* @param res The available resources of the peer.
*/
Peer::Peer(const PeerID pId,
const TransportAddress & aTa,
const PeerState & aState,
const PeerDescription & desc,
const PeerResources & res) :
peerId(pId),
role(MASTER),
ta(aTa),
state(aState),
currCluster(NULL),
pDesc(desc),
peerResources(res) {
}
/**
* @brief Constructor.
* @param mDesc The member description describing this peer.
*/
Peer::Peer(const MemberDescription & mDesc) : peerId(mDesc.getPeerID()), role(MASTER), ta(mDesc.getTransportAddress()), state(mDesc.getState()), currCluster(NULL), pDesc(mDesc.getPeerDescription()), peerResources(mDesc.getPeerResources()) {
}
/**
* @brief Copy constructor
* @param orig The peer to copy.
*/
Peer::Peer(const Peer & orig) {
operator =(orig);
}
/**
* @brief Destructor
*/
Peer::~Peer() {
}
/**
* @brief Returns the local address of the current peer.
* @return The desired address.
*/
const TransportAddress &
Peer::getLocalAddress() const {
return ta;
}
/**
* @brief Sets the local address of the current peer.
* @param aTa The address to set.
*/
void
Peer::setLocalAddress(const TransportAddress & aTa) {
ta = aTa;
}
/**
* @brief Returns the state of the peer.
* @return The desired state of the current peer.
*/
PeerState &
Peer::getPeerState() {
return state;
}
/**
* @brief Returns the state of the peer.
* @return The desired state of the current peer.
*/
const PeerState &
Peer::getPeerState() const {
return state;
}
/**
* @brief Sets the state of the peer.
* @param aState The new state of the peer.
*/
void
Peer::setPeerState(const PeerState & aState) {
state = aState;
}
/**
* @brief Returns the state of the peer's state.
* @return The state of the peer.
*/
const State &
Peer::getState() const {
return state.getState();
}
/**
* @brief Sets the state (main state) of the peer's state.
* @param aState The (main) state to set.
*/
void
Peer::setState(const State & aState) {
state.setState(aState);
}
/**
* @brief Returns the sub state of the peer state.
* @return The sub state of the peer state
*/
const SubState &
Peer::getSubState() const {
return state.getSubState();
}
/**
* @brief Sets the sub state of the peer state
* @param aSubState The sub state to set
*/
void
Peer::setSubState(const SubState & aSubState) {
state.setSubState(aSubState);
}
/**
* @brief Returns the state operation of the peer state.
* @return The state operation of the peer state.
*/
const StateOperation &
Peer::getStateOperation() const {
return state.getStateOperation();
}
/**
* @brief Sets the state operation of the peer state.
* @param aStateOperation The state operation to set.
*/
void
Peer::setStateOperation(const StateOperation & aStateOperation) {
state.setStateOperation(aStateOperation);
}
/**
* @brief Determines, is the current peer a master peer
* @return True, if the current peer a master, False otherwise.
*/
bool
Peer::isMaster() const {
return (role == MASTER) ? true : false;
}
/**
* @brief Sets the role of the current peer.
* @param flag True, if the peer a master peer, false otherwise.
*/
void
Peer::setIsMaster(bool flag) {
if (flag) {
role = MASTER;
}
else {
role = SLAVE;
}//End else
}
/**
* @brief Sets a referrer, referring to the cluster, associated with the current peer.
* @param cRef The cluster referrer to set.
*/
void
Peer::setClusterReferrer(ClusterReferrer * cRef) {
currCluster = cRef;
}
/**
* @brief Returns a reference to the current cluster.
* @return The desired cluster referrer.
*/
const ClusterReferrer *
Peer::clusterReferrer() const {
return currCluster;
}
/**
* @brief Identifies the master peer
* @throw AttributeNotSetException if the cluster referrer not set
* @return the ID of the peer, which is the master of the current peer.
*/
PeerID
Peer::getMasterID() const {
if (currCluster) {
return currCluster->getMasterID();
}//End if
//dummy peer? return your own ID as master ID
return peerId;
}
/**
* @brief Identifies the cluster, associated with this peer.
* @return The id of the peers home cluster.
* @throw AttributeNotSetException if the cluster referrer not set
*/
ClusterID
Peer::getClusterID() const {
if (currCluster) {
return currCluster->getClusterID();
}//End if
//dummy peer? return the initial cluster ID as cluster id
return INITIAL_CLUSTER_ID;
}
/**
* @brief Sets the ID of the current instance.
* @param aId The id to set.
*/
void
Peer::setPeerID(const PeerID & aId) {
peerId = aId;
}
/**
* @brief Returns the ID of the current instance.
* @return The desired ID.
*/
const PeerID &
Peer::getPeerID() const {
return peerId;
}//End getPeerID
/**
* @brief Equality operator.
* @param a The peer instance to prove for equality.
* @return True, if the given peer equal, false otherwise
*/
bool Peer::operator==(const Peer& a) const {
return (peerId == a.getPeerID());
}
/**
* @brief Not equal operator.
* @param a The peer instance to prove for not equality.
* @return True, if the given peer is not equal, false otherwise.
*/
bool Peer::operator!=(const Peer& a) const {
return !operator ==(a);
}
/**
* Assignment operator.
* @param rhs The instance to assign.
* @return A reference of the current object.
*/
Peer & Peer::operator=(const Peer & rhs) {
//no self assignment
if (&rhs != this) {
peerId = rhs.peerId;
role = rhs.role;
ta = rhs.ta;
state = rhs.state;
currCluster = rhs.currCluster;
pDesc = rhs.pDesc;
peerResources = rhs.peerResources;
}
return *this;
}
/**
* Stores a short description of the peer.
* @param pD The description to store.
*/
void
Peer::setPeerDescription(const PeerDescription & pD) {
pDesc = pD;
}
/**
* Returns a short description of the current peer.
* @return The desired description.
*/
const PeerDescription &
Peer::getPeerDescription() const {
return pDesc;
}
/**
* @brief Set a ResourceValue to this peer
* @param res the resource value of the peer
*/
void
Peer::setPeerResources(const PeerResources & res) {
peerResources = res;
}
/**
* @brief Returns the value of the resource(level) for this peer
* @return A generic representation of the peer resource level.
*/
const PeerResources &
Peer::getPeerResources() const {
return peerResources;
}
}
}