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/utils/on_message_waker.h>
25 #include <blackboard/blackboard.h>
26 #include <interface/interface.h>
27 #include <interface/message.h>
28 #include <core/threading/thread.h>
29 
30 namespace fawkes {
31 #if 0 /* just to make Emacs auto-indent happy */
32 }
33 #endif
34 
35 /** @class BlackBoardOnMessageWaker <blackboard/utils/on_message_waker.h>
36  * Wake threads on receiving a blackboard message.
37  * This utility class registers as a BlackBoardInterfaceListener and
38  * if a message is received it wakes the given thread.
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor.
43  * @param bb blackboard to register with
44  * @param interface Interface to monitor for incoming messages
45  * @param thread thread to wake
46  */
48  Interface *interface,
49  Thread *thread)
50  : BlackBoardInterfaceListener("OnMessageWaker[%s]", interface->uid()),
51  bb_(bb), thread_(thread)
52 {
53  bbil_add_message_interface(interface);
55 }
56 
57 
58 /** Destructor.
59  * Unregisters from the blackboard.
60  */
62 {
63  bb_->unregister_listener(this);
64 }
65 
66 
67 bool
69  Message *message) throw()
70 {
71  try {
72  interface->msgq_append(message);
73  thread_->wakeup();
74  return false;
75  } catch (Exception &e) {
76  return true;
77  }
78 }
79 
80 
81 } // end namespace fawkes
82 
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
Fawkes library namespace.
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Definition: blackboard.cpp:218
Thread class encapsulation of pthreads.
Definition: thread.h:42
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
virtual ~BlackBoardOnMessageWaker()
Destructor.
consider message received events
Definition: blackboard.h:100
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:190
void wakeup()
Wake up thread.
Definition: thread.cpp:1000
Base class for exceptions in Fawkes.
Definition: exception.h:36
BlackBoardOnMessageWaker(BlackBoard *bb, Interface *interface, Thread *thread)
Constructor.
The BlackBoard abstract class.
Definition: blackboard.h:48
virtual bool bb_interface_message_received(Interface *interface, Message *message)
BlackBoard message received notification.
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
BlackBoard interface listener.