Fawkes API  Fawkes Development Version
bbsync_plugin.cpp
1 
2 /***************************************************************************
3  * bbsync_plugin.cpp - Fawkes BlackBoardSynchronization Plugin
4  *
5  * Created: Fri Jun 29 11:47:53 2007 (on flight to RoboCup 2007, Atlanta)
6  * Copyright 2006 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.
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 file in the doc directory.
21  */
22 
23 #include "bbsync_plugin.h"
24 
25 #include "sync_thread.h"
26 
27 #include <set>
28 
29 using namespace fawkes;
30 
31 /** @class BlackBoardSynchronizationPlugin "bbsync_plugin.h"
32  * BlackBoard synchronization plugin.
33  * This plugin synchronizes one or more (or even all) interfaces of two or
34  * more Fawkes instances over the network.
35  *
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor.
40  * @param config Fawkes configuration
41  */
43 : Plugin(config)
44 {
45  std::set<std::string> peers;
46  std::set<std::string> ignored_peers;
47 
48  std::string prefix = "/fawkes/bbsync/";
49  std::string peers_prefix = prefix + "peers/";
50 
51  Configuration::ValueIterator *i = config->search(peers_prefix.c_str());
52  while (i->next()) {
53  std::string peer = std::string(i->path()).substr(peers_prefix.length());
54  peer = peer.substr(0, peer.find("/"));
55 
56  if ((peers.find(peer) == peers.end()) && (ignored_peers.find(peer) == ignored_peers.end())) {
57  std::string peer_prefix = peers_prefix + peer + "/";
58 
59  bool active = true;
60  try {
61  active = config->get_bool((peer_prefix + "active").c_str());
62  } catch (Exception &e) {
63  } // ignored, assume enabled
64 
65  if (active) {
66  //printf("Adding sync thread for peer %s\n", peer.c_str());
68  sync_thread = new BlackBoardSynchronizationThread(prefix, peer_prefix, peer);
69  peers.insert(peer);
70  thread_list.push_back(sync_thread);
71  } else {
72  //printf("Ignoring sync peer %s\n", peer.c_str());
73  ignored_peers.insert(peer);
74  }
75  }
76  }
77  delete i;
78 
79  if (thread_list.empty()) {
80  throw Exception("No synchronization peers configured, aborting");
81  }
82 }
83 
84 PLUGIN_DESCRIPTION("Synchronize with remote Fawkes BlackBoards")
fawkes::Plugin::config
Configuration * config
Fawkes configuration.
Definition: plugin.h:62
fawkes::Plugin::thread_list
ThreadList thread_list
Thread list member.
Definition: plugin.h:57
fawkes::ThreadList::push_back
void push_back(Thread *thread)
Add thread to the end.
Definition: thread_list.cpp:777
fawkes::Configuration::get_bool
virtual bool get_bool(const char *path)=0
fawkes::Configuration::ValueIterator
Definition: config.h:75
fawkes::Configuration
Definition: config.h:68
fawkes::Configuration::search
virtual ValueIterator * search(const char *path)=0
BlackBoardSynchronizationPlugin
Definition: bbsync_plugin.h:27
fawkes
fawkes::Configuration::ValueIterator::path
virtual const char * path() const =0
fawkes::Configuration::ValueIterator::next
virtual bool next()=0
BlackBoardSynchronizationThread
Definition: sync_thread.h:43
fawkes::Plugin
Definition: plugin.h:37
BlackBoardSynchronizationPlugin::BlackBoardSynchronizationPlugin
BlackBoardSynchronizationPlugin(fawkes::Configuration *config)
Constructor.
Definition: bbsync_plugin.cpp:42
fawkes::Exception
Definition: exception.h:39