Fawkes API  Fawkes Development Version
sync_thread.h
00001 
00002 /***************************************************************************
00003  *  sync_thread.h - Fawkes BlackBoard Synchronization Thread
00004  *
00005  *  Created: Thu Jun 04 18:10:17 2009
00006  *  Copyright  2006-2009  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #ifndef __PLUGINS_BBSYNC_SYNC_THREAD_H_
00024 #define __PLUGINS_BBSYNC_SYNC_THREAD_H_
00025 
00026 #include "sync_listener.h"
00027 #include "writer_listener.h"
00028 
00029 #include <core/threading/thread.h>
00030 #include <core/utils/lock_map.h>
00031 #include <aspect/logging.h>
00032 #include <aspect/configurable.h>
00033 #include <aspect/blackboard.h>
00034 #include <aspect/clock.h>
00035 
00036 #include <string>
00037 #include <map>
00038 #include <utility>
00039 
00040 namespace fawkes {
00041   class TimeWait;
00042 }
00043 
00044 class BlackBoardSynchronizationThread
00045 : public fawkes::Thread,
00046   public fawkes::LoggingAspect,
00047   public fawkes::ConfigurableAspect,
00048   public fawkes::BlackBoardAspect,
00049   public fawkes::ClockAspect
00050 {
00051  public:
00052   BlackBoardSynchronizationThread(std::string &bbsync_cfg_prefix,
00053                                   std::string &peer_cfg_prefix, std::string &peer);
00054   virtual ~BlackBoardSynchronizationThread();
00055 
00056   virtual void init();
00057   virtual void loop();
00058   virtual void finalize();
00059 
00060   void writer_added(fawkes::Interface *interface) throw();
00061   void writer_removed(fawkes::Interface *interface) throw();
00062 
00063  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
00064  protected: virtual void run() { Thread::run(); }
00065 
00066  private:
00067   /** Interface combo struct */
00068   typedef struct {
00069     std::string type;           /**< Combo type */
00070     std::string reader_id;      /**< reader interface ID */
00071     std::string writer_id;      /**< writer interface ID */
00072     bool remote_writer;         /**< true if remote writer */
00073   } combo_t;
00074 
00075   class InterfaceInfo {
00076    public:
00077     /** Combo configuration */
00078     combo_t            *combo;
00079     /** Writing interface */
00080     fawkes::Interface  *writer;
00081     /** Blackboard to read from */
00082     fawkes::BlackBoard *reader_bb;
00083     /** Blackboard to write to */
00084     fawkes::BlackBoard *writer_bb;
00085 
00086     /** Constructor. */
00087     InterfaceInfo()
00088       : combo(NULL), writer(NULL), reader_bb(NULL), writer_bb(NULL)
00089     {}
00090 
00091     /** Constructor.
00092      * @param pcombo combo configuration
00093      * @param pwriter Writing interface
00094      * @param preader_bb Blackboard to read from
00095      * @param pwriter_bb Blackboard to write to
00096      */
00097     InterfaceInfo(combo_t *pcombo, fawkes::Interface  *pwriter,
00098                   fawkes::BlackBoard *preader_bb, fawkes::BlackBoard *pwriter_bb)
00099       : combo(pcombo), writer(pwriter), reader_bb(preader_bb), writer_bb(pwriter_bb)
00100     {}
00101 
00102     /** Assignment operator.
00103      * @param ii interface info to assign
00104      * @return reference to this instance
00105      */
00106     InterfaceInfo & operator=(const InterfaceInfo &ii)
00107     {
00108       combo=ii.combo; writer=ii.writer; reader_bb=ii.reader_bb; writer_bb=ii.writer_bb;
00109       return *this;
00110     }
00111   };
00112 
00113   typedef std::map<std::string, combo_t > ComboMap;
00114   typedef fawkes::LockMap<fawkes::Interface *, InterfaceInfo> InterfaceMap;
00115   typedef fawkes::LockMap<fawkes::Interface *, SyncInterfaceListener *> SyncListenerMap;
00116 
00117   bool check_connection();
00118   void read_config_combos(std::string prefix, bool writing);
00119   void open_interfaces();
00120   void close_interfaces();
00121 
00122  private:
00123   std::string   __bbsync_cfg_prefix;
00124   std::string   __peer_cfg_prefix;
00125   std::string   __peer;
00126 
00127   std::string   __host;
00128   unsigned int  __port;
00129 
00130   fawkes::TimeWait    *__timewait;
00131 
00132   fawkes::BlackBoard  *__remote_bb;
00133 
00134   ComboMap __combos;
00135 
00136   // Maps reading -> writing interface
00137   InterfaceMap __interfaces;
00138   // Maps reading interface -> sync lsitener
00139   SyncListenerMap __sync_listeners;
00140 
00141   SyncWriterInterfaceListener *__wsl_local;
00142   SyncWriterInterfaceListener *__wsl_remote;
00143 };
00144 
00145 
00146 #endif