AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
DirectiveProcessor.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_ADSL_INCLUDE_ADSL_DIRECTIVEPROCESSOR_H_
17 #define ALEXA_CLIENT_SDK_ADSL_INCLUDE_ADSL_DIRECTIVEPROCESSOR_H_
18 
19 #include <array>
20 #include <condition_variable>
21 #include <deque>
22 #include <functional>
23 #include <memory>
24 #include <mutex>
25 #include <set>
26 #include <string>
27 #include <thread>
28 #include <unordered_map>
29 
34 
35 #include "ADSL/DirectiveRouter.h"
36 
37 namespace alexaClientSDK {
38 namespace adsl {
39 
66 public:
72  DirectiveProcessor(DirectiveRouter* directiveRouter);
73 
78 
85  void setDialogRequestId(const std::string& dialogRequestId);
86 
94 
101  bool onDirective(std::shared_ptr<avsCommon::avs::AVSDirective> directive);
102 
107  void shutdown();
108 
113  void disable();
114 
120  bool enable();
121 
122 private:
136  using ProcessorHandle = unsigned int;
137 
139  using DirectiveAndPolicy = std::pair<std::shared_ptr<avsCommon::avs::AVSDirective>, avsCommon::avs::BlockingPolicy>;
140 
145  class DirectiveHandlerResult : public avsCommon::sdkInterfaces::DirectiveHandlerResultInterface {
146  public:
153  DirectiveHandlerResult(
154  ProcessorHandle processorHandle,
155  std::shared_ptr<avsCommon::avs::AVSDirective> directive);
156 
157  void setCompleted() override;
158 
159  void setFailed(const std::string& description) override;
160 
161  private:
163  ProcessorHandle m_processorHandle;
164 
166  std::shared_ptr<avsCommon::avs::AVSDirective> m_directive;
167  };
168 
174  void onHandlingCompleted(std::shared_ptr<avsCommon::avs::AVSDirective> directive);
175 
182  void onHandlingFailed(std::shared_ptr<avsCommon::avs::AVSDirective> directive, const std::string& description);
183 
190  void removeDirectiveLocked(std::shared_ptr<avsCommon::avs::AVSDirective> directive);
191 
195  void processingLoop();
196 
205  bool processCancelingQueueLocked(std::unique_lock<std::mutex>& lock);
206 
215  bool handleQueuedDirectivesLocked(std::unique_lock<std::mutex>& lock);
216 
224  void setDialogRequestIdLocked(const std::string& dialogRequestId);
225 
233  void scrubDialogRequestIdLocked(const std::string& dialogRequestId);
234 
240  void queueAllDirectivesForCancellationLocked();
241 
251  void setDirectiveBeingHandledLocked(
252  const std::shared_ptr<avsCommon::avs::AVSDirective>& directive,
253  const avsCommon::avs::BlockingPolicy policy);
254 
262  void clearDirectiveBeingHandledLocked(const avsCommon::avs::BlockingPolicy policy);
263 
273  std::set<std::shared_ptr<avsCommon::avs::AVSDirective>> clearDirectiveBeingHandledLocked(
274  std::function<bool(const std::shared_ptr<avsCommon::avs::AVSDirective>&)> shouldClear);
275 
281  std::deque<DirectiveAndPolicy>::iterator getNextUnblockedDirectiveLocked();
282 
284  int m_handle;
285 
287  std::mutex m_mutex;
288 
290  DirectiveRouter* m_directiveRouter;
291 
293  bool m_isShuttingDown;
294 
296  bool m_isEnabled;
297 
299  std::string m_dialogRequestId;
300 
302  std::deque<std::shared_ptr<avsCommon::avs::AVSDirective>> m_cancelingQueue;
303 
305  std::shared_ptr<avsCommon::avs::AVSDirective> m_directiveBeingPreHandled;
306 
312  std::deque<DirectiveAndPolicy> m_handlingQueue;
313 
316 
318  std::thread m_processingThread;
319 
321  std::mutex m_onDirectiveMutex;
322 
324  static std::mutex m_handleMapMutex;
325 
330  static std::unordered_map<ProcessorHandle, DirectiveProcessor*> m_handleMap;
331 
333  static ProcessorHandle m_nextProcessorHandle;
334 
341  std::array<std::shared_ptr<avsCommon::avs::AVSDirective>, avsCommon::avs::BlockingPolicy::Medium::COUNT>
342  m_directivesBeingHandled;
343 
345  std::shared_ptr<avsCommon::utils::power::PowerResource> m_powerResource;
346 };
347 
348 } // namespace adsl
349 } // namespace alexaClientSDK
350 
351 #endif // ALEXA_CLIENT_SDK_ADSL_INCLUDE_ADSL_DIRECTIVEPROCESSOR_H_
DirectiveProcessor(DirectiveRouter *directiveRouter)
::std::string string
Definition: gtest-port.h:1097
Number of mediums. This MUST be the last enum member.
Definition: BlockingPolicy.h:44
Definition: DirectiveRouter.h:36
Definition: DirectiveProcessor.h:65
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
Definition: BlockingPolicy.h:31
void setDialogRequestId(const std::string &dialogRequestId)
bool onDirective(std::shared_ptr< avsCommon::avs::AVSDirective > directive)

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