AlexaClientSDK  1.26.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  std::shared_ptr<avsCommon::sdkInterfaces::ContextRequesterInterface> contextRequester;
193  bool skipReportableStateProperties;
194 
198  RequestTracker();
199 
207  RequestTracker(
209  std::shared_ptr<avsCommon::sdkInterfaces::ContextRequesterInterface> contextRequester,
210  bool skipReportableProperties);
211  };
212 
213 private: // Private method declarations.
222  const std::string& defaultEndpointId,
223  std::shared_ptr<avsCommon::utils::timing::MultiTimer> multiTimer,
224  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder);
226 
235  void updateCapabilityState(
236  const avsCommon::avs::CapabilityTag& capabilityIdentifier,
237  const std::string& jsonState,
238  const avsCommon::avs::StateRefreshPolicy& refreshPolicy);
239 
247  void updateCapabilityState(
248  const avsCommon::avs::CapabilityTag& capabilityIdentifier,
249  const avsCommon::avs::CapabilityState& capabilityState);
250 
263  std::shared_ptr<avsCommon::sdkInterfaces::ContextRequesterInterface> contextRequester,
264  const std::string& endpointId,
265  const std::chrono::milliseconds& timeout,
266  bool skipReportableStateProperties);
267 
281  std::function<void()> getContextAvailableCallbackIfReadyLocked(
284 
298  std::function<void()> getContextFailureCallbackLocked(
301 
307  inline avsCommon::sdkInterfaces::ContextRequestToken generateToken();
308 
309 private: // Member variable declarations
311  std::mutex m_endpointsStateMutex;
312 
315  std::unordered_map<EndpointIdentifier, CapabilitiesState> m_endpointsState;
316 
318  std::mutex m_requestsMutex;
319 
321  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> m_metricRecorder;
322 
324  std::atomic<unsigned int> m_requestCounter;
325 
327  std::unordered_map<unsigned int, std::unordered_set<avsCommon::avs::CapabilityTag>> m_pendingStateRequest;
328 
330  std::unordered_map<avsCommon::sdkInterfaces::ContextRequestToken, RequestTracker> m_pendingRequests;
331 
333  std::mutex m_observerMutex;
334 
336  std::list<std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerObserverInterface>> m_observers;
337 
339  std::atomic_bool m_shutdown;
340 
342  const std::string m_defaultEndpointId;
343 
345  std::shared_ptr<avsCommon::utils::timing::MultiTimer> m_multiTimer;
346 
349 };
350 
351 } // namespace contextManager
352 } // namespace alexaClientSDK
353 
354 #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
Definition: ContextManager.cpp:215
void removeStateProvider(const avsCommon::avs::CapabilityTag &capabilityIdentifier) override
Definition: ContextManager.cpp:130
SetStateResult
Definition: ContextManagerInterface.h:39
void addStateProvider(const avsCommon::avs::CapabilityTag &capabilityIdentifier, std::shared_ptr< avsCommon::sdkInterfaces::StateProviderInterface > stateProvider) override
Definition: ContextManager.cpp:115
void removeContextManagerObserver(const std::shared_ptr< avsCommon::sdkInterfaces::ContextManagerObserverInterface > &observer) override
Definition: ContextManager.cpp:332
avsCommon::sdkInterfaces::ContextRequestToken getContext(std::shared_ptr< avsCommon::sdkInterfaces::ContextRequesterInterface > contextRequester, const std::string &endpointId, const std::chrono::milliseconds &timeout) override
Definition: ContextManager.cpp:341
Definition: CapabilityTag.h:32
ContextRequestError
Definition: ContextRequesterInterface.h:40
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< 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)
Definition: ContextManager.cpp:61
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: ContextManager.cpp:50
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: ContextManager.cpp:138
void provideStateResponse(const avsCommon::avs::CapabilityTag &capabilityIdentifier, const avsCommon::avs::CapabilityState &capabilityState, avsCommon::sdkInterfaces::ContextRequestToken stateRequestToken) override
Definition: ContextManager.cpp:230
~ContextManager() override
Destructor.
Definition: ContextManager.cpp:75
Definition: CapabilityState.h:31
Definition: ContextManager.h:46
void provideStateUnavailableResponse(const avsCommon::avs::CapabilityTag &capabilityIdentifier, avsCommon::sdkInterfaces::ContextRequestToken stateRequestToken, bool isEndpointUnreachable) override
Definition: ContextManager.cpp:271
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
Definition: ContextManager.cpp:349
void setStateProvider(const avsCommon::avs::CapabilityTag &stateProviderName, std::shared_ptr< avsCommon::sdkInterfaces::StateProviderInterface > stateProvider) override
Definition: ContextManager.cpp:105
void addContextManagerObserver(std::shared_ptr< avsCommon::sdkInterfaces::ContextManagerObserverInterface > observer) override
Definition: ContextManager.cpp:325

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