Gazebo Sim

API Reference

10.1.1
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
42namespace gz
43{
44namespace sim
45{
46// Inline bracket to help doxygen filtering.
47inline namespace GZ_SIM_VERSION_NAMESPACE {
48namespace systems
49{
79
176 : public System,
177 public ISystemConfigure
178 {
180 public: WebsocketServer() = default;
181
183 public: virtual ~WebsocketServer();
184
186 public: void Configure(const Entity &_entity,
189 EventManager &_eventMgr) final;
190
191 public: void Run();
192
193 private: void OnWebsocketSubscribedMessage(const char *_data,
194 const size_t _size,
195 const gz::transport::MessageInfo &_info);
196
200 private: void OnWebsocketSubscribedImageMessage(
201 const gz::msgs::Image &_msg,
202 const gz::transport::MessageInfo &_info);
203
207 public: void OnConnect(int _socketId);
208
211 public: void OnDisconnect(int _socketId);
212
216 public: void OnMessage(int _socketId, const std::string _msg);
217
228 public: bool UpdateMsgTypeSubscriptionCount(const std::string &_topic,
229 int _socketId, bool _subscribe);
230
234 private: void OnAsset(int _socketId,
235 const std::vector<std::string> &_frameParts);
236
240 private: void OnRequest(int _socketId,
241 const std::vector<std::string> &_frameParts);
242
243 private: gz::transport::Node node;
244
245 private: bool run = true;
246 private: std::thread *thread = nullptr;
247 private: struct lws_context *context = nullptr;
248
249 private: std::vector<struct lws_protocols> protocols;
250 private: class Connection
251 {
252 public: std::chrono::system_clock::time_point creationTime;
254 public: std::list<int> len;
255 public: std::mutex mutex;
256
257 public: bool authorized{false};
258
262 topicPublishPeriods;
263
266 public: std::map<std::string,
268
272 public: std::map<std::string, int> msgTypeSubscriptionCount;
273 };
274
275 private: void QueueMessage(Connection *_connection,
276 const char *_data, const size_t _size);
277
279
283
286
291
296
299
302
305 public: int messageCount{0};
306
309 public: int maxConnections{-1};
310
314 private: std::map<std::string,
316 topicTimestamps;
317
320 public: int queueSizePerConnection{-1};
321
324 private: enum Operation
325 {
327 SUBSCRIBE = 0,
328
330 PUBLISH = 1,
331
333 TOPICS = 2,
334
336 PROTOS = 3,
337
339 ASSET = 4,
340
342 REQUEST = 5,
343 };
344
348 private: std::vector<std::string> operations{
349 "sub", "pub", "topics", "protos", "asset", "req"};
350
353 private: std::map<std::string, std::string> publishHeaders;
354
358 private: std::chrono::nanoseconds publishPeriod;
359
361 private: std::string authorizationKey;
362
365 private: std::string adminAuthorizationKey;
366 };
367}
368}
369}
370}
371
372#endif