qa_bb_messaging.cpp

00001 
00002 /***************************************************************************
00003  *  qa_bb_messaging.h - BlackBoard messaging QA
00004  *
00005  *  Generated: Tue Oct 31 15:36:19 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/local.h>
00028 #include <blackboard/remote.h>
00029 #include <blackboard/exceptions.h>
00030 #include <blackboard/bbconfig.h>
00031 
00032 #include <interfaces/TestInterface.h>
00033 
00034 #include <core/threading/thread.h>
00035 #include <core/exceptions/system.h>
00036 #include <utils/time/time.h>
00037 
00038 #include <signal.h>
00039 #include <cstdlib>
00040 #include <cstdio>
00041 
00042 #include <iostream>
00043 #include <vector>
00044 
00045 using namespace std;
00046 using namespace fawkes;
00047 
00048 
00049 bool quit = false;
00050 
00051 void
00052 signal_handler(int signum)
00053 {
00054   quit = true;
00055 }
00056 
00057 
00058 #define NUM_CHUNKS 5
00059 #define BLACKBOARD_MEMSIZE 2 * 1024 * 1024
00060 #define BLACKBOARD_MAGIC_TOKEN "FawkesBlackBoard"
00061 
00062 int
00063 main(int argc, char **argv)
00064 {
00065 
00066   Thread::init_main();
00067 
00068   signal(SIGINT, signal_handler);
00069 
00070   BlackBoard *bb = new LocalBlackBoard(BLACKBOARD_MEMSIZE);
00071   //BlackBoard *bb = new RemoteBlackBoard("localhost", 1910);
00072 
00073   TestInterface *ti_writer;
00074   TestInterface *ti_reader;
00075 
00076   try {
00077     cout << "Opening interfaces.. " << flush;
00078     ti_writer = bb->open_for_writing<TestInterface>("SomeID");
00079     ti_reader = bb->open_for_reading<TestInterface>("SomeID");
00080     cout << "success" << endl;
00081   } catch (Exception &e) {
00082     cout << "failed! Aborting" << endl;
00083     e.print_trace();
00084     exit(1);
00085   }
00086 
00087   cout << "Writing initial value ("
00088        << TestInterface::TEST_CONSTANT << ") into interface as TestInt" << endl;
00089   ti_writer->set_test_int( 5 );
00090   try {
00091     ti_writer->write();
00092   } catch (InterfaceWriteDeniedException &e) {
00093     cout << "BUG: caught write denied exception" << endl;
00094     e.print_trace();
00095   }
00096 
00097   cout << "Reading value from reader interface.. " << flush;
00098   ti_reader->read();
00099   int val = ti_reader->test_int();
00100   if ( val == TestInterface::TEST_CONSTANT ) {
00101     cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
00102   } else {
00103     cout << " failure, value is " << ti_reader->test_int() << ", expected "
00104          << TestInterface::TEST_CONSTANT << endl;
00105   }
00106 
00107   printf("Reader instance serial: %u\n", ti_reader->serial());
00108 
00109   cout << "Harnessing message queues by excessively sending messages" << endl
00110        << "Press Ctrl-C to stop testing. No output means everything is fine" << endl;
00111   while ( ! quit ) {
00112     int expval = ti_reader->test_int() + 1;
00113     TestInterface::SetTestIntMessage *m = new TestInterface::SetTestIntMessage(expval);
00114     unsigned int msgid = ti_reader->msgq_enqueue(m);
00115     printf("Sent with message ID %u\n", msgid);
00116 
00117     if ( ti_writer->msgq_size() > 1 ) {
00118       cout << "Error, more than one message! flushing." << endl;
00119       ti_writer->msgq_flush();
00120     }
00121 
00122     usleep(100000);
00123 
00124     if ( ti_writer->msgq_first() != NULL ) {
00125       if ( ti_writer->msgq_first_is<TestInterface::SetTestStringMessage>() ) {
00126         TestInterface::SetTestStringMessage *msg = ti_writer->msgq_first(msg);
00127         printf("Received message of ID %u, Message improperly detected to be a SetTestStringMessage\n", msg->id());
00128       }
00129       if ( ti_writer->msgq_first_is<TestInterface::SetTestIntMessage>() ) {
00130         TestInterface::SetTestIntMessage *m2 = ti_writer->msgq_first<TestInterface::SetTestIntMessage>();
00131         printf("Received message with ID %u (enqueue time: %s)\n", m2->id(),
00132                m2->time_enqueued()->str());
00133         ti_writer->set_test_int( m2->test_int() );
00134         try {
00135           ti_writer->write();
00136         } catch (InterfaceWriteDeniedException &e) {
00137           cout << "BUG: caught write denied exception" << endl;
00138           e.print_trace();
00139         }
00140         ti_writer->msgq_pop();
00141       } else {
00142         cout << "Illegal message '" << ti_writer->msgq_first()->type() << "' type received" << endl;
00143       }
00144 
00145       usleep(100000);
00146 
00147       //cout << "Reading value from reader interface.. " << flush;
00148       ti_reader->read();
00149       int val = ti_reader->test_int();
00150       if ( val == expval ) {
00151         //cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
00152       } else {
00153         cout << " failure, value is " << ti_reader->test_int() << ", expected "
00154              << expval << endl;
00155       }
00156     } else {
00157       printf("No message in queue, if network test this means the message was dropped\n");
00158     }
00159 
00160     usleep(10);
00161   }
00162 
00163   bb->close(ti_reader);
00164   bb->close(ti_writer);
00165 
00166   delete bb;
00167 
00168   cout << "Tests done" << endl;
00169 
00170   Thread::destroy_main();
00171 }
00172 
00173 
00174 /// @endcond

Generated on Tue Feb 22 13:31:25 2011 for Fawkes API by  doxygen 1.4.7