mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 05:17:24 +02:00
add a Node superclass to handle P2P connection
This commit is contained in:
parent
e88b3311ad
commit
e0a721209a
4 changed files with 116 additions and 66 deletions
66
libraries/networking/src/NetworkPeer.cpp
Normal file
66
libraries/networking/src/NetworkPeer.cpp
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
//
|
||||||
|
// NetworkPeer.cpp
|
||||||
|
// libraries/networking/src
|
||||||
|
//
|
||||||
|
// Created by Stephen Birarda on 2014-10-02.
|
||||||
|
// Copyright 2014 High Fidelity, Inc.
|
||||||
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <UUID.h>
|
||||||
|
|
||||||
|
#include "NetworkPeer.h"
|
||||||
|
|
||||||
|
NetworkPeer::NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) :
|
||||||
|
_uuid(uuid),
|
||||||
|
_publicSocket(publicSocket),
|
||||||
|
_localSocket(localSocket),
|
||||||
|
_symmetricSocket(),
|
||||||
|
_activeSocket(NULL)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkPeer::setPublicSocket(const HifiSockAddr& publicSocket) {
|
||||||
|
if (_activeSocket == &_publicSocket) {
|
||||||
|
// if the active socket was the public socket then reset it to NULL
|
||||||
|
_activeSocket = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
_publicSocket = publicSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkPeer::setLocalSocket(const HifiSockAddr& localSocket) {
|
||||||
|
if (_activeSocket == &_localSocket) {
|
||||||
|
// if the active socket was the local socket then reset it to NULL
|
||||||
|
_activeSocket = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
_localSocket = localSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkPeer::setSymmetricSocket(const HifiSockAddr& symmetricSocket) {
|
||||||
|
if (_activeSocket == &_symmetricSocket) {
|
||||||
|
// if the active socket was the symmetric socket then reset it to NULL
|
||||||
|
_activeSocket = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
_symmetricSocket = symmetricSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkPeer::activateLocalSocket() {
|
||||||
|
qDebug() << "Activating local socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid);
|
||||||
|
_activeSocket = &_localSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkPeer::activatePublicSocket() {
|
||||||
|
qDebug() << "Activating public socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid);
|
||||||
|
_activeSocket = &_publicSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkPeer::activateSymmetricSocket() {
|
||||||
|
qDebug() << "Activating symmetric socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid);
|
||||||
|
_activeSocket = &_symmetricSocket;
|
||||||
|
}
|
46
libraries/networking/src/NetworkPeer.h
Normal file
46
libraries/networking/src/NetworkPeer.h
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
//
|
||||||
|
// NetworkPeer.h
|
||||||
|
// libraries/networking/src
|
||||||
|
//
|
||||||
|
// Created by Stephen Birarda on 2014-10-02.
|
||||||
|
// Copyright 2014 High Fidelity, Inc.
|
||||||
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef hifi_NetworkPeer_h
|
||||||
|
#define hifi_NetworkPeer_h
|
||||||
|
|
||||||
|
#include <qobject.h>
|
||||||
|
#include <quuid.h>
|
||||||
|
|
||||||
|
#include "HifiSockAddr.h"
|
||||||
|
|
||||||
|
class NetworkPeer : public QObject {
|
||||||
|
public:
|
||||||
|
NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket);
|
||||||
|
|
||||||
|
const HifiSockAddr& getPublicSocket() const { return _publicSocket; }
|
||||||
|
void setPublicSocket(const HifiSockAddr& publicSocket);
|
||||||
|
const HifiSockAddr& getLocalSocket() const { return _localSocket; }
|
||||||
|
void setLocalSocket(const HifiSockAddr& localSocket);
|
||||||
|
const HifiSockAddr& getSymmetricSocket() const { return _symmetricSocket; }
|
||||||
|
void setSymmetricSocket(const HifiSockAddr& symmetricSocket);
|
||||||
|
|
||||||
|
const HifiSockAddr* getActiveSocket() const { return _activeSocket; }
|
||||||
|
|
||||||
|
void activatePublicSocket();
|
||||||
|
void activateLocalSocket();
|
||||||
|
void activateSymmetricSocket();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
QUuid _uuid;
|
||||||
|
|
||||||
|
HifiSockAddr _publicSocket;
|
||||||
|
HifiSockAddr _localSocket;
|
||||||
|
HifiSockAddr _symmetricSocket;
|
||||||
|
HifiSockAddr* _activeSocket;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // hifi_NetworkPeer_h
|
|
@ -44,14 +44,10 @@ const QString& NodeType::getNodeTypeName(NodeType_t nodeType) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Node::Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) :
|
Node::Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) :
|
||||||
|
NetworkPeer(uuid, publicSocket, localSocket),
|
||||||
_type(type),
|
_type(type),
|
||||||
_uuid(uuid),
|
|
||||||
_wakeTimestamp(QDateTime::currentMSecsSinceEpoch()),
|
_wakeTimestamp(QDateTime::currentMSecsSinceEpoch()),
|
||||||
_lastHeardMicrostamp(usecTimestampNow()),
|
_lastHeardMicrostamp(usecTimestampNow()),
|
||||||
_publicSocket(publicSocket),
|
|
||||||
_localSocket(localSocket),
|
|
||||||
_symmetricSocket(),
|
|
||||||
_activeSocket(NULL),
|
|
||||||
_connectionSecret(),
|
_connectionSecret(),
|
||||||
_bytesReceivedMovingAverage(NULL),
|
_bytesReceivedMovingAverage(NULL),
|
||||||
_linkedData(NULL),
|
_linkedData(NULL),
|
||||||
|
@ -68,48 +64,6 @@ Node::~Node() {
|
||||||
delete _bytesReceivedMovingAverage;
|
delete _bytesReceivedMovingAverage;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::setPublicSocket(const HifiSockAddr& publicSocket) {
|
|
||||||
if (_activeSocket == &_publicSocket) {
|
|
||||||
// if the active socket was the public socket then reset it to NULL
|
|
||||||
_activeSocket = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
_publicSocket = publicSocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Node::setLocalSocket(const HifiSockAddr& localSocket) {
|
|
||||||
if (_activeSocket == &_localSocket) {
|
|
||||||
// if the active socket was the local socket then reset it to NULL
|
|
||||||
_activeSocket = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
_localSocket = localSocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Node::setSymmetricSocket(const HifiSockAddr& symmetricSocket) {
|
|
||||||
if (_activeSocket == &_symmetricSocket) {
|
|
||||||
// if the active socket was the symmetric socket then reset it to NULL
|
|
||||||
_activeSocket = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
_symmetricSocket = symmetricSocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Node::activateLocalSocket() {
|
|
||||||
qDebug() << "Activating local socket for node" << *this;
|
|
||||||
_activeSocket = &_localSocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Node::activatePublicSocket() {
|
|
||||||
qDebug() << "Activating public socket for node" << *this;
|
|
||||||
_activeSocket = &_publicSocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Node::activateSymmetricSocket() {
|
|
||||||
qDebug() << "Activating symmetric socket for node" << *this;
|
|
||||||
_activeSocket = &_symmetricSocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Node::recordBytesReceived(int bytesReceived) {
|
void Node::recordBytesReceived(int bytesReceived) {
|
||||||
if (!_bytesReceivedMovingAverage) {
|
if (!_bytesReceivedMovingAverage) {
|
||||||
_bytesReceivedMovingAverage = new SimpleMovingAverage(100);
|
_bytesReceivedMovingAverage = new SimpleMovingAverage(100);
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
|
|
||||||
#include "HifiSockAddr.h"
|
#include "HifiSockAddr.h"
|
||||||
|
#include "NetworkPeer.h"
|
||||||
#include "NodeData.h"
|
#include "NodeData.h"
|
||||||
#include "SimpleMovingAverage.h"
|
#include "SimpleMovingAverage.h"
|
||||||
#include "MovingPercentile.h"
|
#include "MovingPercentile.h"
|
||||||
|
@ -44,7 +45,7 @@ namespace NodeType {
|
||||||
const QString& getNodeTypeName(NodeType_t nodeType);
|
const QString& getNodeTypeName(NodeType_t nodeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
class Node : public QObject {
|
class Node : public NetworkPeer {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket);
|
Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket);
|
||||||
|
@ -64,19 +65,6 @@ public:
|
||||||
|
|
||||||
quint64 getLastHeardMicrostamp() const { return _lastHeardMicrostamp; }
|
quint64 getLastHeardMicrostamp() const { return _lastHeardMicrostamp; }
|
||||||
void setLastHeardMicrostamp(quint64 lastHeardMicrostamp) { _lastHeardMicrostamp = lastHeardMicrostamp; }
|
void setLastHeardMicrostamp(quint64 lastHeardMicrostamp) { _lastHeardMicrostamp = lastHeardMicrostamp; }
|
||||||
|
|
||||||
const HifiSockAddr& getPublicSocket() const { return _publicSocket; }
|
|
||||||
void setPublicSocket(const HifiSockAddr& publicSocket);
|
|
||||||
const HifiSockAddr& getLocalSocket() const { return _localSocket; }
|
|
||||||
void setLocalSocket(const HifiSockAddr& localSocket);
|
|
||||||
const HifiSockAddr& getSymmetricSocket() const { return _symmetricSocket; }
|
|
||||||
void setSymmetricSocket(const HifiSockAddr& symmetricSocket);
|
|
||||||
|
|
||||||
const HifiSockAddr* getActiveSocket() const { return _activeSocket; }
|
|
||||||
|
|
||||||
void activatePublicSocket();
|
|
||||||
void activateLocalSocket();
|
|
||||||
void activateSymmetricSocket();
|
|
||||||
|
|
||||||
const QUuid& getConnectionSecret() const { return _connectionSecret; }
|
const QUuid& getConnectionSecret() const { return _connectionSecret; }
|
||||||
void setConnectionSecret(const QUuid& connectionSecret) { _connectionSecret = connectionSecret; }
|
void setConnectionSecret(const QUuid& connectionSecret) { _connectionSecret = connectionSecret; }
|
||||||
|
@ -107,13 +95,9 @@ private:
|
||||||
Node& operator=(Node otherNode);
|
Node& operator=(Node otherNode);
|
||||||
|
|
||||||
NodeType_t _type;
|
NodeType_t _type;
|
||||||
QUuid _uuid;
|
|
||||||
quint64 _wakeTimestamp;
|
quint64 _wakeTimestamp;
|
||||||
quint64 _lastHeardMicrostamp;
|
quint64 _lastHeardMicrostamp;
|
||||||
HifiSockAddr _publicSocket;
|
|
||||||
HifiSockAddr _localSocket;
|
|
||||||
HifiSockAddr _symmetricSocket;
|
|
||||||
HifiSockAddr* _activeSocket;
|
|
||||||
QUuid _connectionSecret;
|
QUuid _connectionSecret;
|
||||||
SimpleMovingAverage* _bytesReceivedMovingAverage;
|
SimpleMovingAverage* _bytesReceivedMovingAverage;
|
||||||
NodeData* _linkedData;
|
NodeData* _linkedData;
|
||||||
|
|
Loading…
Reference in a new issue