Fawkes API  Fawkes Development Version
qa_bb_messaging.cpp
1 
2 /***************************************************************************
3  * qa_bb_messaging.h - BlackBoard messaging QA
4  *
5  * Generated: Tue Oct 31 15:36:19 2006
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 
25 /// @cond QA
26 
27 #include <blackboard/local.h>
28 #include <blackboard/remote.h>
29 #include <blackboard/exceptions.h>
30 #include <blackboard/bbconfig.h>
31 
32 #include <interfaces/TestInterface.h>
33 
34 #include <core/threading/thread.h>
35 #include <core/exceptions/system.h>
36 #include <utils/time/time.h>
37 
38 #include <signal.h>
39 #include <cstdlib>
40 #include <cstdio>
41 
42 #include <iostream>
43 #include <vector>
44 
45 using namespace std;
46 using namespace fawkes;
47 
48 
49 bool quit = false;
50 
51 void
52 signal_handler(int signum)
53 {
54  quit = true;
55 }
56 
57 
58 #define NUM_CHUNKS 5
59 #define BLACKBOARD_MEMSIZE 2 * 1024 * 1024
60 #define BLACKBOARD_MAGIC_TOKEN "FawkesBlackBoard"
61 
62 int
63 main(int argc, char **argv)
64 {
65 
66  Thread::init_main();
67 
68  signal(SIGINT, signal_handler);
69 
70  BlackBoard *bb = new LocalBlackBoard(BLACKBOARD_MEMSIZE);
71  //BlackBoard *bb = new RemoteBlackBoard("localhost", 1910);
72 
73  TestInterface *ti_writer;
74  TestInterface *ti_reader;
75 
76  try {
77  cout << "Opening interfaces.. " << flush;
78  ti_writer = bb->open_for_writing<TestInterface>("SomeID");
79  ti_reader = bb->open_for_reading<TestInterface>("SomeID");
80  cout << "success" << endl;
81  } catch (Exception &e) {
82  cout << "failed! Aborting" << endl;
83  e.print_trace();
84  exit(1);
85  }
86 
87  cout << "Writing initial value ("
88  << TestInterface::TEST_CONSTANT << ") into interface as TestInt" << endl;
89  ti_writer->set_test_int( 5 );
90  try {
91  ti_writer->write();
92  } catch (InterfaceWriteDeniedException &e) {
93  cout << "BUG: caught write denied exception" << endl;
94  e.print_trace();
95  }
96 
97  cout << "Reading value from reader interface.. " << flush;
98  ti_reader->read();
99  int val = ti_reader->test_int();
100  if ( val == TestInterface::TEST_CONSTANT ) {
101  cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
102  } else {
103  cout << " failure, value is " << ti_reader->test_int() << ", expected "
104  << TestInterface::TEST_CONSTANT << endl;
105  }
106 
107  printf("Reader instance serial: %u\n", ti_reader->serial());
108 
109  cout << "Harnessing message queues by excessively sending messages" << endl
110  << "Press Ctrl-C to stop testing. No output means everything is fine" << endl;
111  while ( ! quit ) {
112  int expval = ti_reader->test_int() + 1;
114  unsigned int msgid = ti_reader->msgq_enqueue(m);
115  printf("Sent with message ID %u\n", msgid);
116 
117  if ( ti_writer->msgq_size() > 1 ) {
118  cout << "Error, more than one message! flushing." << endl;
119  ti_writer->msgq_flush();
120  }
121 
122  usleep(100000);
123 
124  if ( ti_writer->msgq_first() != NULL ) {
126  TestInterface::SetTestStringMessage *msg = ti_writer->msgq_first(msg);
127  printf("Received message of ID %u, Message improperly detected to be a SetTestStringMessage\n", msg->id());
128  }
129  if ( ti_writer->msgq_first_is<TestInterface::SetTestIntMessage>() ) {
131  printf("Received message with ID %u (enqueue time: %s)\n", m2->id(),
132  m2->time_enqueued()->str());
133  ti_writer->set_test_int( m2->test_int() );
134  try {
135  ti_writer->write();
136  } catch (InterfaceWriteDeniedException &e) {
137  cout << "BUG: caught write denied exception" << endl;
138  e.print_trace();
139  }
140  ti_writer->msgq_pop();
141  } else {
142  cout << "Illegal message '" << ti_writer->msgq_first()->type() << "' type received" << endl;
143  }
144 
145  usleep(100000);
146 
147  //cout << "Reading value from reader interface.. " << flush;
148  ti_reader->read();
149  int val = ti_reader->test_int();
150  if ( val == expval ) {
151  //cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
152  } else {
153  cout << " failure, value is " << ti_reader->test_int() << ", expected "
154  << expval << endl;
155  }
156  } else {
157  printf("No message in queue, if network test this means the message was dropped\n");
158  }
159 
160  usleep(10);
161  }
162 
163  bb->close(ti_reader);
164  bb->close(ti_writer);
165 
166  delete bb;
167 
168  cout << "Tests done" << endl;
169 
170  Thread::destroy_main();
171 }
172 
173 
174 /// @endcond
const Time * time_enqueued() const
Get time when message was enqueued.
Definition: message.cpp:265
unsigned int id() const
Get message ID.
Definition: message.cpp:197
const char * str(bool utc=false) const
Output function.
Definition: time.cpp:872
Fawkes library namespace.
int32_t test_int() const
Get test_int value.
STL namespace.
Local BlackBoard.
Definition: local.h:44
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:500
unsigned int msgq_size()
Get size of message queue.
Definition: interface.cpp:1032
void set_test_int(const int32_t new_test_int)
Set test_int value.
void msgq_pop()
Erase first message from queue.
Definition: interface.cpp:1193
Base class for exceptions in Fawkes.
Definition: exception.h:36
Message * msgq_first()
Get the first message from the message queue.
Definition: interface.cpp:1180
unsigned short serial() const
Get instance serial of interface.
Definition: interface.cpp:697
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:477
SetTestIntMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:68
bool msgq_first_is()
Check if first message has desired type.
Definition: interface.h:314
unsigned int msgq_enqueue(Message *message)
Enqueue message at end of queue.
Definition: interface.cpp:903
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
int32_t test_int() const
Get test_int value.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
void msgq_flush()
Flush all messages.
Definition: interface.cpp:1064
The BlackBoard abstract class.
Definition: blackboard.h:48
SetTestStringMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:94
This exception is thrown if a write has been attempted on a read-only interface.
Definition: interface.h:54
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
const char * type() const
Get message type.
Definition: message.cpp:378
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:33
virtual void close(Interface *interface)=0
Close interface.