Overte C++ Documentation
AssetClient.h
1 //
2 // AssetClient.h
3 // libraries/networking/src
4 //
5 // Created by Ryan Huffman on 2015/07/21
6 // Copyright 2015 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_AssetClient_h
13 #define hifi_AssetClient_h
14 
15 #include <QStandardItemModel>
16 #include <QtQml/QJSEngine>
17 #include <QString>
18 #include <QtCore/QSharedPointer>
19 
20 #include <map>
21 
22 #include <DependencyManager.h>
23 #include <shared/MiniPromises.h>
24 
25 #include "AssetUtils.h"
26 #include "ByteRange.h"
27 #include "ClientServerUtils.h"
28 #include "LimitedNodeList.h"
29 #include "Node.h"
30 #include "ReceivedMessage.h"
31 
32 class GetMappingRequest;
33 class SetMappingRequest;
34 class GetAllMappingsRequest;
35 class DeleteMappingsRequest;
36 class RenameMappingRequest;
37 class SetBakingEnabledRequest;
38 class AssetRequest;
39 class AssetUpload;
40 
41 struct AssetInfo {
42  QString hash;
43  int64_t size;
44 };
45 
46 using MappingOperationCallback = std::function<void(bool responseReceived, AssetUtils::AssetServerError serverError, QSharedPointer<ReceivedMessage> message)>;
47 using ReceivedAssetCallback = std::function<void(bool responseReceived, AssetUtils::AssetServerError serverError, const QByteArray& data)>;
48 using GetInfoCallback = std::function<void(bool responseReceived, AssetUtils::AssetServerError serverError, AssetInfo info)>;
49 using UploadResultCallback = std::function<void(bool responseReceived, AssetUtils::AssetServerError serverError, const QString& hash)>;
50 using ProgressCallback = std::function<void(qint64 totalReceived, qint64 total)>;
51 
52 class AssetClient : public QObject, public Dependency {
53  Q_OBJECT
54 public:
55  AssetClient();
56 
57  Q_INVOKABLE GetMappingRequest* createGetMappingRequest(const AssetUtils::AssetPath& path);
58  Q_INVOKABLE GetAllMappingsRequest* createGetAllMappingsRequest();
59  Q_INVOKABLE DeleteMappingsRequest* createDeleteMappingsRequest(const AssetUtils::AssetPathList& paths);
60  Q_INVOKABLE SetMappingRequest* createSetMappingRequest(const AssetUtils::AssetPath& path, const AssetUtils::AssetHash& hash);
61  Q_INVOKABLE RenameMappingRequest* createRenameMappingRequest(const AssetUtils::AssetPath& oldPath, const AssetUtils::AssetPath& newPath);
62  Q_INVOKABLE SetBakingEnabledRequest* createSetBakingEnabledRequest(const AssetUtils::AssetPathList& path, bool enabled);
63  Q_INVOKABLE AssetRequest* createRequest(const AssetUtils::AssetHash& hash, const ByteRange& byteRange = ByteRange());
64  Q_INVOKABLE AssetUpload* createUpload(const QString& filename);
65  Q_INVOKABLE AssetUpload* createUpload(const QByteArray& data);
66 
67 public slots:
68  void initCaching();
69 
70  void cacheInfoRequest(QObject* reciever, QString slot);
71  MiniPromise::Promise cacheInfoRequestAsync(MiniPromise::Promise deferred = nullptr);
72  MiniPromise::Promise queryCacheMetaAsync(const QUrl& url, MiniPromise::Promise deferred = nullptr);
73  MiniPromise::Promise loadFromCacheAsync(const QUrl& url, MiniPromise::Promise deferred = nullptr);
74  MiniPromise::Promise saveToCacheAsync(const QUrl& url, const QByteArray& data, const QVariantMap& metadata = QVariantMap(), MiniPromise::Promise deferred = nullptr);
75  void clearCache();
76 
77 private slots:
78  void handleAssetMappingOperationReply(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
79  void handleAssetGetInfoReply(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
80  void handleAssetGetReply(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
81  void handleAssetUploadReply(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
82 
83  void handleNodeKilled(SharedNodePointer node);
84  void handleNodeClientConnectionReset(SharedNodePointer node);
85 
86 private:
87  MessageID getAssetMapping(const AssetUtils::AssetHash& hash, MappingOperationCallback callback);
88  MessageID getAllAssetMappings(MappingOperationCallback callback);
89  MessageID setAssetMapping(const QString& path, const AssetUtils::AssetHash& hash, MappingOperationCallback callback);
90  MessageID deleteAssetMappings(const AssetUtils::AssetPathList& paths, MappingOperationCallback callback);
91  MessageID renameAssetMapping(const AssetUtils::AssetPath& oldPath, const AssetUtils::AssetPath& newPath, MappingOperationCallback callback);
92  MessageID setBakingEnabled(const AssetUtils::AssetPathList& paths, bool enabled, MappingOperationCallback callback);
93 
94  MessageID getAssetInfo(const QString& hash, GetInfoCallback callback);
95  MessageID getAsset(const QString& hash, AssetUtils::DataOffset start, AssetUtils::DataOffset end,
96  ReceivedAssetCallback callback, ProgressCallback progressCallback);
97  MessageID uploadAsset(const QByteArray& data, UploadResultCallback callback);
98 
99  bool cancelMappingRequest(MessageID id);
100  bool cancelGetAssetInfoRequest(MessageID id);
101  bool cancelGetAssetRequest(MessageID id);
102  bool cancelUploadAssetRequest(MessageID id);
103 
104  void handleProgressCallback(const QWeakPointer<Node>& node, MessageID messageID, qint64 size, AssetUtils::DataOffset length);
105  void handleCompleteCallback(const QWeakPointer<Node>& node, MessageID messageID, AssetUtils::DataOffset length);
106 
107  void forceFailureOfPendingRequests(SharedNodePointer node);
108 
109  struct GetAssetRequestData {
110  QSharedPointer<ReceivedMessage> message;
111  ReceivedAssetCallback completeCallback;
112  ProgressCallback progressCallback;
113  };
114 
115  static MessageID _currentID;
116  std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, MappingOperationCallback>> _pendingMappingRequests;
117  std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, GetAssetRequestData>> _pendingRequests;
118  std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, GetInfoCallback>> _pendingInfoRequests;
119  std::unordered_map<SharedNodePointer, std::unordered_map<MessageID, UploadResultCallback>> _pendingUploads;
120 
121  QString _cacheDir;
122 
123  friend class AssetRequest;
124  friend class AssetUpload;
125  friend class MappingRequest;
126  friend class GetMappingRequest;
127  friend class GetAllMappingsRequest;
128  friend class SetMappingRequest;
129  friend class DeleteMappingsRequest;
130  friend class RenameMappingRequest;
131  friend class SetBakingEnabledRequest;
132 };
133 
134 #endif