MASA-Core
CmdPeerList.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 "CmdPeerList.hpp"
00023 
00024 CmdPeerList::CmdPeerList() {
00025 }
00026 
00027 CmdPeerList::CmdPeerList(vector<Peer*> peers) {
00028         this->peers = peers;
00029 }
00030 
00031 CmdPeerList::~CmdPeerList() {
00032 
00033 }
00034 
00035 void CmdPeerList::send(Peer* socket) {
00036         socket->send_int16(peers.size());
00037         printf("----- %d\n", peers.size());
00038         for (std::vector<Peer*>::iterator it=peers.begin(); it!=peers.end(); ++it) {
00039             socket->send_vls8((*it)->getRemoteId());
00040             socket->send_vls8((*it)->getRemoteAddress());
00041             socket->send_int8((*it)->getRemoteType());
00042             socket->send_int8((*it)->getConnectionType());
00043             socket->send_int8((*it)->ringType);
00044         }
00045 }
00046 
00047 void CmdPeerList::receive(Peer* socket) {
00048         int len = socket->recv_int16();
00049         for (int i=0; i<len; i++) {
00050                 string remoteId = socket->recv_vls8();
00051                 string remoteAddress = socket->recv_vls8();
00052                 int remoteType = socket->recv_int8();
00053                 int connectionType = socket->recv_int8();
00054                 int ringType = socket->recv_int8();
00055 
00056                 Peer* peer = new Peer(remoteId, remoteAddress, remoteType, ringType, connectionType);
00057                 peers.push_back(peer);
00058                 printf("----- %s (%d)\n", peer->getRemoteId().c_str(), peer->getConnectionType());
00059         }
00060 }
00061 
00062 void CmdPeerList::addPeer(Peer* peer) {
00063         if (peer != NULL) {
00064                 peers.push_back(peer);
00065         }
00066 }
00067 
00068 void CmdPeerList::addPeers(vector<Peer*> peers) {
00069         for (std::vector<Peer*>::iterator it=peers.begin(); it!=peers.end(); ++it) {
00070                 addPeer(*it);
00071         }
00072 }
00073 
00074 const vector<Peer*>& CmdPeerList::getPeers() {
00075         return peers;
00076 }