// Copyright © 2016 Jean-Luc Deltombe (LX3JL). All rights reserved. // urfd -- The universal reflector // Copyright © 2021 Thomas A. Early N7TAE // // 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 . #include #include "Reflector.h" #include "Peer.h" //////////////////////////////////////////////////////////////////////////////////////// // constructor CPeer::CPeer() { memset(m_ReflectorModules, 0, sizeof(m_ReflectorModules)); m_ConnectTime = std::time(nullptr); m_LastHeardTime = std::time(nullptr); } CPeer::CPeer(const CCallsign &callsign, const CIp &ip, const char *modules, const CVersion &version) { m_Callsign = callsign; m_Ip = ip; memset(m_ReflectorModules, 0, sizeof(m_ReflectorModules)); ::strncpy(m_ReflectorModules, modules, sizeof(m_ReflectorModules)-1); m_Version = version; m_LastKeepaliveTime.start(); m_ConnectTime = std::time(nullptr); m_LastHeardTime = std::time(nullptr); } //////////////////////////////////////////////////////////////////////////////////////// // destructors CPeer::~CPeer() { m_Clients.clear(); } //////////////////////////////////////////////////////////////////////////////////////// // operators bool CPeer::operator ==(const CPeer &peer) const { if (peer.m_Callsign != m_Callsign) return false; if (peer.m_Ip != m_Ip) return false; if (! (peer.m_Version == m_Version)) return false; auto it1 = cbegin(); auto it2 = peer.cbegin(); while (true) { if (it1==cend() && it2==peer.cend()) break; if (it1==cend() || it2==peer.cend()) return false; if (*it1 != *it2) return false; it1++; it2++; } return true; } //////////////////////////////////////////////////////////////////////////////////////// // status bool CPeer::IsAMaster(void) const { for ( auto it=cbegin(); it!=cend(); it++ ) { if ((*it)->IsAMaster()) return true; } return false; } void CPeer::Alive(void) { m_LastKeepaliveTime.start(); for ( auto it=begin(); it!=end(); it++ ) { (*it)->Alive(); } } //////////////////////////////////////////////////////////////////////////////////////// // reporting void CPeer::WriteXml(std::ofstream &xmlFile) { xmlFile << "" << std::endl; xmlFile << "\t" << m_Callsign << "" << std::endl; xmlFile << "\t" << m_Ip.GetAddress() << "" << 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; }