#include "MoversightService.h" #include "Dispatcher.h" #include "Moversight.h" #include "common/transport/msg/ExteriorMessage.h" #include "common/time/VirtualLogicalTime.h" #include "mt/msg/MulticastMessage.h" #include "ut/msg/UnicastMessage.h" namespace ubeeme { namespace moversight { /** * @brief Constructor * @param d A reference to the moversight dispatcher */ MoversightService::MoversightService(Dispatcher & d, std::string name) : ObjectListener(name), dispatcher(d), module(dispatcher.getMoversight()) { } /** * @brief Copy constructor * @param other The instance to copy. */ MoversightService::MoversightService(const MoversightService& other) : ObjectListener(other.getServiceName()), dispatcher(other.dispatcher), module(other.module) { } /** * @brief Destructor */ MoversightService::~MoversightService() { } /** * @brief Returns the current view ID. * @return The desired view ID. */ ViewID MoversightService::getViewID() const { return dispatcher.getMembershipService().getViewID(); } /** * @brief Returns the last view ID. * @return The last view ID. */ ViewID MoversightService::getLastViewID() const { return dispatcher.getMembershipService().getLastViewID(); } /** * @brief Returns the master id list from the membership service. * @return A list containing all master IDs. */ PeerIDList MoversightService::getMasterIDList() { return dispatcher.getMembershipService().getMasterPeerIDList(); } /** * @brief Permits access to the local moversight peer instance. * @return The local peer instance. */ Peer & MoversightService::getLocalPeer() { return dispatcher.getMembershipService().getLocalPeer(); } /** * @brief Permits access to the local moversight peer instance. * @return The local peer instance. */ const Peer & MoversightService::getLocalPeer() const { return dispatcher.getMembershipService().getLocalPeer(); } /** * @brief Returns the Peer ID of the local peer. * @return The id of the local peer instance. */ PeerID MoversightService::getLocalID() { return getLocalPeer().getPeerID(); } /** * @brief Returns the transport address of the local peer. * @return The transport address of the local peer. */ TransportAddress const & MoversightService::getLocalAddress() { return module.getLocalAddress(); } /** * @brief Returns true, if the local peer master. * @return True, if the local peer master, false otherwise. */ bool MoversightService::isLocalPeerMaster() { return getLocalPeer().isMaster(); } /** * @brief Returns the peer ID of the master peer from the local peer. * @return The ID of the master peer from the local peer. */ PeerID MoversightService::getLocalMasterID() const { return getLocalPeer().getMasterID(); } /** * @brief Returns the peer state of the local peer. * @return The peer state of the local peer. */ PeerState & MoversightService::getLocalPeerState() { return getLocalPeer().getPeerState(); } /** * @brief Returns the peer state of the local peer. * @return The peer state of the local peer. */ const PeerState & MoversightService::getLocalPeerState() const { return getLocalPeer().getPeerState(); } /** * @brief Returns the (main) state of the local peer. * @return The (main) state of the local peer. */ const State & MoversightService::getLocalState() const { return getLocalPeer().getState(); } /** * @brief Sets the local (main) state of the peer. * @param s The (main) state to set. */ void MoversightService::setLocalState(const State & s) { getLocalPeerState().setState(s); } /** * @brief Returns the sub state of the local peer. * @return The sub state of the local peer. */ const SubState & MoversightService::getLocalSubState() const { return getLocalPeer().getSubState(); } /** * @brief Sets the local sub state. * @param sub The sub state to set. */ void MoversightService::setLocalSubState(const SubState & sub) { getLocalPeerState().setSubState(sub); } /** * @brief Returns the state operation of the local peer. * @return The state operation of the local peer. */ const StateOperation & MoversightService::getLocalStateOperation() const { return getLocalPeer().getStateOperation(); } /** * @brief Sets the state operation of the local peer. * @param op The state operation to set. */ void MoversightService::setLocalStateOperation(const StateOperation & op) { getLocalPeerState().setStateOperation(op); } /** * @brief Returns the peer state of the given peer. * @param id The id of the peer to read. * @return The peer state of the given peer. */ PeerState & MoversightService::getPeerState(PeerID id) { return dispatcher.getMembershipService().getPeer(id).getPeerState(); } /** * @brief Returns the peer state of the given peer. * @param id The id of the peer to read. * @return The peer state of the given peer. */ const PeerState & MoversightService::getPeerState(PeerID id) const { return dispatcher.getMembershipService().getPeer(id).getPeerState(); } /** * @brief Returns the state of the given peer. * @param id The id of the peer to read. * @return The state of the given peer. */ const State & MoversightService::getState(PeerID id) const { return getPeerState(id).getState(); } /** * @brief Sets the state of the given peer. * @param id The id of the peer to set. * @param s The state to set. */ void MoversightService::setState(PeerID id, const State & s) { getPeerState(id).setState(s); } /** * @brief Returns the sub state of the given peer. * @param id The id of the peer to read. * @return The sub state of the given peer. */ const SubState & MoversightService::getSubState(PeerID id) const { return getPeerState(id).getSubState(); } /** * @brief Sets the sub state of the given peer. * @param id The peer which sub state shall be set. * @param sub The sub state to set. */ void MoversightService::setSubState(PeerID id, const SubState & sub) { getPeerState(id).setSubState(sub); } /** * @brief Returns the state operation of the given peer. * @param id The ID of the peer to read. * @return The state operation of the local peer. */ const StateOperation & MoversightService::getStateOperation(PeerID id) const { return getPeerState(id).getStateOperation(); } /** * @brief Sets the state operation of the given peer. * @param id The id of the peer to set. * @param op The state operation to set. */ void MoversightService::setStateOperation(PeerID id, const StateOperation & op) { getPeerState(id).setStateOperation(op); } /* -------------------------------------------------------------------- * Common interface for PDU send operations */ /** * @brief Send a message to a host identified by it's transport address. * @param msg The message to send. * @param dest The destination's transport address. * @param reliable Whether to send the message reliable or not. */ void MoversightService::sendTo(const MoversightMessage& msg, const TransportAddress& dest, const bool reliable /* = false */) { if(getLocalAddress() != dest){ dispatcher.getUnicastTransfer().send(msg, dest, reliable); }//End if } /** * @brief Send a message to a host identified by it's peer ID. * @param msg The message to send. * @param dest The destination's peer ID. * @param reliable Whether to send the message reliable or not. */ void MoversightService::sendTo(const MoversightMessage& msg, const PeerID dest, const bool reliable /* = false */) { TransportAddress ta; if (getLocalSubState() == REJOIN_IN_PROGRESS) { ta = dispatcher.getMembershipService().getLastMemberRegister().getPeer(dest).getLocalAddress(); } else { ta = dispatcher.getMembershipService().getPeer(dest).getLocalAddress(); } sendTo(msg, ta, reliable); } /** * @brief Send a message to a list of hosts identified by their transport addresses. * @param msg The message to send. * @param peers List of destination peer IDs. * @param reliable Whether to send the message reliable or not. */ void MoversightService::sendTo(const MoversightMessage& msg, const PeerIDList& peers, const bool reliable /* = false */) { for (size_t i = 0; i < peers.size(); i++) { sendTo(msg, peers.get(i), reliable); } } /** * @brief Send a message to all peers within this cluster. * @param msg The message to send. * @param reliable Whether to send the message reliable or not. */ void MoversightService::sendToCluster(const MoversightMessage& msg, const bool reliable /* = false */) { PeerIDList peers = dispatcher.getMembershipService().getClusterPeerIDList(getLocalID()); //remove the local peer as receiver of the message peers.remove(getLocalID()); sendTo(msg, peers, reliable); } /** * @brief Send a message to the master peer. * @param msg The message to send. * @param reliable Whether to send the message reliable or not. */ void MoversightService::sendToMaster(const MoversightMessage& msg, const bool reliable /* = false */) { PeerID pid = getLocalPeer().getMasterID(); if (getLocalID() != pid) { sendTo(msg, pid, reliable); } } /** * @brief Send a message to all master peers. * @param msg The message to send. * @param reliable Whether to send the message reliable or not. */ void MoversightService::sendToAllMasters(const MoversightMessage& msg, const bool reliable /* = false */) { PeerIDList peers = dispatcher.getMembershipService().getMasterPeerIDList(); peers.remove(getLocalID()); sendTo(msg, peers, reliable); } /** * @brief Send a message to a peer from the last view identified by it's peer ID. * @param msg The message to send. * @param dest The destination's peer ID. * @param reliable Whether to send the message reliable or not. */ void MoversightService::sendToLastView(const MoversightMessage& msg, const PeerID dest, const bool reliable /* = false */) { TransportAddress ta = dispatcher.getMembershipService().getLastMemberRegister().getPeer(dest).getLocalAddress(); sendTo(msg, ta, reliable); } /** * @brief Send a message to a list of peers from the last view identified by their peer ID. * @param msg The message to send. * @param peers List of destination peers. * @param reliable Whether to send the message reliable or not. */ void MoversightService::sendToLastView(const MoversightMessage& msg, const PeerIDList& peers, const bool reliable /* = false */) { for (size_t i = 0; i < peers.size(); i++) { sendToLastView(msg, peers.get(i), reliable); } } /** * @brief Creates a list of receiver peers. * * This method creates a list of receiver peers for example a multicast dissemination within the current cluster. * The generated list contains all peers of the local cluster and all master, expect it self and the peer, which was the sender of the message. * The sender of the message is given by the sourcePeerId attribute (in common stored in the last hop field of a peer message). * * @param sourcePeerID The sender of a message. Normally identified by the last hop / or sender field of a peer message. * @return The created list. */ PeerIDList MoversightService::createReceiverList(PeerID sourcePeerID) { PeerIDList recList; //sender is master if (dispatcher.getMembershipService().getMasterPeerIDList().contains(sourcePeerID)) { recList = dispatcher.getMembershipService().getClusterPeerIDList(getLocalID()); } //sender is slave else { recList = dispatcher.getMembershipService().getClusterAndMasterPeerIDList(getLocalPeer()); recList.remove(sourcePeerID); } recList.remove(getLocalID()); return recList; } /** * @brief Assignment operator * @param other The instance to assign. * @return A reference to the local instance. */ MoversightService & MoversightService::operator=(const MoversightService & other) { //prevent self assignment if (this == &other) { return *this; } dispatcher = other.dispatcher; module = other.module; return *this; } /** * @brief Permits access to the current logical time. * @return The current logical time. */ const VirtualLogicalTime & MoversightService::getCurrentLogicalTime() const { return dispatcher.getTimeService().getCurrentLogicalTime(); } /** * @brief Returns the last seen logical time for a delivered message. * @return The last seen logical time of a delivered message */ const VirtualLogicalTime & MoversightService::getLastSeenLogicalTime() const { return dispatcher.getMessageTransfer().getLastSeenLogicalTime(); } } }