AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
ContextManager.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_CONTEXTMANAGER_INCLUDE_CONTEXTMANAGER_CONTEXTMANAGER_H_
17 #define ALEXA_CLIENT_SDK_CONTEXTMANAGER_INCLUDE_CONTEXTMANAGER_CONTEXTMANAGER_H_
18 
19 #include <atomic>
20 #include <list>
21 #include <memory>
22 #include <utility>
23 #include <mutex>
24 #include <unordered_map>
25 #include <unordered_set>
26 
38 
39 namespace alexaClientSDK {
40 namespace contextManager {
41 
47 public:
56  static std::shared_ptr<ContextManagerInterface> createContextManagerInterface(
57  const std::shared_ptr<avsCommon::utils::DeviceInfo>& deviceInfo,
58  const std::shared_ptr<avsCommon::utils::timing::MultiTimer>& multiTimer =
59  std::make_shared<avsCommon::utils::timing::MultiTimer>(),
60  const std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface>& metricRecorder = nullptr);
61 
71  static std::shared_ptr<ContextManager> create(
72  const avsCommon::utils::DeviceInfo& deviceInfo,
73  std::shared_ptr<avsCommon::utils::timing::MultiTimer> multiTimer =
74  std::make_shared<avsCommon::utils::timing::MultiTimer>(),
75  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder = nullptr);
76 
78  ~ContextManager() override;
79 
82  void setStateProvider(
83  const avsCommon::avs::CapabilityTag& stateProviderName,
84  std::shared_ptr<avsCommon::sdkInterfaces::StateProviderInterface> stateProvider) override;
85 
86  void addStateProvider(
87  const avsCommon::avs::CapabilityTag& capabilityIdentifier,
88  std::shared_ptr<avsCommon::sdkInterfaces::StateProviderInterface> stateProvider) override;
89 
90  void removeStateProvider(const avsCommon::avs::CapabilityTag& capabilityIdentifier) override;
91 
93  const avsCommon::avs::CapabilityTag& stateProviderName,
94  const std::string& jsonState,
95  const avsCommon::avs::StateRefreshPolicy& refreshPolicy,
96  const avsCommon::sdkInterfaces::ContextRequestToken stateRequestToken = 0) override;
97 
99  std::shared_ptr<avsCommon::sdkInterfaces::ContextRequesterInterface> contextRequester,
100  const std::string& endpointId,
101  const std::chrono::milliseconds& timeout) override;
102 
104  std::shared_ptr<avsCommon::sdkInterfaces::ContextRequesterInterface> contextRequester,
105  const std::string& endpointId,
106  const std::chrono::milliseconds& timeout) override;
107 
108  void reportStateChange(
109  const avsCommon::avs::CapabilityTag& capabilityIdentifier,
110  const avsCommon::avs::CapabilityState& capabilityState,
112 
114  const avsCommon::avs::CapabilityTag& capabilityIdentifier,
115  const avsCommon::avs::CapabilityState& capabilityState,
116  avsCommon::sdkInterfaces::ContextRequestToken stateRequestToken) override;
117 
119  const avsCommon::avs::CapabilityTag& capabilityIdentifier,
121  bool isEndpointUnreachable) override;
122 
124  std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerObserverInterface> observer) override;
125 
127  const std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerObserverInterface>& observer) override;
129 
130 private: // Private type declarations.
134  struct StateInfo {
136  std::shared_ptr<avsCommon::sdkInterfaces::StateProviderInterface> stateProvider;
137 
140 
143  bool legacyCapability;
144 
147 
157  StateInfo(
158  std::shared_ptr<avsCommon::sdkInterfaces::StateProviderInterface> initStateProvider = nullptr,
159  const std::string& initJsonState = "",
161 
168  StateInfo(
169  std::shared_ptr<avsCommon::sdkInterfaces::StateProviderInterface> initStateProvider,
171 
175  StateInfo(const StateInfo&) = default;
176  };
177 
179  using CapabilitiesState = std::unordered_map<avsCommon::avs::CapabilityTag, StateInfo>;
180 
183 
187  struct RequestTracker {
191  const std::shared_ptr<avsCommon::sdkInterfaces::ContextRequesterInterface> contextRequester;
193  const bool skipReportableStateProperties;
194 
202  RequestTracker(
204  std::shared_ptr<avsCommon::sdkInterfaces::ContextRequesterInterface> contextRequester,
205  bool skipReportableProperties);
206  };
207 
208 private: // Private method declarations.
217  const std::string& defaultEndpointId,
218  std::shared_ptr<avsCommon::utils::timing::MultiTimer> multiTimer,
219  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder);
221 
230  void updateCapabilityState(
231  const avsCommon::avs::CapabilityTag& capabilityIdentifier,
232  const std::string& jsonState,
233  const avsCommon::avs::StateRefreshPolicy& refreshPolicy);
234 
242  void updateCapabilityState(
243  const avsCommon::avs::CapabilityTag& capabilityIdentifier,
244  const avsCommon::avs::CapabilityState& capabilityState);
245 
258  std::shared_ptr<avsCommon::sdkInterfaces::ContextRequesterInterface> contextRequester,
259  const std::string& endpointId,
260  const std::chrono::milliseconds& timeout,
261  bool skipReportableStateProperties);
262 
276  std::function<void()> getContextAvailableCallbackIfReadyLocked(
279 
293  std::function<void()> getContextFailureCallbackLocked(
296 
302  inline avsCommon::sdkInterfaces::ContextRequestToken generateToken();
303 
304 private: // Member variable declarations
306  std::mutex m_endpointsStateMutex;
307 
310  std::unordered_map<EndpointIdentifier, CapabilitiesState> m_endpointsState;
311 
313  std::mutex m_requestsMutex;
314 
316  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> m_metricRecorder;
317 
319  std::atomic<unsigned int> m_requestCounter;
320 
322  std::unordered_map<unsigned int, std::unordered_set<avsCommon::avs::CapabilityTag>> m_pendingStateRequest;
323 
325  std::unordered_map<avsCommon::sdkInterfaces::ContextRequestToken, RequestTracker> m_pendingRequests;
326 
328  std::mutex m_observerMutex;
329 
331  std::list<std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerObserverInterface>> m_observers;
332 
334  std::atomic_bool m_shutdown;
335 
337  const std::string m_defaultEndpointId;
338 
340  std::shared_ptr<avsCommon::utils::timing::MultiTimer> m_multiTimer;
341 
344 };
345 
346 } // namespace contextManager
347 } // namespace alexaClientSDK
348 
349 #endif // ALEXA_CLIENT_SDK_CONTEXTMANAGER_INCLUDE_CONTEXTMANAGER_CONTEXTMANAGER_H_
void reportStateChange(const avsCommon::avs::CapabilityTag &capabilityIdentifier, const avsCommon::avs::CapabilityState &capabilityState, avsCommon::sdkInterfaces::AlexaStateChangeCauseType cause) override
void removeStateProvider(const avsCommon::avs::CapabilityTag &capabilityIdentifier) override
SetStateResult
Definition: ContextManagerInterface.h:39
void addStateProvider(const avsCommon::avs::CapabilityTag &capabilityIdentifier, std::shared_ptr< avsCommon::sdkInterfaces::StateProviderInterface > stateProvider) override
void removeContextManagerObserver(const std::shared_ptr< avsCommon::sdkInterfaces::ContextManagerObserverInterface > &observer) override
static std::shared_ptr< ContextManager > create(const avsCommon::utils::DeviceInfo &deviceInfo, std::shared_ptr< avsCommon::utils::timing::MultiTimer > multiTimer=std::make_shared< avsCommon::utils::timing::MultiTimer >(), std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder=nullptr)
static std::shared_ptr< ContextManagerInterface > createContextManagerInterface(const std::shared_ptr< avsCommon::utils::DeviceInfo > &deviceInfo, const std::shared_ptr< avsCommon::utils::timing::MultiTimer > &multiTimer=std::make_shared< avsCommon::utils::timing::MultiTimer >(), const std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > &metricRecorder=nullptr)
Definition: CapabilityTag.h:32
::std::string string
Definition: gtest-port.h:1097
ContextRequestError
Definition: ContextRequesterInterface.h:40
Single-thread executor implementation.
Definition: Executor.h:45
StateRefreshPolicy
Definition: StateRefreshPolicy.h:31
unsigned int ContextRequestToken
Alias for the token used to identify a context request.
Definition: ContextRequestToken.h:23
AlexaStateChangeCauseType
Definition: AlexaStateChangeCauseType.h:28
static std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder
Metric recorder shared ptr.
Definition: BaseAPLCapabilityAgentTest.cpp:261
avsCommon::sdkInterfaces::ContextRequestToken getContext(std::shared_ptr< avsCommon::sdkInterfaces::ContextRequesterInterface > contextRequester, const std::string &endpointId, const std::chrono::milliseconds &timeout) override
void provideStateResponse(const avsCommon::avs::CapabilityTag &capabilityIdentifier, const avsCommon::avs::CapabilityState &capabilityState, avsCommon::sdkInterfaces::ContextRequestToken stateRequestToken) override
avsCommon::sdkInterfaces::SetStateResult setState(const avsCommon::avs::CapabilityTag &stateProviderName, const std::string &jsonState, const avsCommon::avs::StateRefreshPolicy &refreshPolicy, const avsCommon::sdkInterfaces::ContextRequestToken stateRequestToken=0) override
Definition: CapabilityState.h:31
Definition: ContextManager.h:46
void provideStateUnavailableResponse(const avsCommon::avs::CapabilityTag &capabilityIdentifier, avsCommon::sdkInterfaces::ContextRequestToken stateRequestToken, bool isEndpointUnreachable) override
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
uint64_t Token
Alias for the token used to identify a task. This can be used to cancel a task execution.
Definition: MultiTimer.h:42
std::string EndpointIdentifier
Definition: EndpointIdentifier.h:38
avsCommon::sdkInterfaces::ContextRequestToken getContextWithoutReportableStateProperties(std::shared_ptr< avsCommon::sdkInterfaces::ContextRequesterInterface > contextRequester, const std::string &endpointId, const std::chrono::milliseconds &timeout) override
void setStateProvider(const avsCommon::avs::CapabilityTag &stateProviderName, std::shared_ptr< avsCommon::sdkInterfaces::StateProviderInterface > stateProvider) override
void addContextManagerObserver(std::shared_ptr< avsCommon::sdkInterfaces::ContextManagerObserverInterface > observer) override

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