235 lines
6.0 KiB
C++
235 lines
6.0 KiB
C++
/*
|
|
* File: PeerList.cc
|
|
* Author: jgaebler
|
|
*
|
|
* Created on May 10, 2010, 5:33 PM
|
|
*/
|
|
|
|
#include "PeerList.h"
|
|
|
|
#include "ms/register/Cluster.h"
|
|
#include "common/Exception.h"
|
|
|
|
|
|
namespace ubeeme {
|
|
namespace moversight {
|
|
|
|
/**
|
|
* @brief Creates a PeerList instance
|
|
*/
|
|
PeerList::PeerList() {
|
|
}//End PeerList
|
|
|
|
/**
|
|
* @brief Destructor
|
|
*/
|
|
PeerList::~PeerList() {
|
|
}
|
|
|
|
/**
|
|
* @brief Clears the list
|
|
*/
|
|
void PeerList::clear() {
|
|
queue.clear();
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the size of the list.
|
|
* @return The current size of the list.
|
|
*/
|
|
size_t PeerList::size() const {
|
|
return queue.size();
|
|
}
|
|
|
|
/**
|
|
* @brief Returns, if a desired peer element of the list, or not.
|
|
* @param peer The search candidate.
|
|
* @return True on success, false otherwise.
|
|
*/
|
|
bool PeerList::contains(Peer & peer) const {
|
|
return queue.contains(peer);
|
|
}
|
|
|
|
/**
|
|
* @brief Returns, if a desired peer element of the list, or not.
|
|
* @param id The peer id of the peer to find.
|
|
* @return True on success, false otherwise.
|
|
*/
|
|
bool PeerList::contains(PeerID id) const {
|
|
|
|
for (size_t i = 0; i < queue.size(); i++) {
|
|
|
|
if (queue.get(i).getPeerID() == id) {
|
|
return true;
|
|
}//End if
|
|
}//End for
|
|
|
|
return false;
|
|
|
|
}//End contains
|
|
|
|
/**
|
|
* @brief Adds a peer to the list.
|
|
* @param peer The peer to add.
|
|
*/
|
|
void PeerList::add(Peer & peer) {
|
|
queue.add(peer);
|
|
}//End add
|
|
|
|
/**
|
|
* @brief Adds a peer to the list.
|
|
* @param peer The peer to add.
|
|
*/
|
|
void PeerList::add(const Peer & peer) {
|
|
queue.add(peer);
|
|
}//End add
|
|
|
|
/**
|
|
* @brief Adds a number of peers to the list.
|
|
* @param list The list of peers to add.
|
|
*/
|
|
void PeerList::add(PeerList & list) {
|
|
|
|
for (size_t i = 0; i < list.size(); i++) {
|
|
queue.add(list.get(i));
|
|
}//End for
|
|
|
|
}//End add
|
|
|
|
|
|
/**
|
|
* @brief Sets the current cluster referrer for all peers in the list.
|
|
* @param cRef The cluster referrer reference to set.
|
|
*/
|
|
void PeerList::setClusterReferrer(ClusterReferrer & cRef) {
|
|
|
|
for (size_t i = 0; i < queue.size(); i++) {
|
|
queue.get(i).setClusterReferrer(&cRef);
|
|
}//End for
|
|
|
|
}//End setCurrentCluster
|
|
|
|
/**
|
|
* @brief Returns the first element of the peer list
|
|
* @throws PeerNotFoundException If the list is empty.
|
|
* @return The desired peer.
|
|
*/
|
|
Peer & PeerList::getFirst() {
|
|
|
|
if (size() > 0) {
|
|
return queue.get(0);
|
|
}//End if
|
|
|
|
throw PeerNotFoundException("peer not found");
|
|
|
|
}//End getMaster
|
|
|
|
/**
|
|
* @brief Returns the peer at the given index from the list
|
|
* @param index The index to return.
|
|
* @return The desired peer.
|
|
* @throw IndexOutOfBounceException If the given index not within the current list size.
|
|
*/
|
|
Peer & PeerList::get(size_t index) {
|
|
|
|
if (index > queue.size()) {
|
|
throw IndexOutOfBounceException("index out of bounce");
|
|
}//End if
|
|
|
|
return queue.get(index);
|
|
|
|
}//End get
|
|
|
|
/**
|
|
* @brief Returns the peer at the given index from the list
|
|
* @param index The index to return.
|
|
* @return The desired peer.
|
|
* @throw IndexOutOfBounceException If the given index not within the current list size.
|
|
*/
|
|
const Peer & PeerList::get(size_t index) const {
|
|
|
|
if (index > queue.size()) {
|
|
throw IndexOutOfBounceException("index out of bounce");
|
|
}//End if
|
|
|
|
return queue.get(index);
|
|
|
|
}//End get
|
|
|
|
/**
|
|
* @brief Returns the peer identified by its id from the list
|
|
* @param id The peer id to search.
|
|
* @return The desired peer.
|
|
* @throw PeerNotFoundException If the peer not within the list.
|
|
*/
|
|
Peer & PeerList::getByID(PeerID id) {
|
|
|
|
// [i](LWItem a)->bool { return a->GetID()==i; }
|
|
//
|
|
// std::find_if(queue.begin(), queue.end(), [i](LWItem a)->bool { return a->GetID()==i; } )
|
|
|
|
//
|
|
for (size_t i = 0; i < queue.size(); i++) {
|
|
|
|
if (queue.get(i).getPeerID() == id) {
|
|
return queue.get(i);
|
|
}//End if
|
|
|
|
}//End for
|
|
|
|
throw PeerNotFoundException("peer not found");
|
|
|
|
}//End
|
|
|
|
/**
|
|
* @brief Returns the peer identified by its id from the list
|
|
* @param id The peer id to search.
|
|
* @return The desired peer.
|
|
* @throw PeerNotFoundException If the peer not within the list.
|
|
*/
|
|
const Peer & PeerList::getByID(PeerID id) const {
|
|
|
|
for (size_t i = 0; i < queue.size(); i++) {
|
|
|
|
if (queue.get(i).getPeerID() == id) {
|
|
return queue.get(i);
|
|
}//End if
|
|
|
|
}//End for
|
|
|
|
throw PeerNotFoundException("peer not found");
|
|
|
|
}//End getID
|
|
|
|
|
|
|
|
/**
|
|
* @brief Removes a peer from the list.
|
|
* @param peer The peer to remove.
|
|
*/
|
|
void PeerList::remove(const Peer & peer) {
|
|
|
|
queue.remove(peer);
|
|
|
|
}//end remove
|
|
|
|
/**
|
|
* @brief Removes a peer from the list, identified by its id.
|
|
* @param pId The peer id of the peer to remove.
|
|
*/
|
|
void PeerList::remove(PeerID pId) {
|
|
|
|
for (size_t i = 0; i < queue.size(); i++) {
|
|
|
|
Peer & p = queue.get(i);
|
|
|
|
if (p.getPeerID() == pId) {
|
|
queue.remove(p);
|
|
}//End if
|
|
|
|
}//End for
|
|
}//end
|
|
}
|
|
}
|
|
|