Fawkes API  Fawkes Development Version
sync_listener.cpp
1 
2 /***************************************************************************
3  * sync_listener.cpp - Sync Interface Listener
4  *
5  * Created: Fri Jun 05 11:01:23 2009
6  * Copyright 2006-2009 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.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "sync_listener.h"
24 
25 #include <blackboard/blackboard.h>
26 #include <logging/logger.h>
27 
28 using namespace fawkes;
29 
30 /** @class SyncInterfaceListener "sync_listener.h"
31  * Synchronize two interfaces.
32  * This class synchronizes two interfaces, a reading and a writing instance
33  * of the same type. To accomplish this it listens for data changed and message
34  * events and forwards them as appropriate to "the other side".
35  * @author Tim Niemueller
36  */
37 
38 /** Constructor.
39  * Automatically registers the listener with the (two) blackboards as
40  * appropriate. It also automatically unregisters in the destructor.
41  * @param logger logger to write informational output to
42  * @param reader reading interface instance
43  * @param writer writing interface instance of the same type as \p reader
44  * @param reader_bb the BlackBoard instance the reading instance has been
45  * created on
46  * @param writer_bb the BlackBoard instance the writing instance has been
47  * created on
48  */
50  fawkes::Interface *reader,
51  fawkes::Interface *writer,
52  fawkes::BlackBoard *reader_bb,
53  fawkes::BlackBoard *writer_bb)
54  : BlackBoardInterfaceListener("SyncInterfaceListener(%s-%s)", writer->uid(), reader->id())
55 {
56  __logger = logger;
57  __reader = reader;
58  __writer = writer;
59  __reader_bb = reader_bb;
60  __writer_bb = writer_bb;
61 
62  bbil_add_data_interface(__reader);
64 
65  __reader_bb->register_listener(this, BlackBoard::BBIL_FLAG_DATA);
66  __writer_bb->register_listener(this, BlackBoard::BBIL_FLAG_MESSAGES);
67 }
68 
69 
70 /** Destructor. */
72 {
73  __reader_bb->unregister_listener(this);
74  __writer_bb->unregister_listener(this);
75 }
76 
77 
78 bool
80  Message *message) throw()
81 {
82  try {
83  if ( interface == __writer ) {
84  //__logger->log_debug(bbil_name(), "Forwarding message");
85  Message *m = message->clone();
86  m->set_hops(message->hops());
87  m->ref();
88  __reader->msgq_enqueue(m);
89  message->set_id(m->id());
90  m->unref();
91  return false;
92  } else {
93  // Don't know why we were called, let 'em enqueue
94  __logger->log_error(bbil_name(), "Message received for unknown interface");
95  return true;
96  }
97  } catch (Exception &e) {
98  __logger->log_error(bbil_name(), "Exception when message received");
99  __logger->log_error("SyncInterfaceListener", e);
100  return false;
101  }
102 }
103 
104 
105 void
107 {
108  try {
109  if ( interface == __reader ) {
110  //__logger->log_debug(bbil_name(), "Copying data");
111  __reader->read();
112  __writer->copy_values(__reader);
113  __writer->write();
114  } else {
115  // Don't know why we were called, let 'em enqueue
116  __logger->log_error(bbil_name(), "Data changed for unknown interface");
117  }
118  } catch (Exception &e) {
119  __logger->log_error(bbil_name(), "Exception when data changed");
120  __logger->log_error(bbil_name(), e);
121  }
122 }
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
unsigned int id() const
Get message ID.
Definition: message.cpp:197
void unref()
Decrement reference count and conditionally delete this instance.
Definition: refcount.cpp:99
Fawkes library namespace.
virtual ~SyncInterfaceListener()
Destructor.
virtual void bb_interface_data_changed(fawkes::Interface *interface)
BlackBoard data changed notification.
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Definition: blackboard.cpp:218
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:500
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:190
Base class for exceptions in Fawkes.
Definition: exception.h:36
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:477
void ref()
Increment reference count.
Definition: refcount.cpp:70
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
unsigned int msgq_enqueue(Message *message)
Enqueue message at end of queue.
Definition: interface.cpp:903
virtual bool bb_interface_message_received(fawkes::Interface *interface, fawkes::Message *message)
BlackBoard message received notification.
const char * bbil_name() const
Get BBIL name.
The BlackBoard abstract class.
Definition: blackboard.h:48
void set_hops(unsigned int hops)
Set number of hops.
Definition: message.cpp:227
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
virtual void copy_values(const Interface *interface)=0
Copy values from another interface.
virtual Message * clone() const
Clone this message.
Definition: message.cpp:419
BlackBoard interface listener.
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.
Interface for logging.
Definition: logger.h:34
SyncInterfaceListener(fawkes::Logger *logger, fawkes::Interface *reader, fawkes::Interface *writer, fawkes::BlackBoard *reader_bb, fawkes::BlackBoard *writer_bb)
Constructor.