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 
31 #include <list>
32 #include <map>
33 #include <string>
34 
35 namespace fawkes {
36 
37 class Interface;
38 class Message;
39 class BlackBoardNotifier;
40 
41 class BlackBoardInterfaceListener
42 {
43  friend BlackBoardNotifier;
44 
45 public:
46  /** Queue entry type. */
47  typedef enum {
48  DATA = 0, ///< Data changed event entry
49  MESSAGES = 1, ///< Message received event entry
50  READER = 2, ///< Reader event entry
51  WRITER = 3 ///< Writer event entry
53 
54  /** Queue entry type. */
55  typedef struct
56  {
57  QueueEntryType type; ///< What type this entry concerns
58  bool op; ///< true to add, false to remove
59  Interface * interface; ///< interface this entry concerns
60  } QueueEntry;
61 
62  /** Queue of additions/removal of interfaces. */
63  typedef std::list<QueueEntry> InterfaceQueue;
64 
65  /** Map of currently active event subscriptions. */
66  typedef std::map<std::string, Interface *> InterfaceMap;
67 
68  /** Structure to hold maps for active subscriptions. */
69  typedef struct
70  {
71  InterfaceMap data; ///< Data event subscriptions
72  InterfaceMap messages; ///< Message received event subscriptions
73  InterfaceMap reader; ///< Reader event subscriptions
74  InterfaceMap writer; ///< Writer event subscriptions
76 
77  BlackBoardInterfaceListener(const char *name_format, ...);
79 
80  const char *bbil_name() const;
81 
82  virtual void bb_interface_data_changed(Interface *interface) throw();
83  virtual bool bb_interface_message_received(Interface *interface, Message *message) throw();
84  virtual void bb_interface_writer_added(Interface * interface,
85  unsigned int instance_serial) throw();
86  virtual void bb_interface_writer_removed(Interface * interface,
87  unsigned int instance_serial) throw();
88  virtual void bb_interface_reader_added(Interface * interface,
89  unsigned int instance_serial) throw();
90  virtual void bb_interface_reader_removed(Interface * interface,
91  unsigned int instance_serial) throw();
92 
93 protected:
94  void bbil_add_data_interface(Interface *interface);
95  void bbil_add_message_interface(Interface *interface);
96  void bbil_add_reader_interface(Interface *interface);
97  void bbil_add_writer_interface(Interface *interface);
98 
99  void bbil_remove_data_interface(Interface *interface);
100  void bbil_remove_message_interface(Interface *interface);
101  void bbil_remove_reader_interface(Interface *interface);
102  void bbil_remove_writer_interface(Interface *interface);
103 
104  Interface *bbil_data_interface(const char *iuid) throw();
105  Interface *bbil_message_interface(const char *iuid) throw();
106  Interface *bbil_reader_interface(const char *iuid) throw();
107  Interface *bbil_writer_interface(const char *iuid) throw();
108 
109 private:
110  void bbil_queue_add(QueueEntryType type,
111  bool op,
112  InterfaceMap & not_in_map,
113  Interface * interface,
114  const char * hint);
115  Interface *bbil_find_interface(const char *iuid, InterfaceMap &map);
116 
117  const InterfaceQueue &bbil_acquire_queue() throw();
118  void bbil_release_queue(BlackBoard::ListenerRegisterFlag flag) throw();
119 
120  const InterfaceMaps &bbil_acquire_maps() throw();
121  void bbil_release_maps() throw();
122 
123 private:
124  Mutex *bbil_queue_mutex_;
125  Mutex *bbil_maps_mutex_;
126 
127  InterfaceMaps bbil_maps_;
128  InterfaceQueue bbil_queue_;
129 
130  char *name_;
131 };
132 
133 } // end namespace fawkes
134 
135 #endif
fawkes::BlackBoardInterfaceListener::InterfaceQueue
std::list< QueueEntry > InterfaceQueue
Queue of additions/removal of interfaces.
Definition: interface_listener.h:67
fawkes::Mutex
Definition: mutex.h:36
fawkes::BlackBoardInterfaceListener::~BlackBoardInterfaceListener
virtual ~BlackBoardInterfaceListener()
Destructor.
Definition: interface_listener.cpp:104
fawkes::BlackBoardInterfaceListener::bb_interface_message_received
virtual bool bb_interface_message_received(Interface *interface, Message *message)
BlackBoard message received notification.
Definition: interface_listener.cpp:149
fawkes::Message
Definition: message.h:40
fawkes::BlackBoardInterfaceListener::QueueEntry
Queue entry type.
Definition: interface_listener.h:59
fawkes::BlackBoardInterfaceListener::DATA
Data changed event entry.
Definition: interface_listener.h:52
fawkes::BlackBoardInterfaceListener::bbil_remove_reader_interface
void bbil_remove_reader_interface(Interface *interface)
Remove an interface to the reader addition/removal watch list.
Definition: interface_listener.cpp:307
fawkes::BlackBoard
Definition: blackboard.h:48
fawkes::BlackBoardInterfaceListener::bbil_reader_interface
Interface * bbil_reader_interface(const char *iuid)
Get interface instance for given UID.
Definition: interface_listener.cpp:498
fawkes::BlackBoardInterfaceListener::BlackBoardInterfaceListener
BlackBoardInterfaceListener(const char *name_format,...)
Constructor.
Definition: interface_listener.cpp:90
fawkes::BlackBoardInterfaceListener::bbil_remove_writer_interface
void bbil_remove_writer_interface(Interface *interface)
Remove an interface to the writer addition/removal watch list.
Definition: interface_listener.cpp:318
fawkes::BlackBoardInterfaceListener::bbil_message_interface
Interface * bbil_message_interface(const char *iuid)
Get interface instance for given UID.
Definition: interface_listener.cpp:486
fawkes::BlackBoardInterfaceListener::bb_interface_reader_added
virtual void bb_interface_reader_added(Interface *interface, unsigned int instance_serial)
A reading instance has been opened for a watched interface.
Definition: interface_listener.cpp:163
fawkes::BlackBoardInterfaceListener::READER
Reader event entry.
Definition: interface_listener.h:54
fawkes::BlackBoardInterfaceListener::bb_interface_writer_added
virtual void bb_interface_writer_added(Interface *interface, unsigned int instance_serial)
A writing instance has been opened for a watched interface.
Definition: interface_listener.cpp:189
fawkes
fawkes::BlackBoardInterfaceListener::bbil_remove_message_interface
void bbil_remove_message_interface(Interface *interface)
Remove an interface to the message received watch list.
Definition: interface_listener.cpp:296
fawkes::BlackBoardInterfaceListener::bb_interface_data_changed
virtual void bb_interface_data_changed(Interface *interface)
BlackBoard data changed notification.
Definition: interface_listener.cpp:127
fawkes::BlackBoardInterfaceListener::InterfaceMap
std::map< std::string, Interface * > InterfaceMap
Map of currently active event subscriptions.
Definition: interface_listener.h:70
fawkes::BlackBoardInterfaceListener::bbil_add_message_interface
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
Definition: interface_listener.cpp:245
fawkes::Interface
Definition: interface.h:77
fawkes::BlackBoardInterfaceListener::bbil_remove_data_interface
void bbil_remove_data_interface(Interface *interface)
Remove an interface to the data modification watch list.
Definition: interface_listener.cpp:285
fawkes::BlackBoardInterfaceListener::bbil_writer_interface
Interface * bbil_writer_interface(const char *iuid)
Get interface instance for given UID.
Definition: interface_listener.cpp:510
fawkes::BlackBoardInterfaceListener::WRITER
Writer event entry.
Definition: interface_listener.h:55
fawkes::BlackBoardInterfaceListener::bb_interface_writer_removed
virtual void bb_interface_writer_removed(Interface *interface, unsigned int instance_serial)
A writing instance has been closed for a watched interface.
Definition: interface_listener.cpp:202
fawkes::BlackBoardInterfaceListener::bbil_data_interface
Interface * bbil_data_interface(const char *iuid)
Get interface instance for given UID.
Definition: interface_listener.cpp:474
fawkes::BlackBoardInterfaceListener::bb_interface_reader_removed
virtual void bb_interface_reader_removed(Interface *interface, unsigned int instance_serial)
A reading instance has been closed for a watched interface.
Definition: interface_listener.cpp:176
fawkes::BlackBoardInterfaceListener::InterfaceMaps
Structure to hold maps for active subscriptions.
Definition: interface_listener.h:73
fawkes::BlackBoardInterfaceListener::bbil_add_reader_interface
void bbil_add_reader_interface(Interface *interface)
Add an interface to the reader addition/removal watch list.
Definition: interface_listener.cpp:262
fawkes::BlackBoardInterfaceListener::MESSAGES
Message received event entry.
Definition: interface_listener.h:53
fawkes::BlackBoardInterfaceListener::QueueEntryType
QueueEntryType
Queue entry type.
Definition: interface_listener.h:51
fawkes::BlackBoardInterfaceListener::bbil_add_data_interface
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.
Definition: interface_listener.cpp:236
fawkes::BlackBoardInterfaceListener::bbil_name
const char * bbil_name() const
Get BBIL name.
Definition: interface_listener.cpp:116
fawkes::BlackBoardInterfaceListener::bbil_add_writer_interface
void bbil_add_writer_interface(Interface *interface)
Add an interface to the writer addition/removal watch list.
Definition: interface_listener.cpp:274