add a Node superclass to handle P2P connection

This commit is contained in:
Stephen Birarda 2014-10-02 09:55:00 -07:00
parent e88b3311ad
commit e0a721209a
4 changed files with 116 additions and 66 deletions

View 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;
}

View 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

View file

@ -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) :
NetworkPeer(uuid, publicSocket, localSocket),
_type(type),
_uuid(uuid),
_wakeTimestamp(QDateTime::currentMSecsSinceEpoch()),
_lastHeardMicrostamp(usecTimestampNow()),
_publicSocket(publicSocket),
_localSocket(localSocket),
_symmetricSocket(),
_activeSocket(NULL),
_connectionSecret(),
_bytesReceivedMovingAverage(NULL),
_linkedData(NULL),
@ -68,48 +64,6 @@ Node::~Node() {
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) {
if (!_bytesReceivedMovingAverage) {
_bytesReceivedMovingAverage = new SimpleMovingAverage(100);

View file

@ -21,6 +21,7 @@
#include <QMutex>
#include "HifiSockAddr.h"
#include "NetworkPeer.h"
#include "NodeData.h"
#include "SimpleMovingAverage.h"
#include "MovingPercentile.h"
@ -44,7 +45,7 @@ namespace NodeType {
const QString& getNodeTypeName(NodeType_t nodeType);
}
class Node : public QObject {
class Node : public NetworkPeer {
Q_OBJECT
public:
Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket);
@ -64,19 +65,6 @@ public:
quint64 getLastHeardMicrostamp() const { return _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; }
void setConnectionSecret(const QUuid& connectionSecret) { _connectionSecret = connectionSecret; }
@ -107,13 +95,9 @@ private:
Node& operator=(Node otherNode);
NodeType_t _type;
QUuid _uuid;
quint64 _wakeTimestamp;
quint64 _lastHeardMicrostamp;
HifiSockAddr _publicSocket;
HifiSockAddr _localSocket;
HifiSockAddr _symmetricSocket;
HifiSockAddr* _activeSocket;
QUuid _connectionSecret;
SimpleMovingAverage* _bytesReceivedMovingAverage;
NodeData* _linkedData;