AlexaClientSDK  1.25.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
CapabilityAgent.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_AVSCOMMON_AVS_INCLUDE_AVSCOMMON_AVS_CAPABILITYAGENT_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_AVS_INCLUDE_AVSCOMMON_AVS_CAPABILITYAGENT_H_
18 
19 #include <atomic>
20 #include <unordered_map>
21 #include <string>
22 #include <memory>
23 
31 
32 namespace alexaClientSDK {
33 namespace avsCommon {
34 namespace avs {
35 
52 public:
56  virtual ~CapabilityAgent() = default;
57 
58  /*
59  * DirectiveHandlerInterface functions.
60  *
61  * The following four functions implement the @c DirectiveHandlerInterface. Only the message Id is passed to the
62  * @c handleDirective and @c cancelDirective functions, so we need to maintain a mapping of the message Id to the
63  * @c AVSDirective and @c DirectiveHandlerResultInterface, i.e., @c DirectiveAndResultInterface. The
64  * @c DirectiveHandlerInterface functions call the functions of the same name with the
65  * @c DirectiveAndResultInterface as the argument.
66  */
67  void preHandleDirective(
68  std::shared_ptr<AVSDirective> directive,
69  std::unique_ptr<sdkInterfaces::DirectiveHandlerResultInterface> result) override final;
70 
71  bool handleDirective(const std::string& messageId) override final;
72 
73  void cancelDirective(const std::string& messageId) override final;
74 
75  void onDeregistered() override;
76 
77  void onFocusChanged(FocusState newFocus, MixingBehavior behavior) override;
78 
79 protected:
87  const std::string& nameSpace,
88  std::shared_ptr<sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionEncounteredSender);
89 
94  class DirectiveInfo {
95  public:
103  std::shared_ptr<AVSDirective> directiveIn,
104  std::unique_ptr<sdkInterfaces::DirectiveHandlerResultInterface> resultIn);
105 
109  virtual ~DirectiveInfo() = default;
110 
112  std::shared_ptr<AVSDirective> directive;
113 
115  std::shared_ptr<sdkInterfaces::DirectiveHandlerResultInterface> result;
116 
118  std::atomic<bool> isCancelled;
119  };
120 
130  virtual std::shared_ptr<DirectiveInfo> createDirectiveInfo(
131  std::shared_ptr<AVSDirective> directive,
132  std::unique_ptr<sdkInterfaces::DirectiveHandlerResultInterface> result);
133 
147  virtual void preHandleDirective(std::shared_ptr<DirectiveInfo> info) = 0;
148 
161  virtual void handleDirective(std::shared_ptr<DirectiveInfo> info) = 0;
162 
174  virtual void cancelDirective(std::shared_ptr<DirectiveInfo> info) = 0;
175 
185  void removeDirective(const std::string& messageId);
186 
195  std::shared_ptr<DirectiveInfo> info,
196  const std::string& message,
198 
212  const std::pair<std::string, std::string> buildJsonEventString(
213  const std::string& eventName,
214  const std::string& dialogRequestIdString = "",
215  const std::string& payload = "{}",
216  const std::string& context = "") const;
217 
219  const std::string m_namespace;
220 
222  std::shared_ptr<sdkInterfaces::ExceptionEncounteredSenderInterface> m_exceptionEncounteredSender;
223 
224 private:
231  std::shared_ptr<DirectiveInfo> getDirectiveInfo(const std::string& messageId);
232 
234  std::unordered_map<std::string, std::shared_ptr<DirectiveInfo>> m_directiveInfoMap;
235 
237  std::mutex m_mutex;
238 };
239 
240 } // namespace avs
241 } // namespace avsCommon
242 } // namespace alexaClientSDK
243 
244 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_AVS_INCLUDE_AVSCOMMON_AVS_CAPABILITYAGENT_H_
Definition: CapabilityAgent.h:47
MixingBehavior
Definition: MixingBehavior.h:25
void cancelDirective(const std::string &messageId) override final
Definition: CapabilityAgent.cpp:108
void preHandleDirective(std::shared_ptr< AVSDirective > directive, std::unique_ptr< sdkInterfaces::DirectiveHandlerResultInterface > result) override final
std::shared_ptr< sdkInterfaces::ExceptionEncounteredSenderInterface > m_exceptionEncounteredSender
Object to use to send exceptionEncountered messages.
Definition: CapabilityAgent.h:222
ExceptionErrorType
Definition: ExceptionErrorType.h:28
std::shared_ptr< AVSDirective > directive
AVSDirective that is passed during preHandle.
Definition: CapabilityAgent.h:112
FocusState
Definition: FocusState.h:29
virtual std::shared_ptr< DirectiveInfo > createDirectiveInfo(std::shared_ptr< AVSDirective > directive, std::unique_ptr< sdkInterfaces::DirectiveHandlerResultInterface > result)
Definition: CapabilityAgent.cpp:46
void onDeregistered() override
Definition: CapabilityAgent.cpp:144
void sendExceptionEncounteredAndReportFailed(std::shared_ptr< DirectiveInfo > info, const std::string &message, avsCommon::avs::ExceptionErrorType type=avsCommon::avs::ExceptionErrorType::INTERNAL_ERROR)
Definition: CapabilityAgent.cpp:122
CapabilityAgent(const std::string &nameSpace, std::shared_ptr< sdkInterfaces::ExceptionEncounteredSenderInterface > exceptionEncounteredSender)
Definition: CapabilityAgent.cpp:52
std::shared_ptr< sdkInterfaces::DirectiveHandlerResultInterface > result
DirectiveHandlerResultInterface.
Definition: CapabilityAgent.h:115
void removeDirective(const std::string &messageId)
Definition: CapabilityAgent.cpp:148
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
const std::pair< std::string, std::string > buildJsonEventString(const std::string &eventName, const std::string &dialogRequestIdString="", const std::string &payload="{}", const std::string &context="") const
Definition: CapabilityAgent.cpp:158
void onFocusChanged(FocusState newFocus, MixingBehavior behavior) override
Definition: CapabilityAgent.cpp:154
const std::string m_namespace
The namespace of the capability agent.
Definition: CapabilityAgent.h:219
bool handleDirective(const std::string &messageId) override final
Definition: CapabilityAgent.cpp:98
DirectiveInfo(std::shared_ptr< AVSDirective > directiveIn, std::unique_ptr< sdkInterfaces::DirectiveHandlerResultInterface > resultIn)
Definition: CapabilityAgent.cpp:59
std::atomic< bool > isCancelled
Flag to indicate whether the directive is cancelled.
Definition: CapabilityAgent.h:118

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