Files
2014-06-30 13:58:10 +02:00

337 lines
10 KiB
C++

#include "PeerState.h"
namespace ubeeme {
namespace moversight {
/**
* @brief Constructor
*
* Creates a peer state using default values (state = DISJOINED, subState = NO_SUB_STATE, stateOp = NO_STATE_OPERATION)
*/
PeerState::PeerState(){
reset();
}
/**
* @brief Constructor
* @param st The initial state to set
*/
PeerState::PeerState(const State & st){
reset();
setState(st);
}
/**
* @brief Copy constructor
* @param orig The instance to copy
*/
PeerState::PeerState(const PeerState & orig) : state(orig.state), subState(orig.subState), stateOp(orig.stateOp) {
}
/**
* @brief Destructor
*/
PeerState::~PeerState() {
}
/**
* @brief Returns the main state of the peer state.
* @return The (main) state of the peer state.
*/
const State &
PeerState::getState() const {
return state;
}
/**
* @brief Sets the (main) state of the peer state.
* @param s The state to set.
*/
void
PeerState::setState(const State & s) {
state = s;
}
/**
* @brief Returns the sub state of the peer state.
* @return The sub state of the peer state.
*/
const SubState &
PeerState::getSubState() const {
return subState;
}
/**
* @brief Sets the sub state of the peer state.
* @param sub The peer state to set.
*/
void
PeerState::setSubState(const SubState & sub) {
subState = sub;
}
/**
* @brief Returns the state operation of the peer state.
* @return The state operation of the peer state.
*/
const StateOperation &
PeerState::getStateOperation() const {
return stateOp;
}
/**
* @brief Sets the state operation of the peer state.
* @param op The state operation of the peer state.
*/
void
PeerState::setStateOperation(const StateOperation & op) {
stateOp = op;
}
/**
* @brief Equal operator
* @param ps The peer state to compare
* @return True, if the given peer state equal to the local one, false in other cases.
*/
bool PeerState::operator==(const PeerState& ps) const {
if (ps.getState() == state &&
ps.getSubState() == subState &&
ps.getStateOperation() == stateOp) {
return true;
}
return false;
}
/**
* @brief Equal operator. Compares the given state with the one of the local peer state.
* @param s The state to compare to.
* @return True, if the given state equal to the local one, false otherwise.
*/
bool PeerState::operator==(const State& s) const {
if (state == s) return true;
return false;
}
/**
* @brief Equal operator. Compares the given sub state with the one of the local peer state.
* @param sub The sub state to compare to.
* @return True, if the given sub state equal to the local one, false otherwise.
*/
bool PeerState::operator==(const SubState& sub) const {
if (subState == sub) return true;
return false;
}
/**
* @brief Equal operator. Compares the given state operation with the one of the local peer state.
* @param op The state operation to compare to.
* @return True, if the given state operation equal to the local one, false otherwise.
*/
bool PeerState::operator==(const StateOperation& op) const {
if (stateOp == op) return true;
return false;
}
/**
* @brief Unequal operator. Compares the given peer state with the local peer state.
* @param ps The peer state to compare to.
* @return True, if the given peer state unequal to the local one, false otherwise.
*/
bool PeerState::operator!=(const PeerState& ps) const {
if (ps.getState() != state ||
ps.getSubState() != subState ||
ps.getStateOperation() != stateOp) {
return true;
}
return false;
}
/**
* @brief Unequal operator. Compares the given state with the one of the local peer state.
* @param s The state to compare to.
* @return True, if the given state unequal to the local one, false otherwise.
*/
bool PeerState::operator!=(const State& s) const {
if (state != s) return true;
return false;
}
/**
* @brief Unequal operator. Compares the given sub state with the one of the local peer state.
* @param sub The sub state to compare to.
* @return True, if the given sub state unequal to the local one, false otherwise.
*/
bool PeerState::operator!=(const SubState& sub) const {
if (subState != sub) return true;
return false;
}
/**
* @brief Unequal operator. Compares the given state operation with the one of the local peer state.
* @param op The state operation to compare to.
* @return True, if the given state operation equal to the local one, false otherwise.
*/
bool PeerState::operator!=(const StateOperation& op) const {
if (stateOp != op) return true;
return false;
}
/**
* @brief Resets the state to the default values (state = DISJOINED, subState = NO_SUB_STATE, stateOp = NO_STATE_OPERATION).
*/
void
PeerState::reset(){
state = DISJOINED;
subState = NO_SUB_STATE;
stateOp = NO_STATE_OPERATION;
}
/**
* @brief Outputs a string representation of a given peer state.
* @param out The output stream to manipulate.
* @param s The peer state to interpret.
* @return The modified output stream.
*/
std::ostream& operator<<(std::ostream& out, const PeerState & ps) {
out << ps.getState() << "(" << ps.getSubState() << "," << ps.getStateOperation() << ")";
return out;
}
/**
* @brief Outputs a string representation of a given state.
* @param out The output stream to manipulate.
* @param s The state to interpret.
* @return The modified output stream.
*/
std::ostream& operator<<(std::ostream& out, const State & s) {
std::stringbuf buf;
switch (s) {
case DISJOINED:
buf.str("DISJOINED");
break;
case INVITING:
buf.str("INVITING");
break;
case WAITING_FOR_ROSTER:
buf.str("WAITING_FOR_ROSTER");
break;
case JOINED:
buf.str("JOINED");
break;
case REJOIN:
buf.str("REJOIN");
break;
case PENDING:
buf.str("PENDING");
break;
case LEAVING:
buf.str("LEAVING");
break;
case EXCLUDED:
buf.str("EXCLUDED");
break;
case DEAD:
buf.str("DEAD");
break;
default:
buf.str("UNKNOWN_PEER_STATE");
break;
}
out << buf.str();
return out;
}//End operator<<
/**
* @brief Outputs a string representation of a given sub-state.
* @param out The output stream to manipulate.
* @param sub The sub-state to interpret.
* @return The modified output stream.
*/
std::ostream& operator<<(std::ostream& out, const SubState & sub) {
std::stringbuf buf;
switch (sub) {
case WAITING_FOR_FLUSH:
buf.str("WAITING_FOR_FLUSH");
break;
case FLUSHING:
buf.str("FLUSHING");
break;
case WAITING_FOR_MERGE:
buf.str("WAITING_FOR_MERGE");
break;
case MERGING:
buf.str("MERGING");
break;
case SYNCHRONIZING:
buf.str("SYNCHRONIZING");
break;
case WAITING_FOR_REJOIN:
buf.str("WAITING_FOR_REJOIN");
break;
case REJOIN_IN_PROGRESS:
buf.str("REJOIN_IN_PROGRESS");
break;
case NO_SUB_STATE:
default:
buf.str("NO_SUB_STATE");
break;
}
out << buf.str();
return out;
}//End operator<<
/**
* @brief Outputs a string representation of a given state operation.
* @param out The output stream to manipulate.
* @param op The peer state operation to interpret.
* @return The modified output stream.
*/
std::ostream& operator<<(std::ostream& out, const StateOperation & op) {
std::stringbuf buf;
switch (op) {
case LOCKED:
buf.str("LOCKED");
break;
case NO_STATE_OPERATION:
default:
buf.str("NO_STATE_OPERATION");
break;
}
out << buf.str();
return out;
}//End operator<<
}
}