Fawkes API Fawkes Development Version
gex_receiver_thread.cpp
1
2/***************************************************************************
3 * gex_receiver_thread.cpp - Gossip Example Plugin - Receiver
4 *
5 * Created: Thu Mar 06 10:40:11 2014
6 * Copyright 2006-2014 Tim Niemueller [www.niemueller.de]
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#include "gex_receiver_thread.h"
23
24#include "TestMessage.pb.h"
25
26using namespace fawkes;
27
28/** @class GossipExampleReceiverThread "clips-protobuf-thread.h"
29 * Gossip Example Plugin Thread - Receiver.
30 * @author Tim Niemueller
31 */
32
33/** Constructor. */
35: Thread("GossipExampleReceiverThread", Thread::OPMODE_WAITFORWAKEUP),
37 GossipAspect("example")
38{
39}
40
41/** Destructor. */
43{
44}
45
46void
48{
49 try {
50 gossip_group->message_register().add_message_type<gossip_example::TestMessage>();
51 } catch (std::runtime_error &e) {
52 } // ignore, probably already added
53
54 sig_rcvd_conn_ = gossip_group->signal_received().connect(
55 boost::bind(&GossipExampleReceiverThread::handle_peer_msg, this, _1, _2, _3, _4));
56
57 sig_recv_error_conn_ = gossip_group->signal_recv_error().connect(
58 boost::bind(&GossipExampleReceiverThread::handle_peer_recv_error, this, _1, _2));
59
60 sig_send_error_conn_ = gossip_group->signal_send_error().connect(
61 boost::bind(&GossipExampleReceiverThread::handle_peer_send_error, this, _1));
62}
63
64void
66{
67 sig_rcvd_conn_.disconnect();
68 sig_recv_error_conn_.disconnect();
69 sig_send_error_conn_.disconnect();
70}
71
72void
74{
75}
76
77void
78GossipExampleReceiverThread::handle_peer_msg(boost::asio::ip::udp::endpoint &endpoint,
79 uint16_t component_id,
80 uint16_t msg_type,
81 std::shared_ptr<google::protobuf::Message> msg)
82{
83 if (component_id == gossip_example::TestMessage::COMP_ID
84 && msg_type == gossip_example::TestMessage::MSG_TYPE) {
85 std::shared_ptr<gossip_example::TestMessage> tm =
86 std::dynamic_pointer_cast<gossip_example::TestMessage>(msg);
87 if (tm) {
88 logger->log_info(name(), "Received message with counter %u", tm->counter());
89 } else {
91 "Message with proper component_id and msg_type, but no conversion. "
92 " Wrong component ID/message type to C++ type mapping?");
93 }
94 } else {
95 logger->log_warn(name(), "Unknown message received: %u:%u", component_id, msg_type);
96 }
97}
98
99/** Handle error during peer message processing.
100 * @param endpoint endpoint of incoming message
101 * @param msg error message
102 */
103void
104GossipExampleReceiverThread::handle_peer_recv_error(boost::asio::ip::udp::endpoint &endpoint,
105 std::string msg)
106{
108 "Failed to receive peer message from %s:%u: %s",
109 endpoint.address().to_string().c_str(),
110 endpoint.port(),
111 msg.c_str());
112}
113
114/** Handle error during peer message processing.
115 * @param msg error message
116 */
117void
118GossipExampleReceiverThread::handle_peer_send_error(std::string msg)
119{
120 logger->log_warn(name(), "Failed to send peer message: %s", msg.c_str());
121}
virtual void finalize()
Finalize the thread.
virtual ~GossipExampleReceiverThread()
Destructor.
virtual void init()
Initialize the thread.
virtual void loop()
Code to execute in the thread.
Thread aspect to use blocked timing.
Thread aspect to communicate with a group of robots.
Definition: gossip.h:38
RefPtr< GossipGroup > gossip_group
Gossip group to communicate with other robots.
Definition: gossip.h:46
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
Fawkes library namespace.