Gazebo Sim
API Reference
10.1.1
insert_drive_file
Tutorials
library_books
Classes
toc
Namespaces
insert_drive_file
Files
launch
Gazebo Website
Index
List
Hierarchy
Members: All
Members: Functions
Members: Variables
Members: Typedefs
Members: Enumerations
Members: Enumerator
List
Members
Functions
Typedefs
Variables
Enumerations
Enumerator
src
gz-sim
src
systems
websocket_server
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
35
#include <
gz/transport/MessageInfo.hh
>
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
namespace
gz
43
{
44
namespace
sim
45
{
46
// Inline bracket to help doxygen filtering.
47
inline
namespace
GZ_SIM_VERSION_NAMESPACE {
48
namespace
systems
49
{
79
175
class
WebsocketServer
176
:
public
System
,
177
public
ISystemConfigure
178
{
180
public
:
WebsocketServer
() =
default
;
181
183
public
:
virtual
~WebsocketServer
();
184
186
public
:
void
Configure
(
const
Entity
&_entity,
187
const
std::shared_ptr<const sdf::Element>
&_sdf,
188
EntityComponentManager
&_ecm,
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;
253
public
:
std::list<std::unique_ptr<char []>
> buffer;
254
public
:
std::list<int>
len;
255
public
:
std::mutex
mutex;
256
257
public
:
bool
authorized{
false
};
258
261
public
:
std::map<std::string, std::chrono::nanoseconds>
262
topicPublishPeriods;
263
266
public
:
std::map
<
std::string
,
267
std::chrono::time_point<std::chrono::steady_clock>
> topicTimestamps;
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
278
public
:
std::mutex
mutex
;
279
282
public
:
std::mutex
subscriptionMutex
;
283
285
public
:
std::map<int, std::unique_ptr<Connection>
>
connections
;
286
290
public
:
std::map<std::string, std::set<int>
>
topicConnections
;
291
295
public
:
std::map<std::string, int>
msgTypeSubscriptionLimit
;
296
298
public
:
std::mutex
runMutex
;
299
301
public
:
std::condition_variable
runConditionVariable
;
302
305
public
:
int
messageCount{0};
306
309
public
:
int
maxConnections{-1};
310
314
private
:
std::map
<
std::string
,
315
std::chrono::time_point<std::chrono::steady_clock>
>
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