Fawkes API  Fawkes Development Version
gossip_group.cpp
1 
2 /***************************************************************************
3  * gossip_group.cpp - Robot Group Communication - Gossip Group
4  *
5  * Created: Tue Mar 04 11:00: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. A runtime exception applies to
13  * this software (see LICENSE.GPL_WRE file mentioned below for details).
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
21  */
22 
23 #include <netcomm/service_discovery/service.h>
24 #include <netcomm/service_discovery/service_publisher.h>
25 #include <plugins/gossip/gossip/gossip_group.h>
26 #include <protobuf_comm/peer.h>
27 
28 #define GOSSIP_MDNSSD_SERVICE_NAME "_gossip._udp"
29 
30 namespace fawkes {
31 
32 /** @class GossipGroup <plugins/gossip/gossip/gossip_group.h>
33  * Gossip group communication handler.
34  * The group communication handler cares about joining groups and
35  * sending and receiving data.
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor.
40  * @param group_name name of the group to join
41  * @param peer_name local peer name to announce on the network, i.e. robot identifier
42  * @param port UDP port to listen on for messages
43  * @param service_publisher service publisher to announce group membership with
44  * @param crypto_key encryption key
45  * @param crypto_cipher cipher to use
46  */
47 GossipGroup::GossipGroup(std::string & group_name,
48  std::string & peer_name,
49  std::string & broadcast_address,
50  unsigned short broadcast_port,
51  ServicePublisher * service_publisher,
52  const std::string &crypto_key,
53  const std::string &crypto_cipher)
54 : name_(group_name), service_publisher_(service_publisher)
55 {
56  pb_peer_ =
57  std::shared_ptr<protobuf_comm::ProtobufBroadcastPeer>(new protobuf_comm::ProtobufBroadcastPeer(
58  broadcast_address, broadcast_port, crypto_key, crypto_cipher));
59 
60  service_ = std::shared_ptr<NetworkService>(
61  new NetworkService(peer_name.c_str(), GOSSIP_MDNSSD_SERVICE_NAME, broadcast_port));
62 
63  service_->add_txt("group=%s", group_name.c_str());
64  service_publisher_->publish_service(service_.get());
65 }
66 
67 /** Constructor.
68  * @param group_name name of the group to join
69  * @param peer_name local peer name to announce on the network, i.e. robot identifier
70  * @param send_port UDP port to send messages to
71  * @param recv_port UDP port to listen on for messages
72  * @param service_publisher service publisher to announce group membership with
73  * @param crypto_key encryption key
74  * @param crypto_cipher cipher to use
75  */
76 GossipGroup::GossipGroup(std::string & group_name,
77  std::string & peer_name,
78  std::string & broadcast_address,
79  unsigned short send_port,
80  unsigned short recv_port,
81  ServicePublisher * service_publisher,
82  const std::string &crypto_key,
83  const std::string &crypto_cipher)
84 : name_(group_name), service_publisher_(service_publisher)
85 {
86  pb_peer_ =
87  std::shared_ptr<protobuf_comm::ProtobufBroadcastPeer>(new protobuf_comm::ProtobufBroadcastPeer(
88  broadcast_address, send_port, recv_port, crypto_key, crypto_cipher));
89 
90  service_ = std::shared_ptr<NetworkService>(
91  new NetworkService(peer_name.c_str(), GOSSIP_MDNSSD_SERVICE_NAME, recv_port));
92 
93  service_->add_txt("group=%s", group_name.c_str());
94  service_publisher_->publish_service(service_.get());
95 }
96 
97 /** Destructor. */
98 GossipGroup::~GossipGroup()
99 {
100  service_publisher_->unpublish_service(service_.get());
101  service_.reset();
102  pb_peer_.reset();
103 }
104 
105 /** Send a message.
106  * @param peer peer to send message to
107  * @param m message to send
108  */
109 void
110 GossipGroup::send(std::string &peer, google::protobuf::Message &m)
111 {
112  pb_peer_->send(m);
113 }
114 
115 /** Broadcast a message to all peers in the group.
116  * @param m message to send
117  */
118 void
119 GossipGroup::broadcast(google::protobuf::Message &m)
120 {
121  pb_peer_->send(m);
122 }
123 
124 } // end namespace fawkes
protobuf_comm::ProtobufBroadcastPeer
Communicate by broadcasting protobuf messages.
Definition: peer.h:57
fawkes
Fawkes library namespace.