Gazebo Sim

API Reference

10.4.0
WebsocketServer.hh
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019 Open Source Robotics Foundation
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 * You may obtain a copy of the License at
7*
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// This file was ported from:
19// https://github.com/gazebosim/gz-launch/blob/main/plugins/websocket_server
20// and converted to a gz-sim system.
21
22#ifndef GZ_SIM_SYSTEMS_WEBSOCKETSERVER_HH_
23#define GZ_SIM_SYSTEMS_WEBSOCKETSERVER_HH_
24
25#include <chrono>
26#include <cstddef>
27#include <list>
28#include <map>
29#include <memory>
30#include <set>
31#include <string>
32#include <thread>
33#include <vector>
34
36#include <gz/transport/Node.hh>
37#include <gz/common/Util.hh>
38#include <libwebsockets.h>
39
40#include "gz/sim/System.hh"
41
42// Befriended integration-test fixture for security-regression access.
43#ifdef BUILD_TESTING
44class WebsocketServerTest;
45#endif // BUILD_TESTING
46
47namespace gz
48{
49namespace sim
50{
51// Inline bracket to help doxygen filtering.
52inline namespace GZ_SIM_VERSION_NAMESPACE {
53namespace systems
54{
84
181 : public System,
182 public ISystemConfigure
183 {
184 // Grant the integration-test fixture access to private members.
185 // See test/integration/websocket_server.cc.
186#ifdef BUILD_TESTING
187 friend class ::WebsocketServerTest;
188#endif // BUILD_TESTING
189
191 public: WebsocketServer() = default;
192
194 public: virtual ~WebsocketServer();
195
197 public: void Configure(const Entity &_entity,
200 EventManager &_eventMgr) final;
201
202 public: void Run();
203
204 private: void OnWebsocketSubscribedMessage(const char *_data,
205 const size_t _size,
206 const gz::transport::MessageInfo &_info);
207
211 private: void OnWebsocketSubscribedImageMessage(
212 const gz::msgs::Image &_msg,
213 const gz::transport::MessageInfo &_info);
214
218 public: void OnConnect(int _socketId);
219
222 public: void OnDisconnect(int _socketId);
223
227 public: void OnMessage(int _socketId, const std::string _msg);
228
239 public: bool UpdateMsgTypeSubscriptionCount(const std::string &_topic,
240 int _socketId, bool _subscribe);
241
245 private: void OnAsset(int _socketId,
246 const std::vector<std::string> &_frameParts);
247
251 private: void OnRequest(int _socketId,
252 const std::vector<std::string> &_frameParts);
253
254 private: gz::transport::Node node;
255
256 private: bool run = true;
257 private: std::thread *thread = nullptr;
258 private: struct lws_context *context = nullptr;
259
260 private: std::vector<struct lws_protocols> protocols;
261 private: class Connection
262 {
263 public: std::chrono::system_clock::time_point creationTime;
265 public: std::list<int> len;
266 public: std::mutex mutex;
267
268 public: bool authorized{false};
269
273 topicPublishPeriods;
274
277 public: std::map<std::string,
279
283 public: std::map<std::string, int> msgTypeSubscriptionCount;
284 };
285
286 private: void QueueMessage(Connection *_connection,
287 const char *_data, const size_t _size);
288
290
294
297
302
307
310
313
316 public: int messageCount{0};
317
320 public: int maxConnections{-1};
321
325 private: std::map<std::string,
327 topicTimestamps;
328
331 public: int queueSizePerConnection{-1};
332
335 private: enum Operation
336 {
338 SUBSCRIBE = 0,
339
341 PUBLISH = 1,
342
344 TOPICS = 2,
345
347 PROTOS = 3,
348
350 ASSET = 4,
351
353 REQUEST = 5,
354 };
355
359 private: std::vector<std::string> operations{
360 "sub", "pub", "topics", "protos", "asset", "req"};
361
364 private: std::map<std::string, std::string> publishHeaders;
365
369 private: std::chrono::nanoseconds publishPeriod;
370
372 private: std::string authorizationKey;
373
376 private: std::string adminAuthorizationKey;
377 };
378}
379}
380}
381}
382
383#endif