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 <plugins/gossip/gossip/gossip_group.h>
24 
25 #include <netcomm/service_discovery/service.h>
26 #include <netcomm/service_discovery/service_publisher.h>
27 
28 #include <protobuf_comm/peer.h>
29 
30 #define GOSSIP_MDNSSD_SERVICE_NAME "_gossip._udp"
31 
32 
33 namespace fawkes {
34 #if 0 /* just to make Emacs auto-indent happy */
35 }
36 #endif
37 
38 /** @class GossipGroup <plugins/gossip/gossip/gossip_group.h>
39  * Gossip group communication handler.
40  * The group communication handler cares about joining groups and
41  * sending and receiving data.
42  * @author Tim Niemueller
43  */
44 
45 /** Constructor.
46  * @param group_name name of the group to join
47  * @param peer_name local peer name to announce on the network, i.e. robot identifier
48  * @param port UDP port to listen on for messages
49  * @param service_publisher service publisher to announce group membership with
50  * @param crypto_key encryption key
51  * @param crypto_cipher cipher to use
52  */
53 GossipGroup::GossipGroup(std::string &group_name, std::string &peer_name,
54  std::string &broadcast_address, unsigned short broadcast_port,
55  ServicePublisher *service_publisher,
56  const std::string &crypto_key, const std::string &crypto_cipher)
57  : name_(group_name), service_publisher_(service_publisher)
58 {
59  pb_peer_ =
60  std::shared_ptr<protobuf_comm::ProtobufBroadcastPeer>(
61  new protobuf_comm::ProtobufBroadcastPeer(broadcast_address, broadcast_port,
62  crypto_key, crypto_cipher));
63 
64  service_ =
65  std::shared_ptr<NetworkService>(new NetworkService(peer_name.c_str(),
66  GOSSIP_MDNSSD_SERVICE_NAME,
67  broadcast_port));
68 
69  service_->add_txt("group=%s", group_name.c_str());
70  service_publisher_->publish_service(service_.get());
71 }
72 
73 
74 /** Constructor.
75  * @param group_name name of the group to join
76  * @param peer_name local peer name to announce on the network, i.e. robot identifier
77  * @param send_port UDP port to send messages to
78  * @param recv_port UDP port to listen on for messages
79  * @param service_publisher service publisher to announce group membership with
80  * @param crypto_key encryption key
81  * @param crypto_cipher cipher to use
82  */
83 GossipGroup::GossipGroup(std::string &group_name, std::string &peer_name,
84  std::string &broadcast_address,
85  unsigned short send_port, unsigned short recv_port,
86  ServicePublisher *service_publisher,
87  const std::string &crypto_key, const std::string &crypto_cipher)
88  : name_(group_name), service_publisher_(service_publisher)
89 {
90  pb_peer_ =
91  std::shared_ptr<protobuf_comm::ProtobufBroadcastPeer>(
92  new protobuf_comm::ProtobufBroadcastPeer(broadcast_address, send_port, recv_port,
93  crypto_key, crypto_cipher));
94 
95  service_ =
96  std::shared_ptr<NetworkService>(new NetworkService(peer_name.c_str(),
97  GOSSIP_MDNSSD_SERVICE_NAME,
98  recv_port));
99 
100  service_->add_txt("group=%s", group_name.c_str());
101  service_publisher_->publish_service(service_.get());
102 }
103 
104 
105 /** Destructor. */
106 GossipGroup::~GossipGroup()
107 {
108  service_publisher_->unpublish_service(service_.get());
109  service_.reset();
110  pb_peer_.reset();
111 }
112 
113 
114 /** Send a message.
115  * @param peer peer to send message to
116  * @param m message to send
117  */
118 void
119 GossipGroup::send(std::string &peer,
120  google::protobuf::Message &m)
121 {
122  pb_peer_->send(m);
123 }
124 
125 
126 /** Broadcast a message to all peers in the group.
127  * @param m message to send
128  */
129 void
130 GossipGroup::broadcast(google::protobuf::Message &m)
131 {
132  pb_peer_->send(m);
133 }
134 
135 
136 } // end namespace fawkes
Fawkes library namespace.
Communicate by broadcasting protobuf messages.
Definition: peer.h:60