24 #include <blackboard/interface_listener.h>
25 #include <core/exceptions/system.h>
26 #include <core/threading/mutex_locker.h>
27 #include <interface/interface.h>
88 BlackBoardInterfaceListener::BlackBoardInterfaceListener(
const char *name_format, ...)
91 va_start(arg, name_format);
92 if (vasprintf(&__name, name_format, arg) == -1) {
97 __bbil_queue_mutex =
new Mutex();
98 __bbil_maps_mutex =
new Mutex();
103 BlackBoardInterfaceListener::~BlackBoardInterfaceListener()
107 delete __bbil_queue_mutex;
108 delete __bbil_maps_mutex;
116 BlackBoardInterfaceListener::bbil_name()
const
128 BlackBoardInterfaceListener::bb_interface_data_changed(
Interface *interface)
throw()
151 BlackBoardInterfaceListener::bb_interface_message_received(
Interface *interface,
166 BlackBoardInterfaceListener::bb_interface_reader_added(
Interface *interface,
167 unsigned int instance_serial)
throw()
180 BlackBoardInterfaceListener::bb_interface_reader_removed(
Interface *interface,
181 unsigned int instance_serial)
throw()
194 BlackBoardInterfaceListener::bb_interface_writer_added(
Interface *interface,
195 unsigned int instance_serial)
throw()
208 BlackBoardInterfaceListener::bb_interface_writer_removed(
Interface *interface,
209 unsigned int instance_serial)
throw()
215 BlackBoardInterfaceListener::bbil_queue_add(QueueEntryType type,
bool op,
216 InterfaceMap ¬_in_map,
223 if (not_in_map.find(interface->
uid()) != not_in_map.end() ) {
224 throw Exception(
"Interface %s already registered (%s)",
225 interface->
uid(), hint);
228 InterfaceQueue::iterator i;
229 for (i = __bbil_queue.begin(); i != __bbil_queue.end(); ++i) {
230 if ((i->type == type) && (*(i->interface) == *interface)) {
231 __bbil_queue.erase(i);
235 QueueEntry qe = { type, op,
interface };
236 __bbil_queue.push_back(qe);
244 BlackBoardInterfaceListener::bbil_add_data_interface(
Interface *interface)
246 bbil_queue_add(DATA,
true, __bbil_maps.data, interface,
"data");
253 BlackBoardInterfaceListener::bbil_add_message_interface(
Interface *interface)
256 throw Exception(
"Message received events can only be watched "
257 "on writing interface instances (%s)", interface->
uid());
259 bbil_queue_add(MESSAGES,
true, __bbil_maps.messages, interface,
"messages");
270 BlackBoardInterfaceListener::bbil_add_reader_interface(
Interface *interface)
272 bbil_queue_add(READER,
true, __bbil_maps.reader, interface,
"reader");
283 BlackBoardInterfaceListener::bbil_add_writer_interface(
Interface *interface)
285 bbil_queue_add(WRITER,
true, __bbil_maps.writer, interface,
"writer");
296 BlackBoardInterfaceListener::bbil_remove_data_interface(
Interface *interface)
298 bbil_queue_add(DATA,
false, __bbil_maps.data, interface,
"data");
307 BlackBoardInterfaceListener::bbil_remove_message_interface(
Interface *interface)
309 bbil_queue_add(MESSAGES,
false, __bbil_maps.messages, interface,
"messages");
319 BlackBoardInterfaceListener::bbil_remove_reader_interface(
Interface *interface)
321 bbil_queue_add(READER,
false, __bbil_maps.reader, interface,
"reader");
331 BlackBoardInterfaceListener::bbil_remove_writer_interface(
Interface *interface)
333 bbil_queue_add(WRITER,
false, __bbil_maps.writer, interface,
"writer");
338 BlackBoardInterfaceListener::bbil_acquire_queue() throw()
340 __bbil_queue_mutex->lock();
345 BlackBoardInterfaceListener::bbil_release_queue(BlackBoard::ListenerRegisterFlag flag)
throw()
347 __bbil_maps_mutex->lock();
349 InterfaceQueue::iterator i = __bbil_queue.begin();
350 while (i != __bbil_queue.end()) {
354 if (flag & BlackBoard::BBIL_FLAG_DATA) {
355 __bbil_maps.data[i->interface->uid()] = i->interface;
356 i = __bbil_queue.erase(i);
361 if (flag & BlackBoard::BBIL_FLAG_MESSAGES) {
362 __bbil_maps.messages[i->interface->uid()] = i->interface;
363 i = __bbil_queue.erase(i);
368 if (flag & BlackBoard::BBIL_FLAG_READER) {
369 __bbil_maps.reader[i->interface->uid()] = i->interface;
370 i = __bbil_queue.erase(i);
375 if (flag & BlackBoard::BBIL_FLAG_WRITER) {
376 __bbil_maps.writer[i->interface->uid()] = i->interface;
377 i = __bbil_queue.erase(i);
388 if (flag & BlackBoard::BBIL_FLAG_DATA) {
389 __bbil_maps.data.erase(i->interface->uid());
390 i = __bbil_queue.erase(i);
395 if (flag & BlackBoard::BBIL_FLAG_MESSAGES) {
396 __bbil_maps.messages.erase(i->interface->uid());
397 i = __bbil_queue.erase(i);
402 if (flag & BlackBoard::BBIL_FLAG_READER) {
403 __bbil_maps.reader.erase(i->interface->uid());
404 i = __bbil_queue.erase(i);
409 if (flag & BlackBoard::BBIL_FLAG_WRITER) {
410 __bbil_maps.writer.erase(i->interface->uid());
411 i = __bbil_queue.erase(i);
422 __bbil_maps_mutex->unlock();
423 __bbil_queue_mutex->unlock();
427 const BlackBoardInterfaceListener::InterfaceMaps &
428 BlackBoardInterfaceListener::bbil_acquire_maps() throw()
430 __bbil_maps_mutex->lock();
435 BlackBoardInterfaceListener::bbil_release_maps() throw()
437 __bbil_queue_mutex->lock();
439 InterfaceMap::iterator i;
440 for (i = __bbil_maps.data.begin(); i != __bbil_maps.data.end(); ++i) {
441 QueueEntry qe = { DATA,
true, i->second };
442 __bbil_queue.push_back(qe);
444 for (i = __bbil_maps.messages.begin(); i != __bbil_maps.messages.end(); ++i) {
445 QueueEntry qe = { MESSAGES,
true, i->second };
446 __bbil_queue.push_back(qe);
448 for (i = __bbil_maps.reader.begin(); i != __bbil_maps.reader.end(); ++i) {
449 QueueEntry qe = { READER,
true, i->second };
450 __bbil_queue.push_back(qe);
452 for (i = __bbil_maps.writer.begin(); i != __bbil_maps.writer.end(); ++i) {
453 QueueEntry qe = { WRITER,
true, i->second };
454 __bbil_queue.push_back(qe);
457 __bbil_maps.data.clear();
458 __bbil_maps.messages.clear();
459 __bbil_maps.reader.clear();
460 __bbil_maps.writer.clear();
462 __bbil_queue_mutex->unlock();
463 __bbil_maps_mutex->unlock();
468 BlackBoardInterfaceListener::bbil_find_interface(
const char *iuid,
471 MutexLocker lock(__bbil_maps_mutex);
472 InterfaceMap::iterator i;
473 if ((i = map.find((
char *)iuid)) != map.end()) {
488 BlackBoardInterfaceListener::bbil_data_interface(
const char *iuid)
throw()
490 return bbil_find_interface(iuid, __bbil_maps.data);
501 BlackBoardInterfaceListener::bbil_message_interface(
const char *iuid)
throw()
503 return bbil_find_interface(iuid, __bbil_maps.messages);
514 BlackBoardInterfaceListener::bbil_reader_interface(
const char *iuid)
throw()
516 return bbil_find_interface(iuid, __bbil_maps.reader);
527 BlackBoardInterfaceListener::bbil_writer_interface(
const char *iuid)
throw()
529 return bbil_find_interface(iuid, __bbil_maps.writer);
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Base class for all Fawkes BlackBoard interfaces.
const char * uid() const
Get unique identifier of interface.
Base class for exceptions in Fawkes.
std::list< QueueEntry > InterfaceQueue
Queue of additions/removal of interfaces.
bool is_writer() const
Check if this is a writing instance.
Mutex mutual exclusion lock.
System ran out of memory and desired operation could not be fulfilled.