/* PIP - Platform Independent Primitives Peer - named I/O ethernet node Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef PIPEER_H #define PIPEER_H #include "piethernet.h" class PIPeer: public PIEthernet { struct PeerInfo; friend PIByteArray & operator <<(PIByteArray & s, const PIPeer::PeerInfo & v); friend PIByteArray & operator >>(PIByteArray & s, PIPeer::PeerInfo & v); public: PIPeer(const PIString & name); ~PIPeer(); //const PIString & id() const {return id_;} protected: bool threadedRead(uchar * readed, int size); private: EVENT_HANDLER2(PIPeer, void, timerEvent, void * , data, int, delim); static bool func_readed(void * peer, uchar * data, int size); struct PeerInfo { PIString name; PIString nearest_address; PIStringList addresses; int dist; int sync; int _wave; }; bool hasPeer(const PIString & name) {piForeachC (PeerInfo & i, peers) if (i.name == name) return true; return false;} bool removePeer(const PIString & name) {for (uint i = 0; i < peers.size(); ++i) if (peers[i].name == name) {peers.remove(i); return true;} return false;} void sendPeerInfo(const PeerInfo & info); void sendPeerRemove(const PIString & peer); void sendSelfInfo() {sendPeerInfo(self_info);} void sendSelfRemove() {sendPeerRemove(name_);} void syncPeers(); void findNearestAddresses(); struct PeerPacket { int header; // 1 - new peer, 2 - remove peer, 3 - sync peers }; PIList eths; PIEthernet * eth_send; PITimer timer; PeerInfo self_info; PIVector peers; //PIString id_; }; inline PIByteArray & operator <<(PIByteArray & s, const PIPeer::PeerInfo & v) {s << v.name << v.addresses << v.dist; return s;} inline PIByteArray & operator >>(PIByteArray & s, PIPeer::PeerInfo & v) {s >> v.name >> v.addresses >> v.dist; return s;} #endif // PIPEER_H