Showing the changes to libraries/entities/src/EntityScriptServerLogClient.cpp (1 of 6 files). Show all
Unified · Split
| @@ -0,0 +1,85 @@ | |||
| 1 | // | ||
| 2 | // EntityScriptServerLogClient.cpp | ||
| 3 | // interface/src | ||
| 4 | // | ||
| 5 | // Created by Clement Brisset on 2/1/17. | ||
| 6 | // Copyright 2017 High Fidelity, Inc. | ||
| 7 | // | ||
| 8 | // Distributed under the Apache License, Version 2.0. | ||
| 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
| 10 | // | ||
| 11 | |||
| 12 | #include "EntityScriptServerLogClient.h" | ||
| 13 | |||
| 14 | EntityScriptServerLogClient::EntityScriptServerLogClient() { | ||
| 15 | auto nodeList = DependencyManager::get<NodeList>(); | ||
| 16 | auto& packetReceiver = nodeList->getPacketReceiver(); | ||
| 17 | packetReceiver.registerListener(PacketType::EntityServerScriptLog, this, "handleEntityServerScriptLogPacket"); | ||
| 18 | |||
| 19 | QObject::connect(nodeList.data(), &NodeList::nodeActivated, this, &EntityScriptServerLogClient::nodeActivated); | ||
| 20 | QObject::connect(nodeList.data(), &NodeList::nodeKilled, this, &EntityScriptServerLogClient::nodeKilled); | ||
| 21 | |||
| 22 | QObject::connect(nodeList.data(), &NodeList::canRezChanged, this, &EntityScriptServerLogClient::canRezChanged); | ||
| 23 | } | ||
| 24 | |||
| 25 | void EntityScriptServerLogClient::connectNotify(const QMetaMethod& signal) { | ||
| 26 | // This needs to be delayed or "receivers()" will return completely inconsistent values | ||
| 27 | QMetaObject::invokeMethod(this, "connectionsChanged", Qt::QueuedConnection); | ||
| 28 | } | ||
| 29 | |||
| 30 | void EntityScriptServerLogClient::disconnectNotify(const QMetaMethod& signal) { | ||
| 31 | // This needs to be delayed or "receivers()" will return completely inconsistent values | ||
| 32 | QMetaObject::invokeMethod(this, "connectionsChanged", Qt::QueuedConnection); | ||
| 33 | } | ||
| 34 | |||
| 35 | void EntityScriptServerLogClient::connectionsChanged() { | ||
| 36 | auto numReceivers = receivers(SIGNAL(receivedNewLogLines(QString))); | ||
| 37 | if (!_subscribed && numReceivers > 0) { | ||
| 38 | enableToEntityServerScriptLog(DependencyManager::get<NodeList>()->getThisNodeCanRez()); | ||
| 39 | } else if (_subscribed && numReceivers == 0) { | ||
| 40 | enableToEntityServerScriptLog(false); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | void EntityScriptServerLogClient::enableToEntityServerScriptLog(bool enable) { | ||
| 45 | auto nodeList = DependencyManager::get<NodeList>(); | ||
| 46 | |||
| 47 | if (auto node = nodeList->soloNodeOfType(NodeType::EntityScriptServer)) { | ||
| 48 | auto packet = NLPacket::create(PacketType::EntityServerScriptLog, sizeof(bool), true); | ||
| 49 | packet->writePrimitive(enable); | ||
| 50 | nodeList->sendPacket(std::move(packet), *node); | ||
| 51 | |||
| 52 | if (_subscribed != enable) { | ||
| 53 | if (enable) { | ||
| 54 | emit receivedNewLogLines("====================== Subscribded to the Entity Script Server's log ======================"); | ||
| 55 | } else { | ||
| 56 | emit receivedNewLogLines("==================== Unsubscribded from the Entity Script Server's log ===================="); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | _subscribed = enable; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | void EntityScriptServerLogClient::handleEntityServerScriptLogPacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) { | ||
| 64 | emit receivedNewLogLines(QString::fromUtf8(message->readAll())); | ||
| 65 | } | ||
| 66 | |||
| 67 | void EntityScriptServerLogClient::nodeActivated(SharedNodePointer activatedNode) { | ||
| 68 | if (_subscribed && activatedNode->getType() == NodeType::EntityScriptServer) { | ||
| 69 | _subscribed = false; | ||
| 70 | enableToEntityServerScriptLog(DependencyManager::get<NodeList>()->getThisNodeCanRez()); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | void EntityScriptServerLogClient::nodeKilled(SharedNodePointer killedNode) { | ||
| 75 | if (killedNode->getType() == NodeType::EntityScriptServer) { | ||
| 76 | emit receivedNewLogLines("====================== Connection to the Entity Script Server lost ======================"); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | void EntityScriptServerLogClient::canRezChanged(bool canRez) { | ||
| 81 | auto numReceivers = receivers(SIGNAL(receivedNewLogLines(QString))); | ||
| 82 | if (numReceivers > 0) { | ||
| 83 | enableToEntityServerScriptLog(canRez); | ||
| 84 | } | ||
| 85 | } | ||