Skip to content

Commit

Permalink
Version 1.13 alexa-client-sdk
Browse files Browse the repository at this point in the history
Changes in this update:

Release Version 1.13 of Alexa C++ SDK
  • Loading branch information
dhpp committed May 22, 2019
1 parent c93a898 commit c7b3142
Show file tree
Hide file tree
Showing 208 changed files with 4,106 additions and 2,066 deletions.
28 changes: 23 additions & 5 deletions ACL/include/ACL/AVSConnectionManager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2016-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,7 @@
#include <AVSCommon/AVS/MessageRequest.h>
#include <AVSCommon/SDKInterfaces/AVSEndpointAssignerInterface.h>
#include <AVSCommon/SDKInterfaces/ConnectionStatusObserverInterface.h>
#include <AVSCommon/SDKInterfaces/InternetConnectionMonitorInterface.h>
#include <AVSCommon/SDKInterfaces/MessageObserverInterface.h>
#include <AVSCommon/SDKInterfaces/MessageSenderInterface.h>
#include <AVSCommon/Utils/RequiresShutdown.h>
Expand Down Expand Up @@ -71,7 +72,9 @@ class AVSConnectionManager
, public avsCommon::sdkInterfaces::MessageSenderInterface
, public avsCommon::sdkInterfaces::AVSEndpointAssignerInterface
, public MessageRouterObserverInterface
, public avsCommon::utils::RequiresShutdown {
, public avsCommon::sdkInterfaces::InternetConnectionObserverInterface
, public avsCommon::utils::RequiresShutdown
, public std::enable_shared_from_this<AVSConnectionManager> {
public:
/**
* A factory function that creates an AVSConnectionManager object.
Expand All @@ -91,7 +94,9 @@ class AVSConnectionManager
connectionStatusObservers =
std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::ConnectionStatusObserverInterface>>(),
std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::MessageObserverInterface>> messageObservers =
std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::MessageObserverInterface>>());
std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::MessageObserverInterface>>(),
std::shared_ptr<avsCommon::sdkInterfaces::InternetConnectionMonitorInterface> internetConnectionMonitor =
nullptr);

/// @name AVSConnectionManagerInterface method overrides.
/// @{
Expand All @@ -112,6 +117,11 @@ class AVSConnectionManager
*/
void setAVSEndpoint(const std::string& avsEndpoint) override;

/// @name InternetConnectionObserverInterface method overrides.
/// @{
void onConnectionStatusChanged(bool connected) override;
/// @}

private:
/**
* AVSConnectionManager constructor.
Expand All @@ -127,7 +137,9 @@ class AVSConnectionManager
connectionStatusObservers =
std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::ConnectionStatusObserverInterface>>(),
std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::MessageObserverInterface>> messageObserver =
std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::MessageObserverInterface>>());
std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::MessageObserverInterface>>(),
std::shared_ptr<avsCommon::sdkInterfaces::InternetConnectionMonitorInterface> internetConnectionMonitor =
nullptr);

void doShutdown() override;

Expand All @@ -137,8 +149,11 @@ class AVSConnectionManager

void receive(const std::string& contextId, const std::string& message) override;

/// Mutex to serialize access to @c m_isEnabled
std::mutex m_isEnabledMutex;

/// Internal state to indicate if the Connection object is enabled for making an AVS connection.
std::atomic<bool> m_isEnabled;
bool m_isEnabled;

/// Client-provided message listener, which will receive all messages sent from AVS.
std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::MessageObserverInterface>> m_messageObservers;
Expand All @@ -148,6 +163,9 @@ class AVSConnectionManager

/// Internal object that manages the actual connection to AVS.
std::shared_ptr<MessageRouterInterface> m_messageRouter;

/// Object providing notification of gaining and losing internet connectivity.
std::shared_ptr<avsCommon::sdkInterfaces::InternetConnectionMonitorInterface> m_internetConnectionMonitor;
};

} // namespace acl
Expand Down
37 changes: 31 additions & 6 deletions ACL/src/AVSConnectionManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2017-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,7 +39,8 @@ std::shared_ptr<AVSConnectionManager> AVSConnectionManager::create(
std::shared_ptr<MessageRouterInterface> messageRouter,
bool isEnabled,
std::unordered_set<std::shared_ptr<ConnectionStatusObserverInterface>> connectionStatusObservers,
std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::MessageObserverInterface>> messageObservers) {
std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::MessageObserverInterface>> messageObservers,
std::shared_ptr<avsCommon::sdkInterfaces::InternetConnectionMonitorInterface> internetConnectionMonitor) {
if (!avsCommon::avs::initialization::AlexaClientSDKInit::isInitialized()) {
ACSDK_ERROR(LX("createFailed").d("reason", "uninitialziedAlexaClientSdk").d("return", "nullptr"));
return nullptr;
Expand All @@ -64,30 +65,41 @@ std::shared_ptr<AVSConnectionManager> AVSConnectionManager::create(
}
}

auto connectionManager = std::shared_ptr<AVSConnectionManager>(
new AVSConnectionManager(messageRouter, connectionStatusObservers, messageObservers));
auto connectionManager = std::shared_ptr<AVSConnectionManager>(new AVSConnectionManager(
messageRouter, connectionStatusObservers, messageObservers, internetConnectionMonitor));

messageRouter->setObserver(connectionManager);

if (isEnabled) {
connectionManager->enable();
}

if (internetConnectionMonitor) {
ACSDK_DEBUG5(LX(__func__).m("Subscribing to InternetConnectionMonitor Callbacks"));
internetConnectionMonitor->addInternetConnectionObserver(connectionManager);
}

return connectionManager;
}

AVSConnectionManager::AVSConnectionManager(
std::shared_ptr<MessageRouterInterface> messageRouter,
std::unordered_set<std::shared_ptr<ConnectionStatusObserverInterface>> connectionStatusObservers,
std::unordered_set<std::shared_ptr<MessageObserverInterface>> messageObservers) :
std::unordered_set<std::shared_ptr<MessageObserverInterface>> messageObservers,
std::shared_ptr<avsCommon::sdkInterfaces::InternetConnectionMonitorInterface> internetConnectionMonitor) :
AbstractAVSConnectionManager{connectionStatusObservers},
RequiresShutdown{"AVSConnectionManager"},
m_isEnabled{false},
m_messageObservers{messageObservers},
m_messageRouter{messageRouter} {
m_messageRouter{messageRouter},
m_internetConnectionMonitor{internetConnectionMonitor} {
}

void AVSConnectionManager::doShutdown() {
if (m_internetConnectionMonitor) {
m_internetConnectionMonitor->removeInternetConnectionObserver(shared_from_this());
}

disable();
clearObservers();
{
Expand All @@ -98,20 +110,27 @@ void AVSConnectionManager::doShutdown() {
}

void AVSConnectionManager::enable() {
std::lock_guard<std::mutex> lock(m_isEnabledMutex);
ACSDK_DEBUG5(LX(__func__));
m_isEnabled = true;
m_messageRouter->enable();
}

void AVSConnectionManager::disable() {
std::lock_guard<std::mutex> lock(m_isEnabledMutex);
ACSDK_DEBUG5(LX(__func__));
m_isEnabled = false;
m_messageRouter->disable();
}

bool AVSConnectionManager::isEnabled() {
std::lock_guard<std::mutex> lock(m_isEnabledMutex);
return m_isEnabled;
}

void AVSConnectionManager::reconnect() {
std::lock_guard<std::mutex> lock(m_isEnabledMutex);
ACSDK_DEBUG5(LX(__func__).d("isEnabled", m_isEnabled));
if (m_isEnabled) {
m_messageRouter->disable();
m_messageRouter->enable();
Expand All @@ -129,6 +148,12 @@ bool AVSConnectionManager::isConnected() const {
void AVSConnectionManager::setAVSEndpoint(const std::string& avsEndpoint) {
m_messageRouter->setAVSEndpoint(avsEndpoint);
}
void AVSConnectionManager::onConnectionStatusChanged(bool connected) {
ACSDK_DEBUG5(LX(__func__).d("connected", connected));
if (!connected) {
reconnect();
}
}

void AVSConnectionManager::addMessageObserver(
std::shared_ptr<avsCommon::sdkInterfaces::MessageObserverInterface> observer) {
Expand Down
4 changes: 2 additions & 2 deletions ACL/src/Transport/HTTP2TransportFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2017-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,7 +32,7 @@ static const std::string ACL_CONFIG_KEY = "acl";
/// Key for the 'endpoint' value under the @c ACL_CONFIG_KEY configuration node.
static const std::string ENDPOINT_KEY = "endpoint";
/// Default @c AVS endpoint to connect to.
static const std::string DEFAULT_AVS_ENDPOINT = "https://avs-alexa-na.amazon.com";
static const std::string DEFAULT_AVS_ENDPOINT = "https://alexa.na.gateway.devices.a2z.com";

std::shared_ptr<TransportInterface> HTTP2TransportFactory::createTransport(
std::shared_ptr<AuthDelegateInterface> authDelegate,
Expand Down
44 changes: 33 additions & 11 deletions ACL/src/Transport/MessageRouter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2016-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,13 +62,22 @@ MessageRouterInterface::ConnectionStatus MessageRouter::getConnectionStatus() {

void MessageRouter::enable() {
std::lock_guard<std::mutex> lock{m_connectionMutex};
m_isEnabled = true;
if (!m_activeTransport || !m_activeTransport->isConnected()) {
setConnectionStatusLocked(
ConnectionStatusObserverInterface::Status::PENDING,
ConnectionStatusObserverInterface::ChangedReason::ACL_CLIENT_REQUEST);
createActiveTransportLocked();

if (m_isEnabled) {
ACSDK_DEBUG0(LX(__func__).m("already enabled"));
return;
}

if (m_activeTransport != nullptr) {
ACSDK_ERROR(LX("enableFailed").d("reason", "activeTransportNotNull"));
return;
}

m_isEnabled = true;
setConnectionStatusLocked(
ConnectionStatusObserverInterface::Status::PENDING,
ConnectionStatusObserverInterface::ChangedReason::ACL_CLIENT_REQUEST);
createActiveTransportLocked();
}

void MessageRouter::doShutdown() {
Expand Down Expand Up @@ -113,11 +122,24 @@ void MessageRouter::setAVSEndpoint(const std::string& avsEndpoint) {

void MessageRouter::onConnected(std::shared_ptr<TransportInterface> transport) {
std::unique_lock<std::mutex> lock{m_connectionMutex};
if (m_isEnabled) {
setConnectionStatusLocked(
ConnectionStatusObserverInterface::Status::CONNECTED,
ConnectionStatusObserverInterface::ChangedReason::ACL_CLIENT_REQUEST);

/*
* Transport shutdown might be asynchronous,so the following scenarios are valid,
* but we shouldn't update the connection status.
*/
if (!m_isEnabled) {
ACSDK_DEBUG0(LX("onConnectedWhenDisabled"));
return;
}

if (transport != m_activeTransport) {
ACSDK_DEBUG0(LX("onInactiveTransportConnected"));
return;
}

setConnectionStatusLocked(
ConnectionStatusObserverInterface::Status::CONNECTED,
ConnectionStatusObserverInterface::ChangedReason::ACL_CLIENT_REQUEST);
}

void MessageRouter::onDisconnected(
Expand Down
Loading

0 comments on commit c7b3142

Please sign in to comment.