AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
WebSocketServer.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_COMMUNICATION_INCLUDE_COMMUNICATION_WEBSOCKETSERVER_H_
17 #define ALEXA_CLIENT_SDK_COMMUNICATION_INCLUDE_COMMUNICATION_WEBSOCKETSERVER_H_
18 
19 #include <string>
20 #include <set>
21 
22 #include <websocketpp/server.hpp>
23 #ifdef ENABLE_WEBSOCKET_SSL
24 #include <asio/ssl.hpp>
25 #endif
26 
28 
29 #include "WebSocketConfig.h"
30 
31 namespace alexaClientSDK {
32 namespace sampleApplications {
33 namespace ipcServerSampleApp {
34 namespace communication {
35 
36 /*
37  Implementation Notes
38  --------------------
39 
40  This implementation is loosely taken from the example code within the WebSocketPP library.
41 
42  The main idea behind ASIO is that all requests to write/read are asynchronous, generally any request to read/write
43  is provided a callback, which is called when the the request is complete. In cases where the input is required for e.g.
44  for read, ASIO returns control flow immediately, calling the supplied callback at some later time.
45 
46  Top-level sequence expressed serially, session operations are handled by the WebSocketPP library:
47 
48  Setup listener socket on supplied port
49  Wait for connection
50  Get notified of new connection - add it to the set of connections
51 
52  When data is received from a client, onMessage will be called with the message payload
53 
54  When sending a message to clients, the data is sent to each connection which is currently open.
55 
56  When a client disconnects for any reason, onConnectionClose is called - the connection is removed from the
57  set of connections
58 
59  Additional notes
60  ----------------
61  This implementation supports multiple clients; writes will be fanned out to all clients, reads are accepted
62  from any client. The connection_hdl descriptor identifies the client which is opening/closing a connection or
63  sending a message.
64 */
65 
71  : public std::enable_shared_from_this<WebSocketServer>
72  , public MessagingServerInterface {
73 public:
80  WebSocketServer(const std::string& interface, unsigned short port);
81 
89  void setCertificateFile(
90  const std::string& certificateAuthority,
91  const std::string& certificate,
92  const std::string& privateKey);
93 
96  bool start() override;
97  void writeMessage(const std::string& payload) override;
98  void setMessageListener(std::shared_ptr<MessageListenerInterface> messageListener) override;
99  void stop() override;
100  bool isReady() override;
101  void setObserver(const std::shared_ptr<communication::MessagingServerObserverInterface>& observer) override;
103  virtual ~WebSocketServer() = default;
104 
105 private:
106  typedef websocketpp::server<WebSocketConfig> server;
107  using connection_hdl = websocketpp::connection_hdl;
108 
114  void onConnectionOpen(connection_hdl connectionHdl);
115 
121  void onConnectionClose(connection_hdl connectionHdl);
122 
129  void onMessage(connection_hdl connectionHdl, server::message_ptr messagePtr);
130 
131 #ifdef ENABLE_WEBSOCKET_SSL
132 
135  std::shared_ptr<asio::ssl::context> onTlsInit();
136 #endif
137 
144  bool onValidate(connection_hdl connectionHdl);
145 
147  std::atomic_bool m_initialised{false};
148 
150  server m_webSocketServer;
151 
153  std::shared_ptr<MessageListenerInterface> m_messageListener;
154 
156  connection_hdl m_connection;
157 
159  std::string m_certificateAuthorityFile;
160 
162  std::string m_certificateFile;
163 
165  std::string m_privateKeyFile;
166 
168  std::shared_ptr<MessagingServerObserverInterface> m_observer;
169 };
170 
171 } // namespace communication
172 } // namespace ipcServerSampleApp
173 } // namespace sampleApplications
174 } // namespace alexaClientSDK
175 
176 #endif // ALEXA_CLIENT_SDK_COMMUNICATION_INCLUDE_COMMUNICATION_WEBSOCKETSERVER_H_
::std::string string
Definition: gtest-port.h:1097
void setObserver(const std::shared_ptr< communication::MessagingServerObserverInterface > &observer) override
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
void setCertificateFile(const std::string &certificateAuthority, const std::string &certificate, const std::string &privateKey)
void setMessageListener(std::shared_ptr< MessageListenerInterface > messageListener) 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