Fawkes API  Fawkes Development Version
blackboard_listener_thread.h
1 /***************************************************************************
2  * blackboard_listener_thread.h - Convert blackboard events to eclipse terms
3  *
4  * Copyright 2017 Victor MatarĂ©
5  ****************************************************************************/
6 
7 /* This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Library General Public License for more details.
16  *
17  * Read the full text in the LICENSE.GPL file in the doc directory.
18  */
19 
20 #ifndef BLACKBOARD_LISTENER_THREAD_H
21 #define BLACKBOARD_LISTENER_THREAD_H
22 
23 #include "externals/blackboard.h"
24 
25 #include <aspect/blackboard.h>
26 #include <aspect/configurable.h>
27 #include <aspect/logging.h>
28 #include <core/threading/mutex.h>
29 #include <core/threading/thread.h>
30 #include <libs/blackboard/interface_listener.h>
31 #include <libs/blackboard/interface_observer.h>
32 
33 #include <eclipseclass.h>
34 #include <map>
35 #include <memory>
36 #include <queue>
37 
38 /** Keeps a queue of subscribed blackboard events that can be queried in a thread-safe manner */
40  public fawkes::LoggingAspect,
45 {
46 private:
47  using string = std::string;
48  template <class T>
49  using queue = std::queue<T>;
50  template <class T>
51  using shared_ptr = std::shared_ptr<T>;
52  template <class T1, class T2>
53  using map = std::map<T1, T2>;
55  using Mutex = fawkes::Mutex;
56 
57 public:
59 
60  void observe_pattern(const char *type_pattern, const char *id_pattern) noexcept;
61  void listen_for_change(Interface *interface) noexcept;
62 
63  virtual void bb_interface_created(const char *type, const char *id) noexcept override;
64  virtual void bb_interface_destroyed(const char *type, const char *id) noexcept override;
65  virtual void bb_interface_data_changed(Interface *interface) noexcept override;
66 
68  static void cleanup_instance();
69 
70  /** Abstract superclass for blackboard events */
71  class Event
72  {
73  public:
74  /** Constructor
75  * @param type Blackboard interface type as string
76  * @param id Blackboard interface ID
77  */
78  Event(const std::string &type, const std::string &id) : type(type), id(id)
79  {
80  }
81 
82  virtual ~Event();
83 
84  /** Return an eclipse term representing the event (abstract)
85  * @return An eclipse term representing the event (abstract)
86  */
87  virtual operator EC_word() = 0;
88 
89  /** Return the UID (i.e. type::id) of the blackboard interface that triggered the event
90  * @return The UID (i.e. type::id) of the blackboard interface that triggered the event
91  */
92  std::string
93  uid()
94  {
95  return type + "::" + id;
96  }
97 
98  protected:
99  /** Triggering interface's type name */
100  string type;
101 
102  /** Triggering interface's ID */
103  string id;
104  };
105 
106  /** A new interface was created */
107  class Created : public Event
108  {
109  public:
110  using Event::Event;
111  virtual operator EC_word();
112  };
113 
114  /** An interface was destroyed */
115  class Destroyed : public Event
116  {
117  public:
118  using Event::Event;
119  virtual operator EC_word();
120  };
121 
122  /** An interface changed */
123  class Changed : public Event
124  {
125  public:
126  /** Constructor
127  * @param interface The interface that changed
128  */
129  Changed(Interface *interface) : Event(interface->type(), interface->id()), interface(interface)
130  {
131  }
132 
133  virtual operator EC_word();
134 
135  private:
136  fawkes::Interface *interface;
137  };
138 
139  bool event_pending();
140  shared_ptr<Event> event_pop();
141 
142 private:
143  Mutex state_mutex_;
144 
145  static BlackboardListenerThread *instance_;
146 
147  map<string, fawkes::Interface *> last_iface_of_type_;
148  queue<shared_ptr<Event>> iface_events_;
149 };
150 
151 #endif // BLACKBOARD_LISTENER_THREAD_H
BlackboardListenerThread::Destroyed
An interface was destroyed.
Definition: blackboard_listener_thread.h:117
fawkes::Mutex
Definition: mutex.h:36
BlackboardListenerThread::Event::id
string id
Triggering interface's ID.
Definition: blackboard_listener_thread.h:105
BlackboardListenerThread::Changed
An interface changed.
Definition: blackboard_listener_thread.h:125
BlackboardListenerThread::instance
static BlackboardListenerThread * instance()
Get the singleton instance of this thread.
Definition: blackboard_listener_thread.cpp:39
fawkes::BlackBoardInterfaceListener
Definition: interface_listener.h:45
BlackboardListenerThread::Created
A new interface was created.
Definition: blackboard_listener_thread.h:109
BlackboardListenerThread::Event::Event
Event(const std::string &type, const std::string &id)
Constructor.
Definition: blackboard_listener_thread.h:80
BlackboardListenerThread::bb_interface_destroyed
virtual void bb_interface_destroyed(const char *type, const char *id) noexcept override
Called by the BlackBoardInterfaceObserver when an interface is destroyed.
Definition: blackboard_listener_thread.cpp:89
BlackboardListenerThread::observe_pattern
void observe_pattern(const char *type_pattern, const char *id_pattern) noexcept
Trigger events if an interface matching the pattern is created or destroyed.
Definition: blackboard_listener_thread.cpp:58
BlackboardListenerThread::event_pop
shared_ptr< Event > event_pop()
Return and remove the next event in the queue.
Definition: blackboard_listener_thread.cpp:119
BlackboardListenerThread::cleanup_instance
static void cleanup_instance()
Delete singleton instance, e.g.
Definition: blackboard_listener_thread.cpp:48
BlackboardListenerThread
Keeps a queue of subscribed blackboard events that can be queried in a thread-safe manner.
Definition: blackboard_listener_thread.h:38
BlackboardListenerThread::Event
Abstract superclass for blackboard events.
Definition: blackboard_listener_thread.h:73
fawkes::BlackBoardAspect
Definition: blackboard.h:36
fawkes::LoggingAspect
Definition: logging.h:36
BlackboardListenerThread::Event::type
string type
Triggering interface's type name.
Definition: blackboard_listener_thread.h:102
BlackboardListenerThread::bb_interface_data_changed
virtual void bb_interface_data_changed(Interface *interface) noexcept override
Called by the BlackBoardInterfaceListener when an interface changes.
Definition: blackboard_listener_thread.cpp:99
BlackboardListenerThread::listen_for_change
void listen_for_change(Interface *interface) noexcept
Register.
Definition: blackboard_listener_thread.cpp:67
fawkes::BlackBoardInterfaceObserver
Definition: interface_observer.h:40
fawkes::Interface
Definition: interface.h:77
BlackboardListenerThread::Event::uid
std::string uid()
Return the UID (i.e.
Definition: blackboard_listener_thread.h:95
BlackboardListenerThread::Changed::Changed
Changed(Interface *interface)
Constructor.
Definition: blackboard_listener_thread.h:131
BlackboardListenerThread::event_pending
bool event_pending()
Test whether any events are in the queue.
Definition: blackboard_listener_thread.cpp:109
fawkes::Thread
Definition: thread.h:44
fawkes::ConfigurableAspect
Definition: configurable.h:36
BlackboardListenerThread::bb_interface_created
virtual void bb_interface_created(const char *type, const char *id) noexcept override
Called by the BlackBoardInterfaceObserver when an interface matching a subscribed pattern is created.
Definition: blackboard_listener_thread.cpp:78