scenario_simulator_v2 C++ API
zmq_multi_server.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SIMULATION_INTERFACE__ZMQ_MULTI_SERVER_HPP_
16 #define SIMULATION_INTERFACE__ZMQ_MULTI_SERVER_HPP_
17 
18 #include <simulation_api_schema.pb.h>
19 
20 #include <functional>
21 #include <rclcpp/rclcpp.hpp>
24 #include <string>
25 #include <thread>
26 #include <tuple>
27 #include <zmqpp/zmqpp.hpp>
28 
29 namespace zeromq
30 {
32 {
33 public:
34  template <typename... Ts>
35  explicit MultiServer(
37  const simulation_interface::HostName & hostname, const unsigned int socket_port, Ts &&... xs)
38  : context_(zmqpp::context()),
39  type_(zmqpp::socket_type::reply),
40  socket_(context_, type_),
41  functions_(std::forward<decltype(xs)>(xs)...)
42  {
43  socket_.bind(simulation_interface::getEndPoint(protocol, hostname, socket_port));
44  poller_.add(socket_);
45  thread_ = std::thread(&MultiServer::start_poll, this);
46  }
47 
48  ~MultiServer();
49 
50 private:
51  void poll();
52  void start_poll();
53  std::thread thread_;
54  const zmqpp::context context_;
55  const zmqpp::socket_type type_;
56  zmqpp::poller poller_;
57  zmqpp::socket socket_;
58 
59 #define DEFINE_FUNCTION_TYPE(TYPENAME) \
60  using TYPENAME = std::function<simulation_api_schema::TYPENAME##Response( \
61  const simulation_api_schema::TYPENAME##Request &)>
62 
63  DEFINE_FUNCTION_TYPE(Initialize);
64  DEFINE_FUNCTION_TYPE(UpdateFrame);
65  DEFINE_FUNCTION_TYPE(SpawnVehicleEntity);
66  DEFINE_FUNCTION_TYPE(SpawnPedestrianEntity);
67  DEFINE_FUNCTION_TYPE(SpawnMiscObjectEntity);
68  DEFINE_FUNCTION_TYPE(DespawnEntity);
69  DEFINE_FUNCTION_TYPE(UpdateEntityStatus);
70  DEFINE_FUNCTION_TYPE(AttachImuSensor);
71  DEFINE_FUNCTION_TYPE(AttachLidarSensor);
72  DEFINE_FUNCTION_TYPE(AttachDetectionSensor);
73  DEFINE_FUNCTION_TYPE(AttachOccupancyGridSensor);
74  DEFINE_FUNCTION_TYPE(UpdateTrafficLights);
75  DEFINE_FUNCTION_TYPE(AttachPseudoTrafficLightDetector);
76  DEFINE_FUNCTION_TYPE(UpdateStepTime);
77 
78 #undef DEFINE_FUNCTION_TYPE
79 
80  std::tuple<
81  Initialize, UpdateFrame, SpawnVehicleEntity, SpawnPedestrianEntity, SpawnMiscObjectEntity,
82  DespawnEntity, UpdateEntityStatus, AttachImuSensor, AttachLidarSensor, AttachDetectionSensor,
83  AttachOccupancyGridSensor, UpdateTrafficLights, AttachPseudoTrafficLightDetector,
84  UpdateStepTime>
85  functions_;
86 };
87 } // namespace zeromq
88 
89 #endif // SIMULATION_INTERFACE__ZMQ_MULTI_SERVER_HPP_
Definition: zmq_multi_server.hpp:32
MultiServer(const simulation_interface::TransportProtocol &protocol, const simulation_interface::HostName &hostname, const unsigned int socket_port, Ts &&... xs)
Definition: zmq_multi_server.hpp:35
~MultiServer()
Definition: zmq_multi_server.cpp:21
HostName
Definition: constants.hpp:26
TransportProtocol
Definition: constants.hpp:22
const TransportProtocol protocol
Definition: constants.hpp:30
std::string getEndPoint(const TransportProtocol &protocol, const HostName &hostname, const unsigned int &port)
Definition: constants.cpp:21
Definition: cache.hpp:27
Definition: junit5.hpp:25
Definition: conversions.hpp:56