Fawkes API  Fawkes Development Version
on_message_waker.cpp
1 
2 /***************************************************************************
3  * on_message_waker.h - wake a thread whenever a message is received
4  *
5  * Created: Thu Dec 06 12:05:14 2012
6  * Copyright 2012 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 #include <blackboard/blackboard.h>
25 #include <blackboard/utils/on_message_waker.h>
26 #include <core/threading/thread.h>
27 #include <interface/interface.h>
28 #include <interface/message.h>
29 
30 namespace fawkes {
31 
32 /** @class BlackBoardOnMessageWaker <blackboard/utils/on_message_waker.h>
33  * Wake threads on receiving a blackboard message.
34  * This utility class registers as a BlackBoardInterfaceListener and
35  * if a message is received it wakes the given thread.
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor.
40  * @param bb blackboard to register with
41  * @param interface Interface to monitor for incoming messages
42  * @param thread thread to wake
43  */
45  Interface * interface,
46  Thread * thread)
47 : BlackBoardInterfaceListener("OnMessageWaker[%s]", interface->uid()), bb_(bb), thread_(thread)
48 {
49  bbil_add_message_interface(interface);
51 }
52 
53 /** Destructor.
54  * Unregisters from the blackboard.
55  */
57 {
58  bb_->unregister_listener(this);
59 }
60 
61 bool
63  Message * message) throw()
64 {
65  try {
66  interface->msgq_append(message);
67  thread_->wakeup();
68  return false;
69  } catch (Exception &e) {
70  return true;
71  }
72 }
73 
74 } // end namespace fawkes
fawkes::BlackBoardOnMessageWaker::~BlackBoardOnMessageWaker
virtual ~BlackBoardOnMessageWaker()
Destructor.
Definition: on_message_waker.cpp:56
fawkes::BlackBoard::BBIL_FLAG_MESSAGES
@ BBIL_FLAG_MESSAGES
consider message received events
Definition: blackboard.h:89
fawkes::BlackBoard::register_listener
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:185
fawkes::Message
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:45
fawkes::BlackBoardOnMessageWaker::BlackBoardOnMessageWaker
BlackBoardOnMessageWaker(BlackBoard *bb, Interface *interface, Thread *thread)
Constructor.
Definition: on_message_waker.cpp:44
fawkes::BlackBoard::unregister_listener
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Definition: blackboard.cpp:212
fawkes::BlackBoardInterfaceListener
BlackBoard interface listener.
Definition: interface_listener.h:42
fawkes::BlackBoard
The BlackBoard abstract class.
Definition: blackboard.h:46
fawkes::BlackBoardOnMessageWaker::bb_interface_message_received
virtual bool bb_interface_message_received(Interface *interface, Message *message)
BlackBoard message received notification.
Definition: on_message_waker.cpp:62
fawkes
Fawkes library namespace.
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:241
fawkes::Interface
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
fawkes::Thread
Thread class encapsulation of pthreads.
Definition: thread.h:46
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36