Fawkes API  Fawkes Development Version
notifier.h
00001  
00002 /***************************************************************************
00003  *  notifier.h - BlackBoard notifier
00004  *
00005  *  Created: Mon Mar 03 23:25:57 2008
00006  *  Copyright  2006-2008  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. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #ifndef __BLACKBOARD_NOTIFIER_H_
00025 #define __BLACKBOARD_NOTIFIER_H_
00026 
00027 #include <blackboard/blackboard.h>
00028 #include <blackboard/interface_listener.h>
00029 #include <blackboard/interface_observer.h>
00030 
00031 #include <core/utils/rwlock_map.h>
00032 
00033 #include <list>
00034 #include <string>
00035 #include <utility>
00036 
00037 namespace fawkes {
00038 
00039 class Interface;
00040 class Message;
00041 class Mutex;
00042 
00043 class BlackBoardNotifier
00044 {
00045  public:
00046   BlackBoardNotifier();
00047   virtual ~BlackBoardNotifier();
00048 
00049   void register_listener(BlackBoardInterfaceListener *listener,
00050                          BlackBoard::ListenerRegisterFlag flag);
00051   void update_listener(BlackBoardInterfaceListener *listener,
00052                        BlackBoard::ListenerRegisterFlag flag);
00053   void unregister_listener(BlackBoardInterfaceListener *listener);
00054 
00055   void register_observer(BlackBoardInterfaceObserver *observer);
00056   void unregister_observer(BlackBoardInterfaceObserver *observer);
00057 
00058   void notify_of_data_change(const Interface *interface);
00059   bool notify_of_message_received(const Interface *interface, Message *message);
00060   void notify_of_interface_created(const char *type, const char *id) throw();
00061   void notify_of_interface_destroyed(const char *type, const char *id) throw();
00062   void notify_of_writer_added(const Interface *interface,
00063                               unsigned int event_instance_serial) throw();
00064   void notify_of_writer_removed(const Interface *interface,
00065                                 unsigned int event_instance_serial) throw();
00066   void notify_of_reader_added(const Interface *interface,
00067                               unsigned int event_instance_serial) throw();
00068   void notify_of_reader_removed(const Interface *interface,
00069                                 unsigned int event_instance_serial) throw();
00070 
00071  private:
00072   /// @cond INTERNALS
00073   typedef struct {
00074     bool                           op;
00075     std::string                    uid;
00076     Interface                    * interface;
00077     BlackBoardInterfaceListener  * listener;
00078   } BBilQueueEntry;
00079   /// @endcond INTERNALS
00080   typedef std::list< BBilQueueEntry > BBilQueue;
00081 
00082   typedef std::multimap<std::string, BlackBoardInterfaceListener *> BBilMap;
00083   typedef std::pair<BlackBoardInterfaceObserver *, std::list<std::string> > BBioPair;
00084   typedef std::list< BBioPair>                  BBioList;
00085   typedef std::map< std::string, BBioList >     BBioMap;
00086 
00087   // Type to observer, add flags, 0 to remove
00088   typedef std::pair< unsigned int, BlackBoardInterfaceObserver *> BBioQueueEntry;
00089   typedef std::list< BBioQueueEntry > BBioQueue;
00090 
00091   typedef BBilMap::iterator     BBilMapIterator;
00092 
00093   typedef BBioList::iterator    BBioListIterator;
00094   typedef BBioMap::iterator     BBioMapIterator;
00095 
00096   void proc_listener_maybe_queue(bool op, Interface *interface,
00097                                  BlackBoardInterfaceListener *listener,
00098                                  Mutex *mutex, unsigned int &events,
00099                                  BBilMap &map, BBilQueue &queue,
00100                                  const char *hint);
00101 
00102   void add_listener(Interface *interface, BlackBoardInterfaceListener *listener,
00103                     BBilMap &ilmap);
00104   void remove_listener(Interface *interface, BlackBoardInterfaceListener *listener,
00105                        BBilMap &ilmap);
00106   void queue_listener(bool op, Interface *interface,
00107                       BlackBoardInterfaceListener *listener, BBilQueue &queue);
00108 
00109 
00110 
00111   void add_observer(BlackBoardInterfaceObserver *observer,
00112                     BlackBoardInterfaceObserver::ObservedInterfaceLockMap *its,
00113                     BBioMap &bbiomap);
00114 
00115   void remove_observer(BBioMap &iomap, BlackBoardInterfaceObserver *observer);
00116 
00117   void process_writer_queue();
00118   void process_reader_queue();
00119   void process_data_queue();
00120   void process_bbio_queue();
00121 
00122   bool is_in_queue(bool op, BBilQueue &queue, const char *uid,
00123                    BlackBoardInterfaceListener *bbil);
00124 
00125   BBilMap __bbil_data;
00126   BBilMap __bbil_reader;
00127   BBilMap __bbil_writer;
00128   BBilMap __bbil_messages;
00129 
00130   Mutex *__bbil_unregister_mutex;
00131   BBilQueue       __bbil_unregister_queue;
00132 
00133   Mutex *__bbil_writer_mutex;
00134   unsigned int    __bbil_writer_events;
00135   BBilQueue       __bbil_writer_queue;
00136 
00137   Mutex *__bbil_reader_mutex;
00138   unsigned int    __bbil_reader_events;
00139   BBilQueue       __bbil_reader_queue;
00140 
00141   Mutex *__bbil_data_mutex;
00142   unsigned int    __bbil_data_events;
00143   BBilQueue       __bbil_data_queue;
00144 
00145   Mutex *__bbil_messages_mutex;
00146   unsigned int    __bbil_messages_events;
00147   BBilQueue       __bbil_messages_queue;
00148 
00149   BBioMap        __bbio_created;
00150   BBioMap        __bbio_destroyed;
00151 
00152   Mutex *__bbio_mutex;
00153   unsigned int    __bbio_events;
00154   BBioQueue       __bbio_queue;
00155 
00156 };
00157 
00158 } // end namespace fawkes
00159 
00160 #endif