MASA-Core
PeerList.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #include "PeerList.hpp"
00023 
00024 PeerList::PeerList() {
00025         // TODO Auto-generated constructor stub
00026 
00027 }
00028 
00029 PeerList::~PeerList() {
00030         // TODO Auto-generated destructor stub
00031 }
00032 
00033 void PeerList::add(Peer* peer) {
00034         peerMap[peer->getRemoteId()] = peer;
00035 }
00036 
00037 Peer* PeerList::get(const string& id) const {
00038         map<string, Peer*>::const_iterator i = peerMap.find(id);
00039         if (i == peerMap.end()) {
00040                 return NULL;
00041         } else {
00042                 return i->second;
00043         }
00044 }
00045 
00046 void PeerList::erase(const string& id) {
00047         peerMap.erase(id);
00048 }
00049 
00050 void PeerList::copy(vector<Peer*> copy) {
00051         peerMap.clear();
00052         for (vector<Peer*>::iterator it = copy.begin() ; it != copy.end(); ++it) {
00053                 peerMap[(*it)->getRemoteId()] = *it;
00054         }
00055 }
00056 
00057 Peer* PeerList::getPrev(const string& id) const {
00058         map<string, Peer*>::const_iterator i = peerMap.find(id);
00059         if (i == peerMap.end()) {
00060                 return NULL;
00061         } else {
00062                 i--;
00063                 if (i == peerMap.end()) {
00064                         return (peerMap.rbegin())->second;
00065                 } else {
00066                         return i->second;
00067                 }
00068         }
00069 }
00070 
00071 Peer* PeerList::getNext(const string& id) const {
00072         map<string, Peer*>::const_iterator i = peerMap.find(id);
00073         if (i == peerMap.end()) {
00074                 return NULL;
00075         } else {
00076                 i++;
00077                 if (i == peerMap.end()) {
00078                         return (peerMap.begin())->second;
00079                 } else {
00080                         return i->second;
00081                 }
00082         }
00083 }
00084 
00085 vector<Peer*> PeerList::getAllPeers() const {
00086         vector<Peer*> peers;
00087         for (map<string, Peer*>::const_iterator it = peerMap.begin() ; it != peerMap.end(); ++it) {
00088                 peers.push_back(it->second);
00089         }
00090         return peers;
00091 }
00092 
00093 vector<Peer*> PeerList::getProcessingPeers() const {
00094         vector<Peer*> peers;
00095         for (map<string, Peer*>::const_iterator it = peerMap.begin() ; it != peerMap.end(); ++it) {
00096                 if (it->second->getRemoteType() == TYPE_PROCESSING_NODE) {
00097                         peers.push_back(it->second);
00098                 }
00099         }
00100         return peers;
00101 }