Update atp mapping operations to use a single packet

This commit is contained in:
Ryan Huffman 2016-03-01 16:18:35 -08:00 committed by Stephen Birarda
parent 247ebb380b
commit 6b485cf81d
8 changed files with 101 additions and 2 deletions

View file

@ -42,6 +42,7 @@ AssetServer::AssetServer(ReceivedMessage& message) :
packetReceiver.registerListener(PacketType::AssetGet, this, "handleAssetGet"); packetReceiver.registerListener(PacketType::AssetGet, this, "handleAssetGet");
packetReceiver.registerListener(PacketType::AssetGetInfo, this, "handleAssetGetInfo"); packetReceiver.registerListener(PacketType::AssetGetInfo, this, "handleAssetGetInfo");
packetReceiver.registerListener(PacketType::AssetUpload, this, "handleAssetUpload"); packetReceiver.registerListener(PacketType::AssetUpload, this, "handleAssetUpload");
packetReceiver.registerListener(PacketType::AssetMappingOperation, this, "handleAssetMappingOpertation");
} }
void AssetServer::run() { void AssetServer::run() {
@ -161,6 +162,52 @@ void AssetServer::completeSetup() {
nodeList->addNodeTypeToInterestSet(NodeType::Agent); nodeList->addNodeTypeToInterestSet(NodeType::Agent);
} }
void AssetServer::handleAssetMappingOperation(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
MessageID messageID;
message->readPrimitive(&messageID);
AssetMappingOperationType operationType;
message->readPrimitive(&operationType);
auto replyPacket = NLPacket::create(PacketType::AssetMappingOperationReply);
replyPacket->writePrimitive(messageID);
switch (operationType) {
case AssetMappingOperationType::Get: {
QString assetPath = message->readString();
auto it = _fileMapping.find(assetPath);
if (it != _fileMapping.end()) {
auto assetHash = it->second;
qDebug() << "Found mapping for: " << assetPath << "=>" << assetHash;
replyPacket->writePrimitive(AssetServerError::NoError);
replyPacket->write(assetHash.toLatin1().toHex());
}
else {
qDebug() << "Mapping not found for: " << assetPath;
replyPacket->writePrimitive(AssetServerError::AssetNotFound);
}
}
case AssetMappingOperationType::Set: {
QString assetPath = message->readString();
auto assetHash = message->read(SHA256_HASH_LENGTH);
_fileMapping[assetPath] = assetHash;
replyPacket->writePrimitive(AssetServerError::NoError);
}
case AssetMappingOperationType::Delete: {
QString assetPath = message->readString();
bool removed = _fileMapping.erase(assetPath) > 0;
replyPacket->writePrimitive(AssetServerError::NoError);
}
}
auto nodeList = DependencyManager::get<NodeList>();
nodeList->sendPacket(std::move(replyPacket), *senderNode);
}
>>>>>>> db98e46... Update atp mapping operations to use a single packet
void AssetServer::handleAssetGetInfo(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) { void AssetServer::handleAssetGetInfo(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
QByteArray assetHash; QByteArray assetHash;
MessageID messageID; MessageID messageID;
@ -308,3 +355,15 @@ void AssetServer::sendStatsPacket() {
// send off the stats packets // send off the stats packets
ThreadedAssignment::addPacketStatsAndSendStatsPacket(serverStats); ThreadedAssignment::addPacketStatsAndSendStatsPacket(serverStats);
} }
AssetServer::Hash AssetServer::getMapping(Path path) {
return _fileMapping[path];
}
void AssetServer::setMapping(Path path, Hash hash) {
_fileMapping[path] = hash;
}
bool AssetServer::deleteMapping(Path path) {
return _fileMapping.erase(path) > 0;
}

View file

@ -34,11 +34,27 @@ private slots:
void handleAssetGetInfo(QSharedPointer<ReceivedMessage> packet, SharedNodePointer senderNode); void handleAssetGetInfo(QSharedPointer<ReceivedMessage> packet, SharedNodePointer senderNode);
void handleAssetGet(QSharedPointer<ReceivedMessage> packet, SharedNodePointer senderNode); void handleAssetGet(QSharedPointer<ReceivedMessage> packet, SharedNodePointer senderNode);
void handleAssetUpload(QSharedPointer<ReceivedMessage> packetList, SharedNodePointer senderNode); void handleAssetUpload(QSharedPointer<ReceivedMessage> packetList, SharedNodePointer senderNode);
void handleAssetMappingOperation(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
void sendStatsPacket(); void sendStatsPacket();
private: private:
using Path = QString;
using Hash = QString;
using Mapping = std::unordered_map<Path, Hash>;
/// Return the hash mapping for Path `path`
Hash getMapping(Path path);
/// Set the mapping for path to hash
void setMapping(Path path, Hash hash);
/// Delete mapping `path`. Return `true` if mapping existed, else `false`.
bool deleteMapping(Path path);
static void writeError(NLPacketList* packetList, AssetServerError error); static void writeError(NLPacketList* packetList, AssetServerError error);
Mapping _fileMapping;
QDir _resourcesDirectory; QDir _resourcesDirectory;
QThreadPool _taskPool; QThreadPool _taskPool;
}; };

View file

@ -39,6 +39,8 @@ AssetClient::AssetClient() {
auto nodeList = DependencyManager::get<NodeList>(); auto nodeList = DependencyManager::get<NodeList>();
auto& packetReceiver = nodeList->getPacketReceiver(); auto& packetReceiver = nodeList->getPacketReceiver();
packetReceiver.registerListener(PacketType::AssetMappingOperationReply, this, "handleAssetMappingReply");
packetReceiver.registerListener(PacketType::AssetGetInfoReply, this, "handleAssetGetInfoReply"); packetReceiver.registerListener(PacketType::AssetGetInfoReply, this, "handleAssetGetInfoReply");
packetReceiver.registerListener(PacketType::AssetGetReply, this, "handleAssetGetReply", true); packetReceiver.registerListener(PacketType::AssetGetReply, this, "handleAssetGetReply", true);
packetReceiver.registerListener(PacketType::AssetUploadReply, this, "handleAssetUploadReply"); packetReceiver.registerListener(PacketType::AssetUploadReply, this, "handleAssetUploadReply");
@ -471,4 +473,4 @@ void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callb
}); });
assetRequest->start(); assetRequest->start();
} }

