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
40namespace fawkes {
41class TimeWait;
42}
43
49{
50public:
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) noexcept;
61 void writer_removed(fawkes::Interface *interface) noexcept;
62
63 /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
64protected:
65 virtual void
67 {
68 Thread::run();
69 }
70
71private:
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 */
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 */
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
144private:
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
Thread to synchronize two BlackBoards.
Definition: sync_thread.h:49
void writer_removed(fawkes::Interface *interface) noexcept
A writer has been removed for an interface.
virtual void init()
Initialize the thread.
Definition: sync_thread.cpp:66
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: sync_thread.h:66
virtual void finalize()
Finalize the thread.
virtual void loop()
Code to execute in the thread.
void writer_added(fawkes::Interface *interface) noexcept
A writer has been added for an interface.
virtual ~BlackBoardSynchronizationThread()
Destructor.
Definition: sync_thread.cpp:61
BlackBoardSynchronizationThread(std::string &bbsync_cfg_prefix, std::string &peer_cfg_prefix, std::string &peer)
Constructor.
Definition: sync_thread.cpp:45
InterfaceInfo representation for JSON transfer.
Definition: InterfaceInfo.h:31
std::optional< std::string > writer() const
Get writer value.
Listener for writer events in bbsync plugin.
Thread aspect to access to BlackBoard.
Definition: blackboard.h:34
The BlackBoard abstract class.
Definition: blackboard.h:46
Thread aspect that allows to obtain the current time from the clock.
Definition: clock.h:34
Thread aspect to access configuration data.
Definition: configurable.h:33
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
Thread aspect to log output.
Definition: logging.h:33
Thread class encapsulation of pthreads.
Definition: thread.h:46
Time wait utility.
Definition: wait.h:33
Fawkes library namespace.