mirror of
https://github.com/overte-org/overte.git
synced 2025-08-05 01:30:18 +02:00
Add get/set mapping request
This commit is contained in:
parent
9148db2612
commit
4e7b63c72e
1 changed files with 149 additions and 78 deletions
|
@ -30,6 +30,43 @@
|
|||
|
||||
MessageID AssetClient::_currentID = 0;
|
||||
|
||||
GetMappingRequest::GetMappingRequest(AssetPath path) : _path(path) {
|
||||
};
|
||||
|
||||
void GetMappingRequest::start() {
|
||||
if (QThread::currentThread() != thread()) {
|
||||
QMetaObject::invokeMethod(this, "start", Qt::AutoConnection);
|
||||
return;
|
||||
}
|
||||
|
||||
auto assetClient = DependencyManager::get<AssetClient>();
|
||||
assetClient->getAssetMapping(_path, [this](bool responseReceived, AssetServerError error, QSharedPointer<ReceivedMessage> message) {
|
||||
// read message
|
||||
_error = error;
|
||||
if (!error) {
|
||||
//_hash = message->read(SHA256_HASH_HEX_LENGTH);
|
||||
_hash = message->readString();
|
||||
}
|
||||
emit finished(this);
|
||||
});
|
||||
};
|
||||
SetMappingRequest::SetMappingRequest(AssetPath path, AssetHash hash) : _path(path), _hash(hash) {
|
||||
};
|
||||
|
||||
void SetMappingRequest::start() {
|
||||
if (QThread::currentThread() != thread()) {
|
||||
QMetaObject::invokeMethod(this, "start", Qt::AutoConnection);
|
||||
return;
|
||||
}
|
||||
|
||||
auto assetClient = DependencyManager::get<AssetClient>();
|
||||
assetClient->setAssetMapping(_path, _hash, [this](bool responseReceived, AssetServerError error, QSharedPointer<ReceivedMessage> message) {
|
||||
// read message
|
||||
_error = error;
|
||||
emit finished(this);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
AssetClient::AssetClient() {
|
||||
|
||||
|
@ -39,7 +76,7 @@ AssetClient::AssetClient() {
|
|||
|
||||
auto nodeList = DependencyManager::get<NodeList>();
|
||||
auto& packetReceiver = nodeList->getPacketReceiver();
|
||||
packetReceiver.registerListener(PacketType::AssetMappingOperationReply, this, "handleAssetMappingReply");
|
||||
packetReceiver.registerListener(PacketType::AssetMappingOperationReply, this, "handleAssetMappingOperationReply");
|
||||
packetReceiver.registerListener(PacketType::AssetGetInfoReply, this, "handleAssetGetInfoReply");
|
||||
packetReceiver.registerListener(PacketType::AssetGetReply, this, "handleAssetGetReply", true);
|
||||
packetReceiver.registerListener(PacketType::AssetUploadReply, this, "handleAssetUploadReply");
|
||||
|
@ -98,6 +135,33 @@ void AssetClient::clearCache() {
|
|||
}
|
||||
}
|
||||
|
||||
void AssetClient::handleAssetMappingOperationReply(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
|
||||
MessageID messageID;
|
||||
message->readPrimitive(&messageID);
|
||||
|
||||
AssetServerError error;
|
||||
message->readPrimitive(&error);
|
||||
|
||||
// Check if we have any pending requests for this node
|
||||
auto messageMapIt = _pendingMappingRequests.find(senderNode);
|
||||
if (messageMapIt != _pendingMappingRequests.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, message);
|
||||
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 haveAssetServer() {
|
||||
auto nodeList = DependencyManager::get<NodeList>();
|
||||
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
|
||||
|
@ -111,22 +175,15 @@ bool haveAssetServer() {
|
|||
return true;
|
||||
}
|
||||
|
||||
AssetRequest* AssetClient::createRequest(const QUrl& url) {
|
||||
|
||||
auto parts = _url.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) {
|
||||
_result = InvalidURL;
|
||||
_state = Finished;
|
||||
|
||||
emit finished();
|
||||
return;
|
||||
}
|
||||
|
||||
GetMappingRequest* AssetClient::createGetMappingRequest(const AssetPath& path) {
|
||||
return new GetMappingRequest(path);
|
||||
}
|
||||
|
||||
SetMappingRequest* AssetClient::createSetMappingRequest(const AssetPath& path, const AssetHash& hash) {
|
||||
return new SetMappingRequest(path, hash);
|
||||
}
|
||||
|
||||
AssetRequest* AssetClient::createRequest(const AssetHash& hash, const QString& extension) {
|
||||
if (hash.length() != SHA256_HASH_HEX_LENGTH) {
|
||||
qCWarning(asset_client) << "Invalid hash size";
|
||||
return nullptr;
|
||||
|
@ -144,8 +201,6 @@ AssetRequest* AssetClient::createRequest(const QUrl& url) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
AssetUpload* AssetClient::createUpload(const QString& filename) {
|
||||
|
||||
if (haveAssetServer()) {
|
||||
|
@ -171,34 +226,34 @@ AssetUpload* AssetClient::createUpload(const QByteArray& data, const QString& ex
|
|||
}
|
||||
}
|
||||
|
||||
bool AssetClient::getAssetMapping(const QString& path, GetMappingCallback callback) {
|
||||
auto nodeList = DependencyManager::get<NodeList>();
|
||||
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
|
||||
|
||||
if (assetServer) {
|
||||
auto messageID = ++_currentID;
|
||||
|
||||
auto payload = path.toLatin1();
|
||||
auto payloadSize = sizeof(messageID) + payload.size();
|
||||
auto packet = NLPacket::create(PacketType::AssetGetMapping, payloadSize, true);
|
||||
|
||||
qCDebug(asset_client) << "Requesting mapping for" << path << "from asset-server.";
|
||||
|
||||
packet->writePrimitive(messageID);
|
||||
|
||||
auto bytesWritten = packet->write(payload);
|
||||
Q_ASSERT(bytesWritten == payload.size());
|
||||
|
||||
nodeList->sendPacket(std::move(packet), *assetServer);
|
||||
|
||||
_pendingMappingRequests[assetServer][messageID] = callback;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//bool AssetClient::setAssetMapping(const QString& path, MappingOperationCallback callback) {
|
||||
// auto nodeList = DependencyManager::get<NodeList>();
|
||||
// SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
|
||||
//
|
||||
// if (assetServer) {
|
||||
// auto messageID = ++_currentID;
|
||||
//
|
||||
// auto payload = path.toLatin1();
|
||||
// auto payloadSize = sizeof(messageID) + payload.size();
|
||||
// auto packet = NLPacket::create(PacketType::AssetMappingOperation, payloadSize, true);
|
||||
//
|
||||
// qCDebug(asset_client) << "Requesting mapping for" << path << "from asset-server.";
|
||||
//
|
||||
// packet->writePrimitive(messageID);
|
||||
//
|
||||
// auto bytesWritten = packet->write(payload);
|
||||
// Q_ASSERT(bytesWritten == payload.size());
|
||||
//
|
||||
// nodeList->sendPacket(std::move(packet), *assetServer);
|
||||
//
|
||||
// _pendingMappingRequests[assetServer][messageID] = callback;
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// return false;
|
||||
//}
|
||||
//
|
||||
bool AssetClient::getAsset(const QString& hash, const QString& extension, DataOffset start, DataOffset end,
|
||||
ReceivedAssetCallback callback, ProgressCallback progressCallback) {
|
||||
if (hash.length() != SHA256_HASH_HEX_LENGTH) {
|
||||
|
@ -264,39 +319,6 @@ bool AssetClient::getAssetInfo(const QString& hash, const QString& extension, Ge
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
void AssetClient::handleAssetGetMappingReply(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
|
||||
MessageID messageID;
|
||||
message->readPrimitive(&messageID);
|
||||
|
||||
AssetServerError error;
|
||||
message->readPrimitive(&error);
|
||||
|
||||
QString assetHash;
|
||||
if (error == AssetServerError::NoError) {
|
||||
assetHash = message->read(SHA256_HASH_LENGTH);
|
||||
}
|
||||
|
||||
// Check if we have any pending requests for this node
|
||||
auto messageMapIt = _pendingMappingRequests.find(senderNode);
|
||||
if (messageMapIt != _pendingMappingRequests.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, assetHash);
|
||||
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::handleAssetGetInfoReply(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
|
||||
MessageID messageID;
|
||||
message->readPrimitive(&messageID);
|
||||
|
@ -382,6 +404,55 @@ void AssetClient::handleAssetGetReply(QSharedPointer<ReceivedMessage> message, S
|
|||
}
|
||||
}
|
||||
|
||||
bool AssetClient::getAssetMapping(const AssetHash& hash, MappingOperationCallback callback) {
|
||||
auto nodeList = DependencyManager::get<NodeList>();
|
||||
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
|
||||
|
||||
if (assetServer) {
|
||||
auto packetList = NLPacketList::create(PacketType::AssetMappingOperation, QByteArray(), true, true);
|
||||
|
||||
auto messageID = ++_currentID;
|
||||
packetList->writePrimitive(messageID);
|
||||
|
||||
packetList->writePrimitive(AssetMappingOperationType::Get);
|
||||
|
||||
packetList->writeString(hash.toUtf8());
|
||||
|
||||
nodeList->sendPacketList(std::move(packetList), *assetServer);
|
||||
|
||||
_pendingMappingRequests[assetServer][messageID] = callback;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AssetClient::setAssetMapping(const QString& path, const AssetHash& hash, MappingOperationCallback callback) {
|
||||
auto nodeList = DependencyManager::get<NodeList>();
|
||||
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
|
||||
|
||||
if (assetServer) {
|
||||
auto packetList = NLPacketList::create(PacketType::AssetMappingOperation, QByteArray(), true, true);
|
||||
|
||||
auto messageID = ++_currentID;
|
||||
packetList->writePrimitive(messageID);
|
||||
|
||||
packetList->writePrimitive(AssetMappingOperationType::Set);
|
||||
|
||||
packetList->writeString(path.toUtf8());
|
||||
packetList->writeString(hash.toUtf8());
|
||||
|
||||
nodeList->sendPacketList(std::move(packetList), *assetServer);
|
||||
|
||||
_pendingMappingRequests[assetServer][messageID] = callback;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AssetClient::uploadAsset(const QByteArray& data, const QString& extension, UploadResultCallback callback) {
|
||||
auto nodeList = DependencyManager::get<NodeList>();
|
||||
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
|
||||
|
|
Loading…
Reference in a new issue