Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * qa_bb_notify.cpp - BlackBoard notification QA 00004 * 00005 * Created: Mon Nov 12 14:35:53 2007 00006 * Copyright 2006-2007 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 00025 /// @cond QA 00026 00027 #include <blackboard/local.h> 00028 #include <blackboard/remote.h> 00029 #include <blackboard/exceptions.h> 00030 #include <blackboard/bbconfig.h> 00031 #include <blackboard/interface_listener.h> 00032 #include <blackboard/interface_observer.h> 00033 00034 #include <interfaces/TestInterface.h> 00035 00036 #include <core/threading/thread.h> 00037 #include <core/exceptions/system.h> 00038 #include <utils/logging/liblogger.h> 00039 00040 #include <signal.h> 00041 #include <cstdlib> 00042 #include <cstdio> 00043 00044 #include <iostream> 00045 #include <vector> 00046 00047 using namespace std; 00048 using namespace fawkes; 00049 00050 class QaBBEventListener 00051 : public BlackBoardInterfaceListener, 00052 public BlackBoardInterfaceObserver 00053 { 00054 public: 00055 QaBBEventListener() : BlackBoardInterfaceListener("QaBBEventListener") 00056 { 00057 bbio_add_observed_create("TestInterface", "AnotherID *"); 00058 bbio_add_observed_destroy("TestInterface"); 00059 } 00060 00061 virtual void bb_interface_created(const char *type, const char *id) throw() 00062 { 00063 printf("BBIO: Interface %s of type %s has been created\n", id, type); 00064 } 00065 00066 virtual void bb_interface_destroyed(const char *type, const char *id) throw() 00067 { 00068 printf("BBIO: Interface %s of type %s has been destroyed\n", id, type); 00069 } 00070 00071 00072 virtual void bb_interface_data_changed(Interface *interface) throw() 00073 { 00074 printf("BBIL: Data in interface %s has been modified\n", interface->uid()); 00075 } 00076 00077 virtual bool bb_interface_message_received(Interface *interface, Message *message) throw() 00078 { 00079 printf("BBIL: Message of type %s for interface %s has been received\n", 00080 message->type(), interface->uid()); 00081 // do not enqueue, then we do not have to flush it 00082 return false; 00083 } 00084 00085 virtual void bb_interface_writer_added(Interface *interface, unsigned int instance_serial) throw() 00086 { 00087 printf("BBIL: Writer has been added to interface %s/%u (event serial %u)\n", 00088 interface->uid(), interface->serial(), instance_serial); 00089 } 00090 00091 virtual void bb_interface_writer_removed(Interface *interface, unsigned int instance_serial) throw() 00092 { 00093 printf("BBIL: Writer has been removed from interface %s/%u (event serial %u)\n", 00094 interface->uid(), interface->serial(), instance_serial); 00095 } 00096 00097 virtual void bb_interface_reader_added(Interface *interface, unsigned int instance_serial) throw() 00098 { 00099 printf("BBIL: Reader has been added to interface %s/%u (event serial %u)\n", 00100 interface->uid(), interface->serial(), instance_serial); 00101 } 00102 00103 virtual void bb_interface_reader_removed(Interface *interface, unsigned int instance_serial) throw() 00104 { 00105 printf("BBIL: Reader has been removed from interface %s/%u (event serial %u)\n", 00106 interface->uid(), interface->serial(), instance_serial); 00107 } 00108 00109 virtual void add_interface(Interface *interface) throw() 00110 { 00111 printf("Listener: Adding interface %s (this: %p)\n", interface->uid(), this); 00112 bbil_add_data_interface(interface); 00113 try { 00114 if ( ! interface->is_writer() ) { 00115 printf("Trying to add non-writing instance as message listener, this will fail\n"); 00116 } 00117 bbil_add_message_interface(interface); 00118 if ( ! interface->is_writer() ) { 00119 printf("Did not fail!? BUG!\n"); 00120 } 00121 } catch (Exception &e) { 00122 if ( ! interface->is_writer() ) { 00123 printf("Failed as expected (%s). Good.\n", e.what()); 00124 } 00125 } 00126 bbil_add_reader_interface(interface); 00127 bbil_add_writer_interface(interface); 00128 } 00129 }; 00130 00131 00132 int 00133 main(int argc, char **argv) 00134 { 00135 LibLogger::init(); 00136 Thread::init_main(); 00137 00138 //RemoteBlackBoard *bb = new RemoteBlackBoard("localhost", 1910); 00139 BlackBoard *bb = new LocalBlackBoard(BLACKBOARD_MEMSIZE); 00140 00141 QaBBEventListener qabbel; 00142 00143 TestInterface *ti_writer_1; 00144 TestInterface *ti_writer_2; 00145 TestInterface *ti_writer_3; 00146 TestInterface *ti_writer_4; 00147 TestInterface *ti_writer_5; 00148 TestInterface *ti_writer_6; 00149 00150 TestInterface *ti_reader_1; 00151 TestInterface *ti_reader_2; 00152 00153 try { 00154 cout << "Opening interfaces.. (SomeID *)" << endl; 00155 ti_writer_1 = bb->open_for_writing<TestInterface>("SomeID 1"); 00156 ti_reader_1 = bb->open_for_reading<TestInterface>("SomeID 1"); 00157 ti_writer_2 = bb->open_for_writing<TestInterface>("SomeID 2"); 00158 ti_reader_2 = bb->open_for_reading<TestInterface>("SomeID reader 1"); 00159 00160 qabbel.add_interface(ti_writer_1); 00161 qabbel.add_interface(ti_writer_2); 00162 qabbel.add_interface(ti_reader_2); 00163 bb->register_listener(&qabbel, BlackBoard::BBIL_FLAG_ALL); 00164 bb->register_observer(&qabbel, BlackBoard::BBIO_FLAG_ALL); 00165 00166 cout << "Opening interfaces.. (SomeID 3, should NOT trigger BBIO)" << endl; 00167 ti_writer_3 = bb->open_for_writing<TestInterface>("SomeID 3"); 00168 cout << "Opening interfaces.. (AnotherID *, SHOULD trigger BBIO)" << endl; 00169 ti_writer_4 = bb->open_for_writing<TestInterface>("AnotherID 1"); 00170 ti_writer_5 = bb->open_for_writing<TestInterface>("AnotherID 2"); 00171 ti_writer_6 = bb->open_for_writing<TestInterface>("AnotherID 3"); 00172 cout << "success" << endl; 00173 } catch (Exception &e) { 00174 cout << "failed! Aborting" << endl; 00175 e.print_trace(); 00176 exit(1); 00177 } 00178 00179 usleep(100000); 00180 00181 std::list<TestInterface *> readers = bb->open_multiple_for_reading<TestInterface>(); 00182 usleep(100000); 00183 for (std::list<TestInterface *>::iterator i = readers.begin(); i != readers.end(); ++i) { 00184 printf("Opened reader for interface %s of type %s\n", (*i)->id(), (*i)->type()); 00185 bb->close(*i); 00186 } 00187 00188 usleep(100000); 00189 00190 const char* pattern = "AnotherID *"; 00191 readers = bb->open_multiple_for_reading<TestInterface>(pattern); 00192 printf("Found %zu interfaces with pattern \"%s\"\n", readers.size(), pattern); 00193 for (std::list<TestInterface *>::iterator i = readers.begin(); i != readers.end(); ++i) { 00194 printf("Opened reader for interface %s of type %s\n", (*i)->id(), (*i)->type()); 00195 bb->close(*i); 00196 } 00197 00198 usleep(100000); 00199 00200 printf("Sending a message to test message received event\n"); 00201 TestInterface::SetTestIntMessage *m = new TestInterface::SetTestIntMessage(27); 00202 unsigned int msg_id = ti_reader_1->msgq_enqueue(m); 00203 printf("Message ID = %u, enqueued messages: %u\n", msg_id, ti_writer_1->msgq_size()); 00204 00205 printf("Removing writer 1. No BBIL output should appear\n"); 00206 bb->close(ti_writer_1); 00207 00208 bb->unregister_listener(&qabbel); 00209 usleep(100000); 00210 00211 printf("Removing other writers. No warning should appear.\n"); 00212 bb->close(ti_writer_2); 00213 bb->close(ti_writer_3); 00214 bb->close(ti_writer_4); 00215 bb->close(ti_writer_5); 00216 bb->close(ti_writer_6); 00217 00218 bb->close(ti_reader_1); 00219 bb->close(ti_reader_2); 00220 00221 usleep(100000); 00222 00223 delete bb; 00224 Thread::destroy_main(); 00225 LibLogger::finalize(); 00226 } 00227 00228 00229 /// @endcond