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 
27 namespace fawkes {
28 
29 /** @class GossipGroupConfiguration <plugins/gossip/gossip/gossip_group_manager.h>
30  * Group configuration for initial groups.
31  */
32 
33 /** Constructor. */
34 GossipGroupConfiguration::GossipGroupConfiguration() : send_port(0), recv_port(0)
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;
89  send_port = c.send_port;
90  recv_port = c.recv_port;
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  */
136 GossipGroupManager::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  */
153 void
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 
167 void
168 GossipGroupManager::create_group(GossipGroupConfiguration &gc)
169 {
170  if (gc.send_port == gc.recv_port) {
171  groups_[gc.name] = new GossipGroup(gc.name,
172  service_name_,
173  gc.broadcast_addr,
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_,
181  gc.broadcast_addr,
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
fawkes::GossipGroupConfiguration::crypto_key
std::string crypto_key
encryption key
Definition: gossip_group_manager.h:62
fawkes::GossipGroupConfiguration::recv_port
unsigned short recv_port
UDP port to list on for messages.
Definition: gossip_group_manager.h:61
fawkes::RefPtr< fawkes::GossipGroup >
fawkes::GossipGroupManager::join_group
virtual RefPtr< GossipGroup > join_group(const std::string &name)
Join a group.
Definition: gossip_group_manager.cpp:139
fawkes::GossipGroupConfiguration::name
std::string name
name of the group
Definition: gossip_group_manager.h:58
fawkes::GossipGroupManager::leave_group
virtual void leave_group(RefPtr< GossipGroup > &group)
Leave a gossip group.
Definition: gossip_group_manager.cpp:157
fawkes::GossipGroupConfiguration::GossipGroupConfiguration
GossipGroupConfiguration()
Constructor.
Definition: gossip_group_manager.cpp:37
fawkes::GossipGroupConfiguration::operator=
GossipGroupConfiguration & operator=(const GossipGroupConfiguration &c)
Assignment operator.
Definition: gossip_group_manager.cpp:88
fawkes::GossipGroupConfiguration::send_port
unsigned short send_port
UDP port to send messages to.
Definition: gossip_group_manager.h:60
fawkes::GossipGroupConfiguration
Definition: gossip_group_manager.h:43
fawkes
fawkes::ServicePublisher
Definition: service_publisher.h:35
fawkes::GossipGroup
Definition: gossip_group.h:42
fawkes::GossipGroupConfiguration::crypto_cipher
std::string crypto_cipher
encryption cipher
Definition: gossip_group_manager.h:63
fawkes::GossipGroupConfiguration::broadcast_addr
std::string broadcast_addr
Broadcast IP Addr.
Definition: gossip_group_manager.h:59
fawkes::GossipGroupManager::~GossipGroupManager
virtual ~GossipGroupManager()
Destructor.
Definition: gossip_group_manager.cpp:130
fawkes::GossipGroupManager::GossipGroupManager
GossipGroupManager(std::string &service_name, ServicePublisher *service_publisher, std::map< std::string, GossipGroupConfiguration > &initial_groups)
Constructor.
Definition: gossip_group_manager.cpp:111
fawkes::RefPtr::reset
void reset()
Reset pointer.
Definition: refptr.h:459