Leosac  0.8.0
Open Source Access Control
bench-toggle.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
21 #include "tools/log.hpp"
22 #include "zmqpp/zmqpp.hpp"
23 #include <boost/property_tree/ptree.hpp>
24 #include <memory>
25 #include <zmqpp/context.hpp>
26 #include <zmqpp/message.hpp>
27 
32 bool send_request(std::shared_ptr<zmqpp::socket> target, const std::string &cmd1)
33 {
34  target->send(cmd1);
35 
36  zmqpp::poller p;
37 
38  p.add(*target.get(), zmqpp::poller::poll_in);
39  if (!p.poll(1000))
40  {
41  INFO("No response from target (" << target << ")");
42  return false;
43  }
44 
45  zmqpp::message_t m;
46  target->receive(m);
47 
48  std::string rep;
49  m >> rep;
50  INFO("response = " << rep);
51  return true;
52 }
53 
60 extern "C" __attribute__((visibility("default"))) bool
61 start_module(zmqpp::socket *pipe, boost::property_tree::ptree cfg,
62  zmqpp::context &zmq_ctx)
63 {
64  INFO("Bench-Toggle will start");
65  std::string endpoint_to_bench =
66  cfg.get_child("module_config").get_child("target").data();
67  int itr = cfg.get_child("module_config").get<int>("iterations");
68  int wait_for = cfg.get_child("module_config").get<int>("pause");
69  pipe->send(zmqpp::signal::ok);
70 
71  // fixme since module order initialization is not defined we need to wait.
72  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
73 
74  std::shared_ptr<zmqpp::socket> sock(
75  new zmqpp::socket(zmq_ctx, zmqpp::socket_type::req));
76 
77  sock->connect("inproc://" + endpoint_to_bench);
78  INFO("should take about " << itr * wait_for << "ms to run");
79  std::chrono::system_clock::time_point clock = std::chrono::system_clock::now();
80  Leosac::Hardware::FGPIO my_gpio(zmq_ctx, endpoint_to_bench);
81  for (int i = 0; i < itr; i++)
82  {
83  // send_request(sock, "TOGGLE");
84  my_gpio.toggle();
85  std::this_thread::sleep_for(std::chrono::milliseconds(wait_for));
86  }
87  auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
88  std::chrono::system_clock::now() - clock);
89  DEBUG("TOOK " << elapsed.count() << "ms");
90 
91  INFO("Module Bench-Toggle shutting down");
92  return true;
93 }
FGPIO.hpp
zmqpp
Definition: CoreUtils.hpp:27
DEBUG
@ DEBUG
Definition: log.hpp:35
INFO
@ INFO
Definition: log.hpp:34
Leosac::Hardware::FGPIO::toggle
bool toggle()
Toggle the GPIO value by sending a message to the backend GPIO impl.
Definition: FGPIO.cpp:79
Leosac::Hardware::FGPIO
A Facade to a GPIO object.
Definition: FGPIO.hpp:45
send_request
bool send_request(std::shared_ptr< zmqpp::socket > target, const std::string &cmd1)
Send the request to the target and handle the response.
Definition: bench-toggle.cpp:32
__attribute__
__attribute__((visibility("default"))) bool start_module(zmqpp
pipe is pipe back to module manager.
Definition: bench-toggle.cpp:60
log.hpp