AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
CachingDownloadManager.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_LIBIPCSERVERSAMPLEAPP_INCLUDE_IPCSERVERSAMPLEAPP_CACHINGDOWNLOADMANAGER_H_
17 #define ALEXA_CLIENT_SDK_LIBIPCSERVERSAMPLEAPP_INCLUDE_IPCSERVERSAMPLEAPP_CACHINGDOWNLOADMANAGER_H_
18 
19 #include <cstdint>
20 #include <memory>
21 
25 
27 
28 namespace alexaClientSDK {
29 namespace sampleApplications {
30 namespace ipcServerSampleApp {
31 
33 public:
34  class Observer {
35  public:
36  Observer() = default;
37  virtual ~Observer() = default;
38 
42  virtual void onDownloadStarted(){};
43 
47  virtual void onDownloadComplete(){};
48 
52  virtual void onDownloadFailed(){};
53 
57  virtual void onCacheHit(){};
58 
65  virtual void onBytesRead(uint64_t numberOfBytes){};
66  };
67 
77  std::shared_ptr<avsCommon::sdkInterfaces::HTTPContentFetcherInterfaceFactoryInterface>
78  httpContentFetcherInterfaceFactoryInterface,
79  unsigned long cachePeriodInSeconds,
80  unsigned long maxCacheSize,
81  std::shared_ptr<avsCommon::sdkInterfaces::storage::MiscStorageInterface> miscStorage,
82  const std::shared_ptr<registrationManager::CustomerDataManagerInterface> customerDataManager);
83 
91  std::string retrieveContent(const std::string& source, std::shared_ptr<Observer> observer = nullptr);
92 
96  class CachedContent {
97  public:
101  std::chrono::system_clock::time_point importTime;
106 
107  CachedContent() = default;
108 
115  CachedContent(std::chrono::system_clock::time_point importTime, const std::string& content);
116  };
117 
118 private:
125  std::string downloadFromSource(const std::string& source, std::shared_ptr<Observer> observer);
129  void cleanUpCache();
135  void writeToStorage(std::string source, CachedContent content);
136 
139  void clearData() override;
141 
146  void removeFromStorage(std::string source);
150  std::shared_ptr<avsCommon::sdkInterfaces::HTTPContentFetcherInterfaceFactoryInterface> m_contentFetcherFactory;
154  std::chrono::duration<double> m_cachePeriod;
158  unsigned long m_maxCacheSize;
162  std::unordered_map<std::string, CachingDownloadManager::CachedContent> cachedContentMap;
166  std::mutex cachedContentMapMutex;
170  std::shared_ptr<avsCommon::sdkInterfaces::storage::MiscStorageInterface> m_miscStorage;
175 };
176 
181  auto time = std::chrono::duration_cast<std::chrono::seconds>(content.importTime.time_since_epoch()).count();
182  return std::to_string(time) + delimiter + content.content;
183 }
184 
185 } // namespace ipcServerSampleApp
186 } // namespace sampleApplications
187 } // namespace alexaClientSDK
188 
189 #endif // ALEXA_CLIENT_SDK_LIBIPCSERVERSAMPLEAPP_INCLUDE_IPCSERVERSAMPLEAPP_CACHINGDOWNLOADMANAGER_H_
CachingDownloadManager(std::shared_ptr< avsCommon::sdkInterfaces::HTTPContentFetcherInterfaceFactoryInterface > httpContentFetcherInterfaceFactoryInterface, unsigned long cachePeriodInSeconds, unsigned long maxCacheSize, std::shared_ptr< avsCommon::sdkInterfaces::storage::MiscStorageInterface > miscStorage, const std::shared_ptr< registrationManager::CustomerDataManagerInterface > customerDataManager)
int * count
Definition: gmock_stress_test.cc:176
::std::string string
Definition: gtest-port.h:1097
Single-thread executor implementation.
Definition: Executor.h:45
virtual void onDownloadComplete()
Definition: CachingDownloadManager.h:47
std::string cachedContentToString(CachingDownloadManager::CachedContent content, std::string delimiter)
Definition: CachingDownloadManager.h:180
std::chrono::system_clock::time_point importTime
Definition: CachingDownloadManager.h:101
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
std::string retrieveContent(const std::string &source, std::shared_ptr< Observer > observer=nullptr)
virtual void onBytesRead(uint64_t numberOfBytes)
Definition: CachingDownloadManager.h:65

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0