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 /** Destructor. */
43 {
44 }
45 
46 void
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 
64 void
66 {
67  sig_rcvd_conn_.disconnect();
68  sig_recv_error_conn_.disconnect();
69  sig_send_error_conn_.disconnect();
70 }
71 
72 void
74 {
75 }
76 
77 void
78 GossipExampleReceiverThread::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 {
90  logger->log_warn(name(),
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  */
103 void
104 GossipExampleReceiverThread::handle_peer_recv_error(boost::asio::ip::udp::endpoint &endpoint,
105  std::string msg)
106 {
107  logger->log_warn(name(),
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  */
117 void
118 GossipExampleReceiverThread::handle_peer_send_error(std::string msg)
119 {
120  logger->log_warn(name(), "Failed to send peer message: %s", msg.c_str());
121 }
GossipExampleReceiverThread::GossipExampleReceiverThread
GossipExampleReceiverThread()
Constructor.
Definition: gex_receiver_thread.cpp:34
fawkes::GossipAspect
Definition: gossip.h:43
fawkes::Logger::log_info
virtual void log_info(const char *component, const char *format,...)=0
fawkes::BlockedTimingAspect
Definition: blocked_timing.h:56
fawkes::Thread::name
const char * name() const
Definition: thread.h:100
fawkes::LoggingAspect::logger
Logger * logger
Definition: logging.h:53
fawkes
fawkes::GossipAspect::gossip_group
RefPtr< GossipGroup > gossip_group
Definition: gossip.h:52
GossipExampleReceiverThread::init
virtual void init()
Initialize the thread.
Definition: gex_receiver_thread.cpp:47
fawkes::Logger::log_warn
virtual void log_warn(const char *component, const char *format,...)=0
fawkes::Thread
Definition: thread.h:45
GossipExampleReceiverThread::~GossipExampleReceiverThread
virtual ~GossipExampleReceiverThread()
Destructor.
Definition: gex_receiver_thread.cpp:42
GossipExampleReceiverThread::loop
virtual void loop()
Code to execute in the thread.
Definition: gex_receiver_thread.cpp:73
GossipExampleReceiverThread::finalize
virtual void finalize()
Finalize the thread.
Definition: gex_receiver_thread.cpp:65