Fawkes API  Fawkes Development Version
qa_bb_buffers.cpp
00001 
00002 /***************************************************************************
00003  *  qa_bb_buffers.h - BlackBoard interface QA
00004  *
00005  *  Generated: Tue May 24 23:39:22 2011
00006  *  Copyright  2006-2011  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/exceptions.h>
00029 #include <blackboard/bbconfig.h>
00030 
00031 #include <interfaces/TestInterface.h>
00032 
00033 #include <core/exceptions/system.h>
00034 
00035 #include <signal.h>
00036 #include <cstdlib>
00037 #include <cstdio>
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 int
00056 main(int argc, char **argv)
00057 {
00058 
00059   signal(SIGINT, signal_handler);
00060 
00061   LocalBlackBoard *lbb = new LocalBlackBoard(BLACKBOARD_MEMSIZE);
00062 
00063   BlackBoard *bb = lbb;
00064 
00065   TestInterface *ti_writer;
00066   TestInterface *ti_reader;
00067 
00068   try {
00069     cout << "Opening interfaces.. " << flush;
00070     ti_writer = bb->open_for_writing<TestInterface>("SomeID");
00071     ti_reader = bb->open_for_reading<TestInterface>("SomeID");
00072     cout << "success, " <<
00073             "writer hash=" << ti_writer->hash_printable() <<
00074             "  reader hash=" << ti_reader->hash_printable() << endl;
00075   } catch (Exception &e) {
00076     cout << "failed! Aborting" << endl;
00077     e.print_trace();
00078     exit(1);
00079   }
00080 
00081   cout << endl << endl
00082        << "Running data tests =================================================="
00083        << endl;
00084 
00085   cout << "Writing initial value ("
00086        << TestInterface::TEST_CONSTANT << ") into interface as TestInt" << endl;
00087   ti_writer->set_test_int( TestInterface::TEST_CONSTANT );
00088   try {
00089     ti_writer->write();
00090   } catch (InterfaceWriteDeniedException &e) {
00091     cout << "BUG: caught write denied exception" << endl;
00092     e.print_trace();
00093   }
00094 
00095   cout << "Reading value from reader interface.. " << flush;
00096   ti_reader->read();
00097   int val = ti_reader->test_int();
00098   if ( val == TestInterface::TEST_CONSTANT ) {
00099     cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
00100   } else {
00101     cout << " failure, value is " << ti_reader->test_int() << ", expected "
00102          << TestInterface::TEST_CONSTANT << endl;
00103   }
00104 
00105   cout << "Resizing buffer.. " << flush;
00106   try {
00107     ti_reader->resize_buffers(1);
00108     ti_reader->copy_private_to_buffer(0);
00109   } catch (Exception &e) {
00110     cout << "ERROR: Resizing failed, exception follows" << endl;
00111     e.print_trace();
00112     throw;
00113   }
00114 
00115 
00116   cout << "Testing buffers, use Ctrl-C to interrupt" << endl
00117        << "If you do not see any output everything is fine" << endl;
00118   while ( ! quit ) {
00119 
00120     //cout << "Writing value " << expval
00121     // << " into interface as TestInt" << endl;
00122     ti_writer->set_test_int( ti_writer->test_int() + 1 );
00123     try {
00124       ti_writer->write();
00125     } catch (InterfaceWriteDeniedException &e) {
00126       cout << "BUG: caught write denied exception" << endl;
00127       e.print_trace();
00128     }
00129 
00130     //cout << "Reading value from reader interface.. " << flush;
00131     ti_reader->read();
00132     int rval = ti_reader->test_int();
00133     int wval = ti_writer->test_int();
00134 
00135     ti_reader->read_from_buffer(0);
00136     int bval = ti_reader->test_int();
00137 
00138     if ( rval != wval ) {
00139       cout << " failure, reader value is " << rval << ", writer has "
00140            << wval << endl;
00141     }
00142 
00143     if ( rval != bval + 1 ) {
00144       cout << " failure, reader value is " << rval << ", buffer has "
00145            << bval << endl;
00146     }
00147 
00148     // could to copy_shared as well, but that is a little less predictable in
00149     // the case of concurrent writers, hence we want people to copy and paste
00150     // this version.
00151     ti_reader->read();
00152     ti_reader->copy_private_to_buffer(0);
00153 
00154     usleep(10);
00155   }
00156 
00157   cout << "Tests done" << endl;
00158 
00159   bb->close(ti_reader);
00160   bb->close(ti_writer);
00161 
00162   delete bb;
00163 }
00164 
00165 
00166 /// @endcond