Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VIC-13629 Update avs-device-sdk to v.1.11 #4

Merged
merged 2 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 64 additions & 7 deletions ADSL/include/ADSL/DirectiveProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
#ifndef ALEXA_CLIENT_SDK_ADSL_INCLUDE_ADSL_DIRECTIVEPROCESSOR_H_
#define ALEXA_CLIENT_SDK_ADSL_INCLUDE_ADSL_DIRECTIVEPROCESSOR_H_

#include <array>
#include <condition_variable>
#include <deque>
#include <functional>
#include <memory>
#include <mutex>
#include <set>
#include <string>
#include <thread>
#include <unordered_map>
Expand Down Expand Up @@ -122,6 +125,9 @@ class DirectiveProcessor {
*/
using ProcessorHandle = unsigned int;

/// The type of the handling queue items.
using DirectiveAndPolicy = std::pair<std::shared_ptr<avsCommon::avs::AVSDirective>, avsCommon::avs::BlockingPolicy>;

/**
* Implementation of @c DirectiveHandlerResultInterface that forwards the completion / failure status
* to the @c DirectiveProcessor from which it originated.
Expand Down Expand Up @@ -189,14 +195,14 @@ class DirectiveProcessor {
bool processCancelingQueueLocked(std::unique_lock<std::mutex>& lock);

/**
* Process (handle) the next @c AVSDirective in @c m_handlingQueue.
* Process (handle) all the items in @c m_handlingQueue which are not blocked.
* @note This method must only be called by threads that have acquired @c m_mutex.
*
* @param lock A @c unique_lock on m_mutex from the callers context, allowing this method to release
* (and re-acquire) the lock around callbacks that need to be invoked.
* @return Whether an @c AVSDirective from m_handlingQueue was processed.
*/
bool handleDirectiveLocked(std::unique_lock<std::mutex>& lock);
bool handleQueuedDirectivesLocked(std::unique_lock<std::mutex>& lock);

/**
* Set the current @c dialogRequestId. This cancels the processing of any @c AVSDirectives with a non-empty
Expand All @@ -223,6 +229,47 @@ class DirectiveProcessor {
*/
void queueAllDirectivesForCancellationLocked();

/**
* Save a pointer to the given directive as a directive which is being handled.
* A pointer to the directive is saved per @c BlockingPolicy::Channel used by the policy.
* This pointer can be used later to indicate which @c BlockingPolicy::Channel
* is blocked by which @c AVSDirective.
*
* @param directive The @c AVSDirective being handled.
* @param policy The @c BlockingPolicy assiciated with the given directive.
*/
void setDirectiveBeingHandledLocked(
const std::shared_ptr<avsCommon::avs::AVSDirective>& directive,
const avsCommon::avs::BlockingPolicy policy);

/**
* Clear the pointer to the directive being handled.
* @note See the above @c saveDirectiveBeingHandledLocked comment
* for further explanation.
*
* @param policy The @c BlockingPolicy with which the saved directive is associated.
*/
void clearDirectiveBeingHandledLocked(const avsCommon::avs::BlockingPolicy policy);

/**
* Clear the pointer to the directive being handled.
*
* @note See the above @c saveDirectiveBeingHandledLocked comment
* for further explanation.
* @param shouldClear Matcher to indicate which saved directive we should free.
*
* @return An @c std::set of the freed directives.
*/
std::set<std::shared_ptr<avsCommon::avs::AVSDirective>> clearDirectiveBeingHandledLocked(
std::function<bool(const std::shared_ptr<avsCommon::avs::AVSDirective>&)> shouldClear);

/**
* Get the next unblocked @c DirectiveAndPolicy form the handling queue.
*
* @return An @c std::iterator to the next unblocked @c DirectiveAndPolicy.
*/
std::deque<DirectiveAndPolicy>::iterator getNextUnblockedDirectiveLocked();

/// Handle value identifying this instance.
int m_handle;

Expand All @@ -247,11 +294,12 @@ class DirectiveProcessor {
/// The directive (if any) for which a preHandleDirective() call is in progress.
std::shared_ptr<avsCommon::avs::AVSDirective> m_directiveBeingPreHandled;

/// Queue of @c AVSDirectives waiting to be handled.
std::deque<std::shared_ptr<avsCommon::avs::AVSDirective>> m_handlingQueue;

/// Whether @c handleDirective() has been called for the directive at the @c front() of @c m_handlingQueue.
bool m_isHandlingDirective;
/**
* Queue of @c AVSDirectives waiting to be handled.
* @Note: A queue per channel would be more efficient, but as we don't expect more than few
* directives on the queue, we prefer simplicity.
*/
std::deque<DirectiveAndPolicy> m_handlingQueue;

/// Condition variable used to wake @c processingLoop() when it is waiting.
std::condition_variable m_wakeProcessingLoop;
Expand All @@ -273,6 +321,15 @@ class DirectiveProcessor {

/// Next available @c ProcessorHandle value.
static ProcessorHandle m_nextProcessorHandle;

/**
* The directives which are being handled on various channels. A directive is consider as 'being handled'
* while the @c handleDirective method of the directive handler is running, and, if the directive is blocking
* until directive handling has been completed. i.e. @c DirectiveHandlerResult::setCompleted
* or @c DirectiveHandlerResult::setFailed have been called.
*/
std::array<std::shared_ptr<avsCommon::avs::AVSDirective>, avsCommon::avs::BlockingPolicy::Medium::COUNT>
m_directivesBeingHandled;
};

} // namespace adsl
Expand Down
39 changes: 23 additions & 16 deletions ADSL/include/ADSL/DirectiveRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef ALEXA_CLIENT_SDK_ADSL_INCLUDE_ADSL_DIRECTIVEROUTER_H_
#define ALEXA_CLIENT_SDK_ADSL_INCLUDE_ADSL_DIRECTIVEROUTER_H_

#include <map>
#include <set>
#include <unordered_map>

Expand Down Expand Up @@ -63,15 +64,6 @@ class DirectiveRouter : public avsCommon::utils::RequiresShutdown {
*/
bool handleDirectiveImmediately(std::shared_ptr<avsCommon::avs::AVSDirective> directive);

/**
* Check if the directive handler's blocking policy is HANDLE_IMMEDIATELY for this directive, if so invoke @c
* handleDirectiveImmediately() on the handler registered for the given @c AVSDirective.
*
* @param directive The directive to be handled immediately.
* @return Whether or not the handler was invoked.
*/
bool handleDirectiveWithPolicyHandleImmediately(std::shared_ptr<avsCommon::avs::AVSDirective> directive);

/**
* Invoke @c preHandleDirective() on the handler registered for the given @c AVSDirective.
*
Expand All @@ -88,14 +80,10 @@ class DirectiveRouter : public avsCommon::utils::RequiresShutdown {
* Invoke @c handleDirective() on the handler registered for the given @c AVSDirective.
*
* @param directive The directive to be handled.
* @param[out] policyOut If this method returns @c true, @c policyOut is set to the @c BlockingPolicy value that
* was configured when @c handleDirective() was called.
* @return @c true if the the registered handler returned @c true. @c false if there was no registered handler
* or the registered handler returned @c false (indicating that the directive was not recognized.
*/
bool handleDirective(
std::shared_ptr<avsCommon::avs::AVSDirective> directive,
avsCommon::avs::BlockingPolicy* policyOut);
bool handleDirective(const std::shared_ptr<avsCommon::avs::AVSDirective>& directive);

/**
* Invoke cancelDirective() on the handler registered for the given @c AVSDirective.
Expand All @@ -105,6 +93,14 @@ class DirectiveRouter : public avsCommon::utils::RequiresShutdown {
*/
bool cancelDirective(std::shared_ptr<avsCommon::avs::AVSDirective> directive);

/**
* Get the policy associated with the given directive.
*
* @param directive The directive for which the policy is required.
* @return The corresponding @c BlockingPolicy value for the directive.
*/
avsCommon::avs::BlockingPolicy getPolicy(const std::shared_ptr<avsCommon::avs::AVSDirective>& directive);

private:
void doShutdown() override;

Expand Down Expand Up @@ -150,13 +146,24 @@ class DirectiveRouter : public avsCommon::utils::RequiresShutdown {
};

/**
* Look up the @c HandlerAndPolicy value for the specified @c AVSDirective.
* Look up the configured @c HandlerAndPolicy value for the specified @c AVSDirective.
* @note The calling thread must have already acquired @c m_mutex.
*
* @param directive The directive to look up a value for.
* @return The corresponding @c HandlerAndPolicy value for the specified directive.
*/
avsCommon::avs::HandlerAndPolicy getHandlerAndPolicyLocked(std::shared_ptr<avsCommon::avs::AVSDirective> directive);
avsCommon::avs::HandlerAndPolicy getdHandlerAndPolicyLocked(
const std::shared_ptr<avsCommon::avs::AVSDirective>& directive);

/**
* Get the @c DirectiveHandler for this directive.
* @note The calling thread must have already acquired @c m_mutex.
*
* @param directive The @c AVSDirective for which we're looking for handler.
* @return The directive handler for success. @c nullptr in failure.
*/
std::shared_ptr<avsCommon::sdkInterfaces::DirectiveHandlerInterface> getHandlerLocked(
std::shared_ptr<avsCommon::avs::AVSDirective> directive);

/**
* Increment the reference count for the specified handler.
Expand Down
Loading