AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
HTTP2Transport.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_HTTP2TRANSPORT_H_
17 #define ALEXA_CLIENT_SDK_ACL_INCLUDE_ACL_TRANSPORT_HTTP2TRANSPORT_H_
18 
19 #include <atomic>
20 #include <chrono>
21 #include <deque>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <thread>
26 #include <unordered_set>
27 
38 
45 
46 namespace alexaClientSDK {
47 namespace acl {
48 
53  : public std::enable_shared_from_this<HTTP2Transport>
54  , public TransportInterface
60 public:
61  /*
62  * Defines a set of HTTP2/2 connection settings.
63  */
64  struct Configuration {
65  /*
66  * Constructor. Initializes the configuration to default.
67  */
68  Configuration();
69 
71  std::chrono::seconds inactivityTimeout;
72  };
73 
90  static std::shared_ptr<HTTP2Transport> create(
91  std::shared_ptr<avsCommon::sdkInterfaces::AuthDelegateInterface> authDelegate,
92  const std::string& avsGateway,
93  std::shared_ptr<avsCommon::utils::http2::HTTP2ConnectionInterface> http2Connection,
94  std::shared_ptr<MessageConsumerInterface> messageConsumer,
95  std::shared_ptr<avsCommon::avs::attachment::AttachmentManagerInterface> attachmentManager,
96  std::shared_ptr<TransportObserverInterface> transportObserver,
97  std::shared_ptr<PostConnectFactoryInterface> postConnectFactory,
98  std::shared_ptr<SynchronizedMessageRequestQueue> sharedRequestQueue,
99  Configuration configuration = Configuration(),
100  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder = nullptr,
101  std::shared_ptr<avsCommon::sdkInterfaces::EventTracerInterface> eventTracer = nullptr);
102 
108  void addObserver(std::shared_ptr<TransportObserverInterface> transportObserver);
109 
115  void removeObserver(std::shared_ptr<TransportObserverInterface> observer);
116 
122  std::shared_ptr<avsCommon::utils::http2::HTTP2ConnectionInterface> getHTTP2Connection();
123 
126  bool connect() override;
127  void disconnect() override;
128  bool isConnected() override;
129  void onRequestEnqueued() override;
130  void onWakeConnectionRetry() override;
131  void onWakeVerifyConnectivity() override;
133 
137  void sendMessage(std::shared_ptr<avsCommon::avs::MessageRequest> request) override;
139 
142  void onPostConnected() override;
143  void onUnRecoverablePostConnectFailure() override;
145 
148  void onAuthStateChange(
152 
155  void doShutdown() override;
157 
160  void onDownchannelConnected() override;
161  void onDownchannelFinished() override;
162  void onMessageRequestSent(const std::shared_ptr<avsCommon::avs::MessageRequest>& request) override;
163  void onMessageRequestTimeout() override;
164  void onMessageRequestAcknowledged(const std::shared_ptr<avsCommon::avs::MessageRequest>& request) override;
165  void onMessageRequestFinished() override;
166  void onPingRequestAcknowledged(bool success) override;
167  void onPingTimeout() override;
168  void onActivity() override;
169  void onForbidden(const std::string& authToken = "") override;
170  std::shared_ptr<avsCommon::utils::http2::HTTP2RequestInterface> createAndSendRequest(
171  const avsCommon::utils::http2::HTTP2RequestConfig& cfg) override;
172  std::string getAVSGateway() override;
174 
176  void onGoawayReceived() override;
178 
179 private:
183  enum class State {
185  INIT,
187  AUTHORIZING,
189  CONNECTING,
191  WAITING_TO_RETRY_CONNECTING,
194  POST_CONNECTING,
196  CONNECTED,
198  SERVER_SIDE_DISCONNECT,
200  DISCONNECTING,
202  SHUTDOWN
203  };
204 
205  // Friend to allow access to enum class State.
206  friend std::ostream& operator<<(std::ostream& stream, HTTP2Transport::State state);
207 
224  std::shared_ptr<avsCommon::sdkInterfaces::AuthDelegateInterface> authDelegate,
225  const std::string& avsGateway,
226  std::shared_ptr<avsCommon::utils::http2::HTTP2ConnectionInterface> http2Connection,
227  std::shared_ptr<MessageConsumerInterface> messageConsumer,
228  std::shared_ptr<avsCommon::avs::attachment::AttachmentManagerInterface> attachmentManager,
229  std::shared_ptr<TransportObserverInterface> transportObserver,
230  std::shared_ptr<PostConnectFactoryInterface> postConnectFactory,
231  std::shared_ptr<SynchronizedMessageRequestQueue> sharedRequestQueue,
232  Configuration configuration,
233  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder,
234  std::shared_ptr<avsCommon::sdkInterfaces::EventTracerInterface> eventTracer);
235 
239  void mainLoop();
240 
246  State handleInit();
247 
253  State handleAuthorizing();
254 
260  State handleConnecting();
261 
267  State handleWaitingToRetryConnecting();
268 
274  State handlePostConnecting();
275 
281  State handleConnected();
282 
288  State handleServerSideDisconnect();
289 
295  State handleDisconnecting();
296 
302  State handleShutdown();
303 
312  State monitorSharedQueueWhileWaiting(
313  State whileState,
314  std::chrono::time_point<std::chrono::steady_clock> maxWakeTime = avsCommon::utils::timing::getDistantFuture());
315 
322  State sendMessagesAndPings(State whileState, MessageRequestQueueInterface& requestQueue);
323 
333  bool setState(
334  State newState,
336 
346  bool setStateLocked(
347  State newState,
349 
353  void notifyObserversOnConnected();
354 
361 
365  void notifyObserversOnServerSideDisconnect();
366 
372  State getState();
373 
375  std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface> m_metricRecorder;
376 
378  std::mutex m_mutex;
379 
382 
384  State m_state;
385 
387  std::shared_ptr<avsCommon::sdkInterfaces::AuthDelegateInterface> m_authDelegate;
388 
390  std::string m_avsGateway;
391 
393  std::shared_ptr<avsCommon::utils::http2::HTTP2ConnectionInterface> m_http2Connection;
394 
396  std::shared_ptr<MessageConsumerInterface> m_messageConsumer;
397 
399  std::shared_ptr<avsCommon::avs::attachment::AttachmentManagerInterface> m_attachmentManager;
400 
402  std::shared_ptr<PostConnectFactoryInterface> m_postConnectFactory;
403 
405  std::shared_ptr<SynchronizedMessageRequestQueue> m_sharedRequestQueue;
406 
408  std::shared_ptr<avsCommon::sdkInterfaces::EventTracerInterface> m_eventTracer;
409 
411  std::mutex m_observerMutex;
412 
414  std::unordered_set<std::shared_ptr<TransportObserverInterface>> m_observers;
415 
417  std::thread m_thread;
418 
420  std::shared_ptr<PostConnectInterface> m_postConnect;
421 
423  MessageRequestQueue m_requestQueue;
424 
426  int m_connectRetryCount;
427 
429  int m_countOfUnfinishedMessageHandlers;
430 
432  std::shared_ptr<PingHandler> m_pingHandler;
433 
435  std::chrono::time_point<std::chrono::steady_clock> m_timeOfLastActivity;
436 
438  std::atomic<bool> m_postConnected;
439 
441  const Configuration m_configuration;
442 
445 
447  std::shared_ptr<avsCommon::utils::power::PowerResource> m_mainLoopPowerResource;
448 
450  std::shared_ptr<avsCommon::utils::power::PowerResource> m_requestActivityPowerResource;
451 };
452 
453 } // namespace acl
454 } // namespace alexaClientSDK
455 
456 #endif // ALEXA_CLIENT_SDK_ACL_INCLUDE_ACL_TRANSPORT_HTTP2TRANSPORT_H_
void onAuthStateChange(avsCommon::sdkInterfaces::AuthObserverInterface::State newState, avsCommon::sdkInterfaces::AuthObserverInterface::Error error) override
Definition: HTTP2Transport.h:52
friend std::ostream & operator<<(std::ostream &stream, HTTP2Transport::State state)
void onForbidden(const std::string &authToken="") override
ChangedReason
Definition: ConnectionStatusObserverInterface.h:50
void onMessageRequestSent(const std::shared_ptr< avsCommon::avs::MessageRequest > &request) override
Definition: MessageRequestQueueInterface.h:33
void addObserver(std::shared_ptr< TransportObserverInterface > transportObserver)
::std::string string
Definition: gtest-port.h:1097
Definition: ExchangeHandlerContextInterface.h:32
State
The enum State describes the state of authorization.
Definition: AuthObserverInterface.h:34
void removeObserver(std::shared_ptr< TransportObserverInterface > observer)
Definition: PostConnectObserverInterface.h:25
static std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder
Metric recorder shared ptr.
Definition: BaseAPLCapabilityAgentTest.cpp:261
static std::shared_ptr< HTTP2Transport > create(std::shared_ptr< avsCommon::sdkInterfaces::AuthDelegateInterface > authDelegate, const std::string &avsGateway, std::shared_ptr< avsCommon::utils::http2::HTTP2ConnectionInterface > http2Connection, std::shared_ptr< MessageConsumerInterface > messageConsumer, std::shared_ptr< avsCommon::avs::attachment::AttachmentManagerInterface > attachmentManager, std::shared_ptr< TransportObserverInterface > transportObserver, std::shared_ptr< PostConnectFactoryInterface > postConnectFactory, std::shared_ptr< SynchronizedMessageRequestQueue > sharedRequestQueue, Configuration configuration=Configuration(), std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder=nullptr, std::shared_ptr< avsCommon::sdkInterfaces::EventTracerInterface > eventTracer=nullptr)
std::chrono::steady_clock::time_point getDistantFuture()
Definition: DistantFuture.h:34
Error
The enum Error encodes possible errors which may occur when changing state.
Definition: AuthObserverInterface.h:48
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
void onMessageRequestAcknowledged(const std::shared_ptr< avsCommon::avs::MessageRequest > &request) override
void onPingRequestAcknowledged(bool success) override
void onUnRecoverablePostConnectFailure() override
std::chrono::seconds inactivityTimeout
The elapsed time without any activity before sending out a ping.
Definition: HTTP2Transport.h:71
This specifies an interface to send a message.
Definition: MessageSenderInterface.h:26
std::shared_ptr< avsCommon::utils::http2::HTTP2ConnectionInterface > getHTTP2Connection()
Definition: MessageRequestQueue.h:37
void sendMessage(std::shared_ptr< avsCommon::avs::MessageRequest > request) override
Definition: HTTP2ConnectionObserverInterface.h:27
std::string getAVSGateway() override
Definition: TransportInterface.h:31
std::shared_ptr< avsCommon::utils::http2::HTTP2RequestInterface > createAndSendRequest(const avsCommon::utils::http2::HTTP2RequestConfig &cfg) override

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