Leosac  0.8.0
Open Source Access Control
StdinControllerModule.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 
22 using namespace Leosac::Module;
23 
25  zmqpp::socket *pipe,
26  boost::property_tree::ptree const &cfg,
27  CoreUtilsPtr utils)
28  : BaseModule(ctx, pipe, cfg, utils)
29 {
30  reactor_.add(0, std::bind(&StdinControllerModule::handleStdin, this));
31 }
32 
34 {
35  std::array<char, 4096> txt;
36  std::cin.getline(&txt[0], 4096);
37  std::string tmp(&txt[0]);
38  std::stringstream ss(tmp);
39 
40  std::string target;
41  std::string tmp2;
42  std::vector<std::string> cmds;
43 
44  ss >> target;
45  while (ss >> tmp2)
46  cmds.push_back(tmp2);
47  if (!target.empty())
48  {
49  if (endpoints_.count(target) == 0)
50  {
51  endpoints_[target] = std::shared_ptr<zmqpp::socket>(
52  new zmqpp::socket(ctx_, zmqpp::socket_type::req));
53  endpoints_[target]->connect("inproc://" + target);
54  }
55 
56  DEBUG("Read {" << std::string(&txt[0]) << "}, target = " << target);
57 
58  if (!send_request(endpoints_[target], cmds))
59  {
60  endpoints_.erase(target);
61  }
62  }
63 }
64 
65 static bool is_number(const std::string &s)
66 {
67  return !s.empty() && std::find_if(s.begin(), s.end(), [](char c) {
68  return !std::isdigit(c);
69  }) == s.end();
70 }
71 
72 bool StdinControllerModule::send_request(std::shared_ptr<zmqpp::socket> target,
73  const std::vector<std::string> &cmds)
74 {
75  zmqpp::message msg;
76 
77  for (const auto &str : cmds)
78  {
79  if (is_number(str))
80  {
81  int64_t nbr;
82  std::stringstream ss(str);
83  ss >> nbr;
84  msg << nbr;
85  }
86  else
87  {
88  msg << str;
89  }
90  }
91  target->send(msg);
92 
93  zmqpp::poller p;
94 
95  p.add(*target.get(), zmqpp::poller::poll_in);
96  if (!p.poll(1000))
97  {
98  WARN("No response from target (" << target << ")");
99  return false;
100  }
101  // handle response
102  zmqpp::message_t m;
103  target->receive(m);
104 
105  std::string rep;
106  m >> rep;
107  INFO("response = " << rep);
108  return true;
109 }
Leosac::Module::BaseModule
Base class for module implementation.
Definition: BaseModule.hpp:110
WARN
@ WARN
Definition: log.hpp:33
DEBUG
@ DEBUG
Definition: log.hpp:35
INFO
@ INFO
Definition: log.hpp:34
Leosac::Module
All modules that provides features to Leosac shall be in this namespace.
Leosac::Module::BaseModule::reactor_
zmqpp::reactor reactor_
The reactor object we poll() on in the main loop.
Definition: BaseModule.hpp:214
Leosac::Module::StdinControllerModule::handleStdin
void handleStdin(void)
We can read from standard input;.
Definition: StdinControllerModule.cpp:33
StdinControllerModule.hpp
Leosac::Module::StdinControllerModule::StdinControllerModule
StdinControllerModule(zmqpp::context &ctx, zmqpp::socket *pipe, boost::property_tree::ptree const &cfg, CoreUtilsPtr utils)
Definition: StdinControllerModule.cpp:24
Leosac::Module::StdinControllerModule::send_request
bool send_request(std::shared_ptr< zmqpp::socket > target, const std::vector< std::string > &cmds)
Send the request to the target and handle the response.
Definition: StdinControllerModule.cpp:72
Leosac::Module::BaseModule::ctx_
zmqpp::context & ctx_
A reference to the ZeroMQ context in case you need it to create additional socket.
Definition: BaseModule.hpp:183
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
Leosac::Module::StdinControllerModule::endpoints_
std::map< std::string, std::shared_ptr< zmqpp::socket > > endpoints_
Definition: StdinControllerModule.hpp:52