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/// @cond QA
25
26#include <blackboard/bbconfig.h>
27#include <blackboard/exceptions.h>
28#include <blackboard/local.h>
29#include <blackboard/remote.h>
30#include <core/exceptions/system.h>
31#include <core/threading/thread.h>
32#include <interfaces/TestInterface.h>
33#include <utils/time/time.h>
34
35#include <cstdio>
36#include <cstdlib>
37#include <iostream>
38#include <signal.h>
39#include <vector>
40
41using namespace std;
42using namespace fawkes;
43
44bool quit = false;
45
46void
47signal_handler(int signum)
48{
49 quit = true;
50}
51
52#define NUM_CHUNKS 5
53#define BLACKBOARD_MEMSIZE 2 * 1024 * 1024
54#define BLACKBOARD_MAGIC_TOKEN "FawkesBlackBoard"
55
56int
57main(int argc, char **argv)
58{
59 Thread::init_main();
60
61 signal(SIGINT, signal_handler);
62
63 BlackBoard *bb = new LocalBlackBoard(BLACKBOARD_MEMSIZE);
64 //BlackBoard *bb = new RemoteBlackBoard("localhost", 1910);
65
66 TestInterface *ti_writer;
67 TestInterface *ti_reader;
68
69 try {
70 cout << "Opening interfaces.. " << flush;
71 ti_writer = bb->open_for_writing<TestInterface>("SomeID");
72 ti_reader = bb->open_for_reading<TestInterface>("SomeID");
73 cout << "success" << endl;
74 } catch (Exception &e) {
75 cout << "failed! Aborting" << endl;
76 e.print_trace();
77 exit(1);
78 }
79
80 cout << "Writing initial value (" << TestInterface::TEST_CONSTANT << ") into interface as TestInt"
81 << endl;
82 ti_writer->set_test_int(5);
83 try {
84 ti_writer->write();
86 cout << "BUG: caught write denied exception" << endl;
87 e.print_trace();
88 }
89
90 cout << "Reading value from reader interface.. " << flush;
91 ti_reader->read();
92 int val = ti_reader->test_int();
93 if (val == TestInterface::TEST_CONSTANT) {
94 cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
95 } else {
96 cout << " failure, value is " << ti_reader->test_int() << ", expected "
97 << TestInterface::TEST_CONSTANT << endl;
98 }
99
100 printf("Reader instance serial: %u\n", ti_reader->serial());
101
102 cout << "Harnessing message queues by excessively sending messages" << endl
103 << "Press Ctrl-C to stop testing. No output means everything is fine" << endl;
104 while (!quit) {
105 int expval = ti_reader->test_int() + 1;
107 unsigned int msgid = ti_reader->msgq_enqueue(m);
108 printf("Sent with message ID %u\n", msgid);
109
110 if (ti_writer->msgq_size() > 1) {
111 cout << "Error, more than one message! flushing." << endl;
112 ti_writer->msgq_flush();
113 }
114
115 usleep(100000);
116
117 if (ti_writer->msgq_first() != NULL) {
119 TestInterface::SetTestStringMessage *msg = ti_writer->msgq_first(msg);
120 printf(
121 "Received message of ID %u, Message improperly detected to be a SetTestStringMessage\n",
122 msg->id());
123 }
127 printf("Received message with ID %u (enqueue time: %s)\n",
128 m2->id(),
129 m2->time_enqueued()->str());
130 ti_writer->set_test_int(m2->test_int());
131 try {
132 ti_writer->write();
133 } catch (InterfaceWriteDeniedException &e) {
134 cout << "BUG: caught write denied exception" << endl;
135 e.print_trace();
136 }
137 ti_writer->msgq_pop();
138 } else {
139 cout << "Illegal message '" << ti_writer->msgq_first()->type() << "' type received" << endl;
140 }
141
142 usleep(100000);
143
144 //cout << "Reading value from reader interface.. " << flush;
145 ti_reader->read();
146 int val = ti_reader->test_int();
147 if (val == expval) {
148 //cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
149 } else {
150 cout << " failure, value is " << ti_reader->test_int() << ", expected " << expval << endl;
151 }
152 } else {
153 printf("No message in queue, if network test this means the message was dropped\n");
154 }
155
156 usleep(10);
157 }
158
159 bb->close(ti_reader);
160 bb->close(ti_writer);
161
162 delete bb;
163
164 cout << "Tests done" << endl;
165
166 Thread::destroy_main();
167}
168
169/// @endcond
The BlackBoard abstract class.
Definition: blackboard.h:46
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
virtual void close(Interface *interface)=0
Close interface.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void print_trace() noexcept
Prints trace to stderr.
Definition: exception.cpp:601
This exception is thrown if a write has been attempted on a read-only interface.
Definition: interface.h:56
bool msgq_first_is()
Check if first message has desired type.
Definition: interface.h:351
void msgq_pop()
Erase first message from queue.
Definition: interface.cpp:1215
Message * msgq_first()
Get the first message from the message queue.
Definition: interface.cpp:1200
unsigned int msgq_enqueue(Message *message, bool proxy=false)
Enqueue message at end of queue.
Definition: interface.cpp:915
unsigned int msgq_size()
Get size of message queue.
Definition: interface.cpp:1045
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:501
Uuid serial() const
Get instance serial of interface.
Definition: interface.cpp:695
void msgq_flush()
Flush all messages.
Definition: interface.cpp:1079
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:479
Local BlackBoard.
Definition: local.h:45
const char * type() const
Get message type.
Definition: message.cpp:381
const Time * time_enqueued() const
Get time when message was enqueued.
Definition: message.cpp:262
unsigned int id() const
Get message ID.
Definition: message.cpp:181
SetTestIntMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:69
int32_t test_int() const
Get test_int value.
SetTestStringMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:95
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:34
void set_test_int(const int32_t new_test_int)
Set test_int value.
int32_t test_int() const
Get test_int value.
const char * str(bool utc=false) const
Output function.
Definition: time.cpp:790
Fawkes library namespace.