Showing the changes to assignment-client/src/scripts/EntityScriptServer.cpp (1 of 5 files). Show all
Unified · Split
| @@ -30,7 +30,6 @@ · 29 unmodified lines | ||
| 30 | 30 | int EntityScriptServer::_entitiesScriptEngineCount = 0; |
| 31 | 31 | |
| 32 | 32 | EntityScriptServer::EntityScriptServer(ReceivedMessage& message) : ThreadedAssignment(message) { |
| 33 | − _entityEditSender.setPacketsPerSecond(3000); | |
| 34 | 33 | DependencyManager::get<EntityScriptingInterface>()->setPacketSender(&_entityEditSender); |
| 35 | 34 | |
| 36 | 35 | ResourceManager::init(); |
| @@ -101,6 +100,52 @@ void EntityScriptServer::handleEntityScriptGetStatusPacket(QSharedPointer<Receiv · 64 unmodified lines | ||
| 101 | 100 | } |
| 102 | 101 | } |
| 103 | 102 | |
| 103 | +void EntityScriptServer::handleSettings() { | |
| 104 | + | |
| 105 | + auto nodeList = DependencyManager::get<NodeList>(); | |
| 106 | + | |
| 107 | + auto& domainHandler = nodeList->getDomainHandler(); | |
| 108 | + const QJsonObject& settingsObject = domainHandler.getSettingsObject(); | |
| 109 | + | |
| 110 | + static const QString ENTITY_SCRIPT_SERVER_SETTINGS_KEY = "entity_script_server"; | |
| 111 | + | |
| 112 | + if (!settingsObject.contains(ENTITY_SCRIPT_SERVER_SETTINGS_KEY)) { | |
| 113 | + qWarning() << "Received settings from the domain-server with no entity_script_server section."; | |
| 114 | + return; | |
| 115 | + } | |
| 116 | + | |
| 117 | + auto entityScriptServerSettings = settingsObject[ENTITY_SCRIPT_SERVER_SETTINGS_KEY].toObject(); | |
| 118 | + | |
| 119 | + static const QString MAX_ENTITY_PPS_OPTION = "max_total_entity_pps"; | |
| 120 | + static const QString ENTITY_PPS_PER_SCRIPT = "entity_pps_per_script"; | |
| 121 | + | |
| 122 | + if (!entityScriptServerSettings.contains(MAX_ENTITY_PPS_OPTION) || !entityScriptServerSettings.contains(ENTITY_PPS_PER_SCRIPT)) { | |
| 123 | + qWarning() << "Received settings from the domain-server with no max_total_entity_pps or entity_pps_per_script properties."; | |
| 124 | + return; | |
| 125 | + } | |
| 126 | + | |
| 127 | + _maxEntityPPS = std::max(0, entityScriptServerSettings[MAX_ENTITY_PPS_OPTION].toInt()); | |
| 128 | + _entityPPSPerScript = std::max(0, entityScriptServerSettings[ENTITY_PPS_PER_SCRIPT].toInt()); | |
| 129 | + | |
| 130 | + qDebug() << QString("Received entity script server settings, Max Entity PPS: %1, Entity PPS Per Entity Script: %2") | |
| 131 | + .arg(_maxEntityPPS).arg(_entityPPSPerScript); | |
| 132 | +} | |
| 133 | + | |
| 134 | +void EntityScriptServer::updateEntityPPS() { | |
| 135 | + int numRunningScripts = _entitiesScriptEngine->getNumRunningEntityScripts(); | |
| 136 | + int pps; | |
| 137 | + if (std::numeric_limits<int>::max() / _entityPPSPerScript < numRunningScripts) { | |
| 138 | + qWarning() << QString("Integer multiplaction would overflow, clamping to maxint: %1 * %2").arg(numRunningScripts).arg(_entityPPSPerScript); | |
| 139 | + pps = std::numeric_limits<int>::max(); | |
| 140 | + pps = std::min(_maxEntityPPS, pps); | |
| 141 | + } else { | |
| 142 | + pps = _entityPPSPerScript * numRunningScripts; | |
| 143 | + pps = std::min(_maxEntityPPS, pps); | |
| 144 | + } | |
| 145 | + _entityEditSender.setPacketsPerSecond(pps); | |
| 146 | + qDebug() << QString("Updating entity PPS to: %1 @ %2 PPS per script = %3 PPS").arg(numRunningScripts).arg(_entityPPSPerScript).arg(pps); | |
| 147 | +} | |
| 148 | + | |
| 104 | 149 | void EntityScriptServer::run() { |
| 105 | 150 | // make sure we request our script once the agent connects to the domain |
| 106 | 151 | auto nodeList = DependencyManager::get<NodeList>(); |
| @@ -115,6 +160,9 @@ void EntityScriptServer::run() { · 8 unmodified lines | ||
| 115 | 160 | connect(messagesThread, &QThread::started, messagesClient.data(), &MessagesClient::init); |
| 116 | 161 | messagesThread->start(); |
| 117 | 162 | |
| 163 | + DomainHandler& domainHandler = DependencyManager::get<NodeList>()->getDomainHandler(); | |
| 164 | + connect(&domainHandler, &DomainHandler::settingsReceived, this, &EntityScriptServer::handleSettings); | |
| 165 | + | |
| 118 | 166 | // make sure we hear about connected nodes so we can grab an ATP script if a request is pending |
| 119 | 167 | connect(nodeList.data(), &LimitedNodeList::nodeActivated, this, &EntityScriptServer::nodeActivated); |
| 120 | 168 | connect(nodeList.data(), &LimitedNodeList::nodeKilled, this, &EntityScriptServer::nodeKilled); |
| @@ -233,7 +281,9 @@ void EntityScriptServer::resetEntitiesScriptEngine() { · 112 unmodified lines | ||
| 233 | 281 | newEngine->runInThread(); |
| 234 | 282 | DependencyManager::get<EntityScriptingInterface>()->setEntitiesScriptEngine(newEngine.data()); |
| 235 | 283 | |
| 284 | + disconnect(_entitiesScriptEngine.data(), &ScriptEngine::entityScriptDetailsUpdated, this, &EntityScriptServer::updateEntityPPS); | |
| 236 | 285 | _entitiesScriptEngine.swap(newEngine); |
| 286 | + connect(_entitiesScriptEngine.data(), &ScriptEngine::entityScriptDetailsUpdated, this, &EntityScriptServer::updateEntityPPS); | |
| 237 | 287 | } |
| 238 | 288 | |
| 239 | 289 | |