View file

@ -34,6 +34,7 @@ struct AssetInfo {
}; };
using ReceivedAssetCallback = std::function<void(bool responseReceived, AssetServerError serverError, const QByteArray& data)>; using ReceivedAssetCallback = std::function<void(bool responseReceived, AssetServerError serverError, const QByteArray& data)>;
using MappingOperationCallback = std::function<void(bool responseReceived, AssetServerError serverError, const QString& hash)>;
using GetInfoCallback = std::function<void(bool responseReceived, AssetServerError serverError, AssetInfo info)>; using GetInfoCallback = std::function<void(bool responseReceived, AssetServerError serverError, AssetInfo info)>;
using UploadResultCallback = std::function<void(bool responseReceived, AssetServerError serverError, const QString& hash)>; using UploadResultCallback = std::function<void(bool responseReceived, AssetServerError serverError, const QString& hash)>;
using ProgressCallback = std::function<void(qint64 totalReceived, qint64 total)>; using ProgressCallback = std::function<void(qint64 totalReceived, qint64 total)>;
@ -62,6 +63,7 @@ private slots:
void handleNodeKilled(SharedNodePointer node); void handleNodeKilled(SharedNodePointer node);
private: private:
bool getAssetMapping(const QString& path, MappingOperationCallback callback);
bool getAssetInfo(const QString& hash, const QString& extension, GetInfoCallback callback); bool getAssetInfo(const QString& hash, const QString& extension, GetInfoCallback callback);
bool getAsset(const QString& hash, const QString& extension, DataOffset start, DataOffset end, bool getAsset(const QString& hash, const QString& extension, DataOffset start, DataOffset end,
ReceivedAssetCallback callback, ProgressCallback progressCallback); ReceivedAssetCallback callback, ProgressCallback progressCallback);
@ -73,6 +75,7 @@ private:
}; };
static MessageID _currentID; static MessageID _currentID;
std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, MappingOperationCallback>> _pendingMappingRequests;
std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, GetAssetCallbacks>> _pendingRequests; std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, GetAssetCallbacks>> _pendingRequests;
std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, GetInfoCallback>> _pendingInfoRequests; std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, GetInfoCallback>> _pendingInfoRequests;
std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, UploadResultCallback>> _pendingUploads; std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, UploadResultCallback>> _pendingUploads;

View file

@ -32,6 +32,12 @@ enum AssetServerError : uint8_t {
PermissionDenied PermissionDenied
}; };
enum AssetMappingOperationType : uint8_t {
Get = 0,
Set,
Delete
};
QUrl getATPUrl(const QString& hash, const QString& extension = QString()); QUrl getATPUrl(const QString& hash, const QString& extension = QString());
QByteArray hashData(const QByteArray& data); QByteArray hashData(const QByteArray& data);

View file

@ -107,6 +107,15 @@ QByteArray ReceivedMessage::readAll() {
return read(getBytesLeftToRead()); return read(getBytesLeftToRead());
} }
QString ReceivedMessage::readString() {
uint32_t size;
readPrimitive(&size);
Q_ASSERT(size <= _size - _position);
auto string = QString::fromUtf8(_data.constData(), size);
_position += size;
return string;
}
QByteArray ReceivedMessage::readWithoutCopy(qint64 size) { QByteArray ReceivedMessage::readWithoutCopy(qint64 size) {
QByteArray data { QByteArray::fromRawData(_data.constData() + _position, size) }; QByteArray data { QByteArray::fromRawData(_data.constData() + _position, size) };
_position += size; _position += size;

View file

@ -63,6 +63,8 @@ public:
QByteArray read(qint64 size); QByteArray read(qint64 size);
QByteArray readAll(); QByteArray readAll();
QString readString();
QByteArray readHead(qint64 size); QByteArray readHead(qint64 size);
// This will return a QByteArray referencing the underlying data _without_ refcounting that data. // This will return a QByteArray referencing the underlying data _without_ refcounting that data.

View file

@ -91,7 +91,9 @@ public:
MessagesData, MessagesData,
MessagesSubscribe, MessagesSubscribe,
MessagesUnsubscribe, MessagesUnsubscribe,
ICEServerHeartbeatDenied ICEServerHeartbeatDenied,
AssetMappingOperation,
AssetMappingOperationReply
}; };
}; };