ifw-sup  1.0.0
utils.hpp
Go to the documentation of this file.
1 
8 #ifndef SUP_SUBSIM_COMMON_UTILS_IPP_
9 #define SUP_SUBSIM_COMMON_UTILS_IPP_
10 
11 // System headers
12 #include <string>
13 
14 // Third party headers
15 
16 
17 // Local headers
18 #include <rad/smAdapter.hpp>
19 #include <rad/mal/request.hpp>
20 #include <rad/activity.hpp>
22 
23 namespace sup::subsim::common {
24 
25  template <class EVENT_DONE, class EVENT_ERR>
26  void EmulateEvent(const IConfig& config,
27  rad::SMAdapter& sm,
28  const std::string cmd,
29  rad::ThreadActivity* activity) {
30 
31  // In case data is not found, it takes the default value
32  auto delay = config.GetReplyDelay(cmd).value_or(0);
33  auto repl_ok = config.GetReplyOk(cmd).value_or(true);
34  const auto msg = config.GetReplyErrorMessage(cmd).value_or("Generic Error");
35 
36  RAD_LOG_INFO() << "Reply msg: " << msg;
37  if (delay > 0) {
38  RAD_LOG_DEBUG() << "Reply delay: " << delay;
39  int total = 0;
40  int increment = int(delay/100);
41  RAD_LOG_DEBUG() << "Increment: " << increment;
42  while (total < delay) {
43  if (activity->IsStopRequested()) {
44  sm.PostEvent(rad::UniqueEvent(new EVENT_ERR(cmd + " has been stopped !" )));
45  return;
46  }
47  std::this_thread::sleep_for(std::chrono::milliseconds(increment));
48  total += increment;
49  RAD_LOG_DEBUG() << "Total Time: " << total << " [ms]";
50  }
51  }
52  if (repl_ok) {
53  sm.PostEvent(rad::UniqueEvent(new EVENT_DONE));
54  } else {
55  sm.PostEvent(rad::UniqueEvent(new EVENT_ERR(msg )));
56  }
57  }
58 
59 
60 } // namespace
61 
62 #include "utils.ipp"
63 #endif // SUP_SUBSIM_COMMON_UTILS_IPP_
sup::subsim::common
Definition: actionMgr.cpp:32
sup::subsim::common::IConfig::GetReplyOk
virtual std::optional< bool > GetReplyOk(const std::string &name) const =0
sup::subsim::common::IConfig::GetReplyErrorMessage
virtual std::optional< std::string > GetReplyErrorMessage(const std::string &name) const =0
sup::subsim::common::IConfig
Definition: iConfig.hpp:22
config.hpp
Config class header file.
sup::subsim::common::EmulateEvent
void EmulateEvent(const IConfig &config, rad::SMAdapter &sm, const std::string cmd, rad::ThreadActivity *activity)
Definition: utils.hpp:26
sup::subsim::common::IConfig::GetReplyDelay
virtual std::optional< int > GetReplyDelay(const std::string &name) const =0