Fawkes API  Fawkes Development Version
interface_listener.h
00001  
00002 /***************************************************************************
00003  *  interface_listener.h - BlackBoard event listener
00004  *
00005  *  Created: Wed Nov 07 23:55:53 2007 (Saw Ella for the first time)
00006  *  Copyright  2007-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_INTERFACE_LISTENER_H_
00025 #define __BLACKBOARD_INTERFACE_LISTENER_H_
00026 
00027 #include <blackboard/blackboard.h>
00028 #include <core/utils/lock_queue.h>
00029 #include <utils/misc/string_compare.h>
00030 #include <string>
00031 #include <map>
00032 #include <list>
00033 
00034 namespace fawkes {
00035 
00036 class Interface;
00037 class Message;
00038 
00039 class BlackBoardInterfaceListener
00040 {
00041  friend class BlackBoardNotifier;
00042 
00043  public:
00044  /** Queue entry type. */
00045  typedef enum {
00046    DATA = 0,            ///< Data changed event entry
00047    MESSAGES = 1,        ///< Message received event entry
00048    READER = 2,          ///< Reader event entry
00049    WRITER = 3           ///< Writer event entry
00050  } QueueEntryType;
00051 
00052  /** Queue entry type. */
00053  typedef struct {
00054    QueueEntryType type;         ///< What type this entry concerns
00055    bool           op;           ///< true to add, false to remove
00056    Interface *    interface;    ///< interface this entry concerns
00057  } QueueEntry;
00058 
00059   /** Queue of additions/removal of interfaces. */
00060  typedef std::list<QueueEntry> InterfaceQueue;
00061 
00062   /** Map of currently active event subscriptions. */
00063  typedef std::map<std::string, Interface *> InterfaceMap;
00064 
00065  /** Structure to hold maps for active subscriptions. */
00066  typedef struct {
00067    InterfaceMap  data;          ///< Data event subscriptions
00068    InterfaceMap  messages;      ///< Message received event subscriptions
00069    InterfaceMap  reader;        ///< Reader event subscriptions
00070    InterfaceMap  writer;        ///< Writer event subscriptions
00071  } InterfaceMaps;
00072 
00073   BlackBoardInterfaceListener(const char *name_format, ...);
00074   virtual ~BlackBoardInterfaceListener();
00075 
00076   const char * bbil_name() const;
00077 
00078   virtual void bb_interface_data_changed(Interface *interface) throw();
00079   virtual bool bb_interface_message_received(Interface *interface,
00080                                              Message *message) throw();
00081   virtual void bb_interface_writer_added(Interface *interface,
00082                                          unsigned int instance_serial) throw();
00083   virtual void bb_interface_writer_removed(Interface *interface,
00084                                            unsigned int instance_serial) throw();
00085   virtual void bb_interface_reader_added(Interface *interface,
00086                                          unsigned int instance_serial) throw();
00087   virtual void bb_interface_reader_removed(Interface *interface,
00088                                            unsigned int instance_serial) throw();
00089 
00090  protected:
00091   void bbil_add_data_interface(Interface *interface);
00092   void bbil_add_message_interface(Interface *interface);
00093   void bbil_add_reader_interface(Interface *interface);
00094   void bbil_add_writer_interface(Interface *interface);
00095 
00096   void bbil_remove_data_interface(Interface *interface);
00097   void bbil_remove_message_interface(Interface *interface);
00098   void bbil_remove_reader_interface(Interface *interface);
00099   void bbil_remove_writer_interface(Interface *interface);
00100 
00101   Interface * bbil_data_interface(const char *iuid) throw();
00102   Interface * bbil_message_interface(const char *iuid) throw();
00103   Interface * bbil_reader_interface(const char *iuid) throw();
00104   Interface * bbil_writer_interface(const char *iuid) throw();
00105 
00106 
00107  private:
00108   void bbil_queue_add(QueueEntryType type, bool op,
00109                       InterfaceMap &not_in_map,
00110                       Interface *interface, const char *hint);
00111   Interface * bbil_find_interface(const char *iuid, InterfaceMap &map);
00112 
00113   const InterfaceQueue &  bbil_acquire_queue() throw();
00114   void bbil_release_queue(BlackBoard::ListenerRegisterFlag flag) throw();
00115 
00116   const InterfaceMaps & bbil_acquire_maps() throw();
00117   void bbil_release_maps() throw();
00118 
00119 
00120  private:
00121   Mutex *__bbil_queue_mutex;
00122   Mutex *__bbil_maps_mutex;
00123 
00124   InterfaceMaps  __bbil_maps;
00125   InterfaceQueue __bbil_queue;
00126 
00127   char *__name;
00128 };
00129 
00130 } // end namespace fawkes
00131 
00132 #endif