qa_bb_messaging.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00037 #include <signal.h>
00038 #include <cstdlib>
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 #define BLACKBOARD_MEMSIZE 2 * 1024 * 1024
00058 #define BLACKBOARD_MAGIC_TOKEN "FawkesBlackBoard"
00059
00060 int
00061 main(int argc, char **argv)
00062 {
00063
00064 Thread::init_main();
00065
00066 signal(SIGINT, signal_handler);
00067
00068 BlackBoard *bb = new LocalBlackBoard(BLACKBOARD_MEMSIZE);
00069
00070
00071 TestInterface *ti_writer;
00072 TestInterface *ti_reader;
00073
00074 try {
00075 cout << "Opening interfaces.. " << flush;
00076 ti_writer = bb->open_for_writing<TestInterface>("SomeID");
00077 ti_reader = bb->open_for_reading<TestInterface>("SomeID");
00078 cout << "success" << endl;
00079 } catch (Exception &e) {
00080 cout << "failed! Aborting" << endl;
00081 e.print_trace();
00082 exit(1);
00083 }
00084
00085 cout << "Writing initial value ("
00086 << TestInterface::TEST_CONSTANT << ") into interface as TestInt" << endl;
00087 ti_writer->set_test_int( 5 );
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 printf("Reader instance serial: %u\n", ti_reader->serial());
00106
00107 cout << "Harnessing message queues by excessively sending messages" << endl
00108 << "Press Ctrl-C to stop testing. No output means everything is fine" << endl;
00109 while ( ! quit ) {
00110 int expval = ti_reader->test_int() + 1;
00111 TestInterface::SetTestIntMessage *m = new TestInterface::SetTestIntMessage(expval);
00112 unsigned int msgid = ti_reader->msgq_enqueue(m);
00113 printf("Sent with message ID %u\n", msgid);
00114
00115 if ( ti_writer->msgq_size() > 1 ) {
00116 cout << "Error, more than one message! flushing." << endl;
00117 ti_writer->msgq_flush();
00118 }
00119
00120 usleep(100000);
00121
00122 if ( ti_writer->msgq_first() != NULL ) {
00123 if ( ti_writer->msgq_first_is<TestInterface::SetTestStringMessage>() ) {
00124 TestInterface::SetTestStringMessage *msg = ti_writer->msgq_first(msg);
00125 printf("Received message of ID %u, Message improperly detected to be a SetTestStringMessage\n", msg->id());
00126 }
00127 if ( ti_writer->msgq_first_is<TestInterface::SetTestIntMessage>() ) {
00128 TestInterface::SetTestIntMessage *m2 = ti_writer->msgq_first<TestInterface::SetTestIntMessage>();
00129 printf("Received message with ID %u\n", m2->id());
00130 ti_writer->set_test_int( m2->test_int() );
00131 try {
00132 ti_writer->write();
00133 } catch (InterfaceWriteDeniedException &e) {
00134 cout << "BUG: caught write denied exception" << endl;
00135 e.print_trace();
00136 }
00137 ti_writer->msgq_pop();
00138 } else {
00139 cout << "Illegal message '" << ti_writer->msgq_first()->type() << "' type received" << endl;
00140 }
00141
00142 usleep(100000);
00143
00144
00145 ti_reader->read();
00146 int val = ti_reader->test_int();
00147 if ( val == expval ) {
00148
00149 } else {
00150 cout << " failure, value is " << ti_reader->test_int() << ", expected "
00151 << expval << endl;
00152 }
00153 } else {
00154 printf("No message in queue, if network test this means the message was dropped\n");
00155 }
00156
00157 usleep(10);
00158 }
00159
00160 bb->close(ti_reader);
00161 bb->close(ti_writer);
00162
00163 delete bb;
00164
00165 cout << "Tests done" << endl;
00166
00167 Thread::destroy_main();
00168 }
00169
00170
00171