Overte C++ Documentation
ResourceRequest.h
1 //
2 // ResourceRequest.h
3 // libraries/networking/src
4 //
5 // Created by Ryan Huffman on 2015/07/23
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_ResourceRequest_h
13 #define hifi_ResourceRequest_h
14 
15 #include <QObject>
16 #include <QUrl>
17 
18 #include <cstdint>
19 
20 #include "ByteRange.h"
21 
22 const QString STAT_ATP_REQUEST_STARTED = "StartedATPRequest";
23 const QString STAT_HTTP_REQUEST_STARTED = "StartedHTTPRequest";
24 const QString STAT_FILE_REQUEST_STARTED = "StartedFileRequest";
25 const QString STAT_ATP_REQUEST_SUCCESS = "SuccessfulATPRequest";
26 const QString STAT_HTTP_REQUEST_SUCCESS = "SuccessfulHTTPRequest";
27 const QString STAT_FILE_REQUEST_SUCCESS = "SuccessfulFileRequest";
28 const QString STAT_ATP_REQUEST_FAILED = "FailedATPRequest";
29 const QString STAT_HTTP_REQUEST_FAILED = "FailedHTTPRequest";
30 const QString STAT_FILE_REQUEST_FAILED = "FailedFileRequest";
31 const QString STAT_ATP_REQUEST_CACHE = "CacheATPRequest";
32 const QString STAT_HTTP_REQUEST_CACHE = "CacheHTTPRequest";
33 const QString STAT_ATP_MAPPING_REQUEST_STARTED = "StartedATPMappingRequest";
34 const QString STAT_ATP_MAPPING_REQUEST_FAILED = "FailedATPMappingRequest";
35 const QString STAT_ATP_MAPPING_REQUEST_SUCCESS = "SuccessfulATPMappingRequest";
36 const QString STAT_HTTP_RESOURCE_TOTAL_BYTES = "HTTPBytesDownloaded";
37 const QString STAT_ATP_RESOURCE_TOTAL_BYTES = "ATPBytesDownloaded";
38 const QString STAT_FILE_RESOURCE_TOTAL_BYTES = "FILEBytesDownloaded";
39 
40 class ResourceRequest : public QObject {
41  Q_OBJECT
42 public:
43  static const bool IS_OBSERVABLE = true;
44  static const bool IS_NOT_OBSERVABLE = false;
45 
46  ResourceRequest(
47  const QUrl& url,
48  const bool isObservable = IS_OBSERVABLE,
49  const qint64 callerId = -1,
50  const QString& extra = ""
51  ) : _url(url),
52  _isObservable(isObservable),
53  _callerId(callerId),
54  _extra(extra)
55  { }
56 
57  virtual ~ResourceRequest() = default;
58 
59  enum State {
60  NotStarted = 0,
61  InProgress,
62  Finished
63  };
64 
65  enum Result {
66  Success,
67  Error,
68  Timeout,
69  ServerUnavailable,
70  AccessDenied,
71  InvalidByteRange,
72  InvalidURL,
73  NotFound,
74  RedirectFail
75  };
76  Q_ENUM(Result)
77 
78  QByteArray getData() { return _data; }
79  State getState() const { return _state; }
80  Result getResult() const { return _result; }
81  QString getResultString() const;
82  QUrl getUrl() const { return _url; }
83  QUrl getRelativePathUrl() const { return _relativePathURL; }
84  bool loadedFromCache() const { return _loadedFromCache; }
85  bool getRangeRequestSuccessful() const { return _rangeRequestSuccessful; }
86  bool getTotalSizeOfResource() const { return _totalSizeOfResource; }
87  QString getWebMediaType() const { return _webMediaType; }
88  void setFailOnRedirect(bool failOnRedirect) { _failOnRedirect = failOnRedirect; }
89 
90  void setCacheEnabled(bool value) { _cacheEnabled = value; }
91  void setByteRange(ByteRange byteRange) { _byteRange = byteRange; }
92 
93  static QString toHttpDateString(uint64_t msecsSinceEpoch);
94 public slots:
95  void send();
96 
97 signals:
98  void progress(qint64 bytesReceived, qint64 bytesTotal);
99  void finished();
100 
101 protected:
102  virtual void doSend() = 0;
103  void recordBytesDownloadedInStats(const QString& statName, int64_t bytesReceived);
104 
105  QUrl _url;
106  QUrl _relativePathURL;
107  State _state { NotStarted };
108  Result _result;
109  QByteArray _data;
110  bool _failOnRedirect { false };
111  bool _cacheEnabled { true };
112  bool _loadedFromCache { false };
113  ByteRange _byteRange;
114  bool _rangeRequestSuccessful { false };
115  uint64_t _totalSizeOfResource { 0 };
116  QString _webMediaType;
117  int64_t _lastRecordedBytesDownloaded { 0 };
118  bool _isObservable;
119  qint64 _callerId;
120  QString _extra;
121 };
122 
123 #endif