Fawkes API  Fawkes Development Version
message_manager.cpp
1 
2 /***************************************************************************
3  * message_manager.cpp - BlackBoard message manager
4  *
5  * Created: Fri Oct 06 11:36:24 2006
6  * Copyright 2006-2007 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/exceptions.h>
25 #include <blackboard/internal/interface_manager.h>
26 #include <blackboard/internal/message_manager.h>
27 #include <blackboard/internal/notifier.h>
28 #include <core/exceptions/software.h>
29 #include <interface/interface.h>
30 #include <interface/message.h>
31 #include <logging/liblogger.h>
32 
33 namespace fawkes {
34 
35 /** @class BlackBoardMessageManager <blackboard/internal/message_manager.h>
36  * BlackBoard message manager.
37  * Transmits messages from reading interface instances to the writer instance
38  * if the interface, if there is any.
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor.
43  * @param notifier BlackBoard notifier to all for events
44  */
45 BlackBoardMessageManager::BlackBoardMessageManager(BlackBoardNotifier *notifier)
46 {
47  im_ = NULL;
48  notifier_ = notifier;
49 }
50 
51 /** Destructor */
53 {
54 }
55 
56 void
58 {
59  if (im_ == NULL) {
60  throw NullPointerException("InterfaceManager has not been set for MessageManager");
61  }
62  try {
63  Interface *writer = im_->writer_for_mem_serial(message->recipient());
64  if (notifier_->notify_of_message_received(writer, message)) {
65  writer->msgq_append(message);
66  }
67  } catch (BlackBoardNoWritingInstanceException &e) {
68  Interface *iface = message->interface();
69  LibLogger::log_warn("BlackBoardMessageManager",
70  "Cannot transmit message from sender %s "
71  "via interface %s (type %s), no writing "
72  "instance exists!",
73  message->sender_thread_name(),
74  (iface != NULL) ? iface->id() : "Unknown",
75  (iface != NULL) ? iface->type() : "unknown");
76  throw;
77  }
78 }
79 
80 /** Set interface manager.
81  * @param im interface manager
82  */
83 void
84 BlackBoardMessageManager::set_interface_manager(BlackBoardInterfaceManager *im)
85 {
86  im_ = im;
87 }
88 
89 } // end namespace fawkes
fawkes::BlackBoardMessageManager::BlackBoardMessageManager
BlackBoardMessageManager(BlackBoardNotifier *notifier)
Constructor.
Definition: message_manager.cpp:51
fawkes::Message
Definition: message.h:41
fawkes::Message::sender_thread_name
const char * sender_thread_name() const
Get sender of message.
Definition: message.cpp:313
fawkes::Message::interface
Interface * interface() const
Get transmitting interface.
Definition: message.cpp:343
fawkes::BlackBoardNotifier::notify_of_message_received
bool notify_of_message_received(const Interface *interface, Message *message)
Notify of message received Notify all subscribers of the given interface of an incoming message This ...
Definition: notifier.cpp:746
fawkes::LibLogger::log_warn
static void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: liblogger.cpp:162
fawkes::BlackBoardMessageManager::transmit
virtual void transmit(Message *message)
Transmit message.
Definition: message_manager.cpp:63
fawkes
fawkes::BlackBoardMessageManager::~BlackBoardMessageManager
~BlackBoardMessageManager()
Destructor.
Definition: message_manager.cpp:58
fawkes::Interface::msgq_append
void msgq_append(Message *message)
Enqueue message.
Definition: interface.cpp:955
fawkes::Interface
Definition: interface.h:78
fawkes::NullPointerException
Definition: software.h:37
fawkes::Message::recipient
unsigned int recipient() const
Get recipient memory serial.
Definition: message.cpp:257