16 #ifndef ACSDKDAVSCLIENT_DAVSHANDLER_H_ 17 #define ACSDKDAVSCLIENT_DAVSHANDLER_H_ 35 namespace acsdkAssets {
40 ,
public std::enable_shared_from_this<DavsHandler> {
58 static std::shared_ptr<DavsHandler>
create(
59 std::shared_ptr<commonInterfaces::DavsRequest> artifactRequest,
60 const std::shared_ptr<davsInterfaces::DavsDownloadCallbackInterface>& downloadRequester,
61 const std::shared_ptr<davsInterfaces::DavsCheckCallbackInterface>& checkRequester,
63 std::chrono::milliseconds baseBackOffTime,
64 std::chrono::milliseconds maxBackOffTime,
65 std::shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::AuthDelegateInterface> authDelegate,
66 std::shared_ptr<davsInterfaces::DavsEndpointHandlerInterface> davsEndpointHandler,
67 std::shared_ptr<alexaClientSDK::avsCommon::utils::power::PowerResource> powerResource =
nullptr,
68 std::chrono::seconds forcedUpdateInterval = std::chrono::seconds(0));
78 return !m_checkRequester.expired() && !m_downloadRequester.expired();
97 std::lock_guard<std::mutex> lock(m_eventMutex);
98 m_updateEnabled = enable;
107 return m_updateEnabled;
115 return m_artifactRequest;
122 m_firstBackOffTime = firstBackOffTime;
130 std::chrono::milliseconds
getBackOffTime(std::chrono::milliseconds prevBackOffTime);
149 std::shared_ptr<commonInterfaces::DavsRequest> artifactRequest,
150 const std::shared_ptr<davsInterfaces::DavsDownloadCallbackInterface>& downloadRequester,
151 const std::shared_ptr<davsInterfaces::DavsCheckCallbackInterface>& checkRequester,
153 std::chrono::milliseconds baseBackOffTime,
154 std::chrono::milliseconds maxBackOffTime,
155 std::shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::AuthDelegateInterface> authDelegate,
156 std::shared_ptr<davsInterfaces::DavsEndpointHandlerInterface> davsEndpointHandler,
157 std::shared_ptr<alexaClientSDK::avsCommon::utils::power::PowerResource> powerResource,
158 std::chrono::seconds forcedUpdateInterval);
164 void runner(
bool isUserInitiated);
183 const std::shared_ptr<commonInterfaces::VendableArtifact>& artifact,
184 bool isUserInitiated,
200 void downloadWithRetryLoop(
201 const std::shared_ptr<commonInterfaces::VendableArtifact>& artifact,
202 bool isUserInitiated);
211 bool waitForRetry(std::chrono::milliseconds& waitTime);
218 bool waitForNetworkConnection();
223 void handleUpdateLocked();
229 return m_workingDirectory +
"/" + m_artifactRequest->getSummary();
232 inline std::shared_ptr<davsInterfaces::DavsCheckCallbackInterface> getChecker() {
233 auto checker = m_checkRequester.lock();
234 if (checker ==
nullptr) {
236 .
m(
"Check requester is no longer available")
237 .d(
"request", m_artifactRequest->getSummary()));
244 inline std::shared_ptr<davsInterfaces::DavsDownloadCallbackInterface> getDownloader() {
245 auto downloader = m_downloadRequester.lock();
246 if (downloader ==
nullptr) {
248 .
m(
"Download requester is no longer available")
249 .d(
"request", m_artifactRequest->getSummary()));
257 auto checker = getChecker();
258 if (checker ==
nullptr) {
263 .d(
"request", m_artifactRequest->getSummary()));
264 s_metrics().addZeroCounter(
"downloadCheckSuccess").addCounter(
METRIC_PREFIX_ERROR(
"downloadCheckFailed"));
265 checker->onCheckFailure(resultCode);
268 inline bool sendCheckIfOkToDownload(
269 const std::shared_ptr<commonInterfaces::VendableArtifact>& artifact,
270 size_t spaceNeeded) {
271 auto checker = getChecker();
272 if (checker ==
nullptr) {
275 s_metrics().addCounter(
"downloadCheckSuccess");
276 if (!checker->checkIfOkToDownload(artifact, spaceNeeded)) {
278 .
m(
"Requester rejected download")
279 .d(
"request", m_artifactRequest->getSummary()));
280 s_metrics().addCounter(
"downloadRejected");
286 inline bool sendOnStartAndCheckIfAvailable() {
287 auto downloader = getDownloader();
288 if (downloader ==
nullptr) {
292 .
m(
"Download started")
293 .d(
"request", m_artifactRequest->getSummary()));
294 downloader->onStart();
299 auto downloader = getDownloader();
300 if (downloader ==
nullptr) {
304 .
m(
"Download utterly failed")
305 .d(
"request", m_artifactRequest->getSummary()));
306 s_metrics().addZeroCounter(
"downloadSuccess").addCounter(
METRIC_PREFIX_ERROR(
"downloadFailed"));
307 downloader->onDownloadFailure(resultCode);
310 inline void sendOnArtifactDownloaded(
311 const std::shared_ptr<commonInterfaces::VendableArtifact>& artifact,
313 auto downloader = m_downloadRequester.lock();
314 if (downloader ==
nullptr) {
318 .
m(
"Download succeeded")
319 .d(
"request", m_artifactRequest->getSummary()));
320 s_metrics().addCounter(
"downloadSuccess");
321 downloader->onArtifactDownloaded(artifact, path);
326 bool onProgressUpdate(
long dlTotal,
long dlNow,
long ulTotal,
long ulNow)
override;
329 static const std::function<common::AmdMetricsWrapper()> s_metrics;
331 const std::shared_ptr<commonInterfaces::DavsRequest> m_artifactRequest;
332 const std::weak_ptr<davsInterfaces::DavsDownloadCallbackInterface> m_downloadRequester;
333 const std::weak_ptr<davsInterfaces::DavsCheckCallbackInterface> m_checkRequester;
337 const std::chrono::milliseconds m_baseBackOffTime;
338 const std::chrono::milliseconds m_maxBackOffTime;
340 const std::shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::AuthDelegateInterface> m_authDelegate;
342 const std::shared_ptr<davsInterfaces::DavsEndpointHandlerInterface> m_davsEndpointHandler;
344 std::shared_ptr<alexaClientSDK::avsCommon::utils::power::PowerResource> m_powerResource;
346 const std::chrono::seconds m_forcedUpdateInterval;
347 std::chrono::milliseconds m_firstBackOffTime;
350 std::mutex m_eventMutex;
351 std::condition_variable m_eventTrigger;
353 std::atomic_flag m_running;
354 std::future<void> m_taskFuture;
357 bool m_updateEnabled;
361 bool m_networkConnected;
371 #endif // ACSDKDAVSCLIENT_DAVSHANDLER_H_
void setFirstBackOff(std::chrono::milliseconds firstBackOffTime)
Definition: DavsHandler.h:121
Definition: DavsHandler.h:38
void setConnectionState(bool connected)
::std::string string
Definition: gtest-port.h:1097
Definition: CurlProgressCallbackInterface.h:23
void setThrottled(bool throttle)
std::chrono::system_clock::time_point TimeEpoch
Definition: VendableArtifact.h:29
std::mutex m
Definition: AlexaPresentationTest.cpp:91
Timer to schedule task for delayed and periodic execution.
Definition: Timer.h:39
std::shared_ptr< commonInterfaces::DavsRequest > getDavsRequest()
Definition: DavsHandler.h:114
static std::shared_ptr< DavsHandler > create(std::shared_ptr< commonInterfaces::DavsRequest > artifactRequest, const std::shared_ptr< davsInterfaces::DavsDownloadCallbackInterface > &downloadRequester, const std::shared_ptr< davsInterfaces::DavsCheckCallbackInterface > &checkRequester, std::string workingDirectory, std::chrono::milliseconds baseBackOffTime, std::chrono::milliseconds maxBackOffTime, std::shared_ptr< alexaClientSDK::avsCommon::sdkInterfaces::AuthDelegateInterface > authDelegate, std::shared_ptr< davsInterfaces::DavsEndpointHandlerInterface > davsEndpointHandler, std::shared_ptr< alexaClientSDK::avsCommon::utils::power::PowerResource > powerResource=nullptr, std::chrono::seconds forcedUpdateInterval=std::chrono::seconds(0))
#define ACSDK_ERROR(entry)
Definition: Logger.h:481
#define ACSDK_WARN(entry)
Definition: Logger.h:472
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
std::chrono::milliseconds getBackOffTime(std::chrono::milliseconds prevBackOffTime)
void requestAndDownload(bool isUserInitiated)
#define ACSDK_INFO(entry)
Definition: Logger.h:463
ResultCode
Definition: ResultCode.h:24
#define METRIC_PREFIX_ERROR(str)
Definition: AmdMetricWrapper.h:25
bool isRelevant()
Definition: DavsHandler.h:77
static std::string parseFileFromLink(const std::string &url, const std::string &defaultValue)
void enableUpdate(bool enable)
Definition: DavsHandler.h:96
bool isUpdateEnabled()
Definition: DavsHandler.h:106
LogEntry is used to compile the log entry text to log via Logger.
Definition: LogEntry.h:33