mirror of
https://github.com/Armored-Dragon/overte.git
synced 2025-03-11 16:13:16 +01:00
266 lines
11 KiB
C++
266 lines
11 KiB
C++
//
|
|
// AssetServer.cpp
|
|
// assignment-client/src/assets
|
|
//
|
|
// Created by Ryan Huffman on 2015/07/21
|
|
// Copyright 2015 High Fidelity, Inc.
|
|
//
|
|
// Distributed under the Apache License, Version 2.0.
|
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
|
//
|
|
|
|
|
|
#include "AssetServer.h"
|
|
|
|
#include <QCoreApplication>
|
|
#include <QCryptographicHash>
|
|
#include <QDateTime>
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QString>
|
|
|
|
#include "NetworkLogging.h"
|
|
#include "NodeType.h"
|
|
#include "SendAssetTask.h"
|
|
#include "UploadAssetTask.h"
|
|
#include <ServerPathUtils.h>
|
|
|
|
const QString ASSET_SERVER_LOGGING_TARGET_NAME = "asset-server";
|
|
|
|
AssetServer::AssetServer(ReceivedMessage& message) :
|
|
ThreadedAssignment(message),
|
|
_taskPool(this)
|
|
{
|
|
|
|
// Most of the work will be I/O bound, reading from disk and constructing packet objects,
|
|
// so the ideal is greater than the number of cores on the system.
|
|
static const int TASK_POOL_THREAD_COUNT = 50;
|
|
_taskPool.setMaxThreadCount(TASK_POOL_THREAD_COUNT);
|
|
|
|
auto& packetReceiver = DependencyManager::get<NodeList>()->getPacketReceiver();
|
|
packetReceiver.registerListener(PacketType::AssetGet, this, "handleAssetGet");
|
|
packetReceiver.registerListener(PacketType::AssetGetInfo, this, "handleAssetGetInfo");
|
|
packetReceiver.registerListener(PacketType::AssetUpload, this, "handleAssetUpload");
|
|
}
|
|
|
|
void AssetServer::run() {
|
|
ThreadedAssignment::commonInit(ASSET_SERVER_LOGGING_TARGET_NAME, NodeType::AssetServer);
|
|
|
|
auto nodeList = DependencyManager::get<NodeList>();
|
|
nodeList->addNodeTypeToInterestSet(NodeType::Agent);
|
|
|
|
const QString RESOURCES_PATH = "assets";
|
|
|
|
_resourcesDirectory = QDir(ServerPathUtils::getDataDirectory()).filePath(RESOURCES_PATH);
|
|
|
|
qDebug() << "Creating resources directory";
|
|
_resourcesDirectory.mkpath(".");
|
|
|
|
bool noExistingAssets = !_resourcesDirectory.exists() \
|
|
|| _resourcesDirectory.entryList(QDir::Files).size() == 0;
|
|
|
|
if (noExistingAssets) {
|
|
qDebug() << "Asset resources directory not found, searching for existing asset resources";
|
|
QString oldDataDirectory = QCoreApplication::applicationDirPath();
|
|
auto oldResourcesDirectory = QDir(oldDataDirectory).filePath("resources/" + RESOURCES_PATH);
|
|
|
|
|
|
if (QDir(oldResourcesDirectory).exists()) {
|
|
qDebug() << "Existing assets found in " << oldResourcesDirectory << ", copying to " << _resourcesDirectory;
|
|
|
|
|
|
QDir resourcesParentDirectory = _resourcesDirectory.filePath("..");
|
|
if (!resourcesParentDirectory.exists()) {
|
|
qDebug() << "Creating data directory " << resourcesParentDirectory.absolutePath();
|
|
resourcesParentDirectory.mkpath(".");
|
|
}
|
|
|
|
auto files = QDir(oldResourcesDirectory).entryList(QDir::Files);
|
|
|
|
for (auto& file : files) {
|
|
auto from = oldResourcesDirectory + QDir::separator() + file;
|
|
auto to = _resourcesDirectory.absoluteFilePath(file);
|
|
qDebug() << "\tCopying from " << from << " to " << to;
|
|
QFile::copy(from, to);
|
|
}
|
|
|
|
}
|
|
}
|
|
qDebug() << "Serving files from: " << _resourcesDirectory.path();
|
|
|
|
// Scan for new files
|
|
qDebug() << "Looking for new files in asset directory";
|
|
auto files = _resourcesDirectory.entryInfoList(QDir::Files);
|
|
QRegExp filenameRegex { "^[a-f0-9]{" + QString::number(SHA256_HASH_HEX_LENGTH) + "}(\\..+)?$" };
|
|
for (const auto& fileInfo : files) {
|
|
auto filename = fileInfo.fileName();
|
|
if (!filenameRegex.exactMatch(filename)) {
|
|
qDebug() << "Found file: " << filename;
|
|
if (!fileInfo.isReadable()) {
|
|
qDebug() << "\tCan't open file for reading: " << filename;
|
|
continue;
|
|
}
|
|
|
|
// Read file
|
|
QFile file { fileInfo.absoluteFilePath() };
|
|
file.open(QFile::ReadOnly);
|
|
QByteArray data = file.readAll();
|
|
|
|
auto hash = hashData(data);
|
|
auto hexHash = hash.toHex();
|
|
|
|
qDebug() << "\tMoving " << filename << " to " << hexHash;
|
|
|
|
file.rename(_resourcesDirectory.absoluteFilePath(hexHash) + "." + fileInfo.suffix());
|
|
}
|
|
}
|
|
}
|
|
|
|
void AssetServer::handleAssetGetInfo(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
|
|
QByteArray assetHash;
|
|
MessageID messageID;
|
|
uint8_t extensionLength;
|
|
|
|
if (message->getSize() < qint64(SHA256_HASH_LENGTH + sizeof(messageID) + sizeof(extensionLength))) {
|
|
qDebug() << "ERROR bad file request";
|
|
return;
|
|
}
|
|
|
|
message->readPrimitive(&messageID);
|
|
assetHash = message->readWithoutCopy(SHA256_HASH_LENGTH);
|
|
message->readPrimitive(&extensionLength);
|
|
QByteArray extension = message->read(extensionLength);
|
|
|
|
auto replyPacket = NLPacket::create(PacketType::AssetGetInfoReply);
|
|
|
|
QByteArray hexHash = assetHash.toHex();
|
|
|
|
replyPacket->writePrimitive(messageID);
|
|
replyPacket->write(assetHash);
|
|
|
|
QString fileName = QString(hexHash) + "." + extension;
|
|
QFileInfo fileInfo { _resourcesDirectory.filePath(fileName) };
|
|
|
|
if (fileInfo.exists() && fileInfo.isReadable()) {
|
|
qDebug() << "Opening file: " << fileInfo.filePath();
|
|
replyPacket->writePrimitive(AssetServerError::NoError);
|
|
replyPacket->writePrimitive(fileInfo.size());
|
|
} else {
|
|
qDebug() << "Asset not found: " << QString(hexHash);
|
|
replyPacket->writePrimitive(AssetServerError::AssetNotFound);
|
|
}
|
|
|
|
auto nodeList = DependencyManager::get<NodeList>();
|
|
nodeList->sendPacket(std::move(replyPacket), *senderNode);
|
|
}
|
|
|
|
void AssetServer::handleAssetGet(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
|
|
|
|
auto minSize = qint64(sizeof(MessageID) + SHA256_HASH_LENGTH + sizeof(uint8_t) + sizeof(DataOffset) + sizeof(DataOffset));
|
|
|
|
if (message->getSize() < minSize) {
|
|
qDebug() << "ERROR bad file request";
|
|
return;
|
|
}
|
|
|
|
// Queue task
|
|
auto task = new SendAssetTask(message, senderNode, _resourcesDirectory);
|
|
_taskPool.start(task);
|
|
}
|
|
|
|
void AssetServer::handleAssetUpload(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
|
|
|
|
if (senderNode->getCanRez()) {
|
|
qDebug() << "Starting an UploadAssetTask for upload from" << uuidStringWithoutCurlyBraces(senderNode->getUUID());
|
|
|
|
auto task = new UploadAssetTask(message, senderNode, _resourcesDirectory);
|
|
_taskPool.start(task);
|
|
} else {
|
|
// this is a node the domain told us is not allowed to rez entities
|
|
// for now this also means it isn't allowed to add assets
|
|
// so return a packet with error that indicates that
|
|
|
|
auto permissionErrorPacket = NLPacket::create(PacketType::AssetUploadReply, sizeof(MessageID) + sizeof(AssetServerError));
|
|
|
|
MessageID messageID;
|
|
message->readPrimitive(&messageID);
|
|
|
|
// write the message ID and a permission denied error
|
|
permissionErrorPacket->writePrimitive(messageID);
|
|
permissionErrorPacket->writePrimitive(AssetServerError::PermissionDenied);
|
|
|
|
// send off the packet
|
|
auto nodeList = DependencyManager::get<NodeList>();
|
|
nodeList->sendPacket(std::move(permissionErrorPacket), *senderNode);
|
|
}
|
|
}
|
|
|
|
void AssetServer::sendStatsPacket() {
|
|
QJsonObject serverStats;
|
|
|
|
auto stats = DependencyManager::get<NodeList>()->sampleStatsForAllConnections();
|
|
|
|
for (const auto& stat : stats) {
|
|
QJsonObject nodeStats;
|
|
auto endTimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(stat.second.endTime);
|
|
QDateTime date = QDateTime::fromMSecsSinceEpoch(endTimeMs.count());
|
|
|
|
static const float USEC_PER_SEC = 1000000.0f;
|
|
static const float MEGABITS_PER_BYTE = 8.0f / 1000000.0f; // Bytes => Mbits
|
|
float elapsed = (float)(stat.second.endTime - stat.second.startTime).count() / USEC_PER_SEC; // sec
|
|
float megabitsPerSecPerByte = MEGABITS_PER_BYTE / elapsed; // Bytes => Mb/s
|
|
|
|
QJsonObject connectionStats;
|
|
connectionStats["1. Last Heard"] = date.toString();
|
|
connectionStats["2. Est. Max (P/s)"] = stat.second.estimatedBandwith;
|
|
connectionStats["3. RTT (ms)"] = stat.second.rtt;
|
|
connectionStats["4. CW (P)"] = stat.second.congestionWindowSize;
|
|
connectionStats["5. Period (us)"] = stat.second.packetSendPeriod;
|
|
connectionStats["6. Up (Mb/s)"] = stat.second.sentBytes * megabitsPerSecPerByte;
|
|
connectionStats["7. Down (Mb/s)"] = stat.second.receivedBytes * megabitsPerSecPerByte;
|
|
nodeStats["Connection Stats"] = connectionStats;
|
|
|
|
using Events = udt::ConnectionStats::Stats::Event;
|
|
const auto& events = stat.second.events;
|
|
|
|
QJsonObject upstreamStats;
|
|
upstreamStats["1. Sent (P/s)"] = stat.second.sendRate;
|
|
upstreamStats["2. Sent Packets"] = stat.second.sentPackets;
|
|
upstreamStats["3. Recvd ACK"] = events[Events::ReceivedACK];
|
|
upstreamStats["4. Procd ACK"] = events[Events::ProcessedACK];
|
|
upstreamStats["5. Recvd LACK"] = events[Events::ReceivedLightACK];
|
|
upstreamStats["6. Recvd NAK"] = events[Events::ReceivedNAK];
|
|
upstreamStats["7. Recvd TNAK"] = events[Events::ReceivedTimeoutNAK];
|
|
upstreamStats["8. Sent ACK2"] = events[Events::SentACK2];
|
|
upstreamStats["9. Retransmitted"] = events[Events::Retransmission];
|
|
nodeStats["Upstream Stats"] = upstreamStats;
|
|
|
|
QJsonObject downstreamStats;
|
|
downstreamStats["1. Recvd (P/s)"] = stat.second.receiveRate;
|
|
downstreamStats["2. Recvd Packets"] = stat.second.receivedPackets;
|
|
downstreamStats["3. Sent ACK"] = events[Events::SentACK];
|
|
downstreamStats["4. Sent LACK"] = events[Events::SentLightACK];
|
|
downstreamStats["5. Sent NAK"] = events[Events::SentNAK];
|
|
downstreamStats["6. Sent TNAK"] = events[Events::SentTimeoutNAK];
|
|
downstreamStats["7. Recvd ACK2"] = events[Events::ReceivedACK2];
|
|
downstreamStats["8. Duplicates"] = events[Events::Duplicate];
|
|
nodeStats["Downstream Stats"] = downstreamStats;
|
|
|
|
QString uuid;
|
|
auto nodelist = DependencyManager::get<NodeList>();
|
|
if (stat.first == nodelist->getDomainHandler().getSockAddr()) {
|
|
uuid = uuidStringWithoutCurlyBraces(nodelist->getDomainHandler().getUUID());
|
|
nodeStats[USERNAME_UUID_REPLACEMENT_STATS_KEY] = "DomainServer";
|
|
} else {
|
|
auto node = nodelist->findNodeWithAddr(stat.first);
|
|
uuid = uuidStringWithoutCurlyBraces(node ? node->getUUID() : QUuid());
|
|
nodeStats[USERNAME_UUID_REPLACEMENT_STATS_KEY] = uuid;
|
|
}
|
|
|
|
serverStats[uuid] = nodeStats;
|
|
}
|
|
|
|
// send off the stats packets
|
|
ThreadedAssignment::addPacketStatsAndSendStatsPacket(serverStats);
|
|
}
|