From dc7d7ef4442bc0a4ec1b14b56d5e10d88da577df Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 31 Aug 2015 15:17:40 -0600 Subject: [PATCH] make SendAssetTask be consistent with new UploadAssetTask --- assignment-client/src/assets/AssetServer.cpp | 48 ++++++++++--------- .../src/assets/SendAssetTask.cpp | 32 +++++++++---- assignment-client/src/assets/SendAssetTask.h | 18 ++++--- .../src/assets/UploadAssetTask.cpp | 19 -------- 4 files changed, 61 insertions(+), 56 deletions(-) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index c046b39f66..62f1b877b5 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -125,39 +125,43 @@ void AssetServer::handleAssetGetInfo(QSharedPointer packet, SharedNode } void AssetServer::handleAssetGet(QSharedPointer packet, SharedNodePointer senderNode) { - MessageID messageID; - QByteArray assetHash; - uint8_t extensionLength; - DataOffset start; - DataOffset end; - auto minSize = qint64(sizeof(messageID) + SHA256_HASH_LENGTH + sizeof(extensionLength) + sizeof(start) + sizeof(end)); + auto minSize = qint64(sizeof(MessageID) + SHA256_HASH_LENGTH + sizeof(uint8_t) + sizeof(DataOffset) + sizeof(DataOffset)); + if (packet->getPayloadSize() < minSize) { qDebug() << "ERROR bad file request"; return; } - packet->readPrimitive(&messageID); - assetHash = packet->read(SHA256_HASH_LENGTH); - packet->readPrimitive(&extensionLength); - QByteArray extension = packet->read(extensionLength); - packet->readPrimitive(&start); - packet->readPrimitive(&end); - - QByteArray hexHash = assetHash.toHex(); - - qDebug() << "Received a request for the file (" << messageID << "): " << hexHash << " from " << start << " to " << end; - // Queue task - QString filePath = _resourcesDirectory.filePath(QString(hexHash) + "." + QString(extension)); - auto task = new SendAssetTask(messageID, assetHash, filePath, start, end, senderNode); + auto task = new SendAssetTask(packet, senderNode, _resourcesDirectory); _taskPool.start(task); } void AssetServer::handleAssetUpload(QSharedPointer packetList, SharedNodePointer senderNode) { - qDebug() << "Starting an UploadAssetTask for upload from" << uuidStringWithoutCurlyBraces(senderNode->getUUID()); - auto task = new UploadAssetTask(packetList, senderNode, _resourcesDirectory); - _taskPool.start(task); + if (senderNode->getCanRez()) { + qDebug() << "Starting an UploadAssetTask for upload from" << uuidStringWithoutCurlyBraces(senderNode->getUUID()); + + auto task = new UploadAssetTask(packetList, 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; + packetList->readPrimitive(&messageID); + + // write the message ID and a permission denied error + permissionErrorPacket->writePrimitive(messageID); + permissionErrorPacket->writePrimitive(AssetServerError::PERMISSION_DENIED); + + // send off the packet + auto nodeList = DependencyManager::get(); + nodeList->sendPacket(std::move(permissionErrorPacket), *senderNode); + } } diff --git a/assignment-client/src/assets/SendAssetTask.cpp b/assignment-client/src/assets/SendAssetTask.cpp index 91782b95ad..c4c7bf2503 100644 --- a/assignment-client/src/assets/SendAssetTask.cpp +++ b/assignment-client/src/assets/SendAssetTask.cpp @@ -18,23 +18,35 @@ #include #include #include +#include #include "AssetUtils.h" -SendAssetTask::SendAssetTask(MessageID messageID, const QByteArray& assetHash, QString filePath, DataOffset start, DataOffset end, - const SharedNodePointer& sendToNode) : +SendAssetTask::SendAssetTask(QSharedPointer packet, const SharedNodePointer& sendToNode, const QDir& resourcesDir) : QRunnable(), - _messageID(messageID), - _assetHash(assetHash), - _filePath(filePath), - _start(start), - _end(end), - _sendToNode(sendToNode) + _packet(packet), + _senderNode(sendToNode), + _resourcesDir(resourcesDir) { + } void SendAssetTask::run() { + MessageID messageID; + uint8_t extensionLength; + DataOffset start, end; + + _packet->readPrimitive(&messageID); + QByteArray assetHash = _packet->read(SHA256_HASH_LENGTH); + _packet->readPrimitive(&extensionLength); + QByteArray extension = _packet->read(extensionLength); + _packet->readPrimitive(&start); + _packet->readPrimitive(&end); + QString hexHash = _assetHash.toHex(); + + qDebug() << "Received a request for the file (" << messageID << "): " << hexHash << " from " << start << " to " << end; + qDebug() << "Starting task to send asset: " << hexHash << " for messageID " << _messageID; auto replyPacketList = std::unique_ptr(new NLPacketList(PacketType::AssetGetReply, QByteArray(), true, true)); @@ -45,6 +57,8 @@ void SendAssetTask::run() { if (_end <= _start) { writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE); } else { + QString filePath = _resourcesDir.filePath(QString(hexHash) + "." + QString(extension)); + QFile file { _filePath }; if (file.open(QIODevice::ReadOnly)) { @@ -67,5 +81,5 @@ void SendAssetTask::run() { } auto nodeList = DependencyManager::get(); - nodeList->sendPacketList(std::move(replyPacketList), *_sendToNode); + nodeList->sendPacketList(std::move(replyPacketList), *_senderNode); } diff --git a/assignment-client/src/assets/SendAssetTask.h b/assignment-client/src/assets/SendAssetTask.h index 477f42c1a3..1dc6c8dca2 100644 --- a/assignment-client/src/assets/SendAssetTask.h +++ b/assignment-client/src/assets/SendAssetTask.h @@ -12,28 +12,34 @@ #ifndef hifi_SendAssetTask_h #define hifi_SendAssetTask_h -#include -#include -#include +#include +#include +#include +#include #include "AssetUtils.h" #include "AssetServer.h" #include "Node.h" +namespace udt { + class Packet; +} + class SendAssetTask : public QRunnable { public: - SendAssetTask(MessageID messageID, const QByteArray& assetHash, QString filePath, DataOffset start, DataOffset end, - const SharedNodePointer& sendToNode); + SendAssetTask(QSharedPointer packet, const SharedNodePointer& sendToNode, const QDir& resourcesDir); void run(); private: + QSharedPointer _packet; MessageID _messageID; QByteArray _assetHash; QString _filePath; DataOffset _start; DataOffset _end; - SharedNodePointer _sendToNode; + SharedNodePointer _senderNode; + QDir _resourcesDir; }; #endif diff --git a/assignment-client/src/assets/UploadAssetTask.cpp b/assignment-client/src/assets/UploadAssetTask.cpp index 66fdc80e90..d4991f9554 100644 --- a/assignment-client/src/assets/UploadAssetTask.cpp +++ b/assignment-client/src/assets/UploadAssetTask.cpp @@ -37,25 +37,6 @@ void UploadAssetTask::run() { MessageID messageID; buffer.read(reinterpret_cast(&messageID), sizeof(messageID)); - if (!_senderNode->getCanRez()) { - // 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)); - - // write the message ID and a permission denied error - permissionErrorPacket->writePrimitive(messageID); - permissionErrorPacket->writePrimitive(AssetServerError::PERMISSION_DENIED); - - // send off the packet - auto nodeList = DependencyManager::get(); - nodeList->sendPacket(std::move(permissionErrorPacket), *_senderNode); - - // return so we're not attempting to handle upload - return; - } - uint8_t extensionLength; buffer.read(reinterpret_cast(&extensionLength), sizeof(extensionLength));