AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
MessageRouter.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_ACL_INCLUDE_ACL_TRANSPORT_MESSAGEROUTER_H_
17 #define ALEXA_CLIENT_SDK_ACL_INCLUDE_ACL_TRANSPORT_MESSAGEROUTER_H_
18 
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
30 
38 
39 namespace alexaClientSDK {
40 namespace acl {
41 
48  : public MessageRouterInterface
51  , public std::enable_shared_from_this<MessageRouter> {
52 public:
54  static const std::chrono::milliseconds DEFAULT_SERVER_SIDE_DISCONNECT_GRACE_PERIOD;
55 
65  static std::shared_ptr<MessageRouterInterface> createMessageRouterInterface(
66  const std::shared_ptr<acsdkShutdownManagerInterfaces::ShutdownNotifierInterface>& shutdownNotifier,
67  const std::shared_ptr<avsCommon::sdkInterfaces::AuthDelegateInterface>& authDelegate,
68  const std::shared_ptr<avsCommon::avs::attachment::AttachmentManagerInterface>& attachmentManager,
69  const std::shared_ptr<TransportFactoryInterface>& transportFactory);
70 
88  std::shared_ptr<avsCommon::sdkInterfaces::AuthDelegateInterface> authDelegate,
89  std::shared_ptr<avsCommon::avs::attachment::AttachmentManagerInterface> attachmentManager,
90  std::shared_ptr<TransportFactoryInterface> transportFactory,
91  const std::string& avsGateway = "",
93  std::chrono::milliseconds serverSideDisconnectGracePeriod = DEFAULT_SERVER_SIDE_DISCONNECT_GRACE_PERIOD);
94 
97  void enable() override;
98  void disable() override;
100  void sendMessage(std::shared_ptr<avsCommon::avs::MessageRequest> request) override;
101  void setAVSGateway(const std::string& avsGateway) override;
102  std::string getAVSGateway() override;
103  void onWakeConnectionRetry() override;
104  void onWakeVerifyConnectivity() override;
105  void setObserver(std::shared_ptr<MessageRouterObserverInterface> observer) override;
107 
108  void onConnected(std::shared_ptr<TransportInterface> transport) override;
109 
110  void onDisconnected(
111  std::shared_ptr<TransportInterface> transport,
113 
114  void onServerSideDisconnect(std::shared_ptr<TransportInterface> transport) override;
115 
116  void consumeMessage(const std::string& contextId, const std::string& message) override;
117 
118  void doShutdown() override;
119 
120 private:
128  void setConnectionStatusLocked(
131 
142  void notifyObserverOnConnectionStatusChanged(
145 
152  void handleNotifyObserverOnConnectionStatusChanged(
155 
166  void notifyObserverOnReceive(const std::string& contextId, const std::string& message);
167 
172  void createActiveTransportLocked();
173 
181  void disconnectAllTransportsLocked(
182  std::unique_lock<std::mutex>& lock,
184 
190  std::shared_ptr<MessageRouterObserverInterface> getObserver();
191 
197  void safelyResetActiveTransportLocked();
198 
204  void safelyReleaseTransport(std::shared_ptr<TransportInterface> transport);
205 
207  std::shared_ptr<MessageRouterObserverInterface> m_observer;
208 
210  std::string m_avsGateway;
211 
213  std::shared_ptr<avsCommon::sdkInterfaces::AuthDelegateInterface> m_authDelegate;
214 
216  std::mutex m_connectionMutex;
217 
220 
223 
225  int m_engineType;
226 
231  bool m_isEnabled;
232 
234  std::vector<std::shared_ptr<TransportInterface>> m_transports;
235 
237  std::shared_ptr<TransportInterface> m_activeTransport;
238 
240  std::shared_ptr<avsCommon::avs::attachment::AttachmentManagerInterface> m_attachmentManager;
241 
243  std::shared_ptr<TransportFactoryInterface> m_transportFactory;
244 
246  std::shared_ptr<SynchronizedMessageRequestQueue> m_requestQueue;
247 
249  avsCommon::utils::timing::Timer m_serverSideDisconnectTimer;
250 
252  bool m_serverSideDisconnectNotificationPending;
253 
256 
258  const std::chrono::milliseconds m_serverSideReconnectGracePeriod;
259 
260 protected:
267 };
268 
269 } // namespace acl
270 } // namespace alexaClientSDK
271 
272 #endif // ALEXA_CLIENT_SDK_ACL_INCLUDE_ACL_TRANSPORT_MESSAGEROUTER_H_
std::pair< avsCommon::sdkInterfaces::ConnectionStatusObserverInterface::Status, avsCommon::sdkInterfaces::ConnectionStatusObserverInterface::ChangedReason > ConnectionStatus
Alias to a connection status and changed reason pair.
Definition: MessageRouterInterface.h:47
static std::shared_ptr< MessageRouterInterface > createMessageRouterInterface(const std::shared_ptr< acsdkShutdownManagerInterfaces::ShutdownNotifierInterface > &shutdownNotifier, const std::shared_ptr< avsCommon::sdkInterfaces::AuthDelegateInterface > &authDelegate, const std::shared_ptr< avsCommon::avs::attachment::AttachmentManagerInterface > &attachmentManager, const std::shared_ptr< TransportFactoryInterface > &transportFactory)
ChangedReason
Definition: ConnectionStatusObserverInterface.h:50
Definition: MessageRouterInterface.h:40
std::string getAVSGateway() override
void onWakeVerifyConnectivity() override
::std::string string
Definition: gtest-port.h:1097
Single-thread executor implementation.
Definition: Executor.h:45
Definition: TransportObserverInterface.h:30
avsCommon::utils::threading::Executor m_executor
Definition: MessageRouter.h:266
void onDisconnected(std::shared_ptr< TransportInterface > transport, avsCommon::sdkInterfaces::ConnectionStatusObserverInterface::ChangedReason reason) override
MessageRouter(std::shared_ptr< avsCommon::sdkInterfaces::AuthDelegateInterface > authDelegate, std::shared_ptr< avsCommon::avs::attachment::AttachmentManagerInterface > attachmentManager, std::shared_ptr< TransportFactoryInterface > transportFactory, const std::string &avsGateway="", int engineType=avsCommon::sdkInterfaces::ENGINE_TYPE_ALEXA_VOICE_SERVICES, std::chrono::milliseconds serverSideDisconnectGracePeriod=DEFAULT_SERVER_SIDE_DISCONNECT_GRACE_PERIOD)
Definition: MessageConsumerInterface.h:27
void setObserver(std::shared_ptr< MessageRouterObserverInterface > observer) override
Timer to schedule task for delayed and periodic execution.
Definition: Timer.h:39
void sendMessage(std::shared_ptr< avsCommon::avs::MessageRequest > request) override
Definition: MessageRouter.h:47
void onConnected(std::shared_ptr< TransportInterface > transport) override
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
static const std::chrono::milliseconds DEFAULT_SERVER_SIDE_DISCONNECT_GRACE_PERIOD
Amount of time to allow for an automatic reconnect before notifying of a server side disconnect...
Definition: MessageRouter.h:54
ConnectionStatus getConnectionStatus() override
void consumeMessage(const std::string &contextId, const std::string &message) override
void setAVSGateway(const std::string &avsGateway) override
tuple message
Definition: gtest_output_test.py:331
void onServerSideDisconnect(std::shared_ptr< TransportInterface > transport) override
Status
Definition: ConnectionStatusObserverInterface.h:36
static const int ENGINE_TYPE_ALEXA_VOICE_SERVICES
Definition: ConnectionStatusObserverInterface.h:26

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