diff --git a/src/ccallsignlistitem.cpp b/src/ccallsignlistitem.cpp index fb1a9b4..ddea21d 100644 --- a/src/ccallsignlistitem.cpp +++ b/src/ccallsignlistitem.cpp @@ -42,7 +42,7 @@ CCallsignListItem::CCallsignListItem(const CCallsign &callsign, const CIp &ip, c m_Ip = ip; if ( modules != NULL ) { - :: memset(m_Modules, 0, sizeof(m_Modules)); + ::memset(m_Modules, 0, sizeof(m_Modules)); if ( modules[0] == '*' ) { for ( char i = 0; i < NB_OF_MODULES; i++ ) @@ -52,7 +52,15 @@ CCallsignListItem::CCallsignListItem(const CCallsign &callsign, const CIp &ip, c } else { - ::memcpy(m_Modules, modules, MIN(strlen(modules), sizeof(m_Modules)-1)); + int n = MIN((int)::strlen(modules), sizeof(m_Modules)-1); + int j = 0; + for ( int i = 0; i < n; i++ ) + { + if ( (modules[i] - 'A') < NB_OF_MODULES ) + { + m_Modules[j++] = modules[i]; + } + } } } } @@ -64,7 +72,7 @@ CCallsignListItem::CCallsignListItem(const CCallsign &callsign, const char *url, m_Ip = CIp(m_szUrl); if ( modules != NULL ) { - :: memset(m_Modules, 0, sizeof(m_Modules)); + ::memset(m_Modules, 0, sizeof(m_Modules)); if ( modules[0] == '*' ) { for ( char i = 0; i < NB_OF_MODULES; i++ ) @@ -74,7 +82,15 @@ CCallsignListItem::CCallsignListItem(const CCallsign &callsign, const char *url, } else { - ::memcpy(m_Modules, modules, MIN(strlen(modules), sizeof(m_Modules)-1)); + int n = MIN((int)::strlen(modules), sizeof(m_Modules)-1); + int j = 0; + for ( int i = 0; i < n; i++ ) + { + if ( (modules[i] - 'A') < NB_OF_MODULES ) + { + m_Modules[j++] = modules[i]; + } + } } } } diff --git a/src/cclient.cpp b/src/cclient.cpp index 173fc6a..f79dcc2 100644 --- a/src/cclient.cpp +++ b/src/cclient.cpp @@ -76,7 +76,8 @@ void CClient::Alive(void) bool CClient::operator ==(const CClient &client) const { return ((client.m_Callsign == m_Callsign) && - (client.m_Ip == m_Ip)); + (client.m_Ip == m_Ip) && + (client.m_ReflectorModule == m_ReflectorModule)); } //////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/cclient.h b/src/cclient.h index 32f8796..15f74fa 100644 --- a/src/cclient.h +++ b/src/cclient.h @@ -56,7 +56,6 @@ public: bool HasModule(void) const { return m_Callsign.HasModule(); } char GetModule(void) const { return m_Callsign.GetModule(); } char GetReflectorModule(void) const { return m_ReflectorModule; } - virtual bool HasThisReflectorModule(char m) const { return (m_ReflectorModule == m); } // set void SetModule(char c) { m_Callsign.SetModule(c); } diff --git a/src/cclients.cpp b/src/cclients.cpp index 484100f..d0cc5e8 100644 --- a/src/cclients.cpp +++ b/src/cclients.cpp @@ -85,11 +85,12 @@ void CClients::AddClient(CClient *client) // and append m_Clients.push_back(client); std::cout << "New client " << client->GetCallsign() << " at " << client->GetIp() - << " added with protocol " << client->GetProtocol() << std::endl; + << " added with protocol " << client->GetProtocol() + << " on module " << client->GetReflectorModule() << std::endl; // notify g_Reflector.OnClientsChanged(); } - } +} void CClients::RemoveClient(CClient *client) { @@ -105,7 +106,8 @@ void CClients::RemoveClient(CClient *client) { // remove it std::cout << "Client " << m_Clients[i]->GetCallsign() << " at " << m_Clients[i]->GetIp() - << " removed" << std::endl; + << " removed with protocol " << client->GetProtocol() + << " on module " << client->GetReflectorModule() << std::endl; delete m_Clients[i]; m_Clients.erase(m_Clients.begin()+i); found = true; @@ -175,6 +177,25 @@ CClient *CClients::FindClient(const CIp &Ip, int Protocol) return client; } +CClient *CClients::FindClient(const CIp &Ip, int Protocol, char ReflectorModule) +{ + CClient *client = NULL; + + // find client + for ( int i = 0; (i < m_Clients.size()) && (client == NULL); i++ ) + { + if ( (m_Clients[i]->GetIp() == Ip) && + (m_Clients[i]->GetReflectorModule() == ReflectorModule) && + (m_Clients[i]->GetProtocol() == Protocol) ) + { + client = m_Clients[i]; + } + } + + // done + return client; +} + CClient *CClients::FindClient(const CCallsign &Callsign, const CIp &Ip, int Protocol) { CClient *client = NULL; @@ -232,7 +253,6 @@ CClient *CClients::FindClient(const CCallsign &Callsign, int Protocol) return client; } - //////////////////////////////////////////////////////////////////////////////////////// // iterate on clients diff --git a/src/cclients.h b/src/cclients.h index 34cb42d..8c5d010 100644 --- a/src/cclients.h +++ b/src/cclients.h @@ -58,6 +58,7 @@ public: // find clients CClient *FindClient(const CIp &); CClient *FindClient(const CIp &, int); + CClient *FindClient(const CIp &, int, char); CClient *FindClient(const CCallsign &, const CIp &, int); CClient *FindClient(const CCallsign &, char, const CIp &, int); CClient *FindClient(const CCallsign &, int); diff --git a/src/cnotification.h b/src/cnotification.h index da7d570..04ae81b 100644 --- a/src/cnotification.h +++ b/src/cnotification.h @@ -36,6 +36,7 @@ #define NOTIFICATION_USERS 2 #define NOTIFICATION_STREAM_OPEN 3 #define NOTIFICATION_STREAM_CLOSE 4 +#define NOTIFICATION_PEERS 5 //////////////////////////////////////////////////////////////////////////////////////// // class diff --git a/src/cpeer.cpp b/src/cpeer.cpp new file mode 100644 index 0000000..d1db483 --- /dev/null +++ b/src/cpeer.cpp @@ -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 . +// ---------------------------------------------------------------------------- + +#include "main.h" +#include +#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 << "" << std::endl; + xmlFile << "\t" << m_Callsign << "" << std::endl; + xmlFile << "\t" << m_Ip << "" << std::endl; + xmlFile << "\t" << m_ReflectorModules << "" << std::endl; + xmlFile << "\t" << GetProtocolName() << "" << std::endl; + char mbstr[100]; + if (std::strftime(mbstr, sizeof(mbstr), "%A %c", std::localtime(&m_ConnectTime))) + { + xmlFile << "\t" << mbstr << "" << std::endl; + } + if (std::strftime(mbstr, sizeof(mbstr), "%A %c", std::localtime(&m_LastHeardTime))) + { + xmlFile << "\t" << mbstr << "" << std::endl; + } + xmlFile << "" << 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); + } +} diff --git a/src/cpeer.h b/src/cpeer.h new file mode 100644 index 0000000..92dcdc6 --- /dev/null +++ b/src/cpeer.h @@ -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 . +// ---------------------------------------------------------------------------- + +#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 m_Clients; + + // status + CTimePoint m_LastKeepaliveTime; + std::time_t m_ConnectTime; + std::time_t m_LastHeardTime; +}; + +//////////////////////////////////////////////////////////////////////////////////////// +#endif /* cpeer_h */ diff --git a/src/cpeers.cpp b/src/cpeers.cpp new file mode 100644 index 0000000..3a27282 --- /dev/null +++ b/src/cpeers.cpp @@ -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 . +// ---------------------------------------------------------------------------- + +#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; +} + diff --git a/src/cpeers.h b/src/cpeers.h new file mode 100644 index 0000000..65e4514 --- /dev/null +++ b/src/cpeers.h @@ -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 . +// ---------------------------------------------------------------------------- + +#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 m_Peers; +}; + + +//////////////////////////////////////////////////////////////////////////////////////// +#endif /* cpeers_h */ diff --git a/src/creflector.cpp b/src/creflector.cpp index 90e0fef..2ec17b4 100644 --- a/src/creflector.cpp +++ b/src/creflector.cpp @@ -421,6 +421,7 @@ void CReflector::JsonReportThread(CReflector *This) switch ( notification.GetId() ) { case NOTIFICATION_CLIENTS: + case NOTIFICATION_PEERS: //std::cout << "Monitor updating nodes table" << std::endl; This->SendJsonNodesObject(Socket, Ip); break; @@ -455,6 +456,15 @@ void CReflector::JsonReportThread(CReflector *This) //////////////////////////////////////////////////////////////////////////////////////// // notifications +void CReflector::OnPeersChanged(void) +{ + CNotification notification(NOTIFICATION_PEERS); + + m_Notifications.Lock(); + m_Notifications.push(notification); + m_Notifications.Unlock(); +} + void CReflector::OnClientsChanged(void) { CNotification notification(NOTIFICATION_CLIENTS); @@ -544,23 +554,20 @@ void CReflector::WriteXmlFile(std::ofstream &xmlFile) // linked peers xmlFile << "<" << m_Callsign << "linked peers>" << std::endl; // lock - CClients *clients = GetClients(); - // iterate on clients - for ( int i = 0; i < clients->GetSize(); i++ ) + CPeers *peers = GetPeers(); + // iterate on peers + for ( int i = 0; i < peers->GetSize(); i++ ) { - if ( clients->GetClient(i)->IsPeer() ) - { - clients->GetClient(i)->WriteXml(xmlFile); - } + peers->GetPeer(i)->WriteXml(xmlFile); } // unlock - ReleaseClients(); + ReleasePeers(); xmlFile << "" << std::endl; // linked nodes xmlFile << "<" << m_Callsign << "linked nodes>" << std::endl; // lock - clients = GetClients(); + CClients *clients = GetClients(); // iterate on clients for ( int i = 0; i < clients->GetSize(); i++ ) { diff --git a/src/creflector.h b/src/creflector.h index 341a1a2..07700b9 100644 --- a/src/creflector.h +++ b/src/creflector.h @@ -27,6 +27,7 @@ #include "cusers.h" #include "cclients.h" +#include "cpeers.h" #include "cprotocols.h" #include "cpacketstream.h" #include "cnotificationqueue.h" @@ -67,6 +68,10 @@ public: CClients *GetClients(void) { m_Clients.Lock(); return &m_Clients; } void ReleaseClients(void) { m_Clients.Unlock(); } + // peers + CPeers *GetPeers(void) { m_Peers.Lock(); return &m_Peers; } + void ReleasePeers(void) { m_Peers.Unlock(); } + // stream opening & closing CPacketStream *OpenStream(CDvHeaderPacket *, CClient *); bool IsStreaming(char); @@ -82,6 +87,7 @@ public: char GetModuleLetter(int i) const { return 'A' + (char)i; } // notifications + void OnPeersChanged(void); void OnClientsChanged(void); void OnUsersChanged(void); void OnStreamOpen(const CCallsign &); @@ -114,7 +120,8 @@ protected: // objects CUsers m_Users; // sorted list of lastheard stations - CClients m_Clients; // list of linked repeaters/nodes + CClients m_Clients; // list of linked repeaters/nodes/peers's modules + CPeers m_Peers; // list of linked peers CProtocols m_Protocols; // list of supported protocol handlers // queues diff --git a/src/cxlxclient.cpp b/src/cxlxclient.cpp index 97d6be5..63d51d8 100644 --- a/src/cxlxclient.cpp +++ b/src/cxlxclient.cpp @@ -32,31 +32,16 @@ CXlxClient::CXlxClient() { - ::memset(m_ReflectorModules, 0, sizeof(m_ReflectorModules)); } -CXlxClient::CXlxClient(const CCallsign &callsign, const CIp &ip, char *reflectorModules) -: CClient(callsign, ip) +CXlxClient::CXlxClient(const CCallsign &callsign, const CIp &ip, char reflectorModule) +: CClient(callsign, ip, reflectorModule) { - ::memset(m_ReflectorModules, 0, sizeof(m_ReflectorModules)); - if ( reflectorModules != NULL ) - { - while ( *reflectorModules != 0x00 ) - { - if ( (*reflectorModules >= 'A') && (*reflectorModules < ('A'+ NB_OF_MODULES)) ) - { - ::strncat(m_ReflectorModules, reflectorModules, 1); - } - reflectorModules++; - } - } - } CXlxClient::CXlxClient(const CXlxClient &client) : CClient(client) { - ::strcpy(m_ReflectorModules, client.m_ReflectorModules); } //////////////////////////////////////////////////////////////////////////////////////// @@ -67,34 +52,3 @@ bool CXlxClient::IsAlive(void) const return (m_LastKeepaliveTime.DurationSinceNow() < XLX_KEEPALIVE_TIMEOUT); } -//////////////////////////////////////////////////////////////////////////////////////// -// get - -bool CXlxClient::HasThisReflectorModule(char module) const -{ - return (::strchr(m_ReflectorModules, module) != NULL); -} - -//////////////////////////////////////////////////////////////////////////////////////// -// reporting - -void CXlxClient::WriteXml(std::ofstream &xmlFile) -{ - xmlFile << "" << std::endl; - xmlFile << "\t" << m_Callsign << "" << std::endl; - xmlFile << "\t" << m_Ip << "" << std::endl; - xmlFile << "\t" << m_ReflectorModules << "" << std::endl; - xmlFile << "\t" << GetProtocolName() << "" << std::endl; - char mbstr[100]; - if (std::strftime(mbstr, sizeof(mbstr), "%A %c", std::localtime(&m_ConnectTime))) - { - xmlFile << "\t" << mbstr << "" << std::endl; - } - if (std::strftime(mbstr, sizeof(mbstr), "%A %c", std::localtime(&m_LastHeardTime))) - { - xmlFile << "\t" << mbstr << "" << std::endl; - } - xmlFile << "" << std::endl; -} - - diff --git a/src/cxlxclient.h b/src/cxlxclient.h index 862bb74..503df40 100644 --- a/src/cxlxclient.h +++ b/src/cxlxclient.h @@ -40,7 +40,7 @@ class CXlxClient : public CClient public: // constructors CXlxClient(); - CXlxClient(const CCallsign &, const CIp &, char * = NULL); + CXlxClient(const CCallsign &, const CIp &, char = ' '); CXlxClient(const CXlxClient &); // destructor @@ -54,16 +54,8 @@ public: // status bool IsAlive(void) const; - // get - bool HasThisReflectorModule(char) const; - // reporting - void WriteXml(std::ofstream &); - -protected: - // linked to - char m_ReflectorModules[NB_OF_MODULES+1]; - + void WriteXml(std::ofstream &) {} }; //////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/cxlxpeer.cpp b/src/cxlxpeer.cpp new file mode 100644 index 0000000..d96fbaa --- /dev/null +++ b/src/cxlxpeer.cpp @@ -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 . +// ---------------------------------------------------------------------------- + +#include "main.h" +#include +#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); +} + diff --git a/src/cxlxpeer.h b/src/cxlxpeer.h new file mode 100644 index 0000000..1ced2ad --- /dev/null +++ b/src/cxlxpeer.h @@ -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 . +// ---------------------------------------------------------------------------- + +#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 */ diff --git a/src/cxlxprotocol.cpp b/src/cxlxprotocol.cpp index 97a3546..332ca29 100644 --- a/src/cxlxprotocol.cpp +++ b/src/cxlxprotocol.cpp @@ -24,7 +24,7 @@ #include "main.h" #include -#include "cxlxclient.h" +#include "cxlxpeer.h" #include "cxlxprotocol.h" #include "creflector.h" #include "cgatekeeper.h" @@ -52,6 +52,7 @@ bool CXlxProtocol::Init(void) // update time m_LastKeepaliveTime.Now(); + m_LastPeersLinkTime.Now(); // done return ok; @@ -69,6 +70,7 @@ void CXlxProtocol::Task(void) CDvHeaderPacket *Header; CDvFramePacket *Frame; CDvLastFramePacket *LastFrame; + uint8 Major, Minor, Revision; // any incoming packet ? if ( m_Socket.Receive(&Buffer, &Ip, 20) != -1 ) @@ -84,6 +86,7 @@ void CXlxProtocol::Task(void) else if ( (Header = IsValidDvHeaderPacket(Buffer)) != NULL ) { //std::cout << "XLX (DExtra) DV header:" << std::endl << *Header << std::endl; + //std::cout << "XLX (DExtra) DV header on module " << Header->GetRpt2Module() << std::endl; // callsign muted? if ( g_GateKeeper.MayTransmit(Header->GetMyCallsign(), Ip) ) @@ -103,49 +106,80 @@ void CXlxProtocol::Task(void) // handle it OnDvLastFramePacketIn(LastFrame); } - else if ( IsValidConnectPacket(Buffer, &Callsign, Modules) || IsValidAckPacket(Buffer, &Callsign, Modules) ) + else if ( IsValidConnectPacket(Buffer, &Callsign, Modules, &Major, &Minor, &Revision) ) { - std::cout << "XLX connect/ack packet for modules " << Modules << " from " << Callsign << " at " << Ip << std::endl; + std::cout << "XLX (" << (int)Major << "." << (int)Minor << "." << (int)Revision + << ") connect packet for modules " << Modules + << " from " << Callsign << " at " << Ip << std::endl; - // already connected ? - CClients *clients = g_Reflector.GetClients(); - if ( clients->FindClient(Callsign, Ip, PROTOCOL_XLX) == NULL ) + // callsign authorized? + if ( g_GateKeeper.MayLink(Callsign, Ip, PROTOCOL_XLX, Modules) ) { - // callsign authorized? - if ( g_GateKeeper.MayLink(Callsign, Ip, PROTOCOL_XLX, Modules) ) + // following is version dependent + // for backward compatibility, only send ACK once + if ( (Major == 1) && (Minor < 4) ) { - // acknowledge the request - EncodeConnectAckPacket(&Buffer, Modules); - m_Socket.Send(Buffer, Ip); - - // create the client - CXlxClient *client = new CXlxClient(Callsign, Ip, Modules); - - // and append - clients->AddClient(client); + // already connected ? + CPeers *peers = g_Reflector.GetPeers(); + if ( peers->FindPeer(Callsign, Ip, PROTOCOL_XLX) == NULL ) + { + // acknowledge the request + EncodeConnectAckPacket(&Buffer, Modules); + m_Socket.Send(Buffer, Ip); + } + g_Reflector.ReleasePeers(); } else { - // deny the request - EncodeConnectNackPacket(&Buffer); - m_Socket.Send(Buffer, Ip); + // acknowledge the request + EncodeConnectAckPacket(&Buffer, Modules); + m_Socket.Send(Buffer, Ip); } } - // done - g_Reflector.ReleaseClients(); + else + { + // deny the request + EncodeConnectNackPacket(&Buffer); + m_Socket.Send(Buffer, Ip); + } + } + else if ( IsValidAckPacket(Buffer, &Callsign, Modules) ) + { + std::cout << "XLX ack packet for modules " << Modules << " from " << Callsign << " at " << Ip << std::endl; + + // callsign authorized? + if ( g_GateKeeper.MayLink(Callsign, Ip, PROTOCOL_XLX, Modules) ) + { + // already connected ? + CPeers *peers = g_Reflector.GetPeers(); + if ( peers->FindPeer(Callsign, Ip, PROTOCOL_XLX) == NULL ) + { + // create the new peer + // this also create one client per module + CXlxPeer *peer = new CXlxPeer(Callsign, Ip, Modules); + + // append the peer to reflector peer list + // this also add all new clients to reflector client list + peers->AddPeer(peer); + } + g_Reflector.ReleasePeers(); + } } else if ( IsValidDisconnectPacket(Buffer, &Callsign) ) { std::cout << "XLX disconnect packet from " << Callsign << " at " << Ip << std::endl; - // find client & remove it - CClients *clients = g_Reflector.GetClients(); - CClient *client = clients->FindClient(Callsign, Ip, PROTOCOL_XLX); - if ( client != NULL ) + // find peer + CPeers *peers = g_Reflector.GetPeers(); + CPeer *peer = peers->FindPeer(Ip, PROTOCOL_XLX); + if ( peer != NULL ) { - clients->RemoveClient(client); + // remove it from reflector peer list + // this also remove all concerned clients from reflector client list + // and delete them + peers->RemovePeer(peer); } - g_Reflector.ReleaseClients(); + g_Reflector.ReleasePeers(); } else if ( IsValidNackPacket(Buffer, &Callsign) ) { @@ -155,15 +189,15 @@ void CXlxProtocol::Task(void) { //std::cout << "XLX keepalive packet from " << Callsign << " at " << Ip << std::endl; - // find client & keep it alive - CClient *GetClient(const CCallsign &, const CIp &, char, int); - - CClient *client = g_Reflector.GetClients()->FindClient(Callsign, Ip, PROTOCOL_XLX); - if ( client != NULL ) + // find peer + CPeers *peers = g_Reflector.GetPeers(); + CPeer *peer = peers->FindPeer(Ip, PROTOCOL_XLX); + if ( peer != NULL ) { - client->Alive(); + // keep it alive + peer->Alive(); } - g_Reflector.ReleaseClients(); + g_Reflector.ReleasePeers(); } else { @@ -183,11 +217,18 @@ void CXlxProtocol::Task(void) // handle keep alives HandleKeepalives(); + // update time + m_LastKeepaliveTime.Now(); + } + + // peer connections + if ( m_LastPeersLinkTime.DurationSinceNow() > XLX_RECONNECT_PERIOD ) + { // handle remote peers connections HandlePeerLinks(); // update time - m_LastKeepaliveTime.Now(); + m_LastPeersLinkTime.Now(); } } @@ -219,8 +260,7 @@ void CXlxProtocol::HandleQueue(void) while ( (client = clients->FindNextClient(PROTOCOL_XLX, &index)) != NULL ) { // is this client busy ? - // here check that origin module of the stream is listed in client xlx - if ( !client->IsAMaster() && client->HasThisReflectorModule(packet->GetModuleId()) ) + if ( !client->IsAMaster() && (client->GetReflectorModule() == packet->GetModuleId()) ) { // no, send the packet m_Socket.Send(buffer, client->GetIp()); @@ -247,35 +287,35 @@ void CXlxProtocol::HandleKeepalives(void) CBuffer keepalive; EncodeKeepAlivePacket(&keepalive); - // iterate on clients - CClients *clients = g_Reflector.GetClients(); + // iterate on peers + CPeers *peers = g_Reflector.GetPeers(); int index = -1; - CClient *client = NULL; - while ( (client = clients->FindNextClient(PROTOCOL_XLX, &index)) != NULL ) + CPeer *peer = NULL; + while ( (peer = peers->FindNextPeer(PROTOCOL_XLX, &index)) != NULL ) { // send keepalive - m_Socket.Send(keepalive, client->GetIp()); + m_Socket.Send(keepalive, peer->GetIp()); // client busy ? - if ( client->IsAMaster() ) + if ( peer->IsAMaster() ) { // yes, just tickle it - client->Alive(); + peer->Alive(); } // otherwise check if still with us - else if ( !client->IsAlive() ) + else if ( !peer->IsAlive() ) { // no, disconnect CBuffer disconnect; EncodeDisconnectPacket(&disconnect); - m_Socket.Send(disconnect, client->GetIp()); + m_Socket.Send(disconnect, peer->GetIp()); // remove it - std::cout << "XLX peer " << client->GetCallsign() << " keepalive timeout" << std::endl; - clients->RemoveClient(client); + std::cout << "XLX peer " << peer->GetCallsign() << " keepalive timeout" << std::endl; + peers->RemovePeer(peer); } } - g_Reflector.ReleaseClients(); + g_Reflector.ReleasePeers(); } //////////////////////////////////////////////////////////////////////////////////////// @@ -287,23 +327,22 @@ void CXlxProtocol::HandlePeerLinks(void) // get the list of peers CPeerCallsignList *list = g_GateKeeper.GetPeerList(); - // todo: analyse possibility of double-lock hang-up - CClients *clients = g_Reflector.GetClients(); + CPeers *peers = g_Reflector.GetPeers(); - // check if all our connected client are still listed by gatekeeper + // check if all our connected peers are still listed by gatekeeper // if not, disconnect int index = -1; - CClient *client = NULL; - while ( (client = clients->FindNextClient(PROTOCOL_XLX, &index)) != NULL ) + CPeer *peer = NULL; + while ( (peer = peers->FindNextPeer(PROTOCOL_XLX, &index)) != NULL ) { - if ( list->FindListItem(client->GetCallsign()) == NULL ) + if ( list->FindListItem(peer->GetCallsign()) == NULL ) { // send disconnect packet EncodeDisconnectPacket(&buffer); - m_Socket.Send(buffer, client->GetIp()); - std::cout << "Sending disconnect packet to XLX peer " << client->GetCallsign() << std::endl; + m_Socket.Send(buffer, peer->GetIp()); + std::cout << "Sending disconnect packet to XLX peer " << peer->GetCallsign() << std::endl; // remove client - clients->RemoveClient(client); + peers->RemovePeer(peer); } } @@ -312,7 +351,7 @@ void CXlxProtocol::HandlePeerLinks(void) for ( int i = 0; i < list->size(); i++ ) { CCallsignListItem *item = &((list->data())[i]); - if ( clients->FindClient(item->GetCallsign(), PROTOCOL_XLX) == NULL ) + if ( peers->FindPeer(item->GetCallsign(), PROTOCOL_XLX) == NULL ) { // resolve again peer's IP in case it's a dynamic IP item->ResolveIp(); @@ -324,7 +363,7 @@ void CXlxProtocol::HandlePeerLinks(void) } // done - g_Reflector.ReleaseClients(); + g_Reflector.ReleasePeers(); g_GateKeeper.ReleasePeerList(); } @@ -349,7 +388,7 @@ bool CXlxProtocol::OnDvHeaderPacketIn(CDvHeaderPacket *Header, const CIp &Ip) { // no stream open yet, open a new one // find this client - CClient *client = g_Reflector.GetClients()->FindClient(Ip, PROTOCOL_XLX); + CClient *client = g_Reflector.GetClients()->FindClient(Ip, PROTOCOL_XLX, Header->GetRpt2Module()); if ( client != NULL ) { // and try to open the stream @@ -414,7 +453,7 @@ bool CXlxProtocol::IsValidKeepAlivePacket(const CBuffer &Buffer, CCallsign *call } -bool CXlxProtocol::IsValidConnectPacket(const CBuffer &Buffer, CCallsign *callsign, char *modules) +bool CXlxProtocol::IsValidConnectPacket(const CBuffer &Buffer, CCallsign *callsign, char *modules, uint8 *major, uint8 *minor, uint8 *rev) { bool valid = false; if ((Buffer.size() == 39) && (Buffer.data()[0] == 'L') && (Buffer.data()[38] == 0)) @@ -422,6 +461,9 @@ bool CXlxProtocol::IsValidConnectPacket(const CBuffer &Buffer, CCallsign *callsi callsign->SetCallsign((const uint8 *)&(Buffer.data()[1]), 8); ::strcpy(modules, (const char *)&(Buffer.data()[12])); valid = callsign->IsValid(); + *major = Buffer.data()[9]; + *minor = Buffer.data()[10]; + *rev = Buffer.data()[11]; for ( int i = 0; i < ::strlen(modules); i++ ) { valid &= IsLetter(modules[i]); diff --git a/src/cxlxprotocol.h b/src/cxlxprotocol.h index d61d877..2a6f7fc 100644 --- a/src/cxlxprotocol.h +++ b/src/cxlxprotocol.h @@ -28,6 +28,7 @@ #include "ctimepoint.h" #include "cdextraprotocol.h" +#include "cclients.h" //////////////////////////////////////////////////////////////////////////////////////// @@ -65,7 +66,7 @@ protected: // packet decoding helpers bool IsValidKeepAlivePacket(const CBuffer &, CCallsign *); - bool IsValidConnectPacket(const CBuffer &, CCallsign *, char *); + bool IsValidConnectPacket(const CBuffer &, CCallsign *, char *, uint8 *, uint8 *, uint8 *); bool IsValidDisconnectPacket(const CBuffer &, CCallsign *); bool IsValidAckPacket(const CBuffer &, CCallsign *, char *); bool IsValidNackPacket(const CBuffer &, CCallsign *); @@ -80,6 +81,7 @@ protected: protected: // time CTimePoint m_LastKeepaliveTime; + CTimePoint m_LastPeersLinkTime; }; //////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/main.h b/src/main.h index d3bdeac..9ebaca6 100644 --- a/src/main.h +++ b/src/main.h @@ -47,8 +47,8 @@ // version ----------------------------------------------------- #define VERSION_MAJOR 1 -#define VERSION_MINOR 3 -#define VERSION_REVISION 9 +#define VERSION_MINOR 4 +#define VERSION_REVISION 0 // global ------------------------------------------------------ @@ -90,6 +90,7 @@ #define XLX_PORT 10002 // UDP port #define XLX_KEEPALIVE_PERIOD 1 // in seconds #define XLX_KEEPALIVE_TIMEOUT (XLX_KEEPALIVE_PERIOD*30) // in seconds +#define XLX_RECONNECT_PERIOD 5 // in seconds // xml & json reporting -----------------------------------------