From e0a721209a18a77325e53bd638e79eb56f07fb9c Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 2 Oct 2014 09:55:00 -0700 Subject: [PATCH] add a Node superclass to handle P2P connection --- libraries/networking/src/NetworkPeer.cpp | 66 ++++++++++++++++++++++++ libraries/networking/src/NetworkPeer.h | 46 +++++++++++++++++ libraries/networking/src/Node.cpp | 48 +---------------- libraries/networking/src/Node.h | 22 ++------ 4 files changed, 116 insertions(+), 66 deletions(-) create mode 100644 libraries/networking/src/NetworkPeer.cpp create mode 100644 libraries/networking/src/NetworkPeer.h diff --git a/libraries/networking/src/NetworkPeer.cpp b/libraries/networking/src/NetworkPeer.cpp new file mode 100644 index 0000000000..bb26b2a119 --- /dev/null +++ b/libraries/networking/src/NetworkPeer.cpp @@ -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 + +#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; +} \ No newline at end of file diff --git a/libraries/networking/src/NetworkPeer.h b/libraries/networking/src/NetworkPeer.h new file mode 100644 index 0000000000..b4e472b80c --- /dev/null +++ b/libraries/networking/src/NetworkPeer.h @@ -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 +#include + +#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 \ No newline at end of file diff --git a/libraries/networking/src/Node.cpp b/libraries/networking/src/Node.cpp index 662c827069..942f911f23 100644 --- a/libraries/networking/src/Node.cpp +++ b/libraries/networking/src/Node.cpp @@ -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); diff --git a/libraries/networking/src/Node.h b/libraries/networking/src/Node.h index 0d5703b45c..8f276f4e1d 100644 --- a/libraries/networking/src/Node.h +++ b/libraries/networking/src/Node.h @@ -21,6 +21,7 @@ #include #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;