Showing the changes to interface/src/MetavoxelSystem.cpp (1 of 6 files). Show all
Unified · Split
| @@ -25,12 +25,6 @@ MetavoxelSystem::MetavoxelSystem() : · 24 unmodified lines | ||
| 25 | 25 | _buffer(QOpenGLBuffer::VertexBuffer) { |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | −MetavoxelSystem::~MetavoxelSystem() { | |
| 29 | − for (QHash<QUuid, MetavoxelClient*>::const_iterator it = _clients.begin(); it != _clients.end(); it++) { | |
| 30 | − delete it.value(); | |
| 31 | − } | |
| 32 | −} | |
| 33 | − | |
| 34 | 28 | void MetavoxelSystem::init() { |
| 35 | 29 | if (!_program.isLinked()) { |
| 36 | 30 | switchToResourcesParentIfRequired(); |
| @@ -42,33 +36,35 @@ void MetavoxelSystem::init() { · 5 unmodified lines | ||
| 42 | 36 | // let the script cache know to use our common access manager |
| 43 | 37 | ScriptCache::getInstance()->setNetworkAccessManager(Application::getInstance()->getNetworkAccessManager()); |
| 44 | 38 | } |
| 45 | − | |
| 46 | − NodeList* nodeList = NodeList::getInstance(); | |
| 47 | − | |
| 48 | − connect(nodeList, SIGNAL(nodeAdded(SharedNodePointer)), SLOT(nodeAdded(SharedNodePointer))); | |
| 49 | − connect(nodeList, SIGNAL(nodeKilled(SharedNodePointer)), SLOT(nodeKilled(SharedNodePointer))); | |
| 50 | − | |
| 51 | 39 | _buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw); |
| 52 | 40 | _buffer.create(); |
| 41 | + | |
| 42 | + connect(NodeList::getInstance(), SIGNAL(nodeAdded(SharedNodePointer)), SLOT(maybeAttachClient(const SharedNodePointer&))); | |
| 53 | 43 | } |
| 54 | 44 | |
| 55 | 45 | void MetavoxelSystem::applyEdit(const MetavoxelEditMessage& edit) { |
| 56 | − foreach (MetavoxelClient* client, _clients) { | |
| 57 | − client->applyEdit(edit); | |
| 46 | + foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) { | |
| 47 | + if (node->getType() == NodeType::MetavoxelServer) { | |
| 48 | + MetavoxelClient* client = static_cast<MetavoxelClient*>(node->getLinkedData()); | |
| 49 | + if (client) { | |
| 50 | + client->applyEdit(edit); | |
| 51 | + } | |
| 52 | + } | |
| 58 | 53 | } |
| 59 | 54 | } |
| 60 | 55 | |
| 61 | −void MetavoxelSystem::processData(const QByteArray& data, const HifiSockAddr& sender) { | |
| 62 | − QMetaObject::invokeMethod(this, "receivedData", Q_ARG(const QByteArray&, data), Q_ARG(const HifiSockAddr&, sender)); | |
| 63 | −} | |
| 64 | − | |
| 65 | 56 | void MetavoxelSystem::simulate(float deltaTime) { |
| 66 | 57 | // simulate the clients |
| 67 | 58 | _points.clear(); |
| 68 | − foreach (MetavoxelClient* client, _clients) { | |
| 69 | − client->simulate(deltaTime, _pointVisitor); | |
| 59 | + foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) { | |
| 60 | + if (node->getType() == NodeType::MetavoxelServer) { | |
| 61 | + MetavoxelClient* client = static_cast<MetavoxelClient*>(node->getLinkedData()); | |
| 62 | + if (client) { | |
| 63 | + client->simulate(deltaTime, _pointVisitor); | |
| 64 | + } | |
| 65 | + } | |
| 70 | 66 | } |
| 71 | − | |
| 67 | + | |
| 72 | 68 | _buffer.bind(); |
| 73 | 69 | int bytes = _points.size() * sizeof(Point); |
| 74 | 70 | if (_buffer.size() < bytes) { |
| @@ -120,39 +116,10 @@ void MetavoxelSystem::render() { · 45 unmodified lines | ||
| 120 | 116 | _program.release(); |
| 121 | 117 | } |
| 122 | 118 | |
| 123 | −void MetavoxelSystem::nodeAdded(SharedNodePointer node) { | |
| 119 | +void MetavoxelSystem::maybeAttachClient(const SharedNodePointer& node) { | |
| 124 | 120 | if (node->getType() == NodeType::MetavoxelServer) { |
| 125 | − QMetaObject::invokeMethod(this, "addClient", Q_ARG(const SharedNodePointer&, node)); | |
| 126 | − } | |
| 127 | −} | |
| 128 | − | |
| 129 | −void MetavoxelSystem::nodeKilled(SharedNodePointer node) { | |
| 130 | − if (node->getType() == NodeType::MetavoxelServer) { | |
| 131 | − QMetaObject::invokeMethod(this, "removeClient", Q_ARG(const QUuid&, node->getUUID())); | |
| 132 | − } | |
| 133 | −} | |
| 134 | − | |
| 135 | −void MetavoxelSystem::addClient(const SharedNodePointer& node) { | |
| 136 | − MetavoxelClient* client = new MetavoxelClient(node); | |
| 137 | − _clients.insert(node->getUUID(), client); | |
| 138 | − _clientsBySessionID.insert(client->getSessionID(), client); | |
| 139 | −} | |
| 140 | − | |
| 141 | −void MetavoxelSystem::removeClient(const QUuid& uuid) { | |
| 142 | − MetavoxelClient* client = _clients.take(uuid); | |
| 143 | − _clientsBySessionID.remove(client->getSessionID()); | |
| 144 | − delete client; | |
| 145 | −} | |
| 146 | − | |
| 147 | −void MetavoxelSystem::receivedData(const QByteArray& data, const SharedNodePointer& sendingNode) { | |
| 148 | − int headerPlusIDSize; | |
| 149 | − QUuid sessionID = readSessionID(data, sendingNode, headerPlusIDSize); | |
| 150 | − if (sessionID.isNull()) { | |
| 151 | − return; | |
| 152 | − } | |
| 153 | − MetavoxelClient* client = _clientsBySessionID.value(sessionID); | |
| 154 | − if (client) { | |
| 155 | − client->receivedData(data); | |
| 121 | + QMutexLocker locker(&node->getMutex()); | |
| 122 | + node->setLinkedData(new MetavoxelClient(NodeList::getInstance()->nodeWithUUID(node->getUUID()))); | |
| 156 | 123 | } |
| 157 | 124 | } |
| 158 | 125 | |
| @@ -179,16 +146,9 @@ bool MetavoxelSystem::PointVisitor::visit(MetavoxelInfo& info) { · 20 unmodified lines | ||
| 179 | 146 | return false; |
| 180 | 147 | } |
| 181 | 148 | |
| 182 | −static QByteArray createDatagramHeader(const QUuid& sessionID) { | |
| 183 | − QByteArray header = byteArrayWithPopluatedHeader(PacketTypeMetavoxelData); | |
| 184 | − header += sessionID.toRfc4122(); | |
| 185 | − return header; | |
| 186 | −} | |
| 187 | − | |
| 188 | 149 | MetavoxelClient::MetavoxelClient(const SharedNodePointer& node) : |
| 189 | 150 | _node(node), |
| 190 | − _sessionID(QUuid::createUuid()), | |
| 191 | − _sequencer(createDatagramHeader(_sessionID)) { | |
| 151 | + _sequencer(byteArrayWithPopluatedHeader(PacketTypeMetavoxelData)) { | |
| 192 | 152 | |
| 193 | 153 | connect(&_sequencer, SIGNAL(readyToWrite(const QByteArray&)), SLOT(sendData(const QByteArray&))); |
| 194 | 154 | connect(&_sequencer, SIGNAL(readyToRead(Bitstream&)), SLOT(readPacket(Bitstream&))); |
| @@ -223,9 +183,10 @@ void MetavoxelClient::simulate(float deltaTime, MetavoxelVisitor& visitor) { · 28 unmodified lines | ||
| 223 | 183 | _data.guide(visitor); |
| 224 | 184 | } |
| 225 | 185 | |
| 226 | −void MetavoxelClient::receivedData(const QByteArray& data) { | |
| 186 | +int MetavoxelClient::parseData(const QByteArray& packet) { | |
| 227 | 187 | // process through sequencer |
| 228 | − _sequencer.receivedDatagram(data); | |
| 188 | + QMetaObject::invokeMethod(&_sequencer, "receivedDatagram", Q_ARG(const QByteArray&, packet)); | |
| 189 | + return packet.size(); | |
| 229 | 190 | } |
| 230 | 191 | |
| 231 | 192 | void MetavoxelClient::sendData(const QByteArray& data) { |