mirror of https://github.com/LX3JL/xlxd.git
parent
189bb48cc6
commit
8e6f674d7d
@ -0,0 +1,172 @@
|
|||||||
|
//
|
||||||
|
// cpeer.cpp
|
||||||
|
// xlxd
|
||||||
|
//
|
||||||
|
// Created by Jean-Luc Deltombe (LX3JL) on 10/12/2016.
|
||||||
|
// Copyright © 2016 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// This file is part of xlxd.
|
||||||
|
//
|
||||||
|
// xlxd 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.
|
||||||
|
//
|
||||||
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include "creflector.h"
|
||||||
|
#include "cpeer.h"
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// constructor
|
||||||
|
|
||||||
|
|
||||||
|
CPeer::CPeer()
|
||||||
|
{
|
||||||
|
::memset(m_ReflectorModules, 0, sizeof(m_ReflectorModules));
|
||||||
|
m_Clients.reserve(100);
|
||||||
|
m_ConnectTime = std::time(NULL);
|
||||||
|
m_LastHeardTime = std::time(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
CPeer::CPeer(const CCallsign &callsign, const CIp &ip, char *modules)
|
||||||
|
{
|
||||||
|
m_Callsign = callsign;
|
||||||
|
m_Ip = ip;
|
||||||
|
::memset(m_ReflectorModules, 0, sizeof(m_ReflectorModules));
|
||||||
|
::strncpy(m_ReflectorModules, modules, sizeof(m_ReflectorModules)-1);
|
||||||
|
m_LastKeepaliveTime.Now();
|
||||||
|
m_ConnectTime = std::time(NULL);
|
||||||
|
m_LastHeardTime = std::time(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
CPeer::CPeer(const CPeer &peer)
|
||||||
|
{
|
||||||
|
m_Callsign = peer.m_Callsign;
|
||||||
|
m_Ip = peer.m_Ip;
|
||||||
|
::memcpy(m_ReflectorModules, peer.m_ReflectorModules, sizeof(m_ReflectorModules));
|
||||||
|
m_LastKeepaliveTime = peer.m_LastKeepaliveTime;
|
||||||
|
m_ConnectTime = peer.m_ConnectTime;
|
||||||
|
m_LastHeardTime = peer.m_LastHeardTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// destructors
|
||||||
|
|
||||||
|
CPeer::~CPeer()
|
||||||
|
{
|
||||||
|
for ( int i = 0; i < m_Clients.size(); i++ )
|
||||||
|
{
|
||||||
|
delete m_Clients[i];
|
||||||
|
}
|
||||||
|
m_Clients.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// operators
|
||||||
|
|
||||||
|
bool CPeer::operator ==(const CPeer &peer) const
|
||||||
|
{
|
||||||
|
bool same = true;
|
||||||
|
|
||||||
|
same &= (peer.m_Callsign == m_Callsign);
|
||||||
|
same &= (peer.m_Ip == m_Ip);
|
||||||
|
for ( int i = 0; (i < m_Clients.size()) && same ; i++ )
|
||||||
|
{
|
||||||
|
same &= (peer.m_Clients[i] == m_Clients[i]);
|
||||||
|
}
|
||||||
|
return same;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// status
|
||||||
|
|
||||||
|
bool CPeer::IsAMaster(void) const
|
||||||
|
{
|
||||||
|
bool master = false;
|
||||||
|
for ( int i = 0; (i < m_Clients.size()) && !master ; i++ )
|
||||||
|
{
|
||||||
|
master |= m_Clients[i]->IsAMaster();
|
||||||
|
}
|
||||||
|
return master;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CPeer::Alive(void)
|
||||||
|
{
|
||||||
|
m_LastKeepaliveTime.Now();;
|
||||||
|
for ( int i = 0; i < m_Clients.size(); i++ )
|
||||||
|
{
|
||||||
|
m_Clients[i]->Alive();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// manage clients
|
||||||
|
|
||||||
|
CClient *CPeer::GetClient(int i)
|
||||||
|
{
|
||||||
|
if ( (i >= 0) && (i < m_Clients.size()) )
|
||||||
|
{
|
||||||
|
return m_Clients[i];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// reporting
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// reporting
|
||||||
|
|
||||||
|
void CPeer::WriteXml(std::ofstream &xmlFile)
|
||||||
|
{
|
||||||
|
xmlFile << "<PEER>" << std::endl;
|
||||||
|
xmlFile << "\t<Callsign>" << m_Callsign << "</Callsign>" << std::endl;
|
||||||
|
xmlFile << "\t<IP>" << m_Ip << "</IP>" << std::endl;
|
||||||
|
xmlFile << "\t<LinkedModule>" << m_ReflectorModules << "</LinkedModule>" << std::endl;
|
||||||
|
xmlFile << "\t<Protocol>" << GetProtocolName() << "</Protocol>" << std::endl;
|
||||||
|
char mbstr[100];
|
||||||
|
if (std::strftime(mbstr, sizeof(mbstr), "%A %c", std::localtime(&m_ConnectTime)))
|
||||||
|
{
|
||||||
|
xmlFile << "\t<ConnectTime>" << mbstr << "</ConnectTime>" << std::endl;
|
||||||
|
}
|
||||||
|
if (std::strftime(mbstr, sizeof(mbstr), "%A %c", std::localtime(&m_LastHeardTime)))
|
||||||
|
{
|
||||||
|
xmlFile << "\t<LastHeardTime>" << mbstr << "</LastHeardTime>" << std::endl;
|
||||||
|
}
|
||||||
|
xmlFile << "</PEER>" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CPeer::GetJsonObject(char *Buffer)
|
||||||
|
{
|
||||||
|
char sz[512];
|
||||||
|
char mbstr[100];
|
||||||
|
char cs[16];
|
||||||
|
|
||||||
|
if (std::strftime(mbstr, sizeof(mbstr), "%A %c", std::localtime(&m_LastHeardTime)))
|
||||||
|
{
|
||||||
|
m_Callsign.GetCallsignString(cs);
|
||||||
|
|
||||||
|
::sprintf(sz, "{\"callsign\":\"%s\",\"linkedto\":\"%s\",\"time\":\"%s\"}",
|
||||||
|
cs,
|
||||||
|
m_ReflectorModules,
|
||||||
|
mbstr);
|
||||||
|
::strcat(Buffer, sz);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,94 @@
|
|||||||
|
//
|
||||||
|
// cpeer.h
|
||||||
|
// xlxd
|
||||||
|
//
|
||||||
|
// Created by Jean-Luc Deltombe (LX3JL) on 10/12/2016.
|
||||||
|
// Copyright © 2016 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// This file is part of xlxd.
|
||||||
|
//
|
||||||
|
// xlxd 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.
|
||||||
|
//
|
||||||
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef cpeer_h
|
||||||
|
#define cpeer_h
|
||||||
|
|
||||||
|
#include "ctimepoint.h"
|
||||||
|
#include "cip.h"
|
||||||
|
#include "ccallsign.h"
|
||||||
|
#include "cclient.h"
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// class
|
||||||
|
|
||||||
|
class CPeer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// constructors
|
||||||
|
CPeer();
|
||||||
|
CPeer(const CCallsign &, const CIp &, char *);
|
||||||
|
CPeer(const CPeer &);
|
||||||
|
|
||||||
|
// destructor
|
||||||
|
virtual ~CPeer();
|
||||||
|
|
||||||
|
// operators
|
||||||
|
bool operator ==(const CPeer &) const;
|
||||||
|
|
||||||
|
// get
|
||||||
|
const CCallsign &GetCallsign(void) const { return m_Callsign; }
|
||||||
|
const CIp &GetIp(void) const { return m_Ip; }
|
||||||
|
char *GetModulesModules(void) { return m_ReflectorModules; }
|
||||||
|
|
||||||
|
// set
|
||||||
|
|
||||||
|
// identity
|
||||||
|
virtual int GetProtocol(void) const { return PROTOCOL_NONE; }
|
||||||
|
virtual int GetProtocolRevision(void) const { return 0; }
|
||||||
|
virtual const char *GetProtocolName(void) const { return "none"; }
|
||||||
|
|
||||||
|
// status
|
||||||
|
virtual bool IsAMaster(void) const;
|
||||||
|
virtual void Alive(void);
|
||||||
|
virtual bool IsAlive(void) const { return false; }
|
||||||
|
virtual void Heard(void) { m_LastHeardTime = std::time(NULL); }
|
||||||
|
|
||||||
|
// clients access
|
||||||
|
int GetNbClients(void) const { return (int)m_Clients.size(); }
|
||||||
|
void ClearClients(void) { m_Clients.clear(); }
|
||||||
|
CClient *GetClient(int);
|
||||||
|
|
||||||
|
// reporting
|
||||||
|
virtual void WriteXml(std::ofstream &);
|
||||||
|
virtual void GetJsonObject(char *);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// data
|
||||||
|
CCallsign m_Callsign;
|
||||||
|
CIp m_Ip;
|
||||||
|
char m_ReflectorModules[NB_MODULES_MAX+1];
|
||||||
|
std::vector<CClient *> m_Clients;
|
||||||
|
|
||||||
|
// status
|
||||||
|
CTimePoint m_LastKeepaliveTime;
|
||||||
|
std::time_t m_ConnectTime;
|
||||||
|
std::time_t m_LastHeardTime;
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#endif /* cpeer_h */
|
||||||
@ -0,0 +1,230 @@
|
|||||||
|
//
|
||||||
|
// cpeers.cpp
|
||||||
|
// xlxd
|
||||||
|
//
|
||||||
|
// Created by Jean-Luc Deltombe (LX3JL) on 10/12/2016.
|
||||||
|
// Copyright © 2016 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// This file is part of xlxd.
|
||||||
|
//
|
||||||
|
// xlxd 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.
|
||||||
|
//
|
||||||
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include "creflector.h"
|
||||||
|
#include "cpeers.h"
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// constructor
|
||||||
|
|
||||||
|
|
||||||
|
CPeers::CPeers()
|
||||||
|
{
|
||||||
|
m_Peers.reserve(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// destructors
|
||||||
|
|
||||||
|
CPeers::~CPeers()
|
||||||
|
{
|
||||||
|
m_Mutex.lock();
|
||||||
|
{
|
||||||
|
for ( int i = 0; i < m_Peers.size(); i++ )
|
||||||
|
{
|
||||||
|
delete m_Peers[i];
|
||||||
|
}
|
||||||
|
m_Peers.clear();
|
||||||
|
|
||||||
|
}
|
||||||
|
m_Mutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// manage peers
|
||||||
|
|
||||||
|
void CPeers::AddPeer(CPeer *peer)
|
||||||
|
{
|
||||||
|
// first check if peer already exists
|
||||||
|
bool found = false;
|
||||||
|
for ( int i = 0; (i < m_Peers.size()) && !found; i++ )
|
||||||
|
{
|
||||||
|
found = (*peer == *m_Peers[i]);
|
||||||
|
// if found, just do nothing
|
||||||
|
// so *peer keep pointing on a valid object
|
||||||
|
// on function return
|
||||||
|
if ( found )
|
||||||
|
{
|
||||||
|
// delete new one
|
||||||
|
delete peer;
|
||||||
|
//std::cout << "Adding existing peer " << peer->GetCallsign() << " at " << peer->GetIp() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if not, append to the vector
|
||||||
|
if ( !found )
|
||||||
|
{
|
||||||
|
// grow vector capacity if needed
|
||||||
|
if ( m_Peers.capacity() == m_Peers.size() )
|
||||||
|
{
|
||||||
|
m_Peers.reserve(m_Peers.capacity()+10);
|
||||||
|
}
|
||||||
|
// append peer to reflector peer list
|
||||||
|
m_Peers.push_back(peer);
|
||||||
|
std::cout << "New peer " << peer->GetCallsign() << " at " << peer->GetIp()
|
||||||
|
<< " added with protocol " << peer->GetProtocol() << std::endl;
|
||||||
|
// and append all peer's client to reflector client list
|
||||||
|
// it is double lock safe to lock Clients list after Peers list
|
||||||
|
CClients *clients = g_Reflector.GetClients();
|
||||||
|
for ( int i = 0; i < peer->GetNbClients(); i++ )
|
||||||
|
{
|
||||||
|
clients->AddClient(peer->GetClient(i));
|
||||||
|
}
|
||||||
|
g_Reflector.ReleaseClients();
|
||||||
|
|
||||||
|
// notify
|
||||||
|
g_Reflector.OnPeersChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CPeers::RemovePeer(CPeer *peer)
|
||||||
|
{
|
||||||
|
// look for the client
|
||||||
|
bool found = false;
|
||||||
|
for ( int i = 0; (i < m_Peers.size()) && !found; i++ )
|
||||||
|
{
|
||||||
|
// compare objetc pointers
|
||||||
|
if ( (m_Peers[i]) == peer )
|
||||||
|
{
|
||||||
|
// found it !
|
||||||
|
if ( !m_Peers[i]->IsAMaster() )
|
||||||
|
{
|
||||||
|
// remove all clients from reflector client list
|
||||||
|
// it is double lock safe to lock Clients list after Peers list
|
||||||
|
CClients *clients = g_Reflector.GetClients();
|
||||||
|
for ( int i = 0; i < peer->GetNbClients(); i++ )
|
||||||
|
{
|
||||||
|
// this also delete the client object
|
||||||
|
clients->RemoveClient(peer->GetClient(i));
|
||||||
|
}
|
||||||
|
// so clear it then
|
||||||
|
m_Peers[i]->ClearClients();
|
||||||
|
g_Reflector.ReleaseClients();
|
||||||
|
|
||||||
|
// remove it
|
||||||
|
std::cout << "Peer " << m_Peers[i]->GetCallsign() << " at " << m_Peers[i]->GetIp()
|
||||||
|
<< " removed" << std::endl;
|
||||||
|
delete m_Peers[i];
|
||||||
|
m_Peers.erase(m_Peers.begin()+i);
|
||||||
|
found = true;
|
||||||
|
// notify
|
||||||
|
g_Reflector.OnPeersChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CPeer *CPeers::GetPeer(int i)
|
||||||
|
{
|
||||||
|
if ( (i >= 0) && (i < m_Peers.size()) )
|
||||||
|
{
|
||||||
|
return m_Peers[i];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// find peers
|
||||||
|
|
||||||
|
CPeer *CPeers::FindPeer(const CIp &Ip, int Protocol)
|
||||||
|
{
|
||||||
|
CPeer *peer = NULL;
|
||||||
|
|
||||||
|
// find peer
|
||||||
|
for ( int i = 0; (i < m_Peers.size()) && (peer == NULL); i++ )
|
||||||
|
{
|
||||||
|
if ( (m_Peers[i]->GetIp() == Ip) && (m_Peers[i]->GetProtocol() == Protocol))
|
||||||
|
{
|
||||||
|
peer = m_Peers[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// done
|
||||||
|
return peer;
|
||||||
|
}
|
||||||
|
|
||||||
|
CPeer *CPeers::FindPeer(const CCallsign &Callsign, const CIp &Ip, int Protocol)
|
||||||
|
{
|
||||||
|
CPeer *peer = NULL;
|
||||||
|
|
||||||
|
// find peer
|
||||||
|
for ( int i = 0; (i < m_Peers.size()) && (peer == NULL); i++ )
|
||||||
|
{
|
||||||
|
if ( m_Peers[i]->GetCallsign().HasSameCallsign(Callsign) &&
|
||||||
|
(m_Peers[i]->GetIp() == Ip) &&
|
||||||
|
(m_Peers[i]->GetProtocol() == Protocol) )
|
||||||
|
{
|
||||||
|
peer = m_Peers[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// done
|
||||||
|
return peer;
|
||||||
|
}
|
||||||
|
|
||||||
|
CPeer *CPeers::FindPeer(const CCallsign &Callsign, int Protocol)
|
||||||
|
{
|
||||||
|
CPeer *peer = NULL;
|
||||||
|
|
||||||
|
// find peer
|
||||||
|
for ( int i = 0; (i < m_Peers.size()) && (peer == NULL); i++ )
|
||||||
|
{
|
||||||
|
if ( (m_Peers[i]->GetProtocol() == Protocol) &&
|
||||||
|
m_Peers[i]->GetCallsign().HasSameCallsign(Callsign) )
|
||||||
|
{
|
||||||
|
peer = m_Peers[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// done
|
||||||
|
return peer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// iterate on peers
|
||||||
|
|
||||||
|
CPeer *CPeers::FindNextPeer(int Protocol, int *index)
|
||||||
|
{
|
||||||
|
CPeer *peer = NULL;
|
||||||
|
|
||||||
|
// find next peer
|
||||||
|
bool found = false;
|
||||||
|
for ( int i = *index+1; (i < m_Peers.size()) && !found; i++ )
|
||||||
|
{
|
||||||
|
if ( m_Peers[i]->GetProtocol() == Protocol )
|
||||||
|
{
|
||||||
|
found = true;
|
||||||
|
peer = m_Peers[i];
|
||||||
|
*index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return peer;
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,73 @@
|
|||||||
|
//
|
||||||
|
// cpeers.h
|
||||||
|
// xlxd
|
||||||
|
//
|
||||||
|
// Created by Jean-Luc Deltombe (LX3JL) on 10/12/2016.
|
||||||
|
// Copyright © 2016 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// This file is part of xlxd.
|
||||||
|
//
|
||||||
|
// xlxd 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.
|
||||||
|
//
|
||||||
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef cpeers_h
|
||||||
|
#define cpeers_h
|
||||||
|
|
||||||
|
#include "cpeer.h"
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// define
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// class
|
||||||
|
|
||||||
|
class CPeers
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// constructors
|
||||||
|
CPeers();
|
||||||
|
|
||||||
|
// destructors
|
||||||
|
virtual ~CPeers();
|
||||||
|
|
||||||
|
// locks
|
||||||
|
void Lock(void) { m_Mutex.lock(); }
|
||||||
|
void Unlock(void) { m_Mutex.unlock(); }
|
||||||
|
|
||||||
|
// manage peers
|
||||||
|
int GetSize(void) const { return (int)m_Peers.size(); }
|
||||||
|
void AddPeer(CPeer *);
|
||||||
|
void RemovePeer(CPeer *);
|
||||||
|
CPeer *GetPeer(int);
|
||||||
|
|
||||||
|
// find peers
|
||||||
|
CPeer *FindPeer(const CIp &, int);
|
||||||
|
CPeer *FindPeer(const CCallsign &, const CIp &, int);
|
||||||
|
CPeer *FindPeer(const CCallsign &, int);
|
||||||
|
|
||||||
|
// iterate on peers
|
||||||
|
CPeer *FindNextPeer(int, int*);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// data
|
||||||
|
std::mutex m_Mutex;
|
||||||
|
std::vector<CPeer *> m_Peers;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#endif /* cpeers_h */
|
||||||
@ -0,0 +1,84 @@
|
|||||||
|
//
|
||||||
|
// cxlxpeer.cpp
|
||||||
|
// xlxd
|
||||||
|
//
|
||||||
|
// Created by Jean-Luc Deltombe (LX3JL) on 10/12/2016.
|
||||||
|
// Copyright © 2016 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// This file is part of xlxd.
|
||||||
|
//
|
||||||
|
// xlxd 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.
|
||||||
|
//
|
||||||
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include "creflector.h"
|
||||||
|
#include "cxlxpeer.h"
|
||||||
|
#include "cxlxclient.h"
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// constructor
|
||||||
|
|
||||||
|
|
||||||
|
CXlxPeer::CXlxPeer()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
CXlxPeer::CXlxPeer(const CCallsign &callsign, const CIp &ip, char *modules)
|
||||||
|
: CPeer(callsign, ip, modules)
|
||||||
|
{
|
||||||
|
// and construct all xlx clients
|
||||||
|
for ( int i = 0; i < ::strlen(modules); i++ )
|
||||||
|
{
|
||||||
|
// create
|
||||||
|
CXlxClient *client = new CXlxClient(callsign, ip, modules[i]);
|
||||||
|
// and append to vector
|
||||||
|
m_Clients.push_back(client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CXlxPeer::CXlxPeer(const CXlxPeer &peer)
|
||||||
|
: CPeer(peer)
|
||||||
|
{
|
||||||
|
for ( int i = 0; i < peer.m_Clients.size(); i++ )
|
||||||
|
{
|
||||||
|
CXlxClient *client = new CXlxClient((const CXlxClient &)*(peer.m_Clients[i]));
|
||||||
|
// grow vector capacity if needed
|
||||||
|
if ( m_Clients.capacity() == m_Clients.size() )
|
||||||
|
{
|
||||||
|
m_Clients.reserve(m_Clients.capacity()+10);
|
||||||
|
}
|
||||||
|
// and append
|
||||||
|
m_Clients.push_back(client);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// destructors
|
||||||
|
|
||||||
|
CXlxPeer::~CXlxPeer()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// status
|
||||||
|
|
||||||
|
bool CXlxPeer::IsAlive(void) const
|
||||||
|
{
|
||||||
|
return (m_LastKeepaliveTime.DurationSinceNow() < XLX_KEEPALIVE_TIMEOUT);
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
//
|
||||||
|
// cxlxpeer.h
|
||||||
|
// xlxd
|
||||||
|
//
|
||||||
|
// Created by Jean-Luc Deltombe (LX3JL) on 10/12/2016.
|
||||||
|
// Copyright © 2016 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// This file is part of xlxd.
|
||||||
|
//
|
||||||
|
// xlxd 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.
|
||||||
|
//
|
||||||
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef cxlxpeer_h
|
||||||
|
#define cxlxpeer_h
|
||||||
|
|
||||||
|
#include "cpeer.h"
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// class
|
||||||
|
|
||||||
|
class CXlxPeer : public CPeer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// constructors
|
||||||
|
CXlxPeer();
|
||||||
|
CXlxPeer(const CCallsign &, const CIp &, char *);
|
||||||
|
CXlxPeer(const CXlxPeer &);
|
||||||
|
|
||||||
|
// destructor
|
||||||
|
~CXlxPeer();
|
||||||
|
|
||||||
|
// status
|
||||||
|
bool IsAlive(void) const;
|
||||||
|
// identity
|
||||||
|
int GetProtocol(void) const { return PROTOCOL_XLX; }
|
||||||
|
const char *GetProtocolName(void) const { return "XLX"; }
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#endif /* cxlxpeer_h */
|
||||||
Loading…
Reference in new issue