mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-29 11:42:51 +02:00
429 lines
14 KiB
C++
429 lines
14 KiB
C++
//
|
|
// AssetClient.cpp
|
|
// libraries/networking/src
|
|
//
|
|
// 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 "AssetClient.h"
|
|
|
|
#include <cstdint>
|
|
|
|
#include <QtCore/QBuffer>
|
|
#include <QtCore/QStandardPaths>
|
|
#include <QtCore/QThread>
|
|
#include <QtNetwork/QNetworkDiskCache>
|
|
|
|
#include "AssetRequest.h"
|
|
#include "AssetUpload.h"
|
|
#include "AssetUtils.h"
|
|
#include "NetworkAccessManager.h"
|
|
#include "NetworkLogging.h"
|
|
#include "NodeList.h"
|
|
#include "PacketReceiver.h"
|
|
#include "ResourceCache.h"
|
|
|
|
MessageID AssetClient::_currentID = 0;
|
|
|
|
|
|
AssetClient::AssetClient() {
|
|
|
|
setCustomDeleter([](Dependency* dependency){
|
|
static_cast<AssetClient*>(dependency)->deleteLater();
|
|
});
|
|
|
|
auto nodeList = DependencyManager::get<NodeList>();
|
|
auto& packetReceiver = nodeList->getPacketReceiver();
|
|
packetReceiver.registerListener(PacketType::AssetGetInfoReply, this, "handleAssetGetInfoReply");
|
|
packetReceiver.registerMessageListener(PacketType::AssetGetReply, this, "handleAssetGetReply");
|
|
packetReceiver.registerListener(PacketType::AssetUploadReply, this, "handleAssetUploadReply");
|
|
|
|
connect(nodeList.data(), &LimitedNodeList::nodeKilled, this, &AssetClient::handleNodeKilled);
|
|
}
|
|
|
|
void AssetClient::init() {
|
|
if (QThread::currentThread() != thread()) {
|
|
QMetaObject::invokeMethod(this, "init", Qt::BlockingQueuedConnection);
|
|
}
|
|
|
|
// Setup disk cache if not already
|
|
QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
|
|
if (!networkAccessManager.cache()) {
|
|
QString cachePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
|
|
cachePath = !cachePath.isEmpty() ? cachePath : "interfaceCache";
|
|
|
|
QNetworkDiskCache* cache = new QNetworkDiskCache();
|
|
cache->setMaximumCacheSize(MAXIMUM_CACHE_SIZE);
|
|
cache->setCacheDirectory(cachePath);
|
|
networkAccessManager.setCache(cache);
|
|
qCDebug(asset_client) << "AssetClient disk cache setup at" << cachePath
|
|
<< "(size:" << MAXIMUM_CACHE_SIZE / BYTES_PER_GIGABYTES << "GB)";
|
|
}
|
|
}
|
|
|
|
bool haveAssetServer() {
|
|
auto nodeList = DependencyManager::get<NodeList>();
|
|
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
|
|
|
|
if (!assetServer) {
|
|
qCWarning(asset_client) << "Could not complete AssetClient operation "
|
|
<< "since you are not currently connected to an asset-server.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
AssetRequest* AssetClient::createRequest(const QString& hash, const QString& extension) {
|
|
if (hash.length() != SHA256_HASH_HEX_LENGTH) {
|
|
qCWarning(asset_client) << "Invalid hash size";
|
|
return nullptr;
|
|
}
|
|
|
|
if (haveAssetServer()) {
|
|
auto request = new AssetRequest(hash, extension);
|
|
|
|
// Move to the AssetClient thread in case we are not currently on that thread (which will usually be the case)
|
|
request->moveToThread(thread());
|
|
|
|
return request;
|
|
} else {
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
AssetUpload* AssetClient::createUpload(const QString& filename) {
|
|
|
|
if (haveAssetServer()) {
|
|
auto upload = new AssetUpload(filename);
|
|
|
|
upload->moveToThread(thread());
|
|
|
|
return upload;
|
|
} else {
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
AssetUpload* AssetClient::createUpload(const QByteArray& data, const QString& extension) {
|
|
if (haveAssetServer()) {
|
|
auto upload = new AssetUpload(data, extension);
|
|
|
|
upload->moveToThread(thread());
|
|
|
|
return upload;
|
|
} else {
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
bool AssetClient::getAsset(const QString& hash, const QString& extension, DataOffset start, DataOffset end,
|
|
ReceivedAssetCallback callback) {
|
|
if (hash.length() != SHA256_HASH_HEX_LENGTH) {
|
|
qCWarning(asset_client) << "Invalid hash size";
|
|
return false;
|
|
}
|
|
|
|
auto nodeList = DependencyManager::get<NodeList>();
|
|
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
|
|
|
|
if (assetServer) {
|
|
|
|
auto messageID = ++_currentID;
|
|
|
|
auto payloadSize = sizeof(messageID) + SHA256_HASH_LENGTH + sizeof(uint8_t) + extension.length()
|
|
+ sizeof(start) + sizeof(end);
|
|
auto packet = NLPacket::create(PacketType::AssetGet, payloadSize, true);
|
|
|
|
qCDebug(asset_client) << "Requesting data from" << start << "to" << end << "of" << hash << "from asset-server.";
|
|
|
|
packet->writePrimitive(messageID);
|
|
|
|
packet->write(QByteArray::fromHex(hash.toLatin1()));
|
|
|
|
packet->writePrimitive(uint8_t(extension.length()));
|
|
packet->write(extension.toLatin1());
|
|
|
|
packet->writePrimitive(start);
|
|
packet->writePrimitive(end);
|
|
|
|
nodeList->sendPacket(std::move(packet), *assetServer);
|
|
|
|
_pendingRequests[assetServer][messageID] = callback;
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool AssetClient::getAssetInfo(const QString& hash, const QString& extension, GetInfoCallback callback) {
|
|
auto nodeList = DependencyManager::get<NodeList>();
|
|
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
|
|
|
|
if (assetServer) {
|
|
auto messageID = ++_currentID;
|
|
|
|
auto payloadSize = sizeof(messageID) + SHA256_HASH_LENGTH + sizeof(uint8_t) + extension.length();
|
|
auto packet = NLPacket::create(PacketType::AssetGetInfo, payloadSize, true);
|
|
|
|
packet->writePrimitive(messageID);
|
|
packet->write(QByteArray::fromHex(hash.toLatin1()));
|
|
packet->writePrimitive(uint8_t(extension.length()));
|
|
packet->write(extension.toLatin1());
|
|
|
|
nodeList->sendPacket(std::move(packet), *assetServer);
|
|
|
|
_pendingInfoRequests[assetServer][messageID] = callback;
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void AssetClient::handleAssetGetInfoReply(QSharedPointer<NLPacket> packet, SharedNodePointer senderNode) {
|
|
MessageID messageID;
|
|
packet->readPrimitive(&messageID);
|
|
auto assetHash = packet->read(SHA256_HASH_LENGTH);
|
|
|
|
AssetServerError error;
|
|
packet->readPrimitive(&error);
|
|
|
|
AssetInfo info { assetHash.toHex(), 0 };
|
|
|
|
if (error == AssetServerError::NoError) {
|
|
packet->readPrimitive(&info.size);
|
|
}
|
|
|
|
// Check if we have any pending requests for this node
|
|
auto messageMapIt = _pendingInfoRequests.find(senderNode);
|
|
if (messageMapIt != _pendingInfoRequests.end()) {
|
|
|
|
// Found the node, get the MessageID -> Callback map
|
|
auto& messageCallbackMap = messageMapIt->second;
|
|
|
|
// Check if we have this pending request
|
|
auto requestIt = messageCallbackMap.find(messageID);
|
|
if (requestIt != messageCallbackMap.end()) {
|
|
auto callback = requestIt->second;
|
|
callback(true, error, info);
|
|
messageCallbackMap.erase(requestIt);
|
|
}
|
|
|
|
// Although the messageCallbackMap may now be empty, we won't delete the node until we have disconnected from
|
|
// it to avoid constantly creating/deleting the map on subsequent requests.
|
|
}
|
|
}
|
|
|
|
void AssetClient::handleAssetGetReply(QSharedPointer<NLPacketList> packetList, SharedNodePointer senderNode) {
|
|
QByteArray data = packetList->getMessage();
|
|
QBuffer packet { &data };
|
|
packet.open(QIODevice::ReadOnly);
|
|
|
|
auto assetHash = packet.read(SHA256_HASH_LENGTH);
|
|
qCDebug(asset_client) << "Got reply for asset: " << assetHash.toHex();
|
|
|
|
MessageID messageID;
|
|
packet.read(reinterpret_cast<char*>(&messageID), sizeof(messageID));
|
|
|
|
AssetServerError error;
|
|
packet.read(reinterpret_cast<char*>(&error), sizeof(AssetServerError));
|
|
QByteArray assetData;
|
|
|
|
if (!error) {
|
|
DataOffset length;
|
|
packet.read(reinterpret_cast<char*>(&length), sizeof(DataOffset));
|
|
data = packet.read(length);
|
|
} else {
|
|
qCWarning(asset_client) << "Failure getting asset: " << error;
|
|
}
|
|
|
|
// Check if we have any pending requests for this node
|
|
auto messageMapIt = _pendingRequests.find(senderNode);
|
|
if (messageMapIt != _pendingRequests.end()) {
|
|
|
|
// Found the node, get the MessageID -> Callback map
|
|
auto& messageCallbackMap = messageMapIt->second;
|
|
|
|
// Check if we have this pending request
|
|
auto requestIt = messageCallbackMap.find(messageID);
|
|
if (requestIt != messageCallbackMap.end()) {
|
|
auto callback = requestIt->second;
|
|
callback(true, error, data);
|
|
messageCallbackMap.erase(requestIt);
|
|
}
|
|
|
|
// Although the messageCallbackMap may now be empty, we won't delete the node until we have disconnected from
|
|
// it to avoid constantly creating/deleting the map on subsequent requests.
|
|
}
|
|
}
|
|
|
|
bool AssetClient::uploadAsset(const QByteArray& data, const QString& extension, UploadResultCallback callback) {
|
|
auto nodeList = DependencyManager::get<NodeList>();
|
|
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
|
|
|
|
if (assetServer) {
|
|
auto packetList = NLPacketList::create(PacketType::AssetUpload, QByteArray(), true, true);
|
|
|
|
auto messageID = ++_currentID;
|
|
packetList->writePrimitive(messageID);
|
|
|
|
packetList->writePrimitive(static_cast<uint8_t>(extension.length()));
|
|
packetList->write(extension.toLatin1().constData(), extension.length());
|
|
|
|
uint64_t size = data.length();
|
|
packetList->writePrimitive(size);
|
|
packetList->write(data.constData(), size);
|
|
|
|
nodeList->sendPacketList(std::move(packetList), *assetServer);
|
|
|
|
_pendingUploads[assetServer][messageID] = callback;
|
|
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void AssetClient::handleAssetUploadReply(QSharedPointer<NLPacket> packet, SharedNodePointer senderNode) {
|
|
MessageID messageID;
|
|
packet->readPrimitive(&messageID);
|
|
|
|
AssetServerError error;
|
|
packet->readPrimitive(&error);
|
|
|
|
QString hashString;
|
|
|
|
if (error) {
|
|
qCWarning(asset_client) << "Error uploading file to asset server";
|
|
} else {
|
|
auto hash = packet->read(SHA256_HASH_LENGTH);
|
|
hashString = hash.toHex();
|
|
|
|
qCDebug(asset_client) << "Successfully uploaded asset to asset-server - SHA256 hash is " << hashString;
|
|
}
|
|
|
|
// Check if we have any pending requests for this node
|
|
auto messageMapIt = _pendingUploads.find(senderNode);
|
|
if (messageMapIt != _pendingUploads.end()) {
|
|
|
|
// Found the node, get the MessageID -> Callback map
|
|
auto& messageCallbackMap = messageMapIt->second;
|
|
|
|
// Check if we have this pending request
|
|
auto requestIt = messageCallbackMap.find(messageID);
|
|
if (requestIt != messageCallbackMap.end()) {
|
|
auto callback = requestIt->second;
|
|
callback(true, error, hashString);
|
|
messageCallbackMap.erase(requestIt);
|
|
}
|
|
|
|
// Although the messageCallbackMap may now be empty, we won't delete the node until we have disconnected from
|
|
// it to avoid constantly creating/deleting the map on subsequent requests.
|
|
}
|
|
}
|
|
|
|
void AssetClient::handleNodeKilled(SharedNodePointer node) {
|
|
if (node->getType() != NodeType::AssetServer) {
|
|
return;
|
|
}
|
|
|
|
{
|
|
auto messageMapIt = _pendingRequests.find(node);
|
|
if (messageMapIt != _pendingRequests.end()) {
|
|
for (const auto& value : messageMapIt->second) {
|
|
value.second(false, AssetServerError::NoError, QByteArray());
|
|
}
|
|
messageMapIt->second.clear();
|
|
}
|
|
}
|
|
|
|
{
|
|
auto messageMapIt = _pendingInfoRequests.find(node);
|
|
if (messageMapIt != _pendingInfoRequests.end()) {
|
|
AssetInfo info { "", 0 };
|
|
for (const auto& value : messageMapIt->second) {
|
|
value.second(false, AssetServerError::NoError, info);
|
|
}
|
|
messageMapIt->second.clear();
|
|
}
|
|
}
|
|
|
|
{
|
|
auto messageMapIt = _pendingUploads.find(node);
|
|
if (messageMapIt != _pendingUploads.end()) {
|
|
for (const auto& value : messageMapIt->second) {
|
|
value.second(false, AssetServerError::NoError, "");
|
|
}
|
|
messageMapIt->second.clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
void AssetScriptingInterface::uploadData(QString data, QString extension, QScriptValue callback) {
|
|
QByteArray dataByteArray = data.toUtf8();
|
|
auto upload = DependencyManager::get<AssetClient>()->createUpload(dataByteArray, extension);
|
|
QObject::connect(upload, &AssetUpload::finished, this, [callback, extension](AssetUpload* upload, const QString& hash) mutable {
|
|
if (callback.isFunction()) {
|
|
QString url = "atp://" + hash + "." + extension;
|
|
QScriptValueList args { url };
|
|
callback.call(QScriptValue(), args);
|
|
}
|
|
});
|
|
|
|
// start the upload now
|
|
upload->start();
|
|
}
|
|
|
|
void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callback) {
|
|
const QString ATP_SCHEME { "atp://" };
|
|
|
|
if (!urlString.startsWith(ATP_SCHEME)) {
|
|
return;
|
|
}
|
|
|
|
// Make request to atp
|
|
auto path = urlString.right(urlString.length() - ATP_SCHEME.length());
|
|
auto parts = path.split(".", QString::SkipEmptyParts);
|
|
auto hash = parts.length() > 0 ? parts[0] : "";
|
|
auto extension = parts.length() > 1 ? parts[1] : "";
|
|
|
|
if (hash.length() != SHA256_HASH_HEX_LENGTH) {
|
|
return;
|
|
}
|
|
|
|
auto assetClient = DependencyManager::get<AssetClient>();
|
|
auto assetRequest = assetClient->createRequest(hash, extension);
|
|
|
|
if (!assetRequest) {
|
|
return;
|
|
}
|
|
|
|
_pendingRequests << assetRequest;
|
|
|
|
connect(assetRequest, &AssetRequest::finished, [this, callback](AssetRequest* request) mutable {
|
|
Q_ASSERT(request->getState() == AssetRequest::Finished);
|
|
|
|
if (request->getError() == AssetRequest::Error::NoError) {
|
|
if (callback.isFunction()) {
|
|
QString data = QString::fromUtf8(request->getData());
|
|
QScriptValueList args { data };
|
|
callback.call(QScriptValue(), args);
|
|
}
|
|
}
|
|
|
|
request->deleteLater();
|
|
_pendingRequests.remove(request);
|
|
});
|
|
|
|
assetRequest->start();
|
|
}
|
|
|
|
|
|
|