From c2d5accbc974c86f9d81e69700597a25f2166aef Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Sat, 14 Dec 2013 13:25:33 -0800 Subject: [PATCH] implement OctreeScriptingInterface base class to share common behavior for particle and voxel scripting --- assignment-client/src/Agent.cpp | 1 + .../octree/src/OctreeScriptingInterface.cpp | 51 ++++++++++ .../octree/src/OctreeScriptingInterface.h | 95 +++++++++++++++++++ .../particles/src/ParticleEditPacketSender.h | 2 +- .../src/ParticleScriptingInterface.cpp | 45 +-------- .../src/ParticleScriptingInterface.h | 72 +------------- libraries/voxels/src/VoxelEditPacketSender.h | 2 +- .../voxels/src/VoxelScriptingInterface.cpp | 9 +- .../voxels/src/VoxelScriptingInterface.h | 67 ++----------- 9 files changed, 164 insertions(+), 180 deletions(-) create mode 100644 libraries/octree/src/OctreeScriptingInterface.cpp create mode 100644 libraries/octree/src/OctreeScriptingInterface.h diff --git a/assignment-client/src/Agent.cpp b/assignment-client/src/Agent.cpp index 8ee9d1dc36..8f02ed9a2e 100644 --- a/assignment-client/src/Agent.cpp +++ b/assignment-client/src/Agent.cpp @@ -25,6 +25,7 @@ Agent::Agent(const unsigned char* dataBuffer, int numBytes) : ThreadedAssignment(dataBuffer, numBytes) { _particleScriptingInterface.init(); + _voxelScriptingInterface.init(); } void Agent::processDatagram(const QByteArray& dataByteArray, const HifiSockAddr& senderSockAddr) { diff --git a/libraries/octree/src/OctreeScriptingInterface.cpp b/libraries/octree/src/OctreeScriptingInterface.cpp new file mode 100644 index 0000000000..383e7a3877 --- /dev/null +++ b/libraries/octree/src/OctreeScriptingInterface.cpp @@ -0,0 +1,51 @@ +// +// OctreeScriptingInterface.cpp +// hifi +// +// Created by Brad Hefta-Gaub on 12/6/13 +// Copyright (c) 2013 HighFidelity, Inc. All rights reserved. +// + +#include "OctreeScriptingInterface.h" + +OctreeScriptingInterface::OctreeScriptingInterface(OctreeEditPacketSender* packetSender, + JurisdictionListener* jurisdictionListener) +{ + setPacketSender(packetSender); + setJurisdictionListener(jurisdictionListener); +} + +OctreeScriptingInterface::~OctreeScriptingInterface() { + if (_managedJuridiciontListerner) { + delete _jurisdictionListener; + } + if (_managedPacketSender) { + delete _packetSender; + } +} + +void OctreeScriptingInterface::setPacketSender(OctreeEditPacketSender* packetSender) { + _packetSender = packetSender; +} + +void OctreeScriptingInterface::setJurisdictionListener(JurisdictionListener* jurisdictionListener) { + _jurisdictionListener = jurisdictionListener; +} + +void OctreeScriptingInterface::init() { + if (_jurisdictionListener) { + _managedJuridiciontListerner = false; + } else { + _managedJuridiciontListerner = true; + _jurisdictionListener = new JurisdictionListener(getServerNodeType()); + _jurisdictionListener->initialize(true); + } + + if (_packetSender) { + _managedPacketSender = false; + } else { + _managedPacketSender = true; + _packetSender = createPacketSender(); + _packetSender->setServerJurisdictions(_jurisdictionListener->getJurisdictions()); + } +} diff --git a/libraries/octree/src/OctreeScriptingInterface.h b/libraries/octree/src/OctreeScriptingInterface.h new file mode 100644 index 0000000000..7eb0538ca2 --- /dev/null +++ b/libraries/octree/src/OctreeScriptingInterface.h @@ -0,0 +1,95 @@ +// +// OctreeScriptingInterface.h +// hifi +// +// Created by Brad Hefta-Gaub on 12/6/13 +// Copyright (c) 2013 HighFidelity, Inc. All rights reserved. +// + +#ifndef __hifi__OctreeScriptingInterface__ +#define __hifi__OctreeScriptingInterface__ + +#include + +#include "JurisdictionListener.h" +#include "OctreeEditPacketSender.h" + +/// handles scripting of Particle commands from JS passed to assigned clients +class OctreeScriptingInterface : public QObject { + Q_OBJECT +public: + OctreeScriptingInterface(OctreeEditPacketSender* packetSender = NULL, + JurisdictionListener* jurisdictionListener = NULL); + + ~OctreeScriptingInterface(); + + OctreeEditPacketSender* getPacketSender() const { return _packetSender; } + JurisdictionListener* getJurisdictionListener() const { return _jurisdictionListener; } + void setPacketSender(OctreeEditPacketSender* packetSender); + void setJurisdictionListener(JurisdictionListener* jurisdictionListener); + void init(); + + virtual NODE_TYPE getServerNodeType() const = 0; + virtual OctreeEditPacketSender* createPacketSender() = 0; + +public slots: + /// Set the desired max packet size in bytes that should be created + void setMaxPacketSize(int maxPacketSize) { return _packetSender->setMaxPacketSize(maxPacketSize); } + + /// returns the current desired max packet size in bytes that will be created + int getMaxPacketSize() const { return _packetSender->getMaxPacketSize(); } + + /// set the max packets per second send rate + void setPacketsPerSecond(int packetsPerSecond) { return _packetSender->setPacketsPerSecond(packetsPerSecond); } + + /// get the max packets per second send rate + int getPacketsPerSecond() const { return _packetSender->getPacketsPerSecond(); } + + /// does a particle server exist to send to + bool serversExist() const { return _packetSender->serversExist(); } + + /// are there packets waiting in the send queue to be sent + bool hasPacketsToSend() const { return _packetSender->hasPacketsToSend(); } + + /// how many packets are there in the send queue waiting to be sent + int packetsToSendCount() const { return _packetSender->packetsToSendCount(); } + + /// returns the packets per second send rate of this object over its lifetime + float getLifetimePPS() const { return _packetSender->getLifetimePPS(); } + + /// returns the bytes per second send rate of this object over its lifetime + float getLifetimeBPS() const { return _packetSender->getLifetimeBPS(); } + + /// returns the packets per second queued rate of this object over its lifetime + float getLifetimePPSQueued() const { return _packetSender->getLifetimePPSQueued(); } + + /// returns the bytes per second queued rate of this object over its lifetime + float getLifetimeBPSQueued() const { return _packetSender->getLifetimeBPSQueued(); } + + /// returns lifetime of this object from first packet sent to now in usecs + long long unsigned int getLifetimeInUsecs() const { return _packetSender->getLifetimeInUsecs(); } + + /// returns lifetime of this object from first packet sent to now in usecs + float getLifetimeInSeconds() const { return _packetSender->getLifetimeInSeconds(); } + + /// returns the total packets sent by this object over its lifetime + long long unsigned int getLifetimePacketsSent() const { return _packetSender->getLifetimePacketsSent(); } + + /// returns the total bytes sent by this object over its lifetime + long long unsigned int getLifetimeBytesSent() const { return _packetSender->getLifetimeBytesSent(); } + + /// returns the total packets queued by this object over its lifetime + long long unsigned int getLifetimePacketsQueued() const { return _packetSender->getLifetimePacketsQueued(); } + + /// returns the total bytes queued by this object over its lifetime + long long unsigned int getLifetimeBytesQueued() const { return _packetSender->getLifetimeBytesQueued(); } + +protected: + /// attached OctreeEditPacketSender that handles queuing and sending of packets to VS + OctreeEditPacketSender* _packetSender; + JurisdictionListener* _jurisdictionListener; + bool _managedPacketSender; + bool _managedJuridiciontListerner; +}; + +#endif /* defined(__hifi__OctreeScriptingInterface__) */ diff --git a/libraries/particles/src/ParticleEditPacketSender.h b/libraries/particles/src/ParticleEditPacketSender.h index b5d9ac45ca..6a21ca3ae2 100644 --- a/libraries/particles/src/ParticleEditPacketSender.h +++ b/libraries/particles/src/ParticleEditPacketSender.h @@ -15,7 +15,7 @@ #include "Particle.h" /// Utility for processing, packing, queueing and sending of outbound edit voxel messages. -class ParticleEditPacketSender : public virtual OctreeEditPacketSender { +class ParticleEditPacketSender : public OctreeEditPacketSender { public: ParticleEditPacketSender(PacketSenderNotify* notify = NULL) : OctreeEditPacketSender(notify) { } ~ParticleEditPacketSender() { } diff --git a/libraries/particles/src/ParticleScriptingInterface.cpp b/libraries/particles/src/ParticleScriptingInterface.cpp index 74640fa671..cb534cc522 100644 --- a/libraries/particles/src/ParticleScriptingInterface.cpp +++ b/libraries/particles/src/ParticleScriptingInterface.cpp @@ -8,53 +8,10 @@ #include "ParticleScriptingInterface.h" -ParticleScriptingInterface::ParticleScriptingInterface(ParticleEditPacketSender* particleSender, - JurisdictionListener* particleJurisdictionListener) : - _nextCreatorTokenID(0) -{ - setParticlePacketSender(particleSender); - setJurisdictionListener(particleJurisdictionListener); -} - -ParticleScriptingInterface::~ParticleScriptingInterface() { - if (_managedJuridiciontListerner) { - delete _jurisdictionListener; - } - if (_managedPacketSender) { - delete _particlePacketSender; - } -} - - -void ParticleScriptingInterface::setParticlePacketSender(ParticleEditPacketSender* particlePacketSender) { - _particlePacketSender = particlePacketSender; -} - -void ParticleScriptingInterface::setJurisdictionListener(JurisdictionListener* jurisdictionListener) { - _jurisdictionListener = jurisdictionListener; -} - -void ParticleScriptingInterface::init() { - if (_jurisdictionListener) { - _managedJuridiciontListerner = false; - } else { - _managedJuridiciontListerner = true; - _jurisdictionListener = new JurisdictionListener(NODE_TYPE_PARTICLE_SERVER); - _jurisdictionListener->initialize(true); - } - - if (_particlePacketSender) { - _managedPacketSender = false; - } else { - _managedPacketSender = true; - _particlePacketSender = new ParticleEditPacketSender(); - _particlePacketSender->setServerJurisdictions(_jurisdictionListener->getJurisdictions()); - } -} void ParticleScriptingInterface::queueParticleAdd(PACKET_TYPE addPacketType, ParticleDetail& addParticleDetails) { - _particlePacketSender->queueParticleEditMessages(addPacketType, 1, &addParticleDetails); + getParticlePacketSender()->queueParticleEditMessages(addPacketType, 1, &addParticleDetails); } unsigned int ParticleScriptingInterface::queueParticleAdd(glm::vec3 position, float radius, diff --git a/libraries/particles/src/ParticleScriptingInterface.h b/libraries/particles/src/ParticleScriptingInterface.h index 0aaec977db..85dd0c6caf 100644 --- a/libraries/particles/src/ParticleScriptingInterface.h +++ b/libraries/particles/src/ParticleScriptingInterface.h @@ -12,86 +12,24 @@ #include #include +#include #include "ParticleEditPacketSender.h" /// handles scripting of Particle commands from JS passed to assigned clients -class ParticleScriptingInterface : public QObject { +class ParticleScriptingInterface : public OctreeScriptingInterface { Q_OBJECT public: - ParticleScriptingInterface(ParticleEditPacketSender* particleSender = NULL, - JurisdictionListener* particleJurisdictionListener = NULL); + ParticleEditPacketSender* getParticlePacketSender() const { return (ParticleEditPacketSender*)getPacketSender(); } + virtual NODE_TYPE getServerNodeType() const { return NODE_TYPE_PARTICLE_SERVER; } + virtual OctreeEditPacketSender* createPacketSender() { return new ParticleEditPacketSender(); } - ~ParticleScriptingInterface(); - - ParticleEditPacketSender* getParticlePacketSender() const { return _particlePacketSender; } - JurisdictionListener* getJurisdictionListener() const { return _jurisdictionListener; } - void setParticlePacketSender(ParticleEditPacketSender* particlePacketSender); - void setJurisdictionListener(JurisdictionListener* jurisdictionListener); - void init(); public slots: /// queues the creation of a Particle which will be sent by calling process on the PacketSender /// returns the creatorTokenID for the newly created particle unsigned int queueParticleAdd(glm::vec3 position, float radius, xColor color, glm::vec3 velocity, glm::vec3 gravity, float damping, QString updateScript); - - /// Set the desired max packet size in bytes that should be created - void setMaxPacketSize(int maxPacketSize) { return _particlePacketSender->setMaxPacketSize(maxPacketSize); } - - /// returns the current desired max packet size in bytes that will be created - int getMaxPacketSize() const { return _particlePacketSender->getMaxPacketSize(); } - - /// set the max packets per second send rate - void setPacketsPerSecond(int packetsPerSecond) { return _particlePacketSender->setPacketsPerSecond(packetsPerSecond); } - - /// get the max packets per second send rate - int getPacketsPerSecond() const { return _particlePacketSender->getPacketsPerSecond(); } - - /// does a particle server exist to send to - bool serversExist() const { return _particlePacketSender->serversExist(); } - - /// are there packets waiting in the send queue to be sent - bool hasPacketsToSend() const { return _particlePacketSender->hasPacketsToSend(); } - - /// how many packets are there in the send queue waiting to be sent - int packetsToSendCount() const { return _particlePacketSender->packetsToSendCount(); } - - /// returns the packets per second send rate of this object over its lifetime - float getLifetimePPS() const { return _particlePacketSender->getLifetimePPS(); } - - /// returns the bytes per second send rate of this object over its lifetime - float getLifetimeBPS() const { return _particlePacketSender->getLifetimeBPS(); } - - /// returns the packets per second queued rate of this object over its lifetime - float getLifetimePPSQueued() const { return _particlePacketSender->getLifetimePPSQueued(); } - - /// returns the bytes per second queued rate of this object over its lifetime - float getLifetimeBPSQueued() const { return _particlePacketSender->getLifetimeBPSQueued(); } - - /// returns lifetime of this object from first packet sent to now in usecs - long long unsigned int getLifetimeInUsecs() const { return _particlePacketSender->getLifetimeInUsecs(); } - - /// returns lifetime of this object from first packet sent to now in usecs - float getLifetimeInSeconds() const { return _particlePacketSender->getLifetimeInSeconds(); } - - /// returns the total packets sent by this object over its lifetime - long long unsigned int getLifetimePacketsSent() const { return _particlePacketSender->getLifetimePacketsSent(); } - - /// returns the total bytes sent by this object over its lifetime - long long unsigned int getLifetimeBytesSent() const { return _particlePacketSender->getLifetimeBytesSent(); } - - /// returns the total packets queued by this object over its lifetime - long long unsigned int getLifetimePacketsQueued() const { return _particlePacketSender->getLifetimePacketsQueued(); } - - /// returns the total bytes queued by this object over its lifetime - long long unsigned int getLifetimeBytesQueued() const { return _particlePacketSender->getLifetimeBytesQueued(); } private: - /// attached ParticleEditPacketSender that handles queuing and sending of packets to VS - ParticleEditPacketSender* _particlePacketSender; - JurisdictionListener* _jurisdictionListener; - bool _managedPacketSender; - bool _managedJuridiciontListerner; - void queueParticleAdd(PACKET_TYPE addPacketType, ParticleDetail& addParticleDetails); uint32_t _nextCreatorTokenID; diff --git a/libraries/voxels/src/VoxelEditPacketSender.h b/libraries/voxels/src/VoxelEditPacketSender.h index 1bfa4a2cec..d0ce018d95 100644 --- a/libraries/voxels/src/VoxelEditPacketSender.h +++ b/libraries/voxels/src/VoxelEditPacketSender.h @@ -14,7 +14,7 @@ #include /// Utility for processing, packing, queueing and sending of outbound edit voxel messages. -class VoxelEditPacketSender : public virtual OctreeEditPacketSender { +class VoxelEditPacketSender : public OctreeEditPacketSender { public: VoxelEditPacketSender(PacketSenderNotify* notify = NULL) : OctreeEditPacketSender(notify) { } ~VoxelEditPacketSender() { } diff --git a/libraries/voxels/src/VoxelScriptingInterface.cpp b/libraries/voxels/src/VoxelScriptingInterface.cpp index 90755f8c65..9c2ca45418 100644 --- a/libraries/voxels/src/VoxelScriptingInterface.cpp +++ b/libraries/voxels/src/VoxelScriptingInterface.cpp @@ -8,13 +8,8 @@ #include "VoxelScriptingInterface.h" -VoxelScriptingInterface::VoxelScriptingInterface() { - _jurisdictionListener.initialize(true); - _voxelPacketSender.setVoxelServerJurisdictions(_jurisdictionListener.getJurisdictions()); -} - void VoxelScriptingInterface::queueVoxelAdd(PACKET_TYPE addPacketType, VoxelDetail& addVoxelDetails) { - _voxelPacketSender.queueVoxelEditMessages(addPacketType, 1, &addVoxelDetails); + getVoxelPacketSender()->queueVoxelEditMessages(addPacketType, 1, &addVoxelDetails); } void VoxelScriptingInterface::queueVoxelAdd(float x, float y, float z, float scale, uchar red, uchar green, uchar blue) { @@ -39,6 +34,6 @@ void VoxelScriptingInterface::queueVoxelDelete(float x, float y, float z, float // setup a VoxelDetail struct with data VoxelDetail deleteVoxelDetail = {x, y, z, scale, 0, 0, 0}; - _voxelPacketSender.queueVoxelEditMessages(PACKET_TYPE_VOXEL_ERASE, 1, &deleteVoxelDetail); + getVoxelPacketSender()->queueVoxelEditMessages(PACKET_TYPE_VOXEL_ERASE, 1, &deleteVoxelDetail); } diff --git a/libraries/voxels/src/VoxelScriptingInterface.h b/libraries/voxels/src/VoxelScriptingInterface.h index aae05b8c70..db2835b7dd 100644 --- a/libraries/voxels/src/VoxelScriptingInterface.h +++ b/libraries/voxels/src/VoxelScriptingInterface.h @@ -12,16 +12,18 @@ #include #include +#include #include "VoxelEditPacketSender.h" /// handles scripting of voxel commands from JS passed to assigned clients -class VoxelScriptingInterface : public QObject { +class VoxelScriptingInterface : public OctreeScriptingInterface { Q_OBJECT public: - VoxelScriptingInterface(); - - VoxelEditPacketSender* getVoxelPacketSender() { return &_voxelPacketSender; } - JurisdictionListener* getJurisdictionListener() { return &_jurisdictionListener; } + VoxelEditPacketSender* getVoxelPacketSender() { return (VoxelEditPacketSender*)getPacketSender(); } + + virtual NODE_TYPE getServerNodeType() const { return NODE_TYPE_VOXEL_SERVER; } + virtual OctreeEditPacketSender* createPacketSender() { return new VoxelEditPacketSender(); } + public slots: /// queues the creation of a voxel which will be sent by calling process on the PacketSender /// \param x the x-coordinate of the voxel (in VS space) @@ -50,62 +52,7 @@ public slots: /// \param scale the scale of the voxel (in VS space) void queueVoxelDelete(float x, float y, float z, float scale); - /// Set the desired max packet size in bytes that should be created - void setMaxPacketSize(int maxPacketSize) { return _voxelPacketSender.setMaxPacketSize(maxPacketSize); } - - /// returns the current desired max packet size in bytes that will be created - int getMaxPacketSize() const { return _voxelPacketSender.getMaxPacketSize(); } - - /// set the max packets per second send rate - void setPacketsPerSecond(int packetsPerSecond) { return _voxelPacketSender.setPacketsPerSecond(packetsPerSecond); } - - /// get the max packets per second send rate - int getPacketsPerSecond() const { return _voxelPacketSender.getPacketsPerSecond(); } - - /// does a voxel server exist to send to - bool voxelServersExist() const { return _voxelPacketSender.voxelServersExist(); } - - /// are there packets waiting in the send queue to be sent - bool hasPacketsToSend() const { return _voxelPacketSender.hasPacketsToSend(); } - - /// how many packets are there in the send queue waiting to be sent - int packetsToSendCount() const { return _voxelPacketSender.packetsToSendCount(); } - - /// returns the packets per second send rate of this object over its lifetime - float getLifetimePPS() const { return _voxelPacketSender.getLifetimePPS(); } - - /// returns the bytes per second send rate of this object over its lifetime - float getLifetimeBPS() const { return _voxelPacketSender.getLifetimeBPS(); } - - /// returns the packets per second queued rate of this object over its lifetime - float getLifetimePPSQueued() const { return _voxelPacketSender.getLifetimePPSQueued(); } - - /// returns the bytes per second queued rate of this object over its lifetime - float getLifetimeBPSQueued() const { return _voxelPacketSender.getLifetimeBPSQueued(); } - - /// returns lifetime of this object from first packet sent to now in usecs - long long unsigned int getLifetimeInUsecs() const { return _voxelPacketSender.getLifetimeInUsecs(); } - - /// returns lifetime of this object from first packet sent to now in usecs - float getLifetimeInSeconds() const { return _voxelPacketSender.getLifetimeInSeconds(); } - - /// returns the total packets sent by this object over its lifetime - long long unsigned int getLifetimePacketsSent() const { return _voxelPacketSender.getLifetimePacketsSent(); } - - /// returns the total bytes sent by this object over its lifetime - long long unsigned int getLifetimeBytesSent() const { return _voxelPacketSender.getLifetimeBytesSent(); } - - /// returns the total packets queued by this object over its lifetime - long long unsigned int getLifetimePacketsQueued() const { return _voxelPacketSender.getLifetimePacketsQueued(); } - - /// returns the total bytes queued by this object over its lifetime - long long unsigned int getLifetimeBytesQueued() const { return _voxelPacketSender.getLifetimeBytesQueued(); } - private: - /// attached VoxelEditPacketSender that handles queuing and sending of packets to VS - VoxelEditPacketSender _voxelPacketSender; - JurisdictionListener _jurisdictionListener; - void queueVoxelAdd(PACKET_TYPE addPacketType, VoxelDetail& addVoxelDetails); };