Leosac  0.8.0
Open Source Access Control
InstrumentationModule.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 
23 using namespace Leosac::Module::Instrumentation;
24 
26  zmqpp::socket *pipe,
27  const boost::property_tree::ptree &cfg,
28  CoreUtilsPtr utils)
29  : BaseModule(ctx, pipe, cfg, utils)
30  , bus_push_(ctx, zmqpp::socket_type::push)
31  , controller_(ctx, zmqpp::socket_type::router)
32 {
33  std::string bind_str =
34  "ipc://" +
35  config_.get_child("module_config").get<std::string>("ipc_endpoint");
36  controller_.bind(bind_str);
37  INFO("Binding to: " << bind_str);
38  bus_push_.connect("inproc://zmq-bus-pull");
40  std::bind(&InstrumentationModule::handle_command, this));
41 }
42 
44 {
45  INFO("COMMAND AVAILABLE");
46  zmqpp::message msg;
47 
48  controller_.receive(msg);
49  std::string identity;
50  std::string str;
51 
52  msg >> identity >> str;
53  if (str == "GPIO")
54  {
55  handle_gpio_command(&msg);
56  }
57  else
58  {
59  // since this is a test/debug module, lets die if we receive bad input.
60  assert(0);
61  }
62 }
63 
65 {
66  assert(str);
67  std::string cmd;
68  std::string gpio_name;
69 
70  *str >> gpio_name >> cmd;
71  DEBUG("GPIO CMD: " << gpio_name << ", " << cmd);
72  if (cmd == "ON")
73  {
74  bus_push_.send(zmqpp::message() << ("S_" + gpio_name) << "ON");
75  }
76  else if (cmd == "OFF")
77  {
78  bus_push_.send(zmqpp::message() << ("S_" + gpio_name) << "OFF");
79  }
80  else if (cmd == "INT")
81  {
82  bus_push_.send(zmqpp::message() << std::string("S_INT:" + gpio_name));
83  }
84 }
Leosac::Module::Instrumentation::InstrumentationModule::handle_gpio_command
void handle_gpio_command(zmqpp::message *str)
Definition: InstrumentationModule.cpp:64
Leosac::Module::BaseModule
Base class for module implementation.
Definition: BaseModule.hpp:110
InstrumentationModule.hpp
zmqpp
Definition: CoreUtils.hpp:27
Leosac::Module::Instrumentation::InstrumentationModule::handle_command
void handle_command()
Definition: InstrumentationModule.cpp:43
DEBUG
@ DEBUG
Definition: log.hpp:35
INFO
@ INFO
Definition: log.hpp:34
Leosac::Module::Instrumentation::InstrumentationModule::InstrumentationModule
InstrumentationModule(zmqpp::context &ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
Definition: InstrumentationModule.cpp:25
Leosac::Module::Instrumentation::InstrumentationModule::bus_push_
zmqpp::socket bus_push_
Definition: InstrumentationModule.hpp:50
Leosac::Module::BaseModule::config_
boost::property_tree::ptree config_
The configuration tree passed to the start_module function.
Definition: BaseModule.hpp:193
Leosac::Module::BaseModule::reactor_
zmqpp::reactor reactor_
The reactor object we poll() on in the main loop.
Definition: BaseModule.hpp:214
Leosac::Module::Instrumentation
The instrumentation expose some internal of the program through IPC.
Definition: InstrumentationModule.hpp:31
log.hpp
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
Leosac::Module::Instrumentation::InstrumentationModule::controller_
zmqpp::socket controller_
IPC ROUTER socket.
Definition: InstrumentationModule.hpp:55