18.03.2013 - Bug fixes, add in/out speed diagnostic to PIProtocol, fixed PIConsole tab switch segfault, PIObject EVENT / EVENT_HANDLER mechanism update - new EVENT macros that use EVENT_HANDLER with raiseEvent implementation.
This allow compile check event for CONNECT and use EVENT as CONNECT target, also raise event now is simple execute EVENT function.
This commit is contained in:
81
pipeer.h
Normal file
81
pipeer.h
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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<PIEthernet * > eths;
|
||||
PIEthernet * eth_send;
|
||||
PITimer timer;
|
||||
|
||||
PeerInfo self_info;
|
||||
PIVector<PeerInfo> 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
|
||||
Reference in New Issue
Block a user