Fawkes API  Fawkes Development Version
sync_thread.h
1 
2 /***************************************************************************
3  * sync_thread.h - Fawkes BlackBoard Synchronization Thread
4  *
5  * Created: Thu Jun 04 18:10:17 2009
6  * Copyright 2006-2009 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 #ifndef _PLUGINS_BBSYNC_SYNC_THREAD_H_
24 #define _PLUGINS_BBSYNC_SYNC_THREAD_H_
25 
26 #include "sync_listener.h"
27 #include "writer_listener.h"
28 
29 #include <aspect/blackboard.h>
30 #include <aspect/clock.h>
31 #include <aspect/configurable.h>
32 #include <aspect/logging.h>
33 #include <core/threading/thread.h>
34 #include <core/utils/lock_map.h>
35 
36 #include <map>
37 #include <string>
38 #include <utility>
39 
40 namespace fawkes {
41 class TimeWait;
42 }
43 
45  public fawkes::LoggingAspect,
48  public fawkes::ClockAspect
49 {
50 public:
51  BlackBoardSynchronizationThread(std::string &bbsync_cfg_prefix,
52  std::string &peer_cfg_prefix,
53  std::string &peer);
55 
56  virtual void init();
57  virtual void loop();
58  virtual void finalize();
59 
60  void writer_added(fawkes::Interface *interface) throw();
61  void writer_removed(fawkes::Interface *interface) throw();
62 
63  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
64 protected:
65  virtual void
66  run()
67  {
68  Thread::run();
69  }
70 
71 private:
72  /** Interface combo struct */
73  typedef struct
74  {
75  std::string type; /**< Combo type */
76  std::string reader_id; /**< reader interface ID */
77  std::string writer_id; /**< writer interface ID */
78  bool remote_writer; /**< true if remote writer */
79  } combo_t;
80 
81  class InterfaceInfo
82  {
83  public:
84  /** Combo configuration */
85  combo_t *combo;
86  /** Writing interface */
87  fawkes::Interface *writer;
88  /** Blackboard to read from */
89  fawkes::BlackBoard *reader_bb;
90  /** Blackboard to write to */
91  fawkes::BlackBoard *writer_bb;
92 
93  /** Constructor. */
94  InterfaceInfo() : combo(NULL), writer(NULL), reader_bb(NULL), writer_bb(NULL)
95  {
96  }
97 
98  /** Constructor.
99  * @param pcombo combo configuration
100  * @param pwriter Writing interface
101  * @param preader_bb Blackboard to read from
102  * @param pwriter_bb Blackboard to write to
103  */
104  InterfaceInfo(combo_t * pcombo,
105  fawkes::Interface * pwriter,
106  fawkes::BlackBoard *preader_bb,
107  fawkes::BlackBoard *pwriter_bb)
108  : combo(pcombo), writer(pwriter), reader_bb(preader_bb), writer_bb(pwriter_bb)
109  {
110  }
111 
112  /** Copy constructor.
113  * @param ii info to copy from
114  */
115  InterfaceInfo(const InterfaceInfo &ii)
116  : combo(ii.combo), writer(ii.writer), reader_bb(ii.reader_bb), writer_bb(ii.writer_bb)
117  {
118  }
119 
120  /** Assignment operator.
121  * @param ii interface info to assign
122  * @return reference to this instance
123  */
124  InterfaceInfo &
125  operator=(const InterfaceInfo &ii)
126  {
127  combo = ii.combo;
128  writer = ii.writer;
129  reader_bb = ii.reader_bb;
130  writer_bb = ii.writer_bb;
131  return *this;
132  }
133  };
134 
135  typedef std::map<std::string, combo_t> ComboMap;
138 
139  bool check_connection();
140  void read_config_combos(std::string prefix, bool writing);
141  void open_interfaces();
142  void close_interfaces();
143 
144 private:
145  std::string bbsync_cfg_prefix_;
146  std::string peer_cfg_prefix_;
147  std::string peer_;
148 
149  std::string host_;
150  unsigned int port_;
151 
152  fawkes::TimeWait *timewait_;
153 
154  fawkes::BlackBoard *remote_bb_;
155 
156  ComboMap combos_;
157 
158  // Maps reading -> writing interface
159  InterfaceMap interfaces_;
160  // Maps reading interface -> sync lsitener
161  SyncListenerMap sync_listeners_;
162 
163  SyncWriterInterfaceListener *wsl_local_;
164  SyncWriterInterfaceListener *wsl_remote_;
165 };
166 
167 #endif
BlackBoardSynchronizationThread::loop
virtual void loop()
Code to execute in the thread.
Definition: sync_thread.cpp:125
BlackBoardSynchronizationThread::~BlackBoardSynchronizationThread
virtual ~BlackBoardSynchronizationThread()
Destructor.
Definition: sync_thread.cpp:61
fawkes::LockMap< fawkes::Interface *, InterfaceInfo >
InterfaceInfo::writer
std::optional< std::string > writer() const
Get writer value.
Definition: InterfaceInfo.h:180
BlackBoardSynchronizationThread::finalize
virtual void finalize()
Finalize the thread.
Definition: sync_thread.cpp:112
InterfaceInfo
InterfaceInfo representation for JSON transfer.
Definition: InterfaceInfo.h:31
fawkes::BlackBoard
The BlackBoard abstract class.
Definition: blackboard.h:46
BlackBoardSynchronizationThread::BlackBoardSynchronizationThread
BlackBoardSynchronizationThread(std::string &bbsync_cfg_prefix, std::string &peer_cfg_prefix, std::string &peer)
Constructor.
Definition: sync_thread.cpp:45
BlackBoardSynchronizationThread::writer_added
void writer_added(fawkes::Interface *interface)
A writer has been added for an interface.
Definition: sync_thread.cpp:310
fawkes::BlackBoardAspect
Thread aspect to access to BlackBoard.
Definition: blackboard.h:34
fawkes
Fawkes library namespace.
fawkes::LoggingAspect
Thread aspect to log output.
Definition: logging.h:33
BlackBoardSynchronizationThread::writer_removed
void writer_removed(fawkes::Interface *interface)
A writer has been removed for an interface.
Definition: sync_thread.cpp:358
fawkes::Interface
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
BlackBoardSynchronizationThread::run
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: sync_thread.h:66
SyncWriterInterfaceListener
Listener for writer events in bbsync plugin.
Definition: writer_listener.h:36
fawkes::Thread
Thread class encapsulation of pthreads.
Definition: thread.h:46
fawkes::TimeWait
Time wait utility.
Definition: wait.h:33
BlackBoardSynchronizationThread::init
virtual void init()
Initialize the thread.
Definition: sync_thread.cpp:66
fawkes::ConfigurableAspect
Thread aspect to access configuration data.
Definition: configurable.h:33
fawkes::ClockAspect
Thread aspect that allows to obtain the current time from the clock.
Definition: clock.h:34
BlackBoardSynchronizationThread
Thread to synchronize two BlackBoards.
Definition: sync_thread.h:49