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
Changed(Interface *interface)
Constructor.
Abstract superclass for blackboard events.
std::string uid()
Return the UID (i.e.
string type
Triggering interface's type name.
Event(const std::string &type, const std::string &id)
Constructor.
string id
Triggering interface's ID.
Keeps a queue of subscribed blackboard events that can be queried in a thread-safe manner.
virtual void bb_interface_data_changed(Interface *interface) noexcept override
Called by the BlackBoardInterfaceListener when an interface changes.
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.
static void cleanup_instance()
Delete singleton instance, e.g.
bool event_pending()
Test whether any events are in the queue.
static BlackboardListenerThread * instance()
Get the singleton instance of this thread.
virtual void bb_interface_destroyed(const char *type, const char *id) noexcept override
Called by the BlackBoardInterfaceObserver when an interface is destroyed.
shared_ptr< Event > event_pop()
Return and remove the next event in the queue.
void listen_for_change(Interface *interface) noexcept
Register.
void observe_pattern(const char *type_pattern, const char *id_pattern) noexcept
Trigger events if an interface matching the pattern is created or destroyed.
Thread aspect to access to BlackBoard.
Definition: blackboard.h:34
BlackBoard interface listener.
BlackBoard interface observer.
Thread aspect to access configuration data.
Definition: configurable.h:33
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
Thread aspect to log output.
Definition: logging.h:33
Mutex mutual exclusion lock.
Definition: mutex.h:33
Thread class encapsulation of pthreads.
Definition: thread.h:46