Fawkes API  Fawkes Development Version
blackboard_listener_thread.cpp
1 /***************************************************************************
2  * blackboard_listener_thread.cpp - 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 #include "blackboard_listener_thread.h"
21 
22 #include <core/threading/mutex_locker.h>
23 
24 using namespace fawkes;
25 using namespace std;
26 
27 BlackboardListenerThread *BlackboardListenerThread::instance_ = nullptr;
28 
29 BlackboardListenerThread::BlackboardListenerThread()
30 : Thread("ProtoboardBlackboardManager", Thread::OPMODE_WAITFORWAKEUP),
31  BlackBoardInterfaceListener("eclipse-clp")
32 {
33 }
34 
35 /** Get the singleton instance of this thread
36  * @return THE instance
37  */
40 {
41  if (!instance_)
42  instance_ = new BlackboardListenerThread();
43  return instance_;
44 }
45 
46 /** Delete singleton instance, e.g. when unloading the plugin */
47 void
49 {
50  delete instance_;
51 }
52 
53 /** Trigger events if an interface matching the pattern is created or destroyed
54  * @param type_pattern See BlackBoardInterfaceObserver::bbio_add_observed_create
55  * @param id_pattern See BlackBoardInterfaceObserver::bbio_add_observed_create
56  */
57 void
58 BlackboardListenerThread::observe_pattern(const char *type_pattern, const char *id_pattern) noexcept
59 {
60  MutexLocker lock(&state_mutex_);
61  bbio_add_observed_create(type_pattern, id_pattern);
62  bbio_add_observed_destroy(type_pattern, id_pattern);
63 }
64 
65 /** Register @param interface for change notifications */
66 void
68 {
69  MutexLocker lock(&state_mutex_);
70  bbil_add_data_interface(interface);
71 }
72 
73 /** Called by the BlackBoardInterfaceObserver when an interface matching a subscribed pattern is created
74  * @param type Interface type name
75  * @param id Interface ID
76  */
77 void
78 BlackboardListenerThread::bb_interface_created(const char *type, const char *id) noexcept
79 {
80  MutexLocker lock(&state_mutex_);
81  iface_events_.emplace(new BlackboardListenerThread::Created{type, id});
82 }
83 
84 /** Called by the BlackBoardInterfaceObserver when an interface is destroyed
85  * @param type Interface type name
86  * @param id Interface ID
87  */
88 void
89 BlackboardListenerThread::bb_interface_destroyed(const char *type, const char *id) noexcept
90 {
91  MutexLocker lock(&state_mutex_);
92  iface_events_.emplace(new BlackboardListenerThread::Destroyed{type, id});
93 }
94 
95 /** Called by the BlackBoardInterfaceListener when an interface changes
96  * @param interface The changed interface
97  */
98 void
100 {
101  MutexLocker lock(&state_mutex_);
102  iface_events_.emplace(new BlackboardListenerThread::Changed{interface});
103 }
104 
105 /** Test whether any events are in the queue
106  * @return Whether any events are in the queue
107  */
108 bool
110 {
111  MutexLocker lock(&state_mutex_);
112  return !iface_events_.empty();
113 }
114 
115 /** Return and remove the next event in the queue
116  * @return The next event
117  */
118 shared_ptr<BlackboardListenerThread::Event>
120 {
121  MutexLocker lock(&state_mutex_);
122  shared_ptr<BlackboardListenerThread::Event> rv = iface_events_.front();
123  iface_events_.pop();
124  return rv;
125 }
126 
127 /** Return an eclipse term representing the event
128  * @return An eclipse term representing the event: bb_created(UID)
129  */
130 BlackboardListenerThread::Created::operator EC_word()
131 {
132  return ::term(EC_functor("bb_created", 1), uid().c_str());
133 }
134 
135 /** Return an eclipse term representing the event
136  * @return An eclipse term representing the event: bb_destroyed(UID)
137  */
138 BlackboardListenerThread::Destroyed::operator EC_word()
139 {
140  return ::term(EC_functor("bb_destroyed", 1), uid().c_str());
141 }
142 
143 /** Return an eclipse term representing the event
144  * @return An eclipse term representing the event: bb_changed(UID)
145  */
146 BlackboardListenerThread::Changed::operator EC_word()
147 {
148  return ::term(EC_functor("bb_changed", 1), uid().c_str());
149 }
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.
BlackBoard interface listener.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
Mutex locking helper.
Definition: mutex_locker.h:34
Thread class encapsulation of pthreads.
Definition: thread.h:46
Fawkes library namespace.