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 <core/threading/thread.h>
30 #include <core/utils/lock_map.h>
31 #include <aspect/logging.h>
32 #include <aspect/configurable.h>
33 #include <aspect/blackboard.h>
34 #include <aspect/clock.h>
35 
36 #include <string>
37 #include <map>
38 #include <utility>
39 
40 namespace fawkes {
41  class TimeWait;
42 }
43 
45 : public fawkes::Thread,
46  public fawkes::LoggingAspect,
49  public fawkes::ClockAspect
50 {
51  public:
52  BlackBoardSynchronizationThread(std::string &bbsync_cfg_prefix,
53  std::string &peer_cfg_prefix, 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: virtual void run() { Thread::run(); }
65 
66  private:
67  /** Interface combo struct */
68  typedef struct {
69  std::string type; /**< Combo type */
70  std::string reader_id; /**< reader interface ID */
71  std::string writer_id; /**< writer interface ID */
72  bool remote_writer; /**< true if remote writer */
73  } combo_t;
74 
75  class InterfaceInfo {
76  public:
77  /** Combo configuration */
78  combo_t *combo;
79  /** Writing interface */
80  fawkes::Interface *writer;
81  /** Blackboard to read from */
82  fawkes::BlackBoard *reader_bb;
83  /** Blackboard to write to */
84  fawkes::BlackBoard *writer_bb;
85 
86  /** Constructor. */
87  InterfaceInfo()
88  : combo(NULL), writer(NULL), reader_bb(NULL), writer_bb(NULL)
89  {}
90 
91  /** Constructor.
92  * @param pcombo combo configuration
93  * @param pwriter Writing interface
94  * @param preader_bb Blackboard to read from
95  * @param pwriter_bb Blackboard to write to
96  */
97  InterfaceInfo(combo_t *pcombo, fawkes::Interface *pwriter,
98  fawkes::BlackBoard *preader_bb, fawkes::BlackBoard *pwriter_bb)
99  : combo(pcombo), writer(pwriter), reader_bb(preader_bb), writer_bb(pwriter_bb)
100  {}
101 
102  /** Assignment operator.
103  * @param ii interface info to assign
104  * @return reference to this instance
105  */
106  InterfaceInfo & operator=(const InterfaceInfo &ii)
107  {
108  combo=ii.combo; writer=ii.writer; reader_bb=ii.reader_bb; writer_bb=ii.writer_bb;
109  return *this;
110  }
111  };
112 
113  typedef std::map<std::string, combo_t > ComboMap;
116 
117  bool check_connection();
118  void read_config_combos(std::string prefix, bool writing);
119  void open_interfaces();
120  void close_interfaces();
121 
122  private:
123  std::string __bbsync_cfg_prefix;
124  std::string __peer_cfg_prefix;
125  std::string __peer;
126 
127  std::string __host;
128  unsigned int __port;
129 
130  fawkes::TimeWait *__timewait;
131 
132  fawkes::BlackBoard *__remote_bb;
133 
134  ComboMap __combos;
135 
136  // Maps reading -> writing interface
137  InterfaceMap __interfaces;
138  // Maps reading interface -> sync lsitener
139  SyncListenerMap __sync_listeners;
140 
141  SyncWriterInterfaceListener *__wsl_local;
142  SyncWriterInterfaceListener *__wsl_remote;
143 };
144 
145 
146 #endif
Thread aspect to access to BlackBoard.
Definition: blackboard.h:34
Thread aspect that allows to obtain the current time from the clock.
Definition: clock.h:36
Fawkes library namespace.
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:939
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: sync_thread.h:64
Thread class encapsulation of pthreads.
Definition: thread.h:42
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
Thread aspect to log output.
Definition: logging.h:35
Thread to synchronize two BlackBoards.
Definition: sync_thread.h:44
Thread aspect to access configuration data.
Definition: configurable.h:35
Listener for writer events in bbsync plugin.
The BlackBoard abstract class.
Definition: blackboard.h:48
Time wait utility.
Definition: wait.h:32