25 #include <zmqpp/message.hpp> 32 std::vector<std::string> auth_sources,
33 std::vector<std::string> connect_to,
34 std::vector<std::string> bind_to,
36 : bus_sub_(ctx,
zmqpp::socket_type::sub)
37 , tcp_(ctx,
zmqpp::socket_type::stream)
38 , protocol_(
std::move(protocol_handler))
40 ASSERT_LOG(connect_to.empty() || bind_to.empty(),
41 "Cannot bind and connect at the same time.");
42 ASSERT_LOG(!connect_to.empty() || !bind_to.empty(),
"Cannot do nothing");
47 bus_sub_.connect(
"inproc://zmq-bus-pub");
48 for (
const auto &src : auth_sources)
67 msg << target.zmq_identity_;
70 auto data =
protocol_->build_cred_msg(card);
71 msg.add_raw(&data[0], data.size());
75 WARN(
"TCPNotifier: Protocol error: " << e.what());
78 auto ret =
tcp_.send(msg,
true);
81 ERROR(
"Sending to client would block.");
88 std::string routing_id;
91 assert(msg.parts() == 2);
92 msg >> routing_id >> data;
108 INFO(
"TCP-Notifier: New client connected.");
114 return info.zmq_identity_ ==
118 INFO(
"TCP-Notifier: Client disconnected.");
122 ASSERT_LOG(target,
"Why did we lose a target?");
125 INFO(
"Lost connection with client.");
127 INFO(
"Successfully connected to client.");
129 target->status_ = !target->status_;
137 while (
tcp_.receive(msg,
true))
148 return target.zmq_identity_ == routing_id;
166 WARN(
"Unexpected message content.");
169 msg >> src >> type >> card >> bits;
172 INFO(
"TCP-Notifier cannot handle this type of credential yet.");
175 auto wiegand_card = std::make_shared<Cred::RFIDCard>();
176 wiegand_card->card_id(card);
177 wiegand_card->nb_bits(bits);
183 const std::vector<std::string> &endpoints)
185 tcp_.set(zmqpp::socket_option::backlog, 5);
188 for (
auto &endpoint : endpoints)
190 INFO(
"TCP-Notifier binding to: tcp://" << endpoint);
191 tcp_.bind(
"tcp://" + endpoint);
196 for (
auto &endpoint : endpoints)
199 target.
url_ =
"tcp://" + endpoint;
204 targets_.push_back(std::move(target));
This define message formatting for data source SIMPLE_WIEGAND.
This is the header file for a generated source file, GitSHA1.cpp.
void handle_one(zmqpp::message &msg)
std::string zmq_identity_
TargetInfo * find_target(const std::string &routing_id)
Attempt to find a target from its routing_id.
Some information for each tcp server target.
bool act_as_server_
Are we a server or a client ?
void handle_credential(Cred::RFIDCard &card)
Notify the peers of the card credential.
void handle_tcp_msg()
Some event on our ZMQ Stream socket.
std::string green(const T &in)
std::unique_ptr< ProtocolHandler > ProtocolHandlerUPtr
All modules that provides features to Leosac shall be in this namespace.
zmqpp::socket bus_sub_
Read internal message bus.
#define ASSERT_LOG(cond, msg)
ProtocolHandlerUPtr protocol_
NotifierInstance(zmqpp::context &ctx, zmqpp::reactor &reactor, std::vector< std::string > auth_sources, std::vector< std::string > connect_to, std::vector< std::string > bind_to, ProtocolHandlerUPtr protocol_handler)
Create a new notifier instance.
void configure_tcp_socket(const std::vector< std::string > &endpoints)
zmqpp::socket tcp_
Stream socket used to connect to remote client we want to notify.
An specialized exception that ProtocolHandler can throw when converting the credential to a message f...
std::list< TargetInfo > targets_