Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 
25 /// @cond QA
26 
27 #include <blackboard/internal/memory_manager.h>
28 #include <blackboard/local.h>
29 #include <blackboard/exceptions.h>
30 #include <blackboard/bbconfig.h>
31 
32 #include <interfaces/TestInterface.h>
33 
34 #include <core/exceptions/system.h>
35 
36 #include <signal.h>
37 #include <cstdlib>
38 #include <cstdio>
39 
40 #include <iostream>
41 #include <vector>
42 
43 using namespace std;
44 using namespace fawkes;
45 
46 
47 bool quit = false;
48 
49 void
50 signal_handler(int signum)
51 {
52  quit = true;
53 }
54 
55 
56 #define NUM_CHUNKS 5
57 
58 int
59 main(int argc, char **argv)
60 {
61 
62  signal(SIGINT, signal_handler);
63 
64  LocalBlackBoard *lbb = new LocalBlackBoard(BLACKBOARD_MEMSIZE);
65 
66  BlackBoard *bb = lbb;
67  const BlackBoardMemoryManager *mm = lbb->memory_manager();
68 
69  TestInterface *ti_writer;
70  TestInterface *ti_reader;
71 
72  try {
73  cout << "Opening interfaces.. " << flush;
74  ti_writer = bb->open_for_writing<TestInterface>("SomeID");
75  ti_reader = bb->open_for_reading<TestInterface>("SomeID");
76  cout << "success, " <<
77  "writer hash=" << ti_writer->hash_printable() <<
78  " reader hash=" << ti_reader->hash_printable() << endl;
79  } catch (Exception &e) {
80  cout << "failed! Aborting" << endl;
81  e.print_trace();
82  exit(1);
83  }
84 
85  try {
86  cout << "Trying to open second writer.. " << flush;
87  TestInterface *ti_writer_two;
88  ti_writer_two = bb->open_for_writing<TestInterface>("SomeID");
89  bb->close(ti_writer_two);
90  cout << "BUG: Detection of second writer did NOT work!" << endl;
91  exit(2);
92  } catch (BlackBoardWriterActiveException &e) {
93  cout << "exception caught as expected, detected and prevented second writer!" << endl;
94  }
95 
96  cout << "Printing some meminfo ===============================================" << endl;
97  cout << "Free chunks:" << endl;
99  cout << "Allocated chunks:" << endl;
102  cout << "End of meminfo ======================================================" << endl;
103 
104  try {
105  cout << "Trying to open third writer.. " << flush;
106  TestInterface *ti_writer_three;
107  ti_writer_three = bb->open_for_writing<TestInterface>("AnotherID");
108  cout << "No exception as expected, different ID ok!" << endl;
109  bb->close(ti_writer_three);
110  } catch (BlackBoardWriterActiveException &e) {
111  cout << "BUG: Third writer with different ID detected as another writer!" << endl;
112  exit(3);
113  }
114 
115  cout << endl << endl
116  << "Running data tests ==================================================" << endl;
117 
118  cout << "Writing initial value ("
119  << TestInterface::TEST_CONSTANT << ") into interface as TestInt" << endl;
120  ti_writer->set_test_int( TestInterface::TEST_CONSTANT );
121  try {
122  ti_writer->write();
123  } catch (InterfaceWriteDeniedException &e) {
124  cout << "BUG: caught write denied exception" << endl;
125  e.print_trace();
126  }
127 
128  cout << "Reading value from reader interface.. " << flush;
129  ti_reader->read();
130  int val = ti_reader->test_int();
131  if ( val == TestInterface::TEST_CONSTANT ) {
132  cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
133  } else {
134  cout << " failure, value is " << ti_reader->test_int() << ", expected "
135  << TestInterface::TEST_CONSTANT << endl;
136  }
137 
138 
139  cout << "Iterating over reader interface.." << endl;
141  for ( fi = ti_reader->fields(); fi != ti_reader->fields_end(); ++fi) {
142  printf("Name: %20s Type: %10s Value: %s\n", fi.get_name(), fi.get_typename(), fi.get_value_string());
143  }
144  cout << "done" << endl;
145 
146  cout << "Harnessing interface by excessive reading and writing, use Ctrl-C to interrupt" << endl
147  << "If you do not see any output everything is fine" << endl;
148  while ( ! quit ) {
149  int expval = ti_reader->test_int() + 1;
150  //cout << "Writing value " << expval
151  // << " into interface as TestInt" << endl;
152  ti_writer->set_test_int( expval );
153  try {
154  ti_writer->write();
155  } catch (InterfaceWriteDeniedException &e) {
156  cout << "BUG: caught write denied exception" << endl;
157  e.print_trace();
158  }
159 
160  //cout << "Reading value from reader interface.. " << flush;
161  ti_reader->read();
162  int val = ti_reader->test_int();
163  if ( val == expval ) {
164  //cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
165  } else {
166  cout << " failure, value is " << ti_reader->test_int() << ", expected "
167  << expval << endl;
168  }
169 
170  usleep(10);
171  }
172 
173  cout << "Tests done" << endl;
174 
175  bb->close(ti_reader);
176  bb->close(ti_writer);
177 
178  delete bb;
179 }
180 
181 
182 /// @endcond