Fawkes API  Fawkes Development Version
qa_bb_interface.cpp
00001 
00002 /***************************************************************************
00003  *  qa_bb_interface.h - BlackBoard interface QA
00004  *
00005  *  Generated: Tue Oct 17 15:48:45 2006
00006  *  Copyright  2006  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/internal/memory_manager.h>
00028 #include <blackboard/local.h>
00029 #include <blackboard/exceptions.h>
00030 #include <blackboard/bbconfig.h>
00031 
00032 #include <interfaces/TestInterface.h>
00033 
00034 #include <core/exceptions/system.h>
00035 
00036 #include <signal.h>
00037 #include <cstdlib>
00038 #include <cstdio>
00039 
00040 #include <iostream>
00041 #include <vector>
00042 
00043 using namespace std;
00044 using namespace fawkes;
00045 
00046 
00047 bool quit = false;
00048 
00049 void
00050 signal_handler(int signum)
00051 {
00052   quit = true;
00053 }
00054 
00055 
00056 #define NUM_CHUNKS 5
00057 
00058 int
00059 main(int argc, char **argv)
00060 {
00061 
00062   signal(SIGINT, signal_handler);
00063 
00064   LocalBlackBoard *lbb = new LocalBlackBoard(BLACKBOARD_MEMSIZE);
00065 
00066   BlackBoard *bb = lbb;
00067   const BlackBoardMemoryManager *mm = lbb->memory_manager();
00068 
00069   TestInterface *ti_writer;
00070   TestInterface *ti_reader;
00071 
00072   try {
00073     cout << "Opening interfaces.. " << flush;
00074     ti_writer = bb->open_for_writing<TestInterface>("SomeID");
00075     ti_reader = bb->open_for_reading<TestInterface>("SomeID");
00076     cout << "success, " <<
00077             "writer hash=" << ti_writer->hash_printable() <<
00078             "  reader hash=" << ti_reader->hash_printable() << endl;
00079   } catch (Exception &e) {
00080     cout << "failed! Aborting" << endl;
00081     e.print_trace();
00082     exit(1);
00083   }
00084 
00085   try {
00086     cout << "Trying to open second writer.. " << flush;
00087     TestInterface *ti_writer_two;
00088     ti_writer_two = bb->open_for_writing<TestInterface>("SomeID");
00089     bb->close(ti_writer_two);
00090     cout << "BUG: Detection of second writer did NOT work!" << endl;
00091     exit(2);
00092   } catch (BlackBoardWriterActiveException &e) {
00093     cout << "exception caught as expected, detected and prevented second writer!" << endl;
00094   }
00095 
00096   cout << "Printing some meminfo ===============================================" << endl;
00097   cout << "Free chunks:" << endl;
00098   mm->print_free_chunks_info();
00099   cout << "Allocated chunks:" << endl;
00100   mm->print_allocated_chunks_info();
00101   mm->print_performance_info();
00102   cout << "End of meminfo ======================================================" << endl;
00103 
00104   try {
00105     cout << "Trying to open third writer.. " << flush;
00106     TestInterface *ti_writer_three;
00107     ti_writer_three = bb->open_for_writing<TestInterface>("AnotherID");
00108     cout << "No exception as expected, different ID ok!" << endl;
00109     bb->close(ti_writer_three);
00110   } catch (BlackBoardWriterActiveException &e) {
00111     cout << "BUG: Third writer with different ID detected as another writer!" << endl;
00112     exit(3);
00113   }
00114 
00115   cout << endl << endl
00116        << "Running data tests ==================================================" << endl;
00117 
00118   cout << "Writing initial value ("
00119        << TestInterface::TEST_CONSTANT << ") into interface as TestInt" << endl;
00120   ti_writer->set_test_int( TestInterface::TEST_CONSTANT );
00121   try {
00122     ti_writer->write();
00123   } catch (InterfaceWriteDeniedException &e) {
00124     cout << "BUG: caught write denied exception" << endl;
00125     e.print_trace();
00126   }
00127 
00128   cout << "Reading value from reader interface.. " << flush;
00129   ti_reader->read();
00130   int val = ti_reader->test_int();
00131   if ( val == TestInterface::TEST_CONSTANT ) {
00132     cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
00133   } else {
00134     cout << " failure, value is " << ti_reader->test_int() << ", expected "
00135          << TestInterface::TEST_CONSTANT << endl;
00136   }
00137 
00138 
00139   cout << "Iterating over reader interface.." << endl;
00140   InterfaceFieldIterator fi;
00141   for ( fi = ti_reader->fields(); fi != ti_reader->fields_end(); ++fi) {
00142     printf("Name: %20s  Type: %10s  Value: %s\n", fi.get_name(), fi.get_typename(), fi.get_value_string());
00143   }
00144   cout << "done" << endl;
00145 
00146   cout << "Harnessing interface by excessive reading and writing, use Ctrl-C to interrupt" << endl
00147        << "If you do not see any output everything is fine" << endl;
00148   while ( ! quit ) {
00149     int expval = ti_reader->test_int() + 1;
00150     //cout << "Writing value " << expval
00151     // << " into interface as TestInt" << endl;
00152     ti_writer->set_test_int( expval );
00153     try {
00154       ti_writer->write();
00155     } catch (InterfaceWriteDeniedException &e) {
00156       cout << "BUG: caught write denied exception" << endl;
00157       e.print_trace();
00158     }
00159 
00160     //cout << "Reading value from reader interface.. " << flush;
00161     ti_reader->read();
00162     int val = ti_reader->test_int();
00163     if ( val == expval ) {
00164       //cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
00165     } else {
00166       cout << " failure, value is " << ti_reader->test_int() << ", expected "
00167            << expval << endl;
00168     }
00169 
00170     usleep(10);
00171   }
00172 
00173   cout << "Tests done" << endl;
00174 
00175   bb->close(ti_reader);
00176   bb->close(ti_writer);
00177 
00178   delete bb;
00179 }
00180 
00181 
00182 /// @endcond