Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 }