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 
26 using 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),
36  BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_ACT),
37  GossipAspect("example")
38 {
39 }
40 
41 
42 /** Destructor. */
44 {
45 }
46 
47 
48 void
50 {
51  try {
52  gossip_group->message_register().add_message_type<gossip_example::TestMessage>();
53  } catch (std::runtime_error &e) {} // ignore, probably already added
54 
55  sig_rcvd_conn_ =
56  gossip_group->signal_received()
57  .connect(boost::bind(&GossipExampleReceiverThread::handle_peer_msg, this, _1, _2, _3, _4));
58 
59  sig_recv_error_conn_ =
60  gossip_group->signal_recv_error()
61  .connect(boost::bind(&GossipExampleReceiverThread::handle_peer_recv_error, this, _1, _2));
62 
63  sig_send_error_conn_ =
64  gossip_group->signal_send_error()
65  .connect(boost::bind(&GossipExampleReceiverThread::handle_peer_send_error, this, _1));
66 }
67 
68 
69 void
71 {
72  sig_rcvd_conn_.disconnect();
73  sig_recv_error_conn_.disconnect();
74  sig_send_error_conn_.disconnect();
75 }
76 
77 
78 void
80 {
81 }
82 
83 void
84 GossipExampleReceiverThread::handle_peer_msg(boost::asio::ip::udp::endpoint &endpoint,
85  uint16_t component_id, uint16_t msg_type,
86  std::shared_ptr<google::protobuf::Message> msg)
87 {
88  if (component_id == gossip_example::TestMessage::COMP_ID &&
89  msg_type == gossip_example::TestMessage::MSG_TYPE)
90  {
91  std::shared_ptr<gossip_example::TestMessage> tm =
92  std::dynamic_pointer_cast<gossip_example::TestMessage>(msg);
93  if (tm) {
94  logger->log_info(name(), "Received message with counter %u", tm->counter());
95  } else {
96  logger->log_warn(name(), "Message with proper component_id and msg_type, but no conversion. "
97  " Wrong component ID/message type to C++ type mapping?");
98  }
99  } else {
100  logger->log_warn(name(), "Unknown message received: %u:%u", component_id, msg_type);
101  }
102 }
103 
104 /** Handle error during peer message processing.
105  * @param endpoint endpoint of incoming message
106  * @param msg error message
107  */
108 void
109 GossipExampleReceiverThread::handle_peer_recv_error(boost::asio::ip::udp::endpoint &endpoint,
110  std::string msg)
111 {
112  logger->log_warn(name(), "Failed to receive peer message from %s:%u: %s",
113  endpoint.address().to_string().c_str(), endpoint.port(), msg.c_str());
114 }
115 
116 /** Handle error during peer message processing.
117  * @param msg error message
118  */
119 void
120 GossipExampleReceiverThread::handle_peer_send_error(std::string msg)
121 {
122  logger->log_warn(name(), "Failed to send peer message: %s", msg.c_str());
123 }
124 
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Fawkes library namespace.
virtual void loop()
Code to execute in the thread.
Thread class encapsulation of pthreads.
Definition: thread.h:42
Thread aspect to communicate with a group of robots.
Definition: gossip.h:39
virtual void init()
Initialize the thread.
virtual ~GossipExampleReceiverThread()
Destructor.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
Thread aspect to use blocked timing.
RefPtr< GossipGroup > gossip_group
Gossip group to communicate with other robots.
Definition: gossip.h:48
const char * name() const
Get name of thread.
Definition: thread.h:95
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void finalize()
Finalize the thread.