From 209c9f93edf85de9d29ffa6437da193a8e941b96 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 15 Aug 2013 14:21:21 -0700 Subject: [PATCH] remove Application dependency from VoxelEditPacketSender class --- interface/src/Application.cpp | 11 +++++-- interface/src/Application.h | 3 +- interface/src/VoxelEditPacketSender.cpp | 40 +++++++++++++++---------- interface/src/VoxelEditPacketSender.h | 13 ++++++++ libraries/shared/src/PacketSender.cpp | 5 +++- libraries/shared/src/PacketSender.h | 11 ++++++- 6 files changed, 61 insertions(+), 22 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index c36f1a0545..c6479d245a 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -222,7 +222,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : #endif _stopNetworkReceiveThread(false), _voxelProcessor(), - _voxelEditSender(), + _voxelEditSender(this), _packetCount(0), _packetsPerSecond(0), _bytesPerSecond(0), @@ -329,6 +329,9 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : _glWidget->setMouseTracking(true); // initialization continues in initializeGL when OpenGL context is ready + + // Tell our voxel edit sender about our known jurisdictions + _voxelEditSender.setVoxelServerJurisdictions(&_voxelServerJurisdictions); } Application::~Application() { @@ -1437,6 +1440,7 @@ void Application::setRenderWarnings(bool renderWarnings) { } void Application::setRenderVoxels(bool voxelRender) { + _voxelEditSender.setShouldSend(voxelRender); if (!voxelRender) { doKillLocalVoxels(); } @@ -4156,5 +4160,6 @@ void Application::updateParticleSystem(float deltaTime) { } } - - +void Application::packetSentNotification(ssize_t length) { + _bandwidthMeter.outputStream(BandwidthMeter::VOXELS).updateValue(length); +} diff --git a/interface/src/Application.h b/interface/src/Application.h index 88cb86312f..7d5d1497af 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -72,7 +72,7 @@ static const float NODE_KILLED_RED = 1.0f; static const float NODE_KILLED_GREEN = 0.0f; static const float NODE_KILLED_BLUE = 0.0f; -class Application : public QApplication, public NodeListHook { +class Application : public QApplication, public NodeListHook, public PacketSenderNotify { Q_OBJECT friend class VoxelPacketProcessor; @@ -131,6 +131,7 @@ public: virtual void nodeAdded(Node* node); virtual void nodeKilled(Node* node); + virtual void packetSentNotification(ssize_t length); public slots: void sendAvatarFaceVideoMessage(int frameCount, const QByteArray& data); diff --git a/interface/src/VoxelEditPacketSender.cpp b/interface/src/VoxelEditPacketSender.cpp index 621e09bd22..cfc3cab218 100644 --- a/interface/src/VoxelEditPacketSender.cpp +++ b/interface/src/VoxelEditPacketSender.cpp @@ -10,29 +10,30 @@ #include -#include "Application.h" +#include +#include #include "VoxelEditPacketSender.h" + +VoxelEditPacketSender::VoxelEditPacketSender(PacketSenderNotify* notify) : + PacketSender(notify), + _shouldSend(true), + _voxelServerJurisdictions(NULL) { +} + void VoxelEditPacketSender::sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail) { - - Application* app = Application::getInstance(); - - // if the app has Voxels disabled, we don't do any of this... - if (!app->_renderVoxels->isChecked()) { + // allows app to disable sending if for example voxels have been disabled + if (!_shouldSend) { return; // bail early } unsigned char* bufferOut; int sizeOut; - int totalBytesSent = 0; if (createVoxelEditMessage(type, 0, 1, &detail, bufferOut, sizeOut)){ actuallySendMessage(UNKNOWN_NODE_ID, bufferOut, sizeOut); // sends to all servers... not ideal! delete[] bufferOut; } - - // Tell the application's bandwidth meters about what we've sent - app->_bandwidthMeter.outputStream(BandwidthMeter::VOXELS).updateValue(totalBytesSent); } void VoxelEditPacketSender::actuallySendMessage(uint16_t nodeID, unsigned char* bufferOut, ssize_t sizeOut) { @@ -48,7 +49,10 @@ void VoxelEditPacketSender::actuallySendMessage(uint16_t nodeID, unsigned char* } void VoxelEditPacketSender::queueVoxelEditMessage(PACKET_TYPE type, unsigned char* codeColorBuffer, ssize_t length) { - Application* app = Application::getInstance(); + + if (!_shouldSend) { + return; // bail early + } // We want to filter out edit messages for voxel servers based on the server's Jurisdiction // But we can't really do that with a packed message, since each edit message could be destined @@ -58,12 +62,16 @@ void VoxelEditPacketSender::queueVoxelEditMessage(PACKET_TYPE type, unsigned cha for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { // only send to the NodeTypes that are NODE_TYPE_VOXEL_SERVER if (node->getActiveSocket() != NULL && node->getType() == NODE_TYPE_VOXEL_SERVER) { - - // we need to get the jurisdiction for this - // here we need to get the "pending packet" for this server uint16_t nodeID = node->getNodeID(); - const JurisdictionMap& map = app->_voxelServerJurisdictions[nodeID]; - if (map.isMyJurisdiction(codeColorBuffer, CHECK_NODE_ONLY) == JurisdictionMap::WITHIN) { + bool isMyJurisdiction = true; + + if (_voxelServerJurisdictions) { + // we need to get the jurisdiction for this + // here we need to get the "pending packet" for this server + const JurisdictionMap& map = (*_voxelServerJurisdictions)[nodeID]; + isMyJurisdiction = (map.isMyJurisdiction(codeColorBuffer, CHECK_NODE_ONLY) == JurisdictionMap::WITHIN); + } + if (isMyJurisdiction) { EditPacketBuffer& packetBuffer = _pendingEditPackets[nodeID]; packetBuffer._nodeID = nodeID; diff --git a/interface/src/VoxelEditPacketSender.h b/interface/src/VoxelEditPacketSender.h index e3b0f7d18a..ebced66b7b 100644 --- a/interface/src/VoxelEditPacketSender.h +++ b/interface/src/VoxelEditPacketSender.h @@ -13,6 +13,7 @@ #include #include // for VoxelDetail +#include /// Used for construction of edit voxel packets class EditPacketBuffer { @@ -27,6 +28,8 @@ public: /// Threaded processor for queueing and sending of outbound edit voxel packets. class VoxelEditPacketSender : public PacketSender { public: + VoxelEditPacketSender(PacketSenderNotify* notify = NULL); + /// Send voxel edit message immediately void sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail); @@ -37,11 +40,21 @@ public: /// flushes all queued packets for all nodes void flushQueue(); + bool getShouldSend() const { return _shouldSend; } + void setShouldSend(bool shouldSend) { _shouldSend = shouldSend; } + + void setVoxelServerJurisdictions(std::map* voxelServerJurisdictions) { + _voxelServerJurisdictions = voxelServerJurisdictions; + } + private: + bool _shouldSend; void actuallySendMessage(uint16_t nodeID, unsigned char* bufferOut, ssize_t sizeOut); void initializePacket(EditPacketBuffer& packetBuffer, PACKET_TYPE type); void flushQueue(EditPacketBuffer& packetBuffer); // flushes specific queued packet std::map _pendingEditPackets; + + std::map* _voxelServerJurisdictions; }; #endif // __shared__VoxelEditPacketSender__ diff --git a/libraries/shared/src/PacketSender.cpp b/libraries/shared/src/PacketSender.cpp index 4c150454a3..ecbe15359e 100644 --- a/libraries/shared/src/PacketSender.cpp +++ b/libraries/shared/src/PacketSender.cpp @@ -16,7 +16,7 @@ const uint64_t SEND_INTERVAL_USECS = 1000 * 5; // no more than 200pps... should #include "PacketSender.h" #include "SharedUtil.h" -PacketSender::PacketSender() { +PacketSender::PacketSender(PacketSenderNotify* notify) : _notify(notify) { _lastSendTime = usecTimestampNow(); } @@ -40,6 +40,9 @@ bool PacketSender::process() { UDPSocket* nodeSocket = NodeList::getInstance()->getNodeSocket(); nodeSocket->send(&packet.getAddress(), packet.getData(), packet.getLength()); + if (_notify) { + _notify->packetSentNotification(packet.getLength()); + } lock(); _packets.erase(_packets.begin()); diff --git a/libraries/shared/src/PacketSender.h b/libraries/shared/src/PacketSender.h index 5a1a63695f..bd2ab99407 100644 --- a/libraries/shared/src/PacketSender.h +++ b/libraries/shared/src/PacketSender.h @@ -14,11 +14,18 @@ #include "GenericThread.h" #include "NetworkPacket.h" +/// Notification Hook for packets being sent by a PacketSender +class PacketSenderNotify { +public: + virtual void packetSentNotification(ssize_t length) = 0; +}; + + /// Generalized threaded processor for queueing and sending of outbound packets. class PacketSender : public GenericThread { public: - PacketSender(); + PacketSender(PacketSenderNotify* notify = NULL); /// Add packet to outbound queue. /// \param sockaddr& address the destination address @@ -33,6 +40,8 @@ private: std::vector _packets; uint64_t _lastSendTime; + PacketSenderNotify* _notify; + }; #endif // __shared__PacketSender__