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
00039 #include <iostream>
00040 #include <vector>
00041
00042 using namespace std;
00043 using namespace fawkes;
00044
00045
00046 bool quit = false;
00047
00048 void
00049 signal_handler(int signum)
00050 {
00051   quit = true;
00052 }
00053
00054
00055 #define NUM_CHUNKS 5
00056 
00057 int
00058 main(int argc, char **argv)
00059 {
00060
00061   signal(SIGINT, signal_handler);
00062
00063   LocalBlackBoard *lbb = new LocalBlackBoard(BLACKBOARD_MEMSIZE,
00064                                              BLACKBOARD_MAGIC_TOKEN,
00065                                              /* master */  true);
00066
00067   BlackBoard *bb = lbb;
00068   const BlackBoardMemoryManager *mm = lbb->memory_manager();
00069
00070   TestInterface *ti_writer;
00071   TestInterface *ti_reader;
00072
00073   try {
00074     cout << "Opening interfaces.. " << flush;
00075     ti_writer = bb->open_for_writing<TestInterface>("SomeID");
00076     ti_reader = bb->open_for_reading<TestInterface>("SomeID");
00077     cout << "success, " <<
00078             "writer hash=" << ti_writer->hash_printable() <<
00079             "  reader hash=" << ti_reader->hash_printable() << endl;
00080   } catch (Exception &e) {
00081     cout << "failed! Aborting" << endl;
00082     e.print_trace();
00083     exit(1);
00084   }
00085
00086   try {
00087     cout << "Trying to open second writer.. " << flush;
00088     TestInterface *ti_writer_two;
00089     ti_writer_two = bb->open_for_writing<TestInterface>("SomeID");
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   Interface::FieldIterator fi;
00141   for ( fi = ti_reader->fields(); fi != ti_reader->fields_end(); ++fi) {
00142     switch (fi.get_type() ) {
00143     case Interface::IFT_BOOL:
00144       printf("Name: %s  Type: bool       Value: %i\n", fi.get_name(), fi.get_bool());
00145       break;
00146     case Interface::IFT_INT:
00147       printf("Name: %s  Type: int        Value: %i\n", fi.get_name(), fi.get_int());
00148       break;
00149     case Interface::IFT_UINT:
00150       printf("Name: %s  Type: uint       Value: %u\n", fi.get_name(), fi.get_uint());
00151       break;
00152     case Interface::IFT_LONGINT:
00153       printf("Name: %s  Type: long int   Value: %li\n", fi.get_name(), fi.get_longint());
00154       break;
00155     case Interface::IFT_LONGUINT:
00156       printf("Name: %s  Type: long uint  Value: %lu\n", fi.get_name(), fi.get_longuint());
00157       break;
00158     case Interface::IFT_FLOAT:
00159       printf("Name: %s  Type: float      Value: %f\n", fi.get_name(), fi.get_float());
00160       break;
00161     case Interface::IFT_STRING:
00162       printf("Name: %s  Type: string     Value: %s\n", fi.get_name(), fi.get_string());
00163       break;
00164     case Interface::IFT_BYTE:
00165       printf("Name: %s  Type: byte       Value: %u\n", fi.get_name(), fi.get_byte());
00166       break;
00167     }
00168   }
00169   cout << "done" << endl;
00170
00171   cout << "Harnessing interface by excessive reading and writing, use Ctrl-C to interrupt" << endl
00172        << "If you do not see any output everything is fine" << endl;
00173   while ( ! quit ) {
00174     int expval = ti_reader->test_int() + 1;
00175     //cout << "Writing value " << expval
00176     // << " into interface as TestInt" << endl;
00177     ti_writer->set_test_int( expval );
00178     try {
00179       ti_writer->write();
00180     } catch (InterfaceWriteDeniedException &e) {
00181       cout << "BUG: caught write denied exception" << endl;
00182       e.print_trace();
00183     }
00184
00185     //cout << "Reading value from reader interface.. " << flush;
00186     ti_reader->read();
00187     int val = ti_reader->test_int();
00188     if ( val == expval ) {
00189       //cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
00190     } else {
00191       cout << " failure, value is " << ti_reader->test_int() << ", expected "
00192            << expval << endl;
00193     }
00194
00195     usleep(10);
00196   }
00197
00198   cout << "Tests done" << endl;
00199
00200   bb->close(ti_reader);
00201   bb->close(ti_writer);
00202
00203   delete bb;
00204 }
00205
00206 
00207 /// @endcond