Fawkes API  Fawkes Development Version
qa_bb_interface.cpp
1 
2 /***************************************************************************
3  * qa_bb_interface.h - BlackBoard interface QA
4  *
5  * Generated: Tue Oct 17 15:48:45 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/internal/memory_manager.h>
29 #include <blackboard/local.h>
30 #include <core/exceptions/system.h>
31 #include <interfaces/TestInterface.h>
32 
33 #include <cstdio>
34 #include <cstdlib>
35 #include <iostream>
36 #include <signal.h>
37 #include <vector>
38 
39 using namespace std;
40 using namespace fawkes;
41 
42 bool quit = false;
43 
44 void
45 signal_handler(int signum)
46 {
47  quit = true;
48 }
49 
50 #define NUM_CHUNKS 5
51 
52 int
53 main(int argc, char **argv)
54 {
55  signal(SIGINT, signal_handler);
56 
57  LocalBlackBoard *lbb = new LocalBlackBoard(BLACKBOARD_MEMSIZE);
58 
59  BlackBoard * bb = lbb;
60  const BlackBoardMemoryManager *mm = lbb->memory_manager();
61 
62  TestInterface *ti_writer;
63  TestInterface *ti_reader;
64 
65  try {
66  cout << "Opening interfaces.. " << flush;
67  ti_writer = bb->open_for_writing<TestInterface>("SomeID");
68  ti_reader = bb->open_for_reading<TestInterface>("SomeID");
69  cout << "success, "
70  << "writer hash=" << ti_writer->hash_printable()
71  << " reader hash=" << ti_reader->hash_printable() << endl;
72  } catch (Exception &e) {
73  cout << "failed! Aborting" << endl;
74  e.print_trace();
75  exit(1);
76  }
77 
78  try {
79  cout << "Trying to open second writer.. " << flush;
80  TestInterface *ti_writer_two;
81  ti_writer_two = bb->open_for_writing<TestInterface>("SomeID");
82  bb->close(ti_writer_two);
83  cout << "BUG: Detection of second writer did NOT work!" << endl;
84  exit(2);
85  } catch (BlackBoardWriterActiveException &e) {
86  cout << "exception caught as expected, detected and prevented second writer!" << endl;
87  }
88 
89  cout << "Printing some meminfo ===============================================" << endl;
90  cout << "Free chunks:" << endl;
92  cout << "Allocated chunks:" << endl;
95  cout << "End of meminfo ======================================================" << endl;
96 
97  try {
98  cout << "Trying to open third writer.. " << flush;
99  TestInterface *ti_writer_three;
100  ti_writer_three = bb->open_for_writing<TestInterface>("AnotherID");
101  cout << "No exception as expected, different ID ok!" << endl;
102  bb->close(ti_writer_three);
103  } catch (BlackBoardWriterActiveException &e) {
104  cout << "BUG: Third writer with different ID detected as another writer!" << endl;
105  exit(3);
106  }
107 
108  cout << endl
109  << endl
110  << "Running data tests ==================================================" << endl;
111 
112  cout << "Writing initial value (" << TestInterface::TEST_CONSTANT << ") into interface as TestInt"
113  << endl;
114  ti_writer->set_test_int(TestInterface::TEST_CONSTANT);
115  try {
116  ti_writer->write();
117  } catch (InterfaceWriteDeniedException &e) {
118  cout << "BUG: caught write denied exception" << endl;
119  e.print_trace();
120  }
121 
122  cout << "Reading value from reader interface.. " << flush;
123  ti_reader->read();
124  int val = ti_reader->test_int();
125  if (val == TestInterface::TEST_CONSTANT) {
126  cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
127  } else {
128  cout << " failure, value is " << ti_reader->test_int() << ", expected "
129  << TestInterface::TEST_CONSTANT << endl;
130  }
131 
132  cout << "Iterating over reader interface.." << endl;
134  for (fi = ti_reader->fields(); fi != ti_reader->fields_end(); ++fi) {
135  printf("Name: %20s Type: %10s Value: %s\n",
136  fi.get_name(),
137  fi.get_typename(),
138  fi.get_value_string());
139  }
140  cout << "done" << endl;
141 
142  cout << "Harnessing interface by excessive reading and writing, use Ctrl-C to interrupt" << endl
143  << "If you do not see any output everything is fine" << endl;
144  while (!quit) {
145  int expval = ti_reader->test_int() + 1;
146  //cout << "Writing value " << expval
147  // << " into interface as TestInt" << endl;
148  ti_writer->set_test_int(expval);
149  try {
150  ti_writer->write();
151  } catch (InterfaceWriteDeniedException &e) {
152  cout << "BUG: caught write denied exception" << endl;
153  e.print_trace();
154  }
155 
156  //cout << "Reading value from reader interface.. " << flush;
157  ti_reader->read();
158  int val = ti_reader->test_int();
159  if (val == expval) {
160  //cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
161  } else {
162  cout << " failure, value is " << ti_reader->test_int() << ", expected " << expval << endl;
163  }
164 
165  usleep(10);
166  }
167 
168  cout << "Tests done" << endl;
169 
170  bb->close(ti_reader);
171  bb->close(ti_writer);
172 
173  delete bb;
174 }
175 
176 /// @endcond
BlackBoard memory manager.
void print_free_chunks_info() const
Print out info about free chunks.
void print_allocated_chunks_info() const
Print out info about allocated chunks.
void print_performance_info() const
Prints out performance info.
Thrown if a writer is already active on an interface that writing has been requested for.
Definition: exceptions.h:125
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()
Prints trace to stderr.
Definition: exception.cpp:601
Interface field iterator.
const char * get_name() const
Get name of current field.
const char * get_value_string(const char *array_sep=", ")
Get value of current field as string.
const char * get_typename() const
Get type of current field as string.
This exception is thrown if a write has been attempted on a read-only interface.
Definition: interface.h:55
const char * hash_printable() const
Get printable interface hash.
Definition: interface.cpp:307
InterfaceFieldIterator fields_end()
Invalid iterator.
Definition: interface.cpp:1207
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:494
InterfaceFieldIterator fields()
Get iterator over all fields of this interface instance.
Definition: interface.cpp:1198
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:472
Local BlackBoard.
Definition: local.h:45
const BlackBoardMemoryManager * memory_manager() const
Get memory manager.
Definition: local.cpp:184
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.
Fawkes library namespace.