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 
41 using namespace std;
42 using namespace fawkes;
43 
44 bool quit = false;
45 
46 void
47 signal_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 
56 int
57 main(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();
85  } catch (InterfaceWriteDeniedException &e) {
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
fawkes::Interface::msgq_first_is
bool msgq_first_is()
Check if first message has desired type.
Definition: interface.h:314
fawkes::Interface::msgq_pop
void msgq_pop()
Erase first message from queue.
Definition: interface.cpp:1182
fawkes::Time::str
const char * str(bool utc=false) const
Output function.
Definition: time.cpp:790
fawkes::LocalBlackBoard
Local BlackBoard.
Definition: local.h:45
fawkes::Interface::read
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:472
fawkes::Message::time_enqueued
const Time * time_enqueued() const
Get time when message was enqueued.
Definition: message.cpp:242
fawkes::TestInterface::set_test_int
void set_test_int(const int32_t new_test_int)
Set test_int value.
Definition: TestInterface.cpp:145
fawkes::BlackBoard
The BlackBoard abstract class.
Definition: blackboard.h:46
fawkes::TestInterface::SetTestIntMessage::test_int
int32_t test_int() const
Get test_int value.
Definition: TestInterface.cpp:370
fawkes::TestInterface::SetTestIntMessage
SetTestIntMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:69
fawkes::BlackBoard::close
virtual void close(Interface *interface)=0
Close interface.
fawkes::TestInterface::test_int
int32_t test_int() const
Get test_int value.
Definition: TestInterface.cpp:125
fawkes
Fawkes library namespace.
fawkes::Exception::print_trace
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:601
fawkes::InterfaceWriteDeniedException
This exception is thrown if a write has been attempted on a read-only interface.
Definition: interface.h:55
fawkes::Message::type
const char * type() const
Get message type.
Definition: message.cpp:346
fawkes::Interface::serial
unsigned short serial() const
Get instance serial of interface.
Definition: interface.cpp:686
fawkes::Interface::msgq_size
unsigned int msgq_size()
Get size of message queue.
Definition: interface.cpp:1012
fawkes::Message::id
unsigned int id() const
Get message ID.
Definition: message.cpp:180
fawkes::BlackBoard::open_for_reading
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
fawkes::Interface::msgq_flush
void msgq_flush()
Flush all messages.
Definition: interface.cpp:1046
fawkes::Interface::msgq_enqueue
unsigned int msgq_enqueue(Message *message)
Enqueue message at end of queue.
Definition: interface.cpp:882
fawkes::TestInterface
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:34
fawkes::Interface::msgq_first
Message * msgq_first()
Get the first message from the message queue.
Definition: interface.cpp:1167
fawkes::Interface::write
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:494
fawkes::BlackBoard::open_for_writing
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
fawkes::TestInterface::SetTestStringMessage
SetTestStringMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:95
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36