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     cout << "BUG: Detection of second writer did NOT work!" << endl;
00090     exit(2);
00091   } catch (BlackBoardWriterActiveException &e) {
00092     cout << "exception caught as expected, detected and prevented second writer!" << endl;
00093   }
00094 
00095   cout << "Printing some meminfo ===============================================" << endl;
00096   cout << "Free chunks:" << endl;
00097   mm->print_free_chunks_info();
00098   cout << "Allocated chunks:" << endl;
00099   mm->print_allocated_chunks_info();
00100   mm->print_performance_info();
00101   cout << "End of meminfo ======================================================" << endl;
00102 
00103   try {
00104     cout << "Trying to open third writer.. " << flush;
00105     TestInterface *ti_writer_three;
00106     ti_writer_three = bb->open_for_writing<TestInterface>("AnotherID");
00107     cout << "No exception as expected, different ID ok!" << endl;
00108     bb->close(ti_writer_three);
00109   } catch (BlackBoardWriterActiveException &e) {
00110     cout << "BUG: Third writer with different ID detected as another writer!" << endl;
00111     exit(3);
00112   }
00113 
00114   cout << endl << endl
00115        << "Running data tests ==================================================" << endl;
00116 
00117   cout << "Writing initial value ("
00118        << TestInterface::TEST_CONSTANT << ") into interface as TestInt" << endl;
00119   ti_writer->set_test_int( TestInterface::TEST_CONSTANT );
00120   try {
00121     ti_writer->write();
00122   } catch (InterfaceWriteDeniedException &e) {
00123     cout << "BUG: caught write denied exception" << endl;
00124     e.print_trace();
00125   }
00126 
00127   cout << "Reading value from reader interface.. " << flush;
00128   ti_reader->read();
00129   int val = ti_reader->test_int();
00130   if ( val == TestInterface::TEST_CONSTANT ) {
00131     cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
00132   } else {
00133     cout << " failure, value is " << ti_reader->test_int() << ", expected "
00134          << TestInterface::TEST_CONSTANT << endl;
00135   }
00136 
00137 
00138   cout << "Iterating over reader interface.." << endl;
00139   InterfaceFieldIterator fi;
00140   for ( fi = ti_reader->fields(); fi != ti_reader->fields_end(); ++fi) {
00141     printf("Name: %20s  Type: %10s  Value: %s\n", fi.get_name(), fi.get_typename(), fi.get_value_string());
00142   }
00143   cout << "done" << endl;
00144 
00145   cout << "Harnessing interface by excessive reading and writing, use Ctrl-C to interrupt" << endl
00146        << "If you do not see any output everything is fine" << endl;
00147   while ( ! quit ) {
00148     int expval = ti_reader->test_int() + 1;
00149     //cout << "Writing value " << expval
00150     // << " into interface as TestInt" << endl;
00151     ti_writer->set_test_int( expval );
00152     try {
00153       ti_writer->write();
00154     } catch (InterfaceWriteDeniedException &e) {
00155       cout << "BUG: caught write denied exception" << endl;
00156       e.print_trace();
00157     }
00158 
00159     //cout << "Reading value from reader interface.. " << flush;
00160     ti_reader->read();
00161     int val = ti_reader->test_int();
00162     if ( val == expval ) {
00163       //cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
00164     } else {
00165       cout << " failure, value is " << ti_reader->test_int() << ", expected "
00166            << expval << endl;
00167     }
00168 
00169     usleep(10);
00170   }
00171 
00172   cout << "Tests done" << endl;
00173 
00174   bb->close(ti_reader);
00175   bb->close(ti_writer);
00176 
00177   delete bb;
00178 }
00179 
00180 
00181 /// @endcond