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 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <plugins/gossip/gossip/gossip_group_manager.h>
25 #include <plugins/gossip/gossip/gossip_group.h>
26 #include <core/exception.h>
27 
28 namespace fawkes {
29 #if 0 /* just to make Emacs auto-indent happy */
30 }
31 #endif
32 
33 /** @class GossipGroupConfiguration <plugins/gossip/gossip/gossip_group_manager.h>
34  * Group configuration for initial groups.
35  */
36 
37 
38 /** Constructor. */
40  : send_port(0), recv_port(0)
41 {
42 }
43 
44 /** Constructor.
45  * @param name name of the group
46  * @param broadcast_address IPv4 address to broadcast to
47  * @param broadcast_port UDP port to listen on for the group
48  */
50  std::string &broadcast_address,
51  unsigned short broadcast_port)
52  : name(name), broadcast_addr(broadcast_address),
53  send_port(broadcast_port), recv_port(broadcast_port)
54 {
55 }
56 
57 /** Constructor.
58  * @param name name of the group
59  * @param broadcast_address IPv4 address to broadcast to
60  * @param send_port UDP port to send messages to
61  * @param recv_port UDP port to listen on for the group
62  */
64  std::string &broadcast_address,
65  unsigned short send_port,
66  unsigned short recv_port)
67  : name(name), broadcast_addr(broadcast_address),
68  send_port(send_port), recv_port(recv_port)
69 {
70 }
71 
72 /** Copy contructor.
73  * @param c group configuration to copy
74  */
78 {
79 }
80 
81 
82 /** @class GossipGroupManager <plugins/gossip/gossip/gossip_group_manager.h>
83  * Abstract class for a Gossip group manager.
84  * @author Tim Niemueller
85  */
86 
87 /** Constructor.
88  * @param service_name service name to announce for each group we join, this
89  * must be unique in the group and should identify the robot
90  * @param service_publisher service discovery publisher to announce groups
91  * @param initial_groups initial group configurations to join
92  */
93 GossipGroupManager::GossipGroupManager(std::string &service_name,
94  ServicePublisher *service_publisher,
95  std::map<std::string, GossipGroupConfiguration> &initial_groups)
96  : service_name_(service_name), service_publisher_(service_publisher)
97 {
98 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
99  for (auto g : initial_groups) {
100  create_group(g.second);
101  }
102 #else
103  std::map<std::string, GossipGroupConfiguration>::iterator g;
104  for (g = initial_groups.begin(); g != initial_groups.end(); ++g) {
105  create_group(g->second);
106  }
107 #endif
108 }
109 
110 
111 /** Destructor. */
113 {
114 }
115 
116 
117 /** Join a group.
118  * @param name the name of the group to join
119  * @return a shared object to communicate with the group.
120  */
122 GossipGroupManager::join_group(const std::string &name)
123 {
124  if (groups_.find(name) == groups_.end()) {
125  // try to join group
126  }
127 
128  if (groups_.find(name) != groups_.end()) {
129  return groups_[name];
130  } else {
131  // still not registered -> fail
132  throw Exception("Cannot register to group %s", name.c_str());
133  }
134 }
135 
136 /** Leave a gossip group.
137  * @param group the gossip group to leave, the handle becomes invalid after this call.
138  */
139 void
141 {
142  group.reset();
143 
144  /*
145  if (groups_.find(name) != groups_.end()) {
146  if (groups_[name].use_count() == 1) {
147  // only us, leave?
148  }
149  }
150  */
151 }
152 
153 
154 void
155 GossipGroupManager::create_group(GossipGroupConfiguration &gc)
156 {
157  if (gc.send_port == gc.recv_port) {
158  groups_[gc.name] = new GossipGroup(gc.name, service_name_,
159  gc.broadcast_addr, gc.recv_port,
160  service_publisher_,
161  gc.crypto_key, gc.crypto_cipher);
162  } else {
163  groups_[gc.name] = new GossipGroup(gc.name, service_name_,
164  gc.broadcast_addr, gc.send_port,
165  gc.recv_port, service_publisher_,
166  gc.crypto_key, gc.crypto_cipher);
167  }
168 }
169 
170 
171 } // end namespace fawkes
std::string crypto_key
encryption key
std::string name
name of the group
Service publisher interface.
virtual void leave_group(RefPtr< GossipGroup > &group)
Leave a gossip group.
Fawkes library namespace.
virtual RefPtr< GossipGroup > join_group(const std::string &name)
Join a group.
unsigned short recv_port
UDP port to list on for messages.
Group configuration for initial groups.
std::string crypto_cipher
encryption cipher
void reset()
Reset pointer.
Definition: refptr.h:464
Base class for exceptions in Fawkes.
Definition: exception.h:36
Gossip group communication handler.
Definition: gossip_group.h:46
virtual ~GossipGroupManager()
Destructor.
unsigned short send_port
UDP port to send messages to.
std::string broadcast_addr
Broadcast IP Addr.
GossipGroupManager(std::string &service_name, ServicePublisher *service_publisher, std::map< std::string, GossipGroupConfiguration > &initial_groups)
Constructor.