From d26a94f84ca10ec2e73e28d3f3eca0af20834c78 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 2 Oct 2014 15:00:59 -0700 Subject: [PATCH] remove inactive peers from memory in IceServer --- ice-server/src/IceServer.cpp | 31 ++++++++++++++++++++++++ ice-server/src/IceServer.h | 5 +++- libraries/networking/src/NetworkPeer.cpp | 7 +++++- libraries/networking/src/NetworkPeer.h | 9 +++++++ libraries/networking/src/Node.cpp | 3 --- libraries/networking/src/Node.h | 9 +------ 6 files changed, 51 insertions(+), 13 deletions(-) diff --git a/ice-server/src/IceServer.cpp b/ice-server/src/IceServer.cpp index a2e4221bfd..be97df37a4 100644 --- a/ice-server/src/IceServer.cpp +++ b/ice-server/src/IceServer.cpp @@ -9,10 +9,16 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include + #include +#include #include "IceServer.h" +const int CLEAR_INACTIVE_PEERS_INTERVAL_MSECS = 1 * 1000; +const int PEER_SILENCE_THRESHOLD_MSECS = 5 * 1000; + IceServer::IceServer(int argc, char* argv[]) : QCoreApplication(argc, argv), _id(QUuid::createUuid()), @@ -24,6 +30,12 @@ IceServer::IceServer(int argc, char* argv[]) : // call our process datagrams slot when the UDP socket has packets ready connect(&_serverSocket, &QUdpSocket::readyRead, this, &IceServer::processDatagrams); + + // setup our timer to clear inactive peers + QTimer* inactivePeerTimer = new QTimer(this); + connect(inactivePeerTimer, &QTimer::timeout, this, &IceServer::clearInactivePeers); + inactivePeerTimer->start(CLEAR_INACTIVE_PEERS_INTERVAL_MSECS); + } void IceServer::processDatagrams() { @@ -65,6 +77,9 @@ void IceServer::processDatagrams() { qDebug() << "Matched hearbeat to existing network peer" << *matchingPeer; } + // update our last heard microstamp for this network peer to now + matchingPeer->setLastHeardMicrostamp(usecTimestampNow()); + // check if this node also included a UUID that they would like to connect to QUuid connectRequestID; hearbeatStream >> connectRequestID; @@ -86,3 +101,19 @@ void IceServer::processDatagrams() { } } } + +void IceServer::clearInactivePeers() { + NetworkPeerHash::iterator peerItem = _activePeers.begin(); + + while (peerItem != _activePeers.end()) { + SharedNetworkPeer peer = peerItem.value(); + + if ((usecTimestampNow() - peer->getLastHeardMicrostamp()) > (PEER_SILENCE_THRESHOLD_MSECS * 1000)) { + qDebug() << "Removing peer from memory for inactivity -" << *peer; + peerItem = _activePeers.erase(peerItem); + } else { + // we didn't kill this peer, push the iterator forwards + ++peerItem; + } + } +} diff --git a/ice-server/src/IceServer.h b/ice-server/src/IceServer.h index a680bafbc3..9c4c4242dc 100644 --- a/ice-server/src/IceServer.h +++ b/ice-server/src/IceServer.h @@ -18,14 +18,17 @@ #include +typedef QHash NetworkPeerHash; + class IceServer : public QCoreApplication { public: IceServer(int argc, char* argv[]); private slots: void processDatagrams(); + void clearInactivePeers(); private: QUuid _id; - QHash _activePeers; + NetworkPeerHash _activePeers; QUdpSocket _serverSocket; }; diff --git a/libraries/networking/src/NetworkPeer.cpp b/libraries/networking/src/NetworkPeer.cpp index 2f94825fe5..1da6836bf9 100644 --- a/libraries/networking/src/NetworkPeer.cpp +++ b/libraries/networking/src/NetworkPeer.cpp @@ -9,6 +9,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include + +#include #include #include "NetworkPeer.h" @@ -18,7 +21,9 @@ NetworkPeer::NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, co _publicSocket(publicSocket), _localSocket(localSocket), _symmetricSocket(), - _activeSocket(NULL) + _activeSocket(NULL), + _wakeTimestamp(QDateTime::currentMSecsSinceEpoch()), + _lastHeardMicrostamp(usecTimestampNow()) { } diff --git a/libraries/networking/src/NetworkPeer.h b/libraries/networking/src/NetworkPeer.h index 052040933b..dd32666b62 100644 --- a/libraries/networking/src/NetworkPeer.h +++ b/libraries/networking/src/NetworkPeer.h @@ -41,6 +41,12 @@ public: void activateLocalSocket(); void activateSymmetricSocket(); + quint64 getWakeTimestamp() const { return _wakeTimestamp; } + void setWakeTimestamp(quint64 wakeTimestamp) { _wakeTimestamp = wakeTimestamp; } + + quint64 getLastHeardMicrostamp() const { return _lastHeardMicrostamp; } + void setLastHeardMicrostamp(quint64 lastHeardMicrostamp) { _lastHeardMicrostamp = lastHeardMicrostamp; } + friend QDataStream& operator<<(QDataStream& out, const NetworkPeer& peer); friend QDataStream& operator>>(QDataStream& in, NetworkPeer& peer); protected: @@ -50,6 +56,9 @@ protected: HifiSockAddr _localSocket; HifiSockAddr _symmetricSocket; HifiSockAddr* _activeSocket; + + quint64 _wakeTimestamp; + quint64 _lastHeardMicrostamp; }; QDebug operator<<(QDebug debug, const NetworkPeer &peer); diff --git a/libraries/networking/src/Node.cpp b/libraries/networking/src/Node.cpp index 942f911f23..a25350c3f3 100644 --- a/libraries/networking/src/Node.cpp +++ b/libraries/networking/src/Node.cpp @@ -16,7 +16,6 @@ #include "SharedUtil.h" #include -#include #include const QString UNKNOWN_NodeType_t_NAME = "Unknown"; @@ -46,8 +45,6 @@ const QString& NodeType::getNodeTypeName(NodeType_t nodeType) { Node::Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) : NetworkPeer(uuid, publicSocket, localSocket), _type(type), - _wakeTimestamp(QDateTime::currentMSecsSinceEpoch()), - _lastHeardMicrostamp(usecTimestampNow()), _connectionSecret(), _bytesReceivedMovingAverage(NULL), _linkedData(NULL), diff --git a/libraries/networking/src/Node.h b/libraries/networking/src/Node.h index f300499add..238331947c 100644 --- a/libraries/networking/src/Node.h +++ b/libraries/networking/src/Node.h @@ -56,12 +56,6 @@ public: char getType() const { return _type; } void setType(char type) { _type = type; } - - quint64 getWakeTimestamp() const { return _wakeTimestamp; } - void setWakeTimestamp(quint64 wakeTimestamp) { _wakeTimestamp = wakeTimestamp; } - - quint64 getLastHeardMicrostamp() const { return _lastHeardMicrostamp; } - void setLastHeardMicrostamp(quint64 lastHeardMicrostamp) { _lastHeardMicrostamp = lastHeardMicrostamp; } const QUuid& getConnectionSecret() const { return _connectionSecret; } void setConnectionSecret(const QUuid& connectionSecret) { _connectionSecret = connectionSecret; } @@ -92,8 +86,7 @@ private: Node& operator=(Node otherNode); NodeType_t _type; - quint64 _wakeTimestamp; - quint64 _lastHeardMicrostamp; + QUuid _connectionSecret; SimpleMovingAverage* _bytesReceivedMovingAverage;