Gazebo Sim

API Reference

10.0.0~pre1
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
173 class WebsocketServer
174 : public System,
175 public ISystemConfigure
176 {
178 public: WebsocketServer() = default;
179
181 public: virtual ~WebsocketServer();
182
184 public: void Configure(const Entity &_entity,
186 EntityComponentManager &_ecm,
187 EventManager &_eventMgr) final;
188
189 public: void Run();
190
191 private: void OnWebsocketSubscribedMessage(const char *_data,
192 const size_t _size,
193 const gz::transport::MessageInfo &_info);
194
198 private: void OnWebsocketSubscribedImageMessage(
199 const gz::msgs::Image &_msg,
200 const gz::transport::MessageInfo &_info);
201
205 public: void OnConnect(int _socketId);
206
209 public: void OnDisconnect(int _socketId);
210
214 public: void OnMessage(int _socketId, const std::string _msg);
215
226 public: bool UpdateMsgTypeSubscriptionCount(const std::string &_topic,
227 int _socketId, bool _subscribe);
228
232 private: void OnAsset(int _socketId,
233 const std::vector<std::string> &_frameParts);
234
238 private: void OnRequest(int _socketId,
239 const std::vector<std::string> &_frameParts);
240
241 private: gz::transport::Node node;
242
243 private: bool run = true;
244 private: std::thread *thread = nullptr;
245 private: struct lws_context *context = nullptr;
246
247 private: std::vector<struct lws_protocols> protocols;
248 private: class Connection
249 {
250 public: std::chrono::system_clock::time_point creationTime;
252 public: std::list<int> len;
253 public: std::mutex mutex;
254
255 public: bool authorized{false};
256
260 topicPublishPeriods;
261
264 public: std::map<std::string,
266
270 public: std::map<std::string, int> msgTypeSubscriptionCount;
271 };
272
273 private: void QueueMessage(Connection *_connection,
274 const char *_data, const size_t _size);
275
276 public: std::mutex mutex;
277
280 public: std::mutex subscriptionMutex;
281
284
288 public: std::map<std::string, std::set<int>> topicConnections;
289
293 public: std::map<std::string, int> msgTypeSubscriptionLimit;
294
296 public: std::mutex runMutex;
297
299 public: std::condition_variable runConditionVariable;
300
303 public: int messageCount{0};
304
307 public: int maxConnections{-1};
308
312 private: std::map<std::string,
314 topicTimestamps;
315
318 public: int queueSizePerConnection{-1};
319
322 private: enum Operation
323 {
325 SUBSCRIBE = 0,
326
328 PUBLISH = 1,
329
331 TOPICS = 2,
332
334 PROTOS = 3,
335
337 ASSET = 4,
338
340 REQUEST = 5,
341 };
342
346 private: std::vector<std::string> operations{
347 "sub", "pub", "topics", "protos", "asset", "req"};
348
351 private: std::map<std::string, std::string> publishHeaders;
352
356 private: std::chrono::nanoseconds publishPeriod;
357
359 private: std::string authorizationKey;
360
363 private: std::string adminAuthorizationKey;
364 };
365}
366}
367}
368}
369
370#endif