make SendAssetTask be consistent with new UploadAssetTask

This commit is contained in:
Stephen Birarda 2015-08-31 15:17:40 -06:00
parent b9d1f39c65
commit dc7d7ef444
4 changed files with 61 additions and 56 deletions

View file

@ -125,39 +125,43 @@ void AssetServer::handleAssetGetInfo(QSharedPointer<NLPacket> packet, SharedNode
} }
void AssetServer::handleAssetGet(QSharedPointer<NLPacket> packet, SharedNodePointer senderNode) { void AssetServer::handleAssetGet(QSharedPointer<NLPacket> 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) { if (packet->getPayloadSize() < minSize) {
qDebug() << "ERROR bad file request"; qDebug() << "ERROR bad file request";
return; 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 // Queue task
QString filePath = _resourcesDirectory.filePath(QString(hexHash) + "." + QString(extension)); auto task = new SendAssetTask(packet, senderNode, _resourcesDirectory);
auto task = new SendAssetTask(messageID, assetHash, filePath, start, end, senderNode);
_taskPool.start(task); _taskPool.start(task);
} }
void AssetServer::handleAssetUpload(QSharedPointer<NLPacketList> packetList, SharedNodePointer senderNode) { void AssetServer::handleAssetUpload(QSharedPointer<NLPacketList> packetList, SharedNodePointer senderNode) {
qDebug() << "Starting an UploadAssetTask for upload from" << uuidStringWithoutCurlyBraces(senderNode->getUUID());
auto task = new UploadAssetTask(packetList, senderNode, _resourcesDirectory); if (senderNode->getCanRez()) {
_taskPool.start(task); 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>();
nodeList->sendPacket(std::move(permissionErrorPacket), *senderNode);
}
} }

View file

@ -18,23 +18,35 @@
#include <NLPacket.h> #include <NLPacket.h>
#include <NLPacketList.h> #include <NLPacketList.h>
#include <NodeList.h> #include <NodeList.h>
#include <udt/Packet.h>
#include "AssetUtils.h" #include "AssetUtils.h"
SendAssetTask::SendAssetTask(MessageID messageID, const QByteArray& assetHash, QString filePath, DataOffset start, DataOffset end, SendAssetTask::SendAssetTask(QSharedPointer<udt::Packet> packet, const SharedNodePointer& sendToNode, const QDir& resourcesDir) :
const SharedNodePointer& sendToNode) :
QRunnable(), QRunnable(),
_messageID(messageID), _packet(packet),
_assetHash(assetHash), _senderNode(sendToNode),
_filePath(filePath), _resourcesDir(resourcesDir)
_start(start),
_end(end),
_sendToNode(sendToNode)
{ {
} }
void SendAssetTask::run() { 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(); 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; qDebug() << "Starting task to send asset: " << hexHash << " for messageID " << _messageID;
auto replyPacketList = std::unique_ptr<NLPacketList>(new NLPacketList(PacketType::AssetGetReply, QByteArray(), true, true)); auto replyPacketList = std::unique_ptr<NLPacketList>(new NLPacketList(PacketType::AssetGetReply, QByteArray(), true, true));
@ -45,6 +57,8 @@ void SendAssetTask::run() {
if (_end <= _start) { if (_end <= _start) {
writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE); writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE);
} else { } else {
QString filePath = _resourcesDir.filePath(QString(hexHash) + "." + QString(extension));
QFile file { _filePath }; QFile file { _filePath };
if (file.open(QIODevice::ReadOnly)) { if (file.open(QIODevice::ReadOnly)) {
@ -67,5 +81,5 @@ void SendAssetTask::run() {
} }
auto nodeList = DependencyManager::get<NodeList>(); auto nodeList = DependencyManager::get<NodeList>();
nodeList->sendPacketList(std::move(replyPacketList), *_sendToNode); nodeList->sendPacketList(std::move(replyPacketList), *_senderNode);
} }

View file

@ -12,28 +12,34 @@
#ifndef hifi_SendAssetTask_h #ifndef hifi_SendAssetTask_h
#define hifi_SendAssetTask_h #define hifi_SendAssetTask_h
#include <QByteArray> #include <QtCore/QByteArray>
#include <QString> #include <QtCore/QSharedPointer>
#include <QRunnable> #include <QtCore/QString>
#include <QtCore/QRunnable>
#include "AssetUtils.h" #include "AssetUtils.h"
#include "AssetServer.h" #include "AssetServer.h"
#include "Node.h" #include "Node.h"
namespace udt {
class Packet;
}
class SendAssetTask : public QRunnable { class SendAssetTask : public QRunnable {
public: public:
SendAssetTask(MessageID messageID, const QByteArray& assetHash, QString filePath, DataOffset start, DataOffset end, SendAssetTask(QSharedPointer<udt::Packet> packet, const SharedNodePointer& sendToNode, const QDir& resourcesDir);
const SharedNodePointer& sendToNode);
void run(); void run();
private: private:
QSharedPointer<udt::Packet> _packet;
MessageID _messageID; MessageID _messageID;
QByteArray _assetHash; QByteArray _assetHash;
QString _filePath; QString _filePath;
DataOffset _start; DataOffset _start;
DataOffset _end; DataOffset _end;
SharedNodePointer _sendToNode; SharedNodePointer _senderNode;
QDir _resourcesDir;
}; };
#endif #endif

View file

@ -37,25 +37,6 @@ void UploadAssetTask::run() {
MessageID messageID; MessageID messageID;
buffer.read(reinterpret_cast<char*>(&messageID), sizeof(messageID)); buffer.read(reinterpret_cast<char*>(&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>();
nodeList->sendPacket(std::move(permissionErrorPacket), *_senderNode);
// return so we're not attempting to handle upload
return;
}
uint8_t extensionLength; uint8_t extensionLength;
buffer.read(reinterpret_cast<char*>(&extensionLength), sizeof(extensionLength)); buffer.read(reinterpret_cast<char*>(&extensionLength), sizeof(extensionLength));