Fawkes API  Fawkes Development Version
interface_listener.h
1 
2 /***************************************************************************
3  * interface_listener.h - BlackBoard event listener
4  *
5  * Created: Wed Nov 07 23:55:53 2007 (Saw Ella for the first time)
6  * Copyright 2007-2008 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. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __BLACKBOARD_INTERFACE_LISTENER_H_
25 #define __BLACKBOARD_INTERFACE_LISTENER_H_
26 
27 #include <blackboard/blackboard.h>
28 #include <core/utils/lock_queue.h>
29 #include <utils/misc/string_compare.h>
30 #include <string>
31 #include <map>
32 #include <list>
33 
34 namespace fawkes {
35 
36 class Interface;
37 class Message;
38 
40 {
41  friend class BlackBoardNotifier;
42 
43  public:
44  /** Queue entry type. */
45  typedef enum {
46  DATA = 0, ///< Data changed event entry
47  MESSAGES = 1, ///< Message received event entry
48  READER = 2, ///< Reader event entry
49  WRITER = 3 ///< Writer event entry
51 
52  /** Queue entry type. */
53  typedef struct {
54  QueueEntryType type; ///< What type this entry concerns
55  bool op; ///< true to add, false to remove
56  Interface * interface; ///< interface this entry concerns
57  } QueueEntry;
58 
59  /** Queue of additions/removal of interfaces. */
60  typedef std::list<QueueEntry> InterfaceQueue;
61 
62  /** Map of currently active event subscriptions. */
63  typedef std::map<std::string, Interface *> InterfaceMap;
64 
65  /** Structure to hold maps for active subscriptions. */
66  typedef struct {
67  InterfaceMap data; ///< Data event subscriptions
68  InterfaceMap messages; ///< Message received event subscriptions
69  InterfaceMap reader; ///< Reader event subscriptions
70  InterfaceMap writer; ///< Writer event subscriptions
71  } InterfaceMaps;
72 
73  BlackBoardInterfaceListener(const char *name_format, ...);
75 
76  const char * bbil_name() const;
77 
78  virtual void bb_interface_data_changed(Interface *interface) throw();
79  virtual bool bb_interface_message_received(Interface *interface,
80  Message *message) throw();
81  virtual void bb_interface_writer_added(Interface *interface,
82  unsigned int instance_serial) throw();
83  virtual void bb_interface_writer_removed(Interface *interface,
84  unsigned int instance_serial) throw();
85  virtual void bb_interface_reader_added(Interface *interface,
86  unsigned int instance_serial) throw();
87  virtual void bb_interface_reader_removed(Interface *interface,
88  unsigned int instance_serial) throw();
89 
90  protected:
91  void bbil_add_data_interface(Interface *interface);
92  void bbil_add_message_interface(Interface *interface);
93  void bbil_add_reader_interface(Interface *interface);
94  void bbil_add_writer_interface(Interface *interface);
95 
96  void bbil_remove_data_interface(Interface *interface);
98  void bbil_remove_reader_interface(Interface *interface);
99  void bbil_remove_writer_interface(Interface *interface);
100 
101  Interface * bbil_data_interface(const char *iuid) throw();
102  Interface * bbil_message_interface(const char *iuid) throw();
103  Interface * bbil_reader_interface(const char *iuid) throw();
104  Interface * bbil_writer_interface(const char *iuid) throw();
105 
106 
107  private:
108  void bbil_queue_add(QueueEntryType type, bool op,
109  InterfaceMap &not_in_map,
110  Interface *interface, const char *hint);
111  Interface * bbil_find_interface(const char *iuid, InterfaceMap &map);
112 
113  const InterfaceQueue & bbil_acquire_queue() throw();
114  void bbil_release_queue(BlackBoard::ListenerRegisterFlag flag) throw();
115 
116  const InterfaceMaps & bbil_acquire_maps() throw();
117  void bbil_release_maps() throw();
118 
119 
120  private:
121  Mutex *__bbil_queue_mutex;
122  Mutex *__bbil_maps_mutex;
123 
124  InterfaceMaps __bbil_maps;
125  InterfaceQueue __bbil_queue;
126 
127  char *__name;
128 };
129 
130 } // end namespace fawkes
131 
132 #endif
Interface * bbil_reader_interface(const char *iuid)
Get interface instance for given UID.
virtual bool bb_interface_message_received(Interface *interface, Message *message)
BlackBoard message received notification.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
InterfaceMap messages
Message received event subscriptions.
InterfaceMap writer
Writer event subscriptions.
Fawkes library namespace.
virtual ~BlackBoardInterfaceListener()
Destructor.
Structure to hold maps for active subscriptions.
void bbil_add_writer_interface(Interface *interface)
Add an interface to the writer addition/removal watch list.
virtual void bb_interface_reader_removed(Interface *interface, unsigned int instance_serial)
A reading instance has been closed for a watched interface.
BlackBoard notifier.
Definition: notifier.h:43
Interface * bbil_message_interface(const char *iuid)
Get interface instance for given UID.
InterfaceMap data
Data event subscriptions.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
void bbil_remove_writer_interface(Interface *interface)
Remove an interface to the writer addition/removal watch list.
void bbil_remove_message_interface(Interface *interface)
Remove an interface to the message received watch list.
virtual void bb_interface_data_changed(Interface *interface)
BlackBoard data changed notification.
InterfaceMap reader
Reader event subscriptions.
BlackBoardInterfaceListener(const char *name_format,...)
Constructor.
void bbil_remove_data_interface(Interface *interface)
Remove an interface to the data modification watch list.
void bbil_add_reader_interface(Interface *interface)
Add an interface to the reader addition/removal watch list.
Interface * interface
interface this entry concerns
std::list< QueueEntry > InterfaceQueue
Queue of additions/removal of interfaces.
Interface * bbil_data_interface(const char *iuid)
Get interface instance for given UID.
Interface * bbil_writer_interface(const char *iuid)
Get interface instance for given UID.
virtual void bb_interface_reader_added(Interface *interface, unsigned int instance_serial)
A reading instance has been opened for a watched interface.
virtual void bb_interface_writer_added(Interface *interface, unsigned int instance_serial)
A writing instance has been opened for a watched interface.
virtual void bb_interface_writer_removed(Interface *interface, unsigned int instance_serial)
A writing instance has been closed for a watched interface.
const char * bbil_name() const
Get BBIL name.
The BlackBoard abstract class.
Definition: blackboard.h:48
Mutex mutual exclusion lock.
Definition: mutex.h:32
std::map< std::string, Interface * > InterfaceMap
Map of currently active event subscriptions.
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
void bbil_remove_reader_interface(Interface *interface)
Remove an interface to the reader addition/removal watch list.
BlackBoard interface listener.
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.
QueueEntryType type
What type this entry concerns.