ifw-sup  3.0.0-pre3
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 <utils/bat/logger.hpp>
19 #include <rad/smAdapter.hpp>
20 #include <rad/mal/request.hpp>
21 #include <rad/activity.hpp>
23 
24 namespace sup::subsim::common {
25 
26  template <class EVENT_DONE, class EVENT_ERR>
27  void EmulateEvent(const Config& config,
28  rad::SMAdapter& sm,
29  const std::string &cmd,
30  rad::ThreadActivity* activity) {
31 
32  // In case data is not found, it takes the default value
33  auto delay = config.GetReplyDelay(cmd).value_or(0);
34  auto repl_ok = config.GetReplyOk(cmd).value_or(true);
35  const auto msg = config.GetReplyErrorMessage(cmd).value_or("Generic Error");
36 
37  LOG4CPLUS_INFO(utils::bat::GetAppLogger(), "Reply msg: " << msg);
38  if (delay > 0) {
39  LOG4CPLUS_DEBUG(utils::bat::GetAppLogger(), "Reply delay: " << delay);
40  int total = 0;
41  int increment = int(delay/100);
42  LOG4CPLUS_DEBUG(utils::bat::GetAppLogger(), "Increment: " << increment);
43  while (total < delay) {
44  if (activity->IsStopRequested()) {
45  sm.PostEvent(rad::UniqueEvent(new EVENT_ERR(cmd + " has been stopped !" )));
46  return;
47  }
48  std::this_thread::sleep_for(std::chrono::milliseconds(increment));
49  total += increment;
50  LOG4CPLUS_DEBUG(utils::bat::GetAppLogger(), "Total Time: " << total << " [ms]");
51  }
52  }
53  if (repl_ok) {
54  sm.PostEvent(rad::UniqueEvent(new EVENT_DONE));
55  } else {
56  sm.PostEvent(rad::UniqueEvent(new EVENT_ERR(msg )));
57  }
58  }
59 
60 
61 } // namespace
62 
63 #include "utils.ipp"
64 #endif // SUP_SUBSIM_COMMON_UTILS_IPP_
Definition: config.hpp:70
std::optional< int > GetReplyDelay(const std::string &name) const
Definition: config.cpp:145
std::optional< std::string > GetReplyErrorMessage(const std::string &name) const
Definition: config.cpp:164
std::optional< bool > GetReplyOk(const std::string &name) const
Definition: config.cpp:120
Definition: actionMgr.cpp:32
void EmulateEvent(const Config &config, rad::SMAdapter &sm, const std::string &cmd, rad::ThreadActivity *activity)
Definition: utils.hpp:27
Config class header file.