Showing the changes to interface/src/EntityScriptServerLogClient.cpp (1 of 6 files). Show all
Unified · Split
| @@ -0,0 +1,83 @@ | ||
| 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 | + | |
| 26 | +void EntityScriptServerLogClient::connectNotify(const QMetaMethod& signal) { | |
| 27 | + auto numReceivers = receivers(SIGNAL(receivedNewLogLines(QString))); | |
| 28 | + if (!_subscribed && numReceivers > 0) { | |
| 29 | + enableToEntityServerScriptLog(DependencyManager::get<NodeList>()->getThisNodeCanRez()); | |
| 30 | + } | |
| 31 | +} | |
| 32 | + | |
| 33 | +void EntityScriptServerLogClient::disconnectNotify(const QMetaMethod& signal) { | |
| 34 | + auto numReceivers = receivers(SIGNAL(receivedNewLogLines(QString))); | |
| 35 | + if (_subscribed && numReceivers == 0) { | |
| 36 | + enableToEntityServerScriptLog(false); | |
| 37 | + } | |
| 38 | +} | |
| 39 | + | |
| 40 | +void EntityScriptServerLogClient::enableToEntityServerScriptLog(bool enable) { | |
| 41 | + auto nodeList = DependencyManager::get<NodeList>(); | |
| 42 | + | |
| 43 | + if (auto node = nodeList->soloNodeOfType(NodeType::EntityScriptServer)) { | |
| 44 | + auto packet = NLPacket::create(PacketType::EntityServerScriptLog, sizeof(bool), true); | |
| 45 | + packet->writePrimitive(enable); | |
| 46 | + nodeList->sendPacket(std::move(packet), *node); | |
| 47 | + | |
| 48 | + if (_subscribed != enable) { | |
| 49 | + if (enable) { | |
| 50 | + emit receivedNewLogLines("====================== Subscribded to the Entity Script Server's log ======================"); | |
| 51 | + } else { | |
| 52 | + emit receivedNewLogLines("==================== Unsubscribded from the Entity Script Server's log ===================="); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + _subscribed = enable; | |
| 56 | + } else { | |
| 57 | + qWarning() << "Entity Script Server not found"; | |
| 58 | + } | |
| 59 | +} | |
| 60 | + | |
| 61 | +void EntityScriptServerLogClient::handleEntityServerScriptLogPacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) { | |
| 62 | + emit receivedNewLogLines(QString::fromUtf8(message->readAll())); | |
| 63 | +} | |
| 64 | + | |
| 65 | +void EntityScriptServerLogClient::nodeActivated(SharedNodePointer activatedNode) { | |
| 66 | + if (_subscribed && activatedNode->getType() == NodeType::EntityScriptServer) { | |
| 67 | + _subscribed = false; | |
| 68 | + enableToEntityServerScriptLog(DependencyManager::get<NodeList>()->getThisNodeCanRez()); | |
| 69 | + } | |
| 70 | +} | |
| 71 | + | |
| 72 | +void EntityScriptServerLogClient::nodeKilled(SharedNodePointer killedNode) { | |
| 73 | + if (killedNode->getType() == NodeType::EntityScriptServer) { | |
| 74 | + emit receivedNewLogLines("====================== Connection to the Entity Script Server lost ======================"); | |
| 75 | + } | |
| 76 | +} | |
| 77 | + | |
| 78 | +void EntityScriptServerLogClient::canRezChanged(bool canRez) { | |
| 79 | + auto numReceivers = receivers(SIGNAL(receivedNewLogLines(QString))); | |
| 80 | + if (numReceivers > 0) { | |
| 81 | + enableToEntityServerScriptLog(canRez); | |
| 82 | + } | |
| 83 | +} | |