Gazebo Sim

API Reference

7.9.0
TriggeredPublisher.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 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 #ifndef GZ_SIM_SYSTEMS_TRIGGEREDPUBLISHER_HH_
18 #define GZ_SIM_SYSTEMS_TRIGGEREDPUBLISHER_HH_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 #include <mutex>
24 #include <gz/transport/Node.hh>
25 #include "gz/sim/System.hh"
26 
27 namespace gz
28 {
29 namespace sim
30 {
31 // Inline bracket to help doxygen filtering.
32 inline namespace GZ_SIM_VERSION_NAMESPACE {
33 namespace systems
34 {
35  // Forward declaration
36  class InputMatcher;
37 
173  class TriggeredPublisher : public System,
174  public ISystemConfigure,
175  public ISystemPreUpdate
176  {
178  public: TriggeredPublisher() = default;
179 
181  public: ~TriggeredPublisher() override;
182 
183  // Documentation inherited
184  public: void Configure(const Entity &_entity,
187  EventManager &_eventMgr) override;
188 
189  // Documentation inherited
190  public: void PreUpdate(
191  const gz::sim::UpdateInfo &_info,
192  gz::sim::EntityComponentManager &_ecm) override;
193 
195  public: void DoWork();
196 
198  private: void CallService(std::size_t pendingSrv);
199 
201  private: void PublishMsg(std::size_t pending);
202 
206  public: bool MatchInput(const transport::ProtoMsg &_inputMsg);
207 
209  private: std::string inputMsgType;
210 
212  private: std::string inputTopic;
213 
215  private: struct OutputInfo
216  {
218  std::string msgType;
219 
221  std::string topic;
222 
225  transport::ProtoMsgPtr msgData;
226 
229  };
230 
232  private: struct SrvOutputInfo
233  {
235  std::string srvName;
236 
238  std::string reqType;
239 
241  std::string repType;
242 
244  std::string reqMsg;
245 
247  int timeout;
248  };
249 
252 
254  private: std::vector<OutputInfo> outputInfo;
255 
257  private: std::vector<SrvOutputInfo> srvOutputInfo;
258 
260  private: transport::Node node;
261 
263  private: std::size_t serviceCount{0};
264 
266  private: std::size_t publishCount{0};
267 
269  private: std::mutex publishCountMutex;
270 
272  private: std::mutex triggerSrvMutex;
273 
275  private: std::condition_variable newMatchSignal;
276 
278  private: std::thread workerThread;
279 
282  private: std::atomic<bool> done{false};
283 
285  private: std::chrono::steady_clock::duration delay{0};
286 
289 
291  private: std::mutex publishQueueMutex;
292  };
293  }
294 }
295 }
296 }
297 
298 #endif