Fawkes API Fawkes Development Version
gossip_group_manager.cpp
1
2/***************************************************************************
3 * gossip_group_manager.cpp - Fawkes Gossip group manager
4 *
5 * Created: Fri Feb 28 16:55:24 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 <core/exception.h>
24#include <plugins/gossip/gossip/gossip_group.h>
25#include <plugins/gossip/gossip/gossip_group_manager.h>
26
27namespace fawkes {
28
29/** @class GossipGroupConfiguration <plugins/gossip/gossip/gossip_group_manager.h>
30 * Group configuration for initial groups.
31 */
32
33/** Constructor. */
35{
36}
37
38/** Constructor.
39 * @param name name of the group
40 * @param broadcast_address IPv4 address to broadcast to
41 * @param broadcast_port UDP port to listen on for the group
42 */
44 std::string & broadcast_address,
45 unsigned short broadcast_port)
46: name(name),
47 broadcast_addr(broadcast_address),
48 send_port(broadcast_port),
49 recv_port(broadcast_port)
50{
51}
52
53/** Constructor.
54 * @param name name of the group
55 * @param broadcast_address IPv4 address to broadcast to
56 * @param send_port UDP port to send messages to
57 * @param recv_port UDP port to listen on for the group
58 */
60 std::string & broadcast_address,
61 unsigned short send_port,
62 unsigned short recv_port)
63: name(name), broadcast_addr(broadcast_address), send_port(send_port), recv_port(recv_port)
64{
65}
66
67/** Copy contructor.
68 * @param c group configuration to copy
69 */
71: name(c.name),
72 broadcast_addr(c.broadcast_addr),
73 send_port(c.send_port),
74 recv_port(c.recv_port),
75 crypto_key(c.crypto_key),
76 crypto_cipher(c.crypto_cipher)
77{
78}
79
80/** Assignment operator.
81 * @param c group configuration to copy from
82 * @return reference to this instance
83 */
86{
87 name = c.name;
93
94 return *this;
95}
96
97/** @class GossipGroupManager <plugins/gossip/gossip/gossip_group_manager.h>
98 * Abstract class for a Gossip group manager.
99 * @author Tim Niemueller
100 */
101
102/** Constructor.
103 * @param service_name service name to announce for each group we join, this
104 * must be unique in the group and should identify the robot
105 * @param service_publisher service discovery publisher to announce groups
106 * @param initial_groups initial group configurations to join
107 */
109 std::string & service_name,
110 ServicePublisher * service_publisher,
111 std::map<std::string, GossipGroupConfiguration> &initial_groups)
112: service_name_(service_name), service_publisher_(service_publisher)
113{
114#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
115 for (auto g : initial_groups) {
116 create_group(g.second);
117 }
118#else
119 std::map<std::string, GossipGroupConfiguration>::iterator g;
120 for (g = initial_groups.begin(); g != initial_groups.end(); ++g) {
121 create_group(g->second);
122 }
123#endif
124}
125
126/** Destructor. */
128{
129}
130
131/** Join a group.
132 * @param name the name of the group to join
133 * @return a shared object to communicate with the group.
134 */
136GossipGroupManager::join_group(const std::string &name)
137{
138 if (groups_.find(name) == groups_.end()) {
139 // try to join group
140 }
141
142 if (groups_.find(name) != groups_.end()) {
143 return groups_[name];
144 } else {
145 // still not registered -> fail
146 throw Exception("Cannot register to group %s", name.c_str());
147 }
148}
149
150/** Leave a gossip group.
151 * @param group the gossip group to leave, the handle becomes invalid after this call.
152 */
153void
155{
156 group.reset();
157
158 /*
159 if (groups_.find(name) != groups_.end()) {
160 if (groups_[name].use_count() == 1) {
161 // only us, leave?
162 }
163 }
164 */
165}
166
167void
168GossipGroupManager::create_group(GossipGroupConfiguration &gc)
169{
170 if (gc.send_port == gc.recv_port) {
171 groups_[gc.name] = new GossipGroup(gc.name,
172 service_name_,
174 gc.recv_port,
175 service_publisher_,
176 gc.crypto_key,
177 gc.crypto_cipher);
178 } else {
179 groups_[gc.name] = new GossipGroup(gc.name,
180 service_name_,
182 gc.send_port,
183 gc.recv_port,
184 service_publisher_,
185 gc.crypto_key,
186 gc.crypto_cipher);
187 }
188}
189
190} // end namespace fawkes
Base class for exceptions in Fawkes.
Definition: exception.h:36
Group configuration for initial groups.
unsigned short send_port
UDP port to send messages to.
unsigned short recv_port
UDP port to list on for messages.
std::string name
name of the group
std::string crypto_cipher
encryption cipher
GossipGroupConfiguration & operator=(const GossipGroupConfiguration &c)
Assignment operator.
std::string crypto_key
encryption key
std::string broadcast_addr
Broadcast IP Addr.
GossipGroupManager(std::string &service_name, ServicePublisher *service_publisher, std::map< std::string, GossipGroupConfiguration > &initial_groups)
Constructor.
virtual void leave_group(RefPtr< GossipGroup > &group)
Leave a gossip group.
virtual RefPtr< GossipGroup > join_group(const std::string &name)
Join a group.
virtual ~GossipGroupManager()
Destructor.
Gossip group communication handler.
Definition: gossip_group.h:44
void reset()
Reset pointer.
Definition: refptr.h:455
Service publisher interface.
Fawkes library namespace.