remove inactive peers from memory in IceServer

This commit is contained in:
Stephen Birarda 2014-10-02 15:00:59 -07:00
parent 970ba2296a
commit d26a94f84c
6 changed files with 51 additions and 13 deletions

View file

@ -9,10 +9,16 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <QTimer>
#include <PacketHeaders.h>
#include <SharedUtil.h>
#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;
}
}
}

View file

@ -18,14 +18,17 @@
#include <NetworkPeer.h>
typedef QHash<QUuid, SharedNetworkPeer> NetworkPeerHash;
class IceServer : public QCoreApplication {
public:
IceServer(int argc, char* argv[]);
private slots:
void processDatagrams();
void clearInactivePeers();
private:
QUuid _id;
QHash<QUuid, SharedNetworkPeer> _activePeers;
NetworkPeerHash _activePeers;
QUdpSocket _serverSocket;
};

View file

@ -9,6 +9,9 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <qdatetime.h>
#include <SharedUtil.h>
#include <UUID.h>
#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())
{
}

View file

@ -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);

View file

@ -16,7 +16,6 @@
#include "SharedUtil.h"
#include <QtCore/QDataStream>
#include <QtCore/QDateTime>
#include <QtCore/QDebug>
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),

View file

@ -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;