Fawkes API  Fawkes Development Version
qa_bb_buffers.cpp
1 
2 /***************************************************************************
3  * qa_bb_buffers.h - BlackBoard interface QA
4  *
5  * Generated: Tue May 24 23:39:22 2011
6  * Copyright 2006-2011 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/exceptions.h>
29 #include <blackboard/bbconfig.h>
30 
31 #include <interfaces/TestInterface.h>
32 
33 #include <core/exceptions/system.h>
34 
35 #include <signal.h>
36 #include <cstdlib>
37 #include <cstdio>
38 
39 #include <iostream>
40 #include <vector>
41 
42 using namespace std;
43 using namespace fawkes;
44 
45 
46 bool quit = false;
47 
48 void
49 signal_handler(int signum)
50 {
51  quit = true;
52 }
53 
54 
55 int
56 main(int argc, char **argv)
57 {
58 
59  signal(SIGINT, signal_handler);
60 
61  LocalBlackBoard *lbb = new LocalBlackBoard(BLACKBOARD_MEMSIZE);
62 
63  BlackBoard *bb = lbb;
64 
65  TestInterface *ti_writer;
66  TestInterface *ti_reader;
67 
68  try {
69  cout << "Opening interfaces.. " << flush;
70  ti_writer = bb->open_for_writing<TestInterface>("SomeID");
71  ti_reader = bb->open_for_reading<TestInterface>("SomeID");
72  cout << "success, " <<
73  "writer hash=" << ti_writer->hash_printable() <<
74  " reader hash=" << ti_reader->hash_printable() << endl;
75  } catch (Exception &e) {
76  cout << "failed! Aborting" << endl;
77  e.print_trace();
78  exit(1);
79  }
80 
81  cout << endl << endl
82  << "Running data tests =================================================="
83  << endl;
84 
85  cout << "Writing initial value ("
86  << TestInterface::TEST_CONSTANT << ") into interface as TestInt" << endl;
87  ti_writer->set_test_int( TestInterface::TEST_CONSTANT );
88  try {
89  ti_writer->write();
90  } catch (InterfaceWriteDeniedException &e) {
91  cout << "BUG: caught write denied exception" << endl;
92  e.print_trace();
93  }
94 
95  cout << "Reading value from reader interface.. " << flush;
96  ti_reader->read();
97  int val = ti_reader->test_int();
98  if ( val == TestInterface::TEST_CONSTANT ) {
99  cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
100  } else {
101  cout << " failure, value is " << ti_reader->test_int() << ", expected "
102  << TestInterface::TEST_CONSTANT << endl;
103  }
104 
105  cout << "Resizing buffer.. " << flush;
106  try {
107  ti_reader->resize_buffers(1);
108  ti_reader->copy_private_to_buffer(0);
109  } catch (Exception &e) {
110  cout << "ERROR: Resizing failed, exception follows" << endl;
111  e.print_trace();
112  throw;
113  }
114 
115 
116  cout << "Testing buffers, use Ctrl-C to interrupt" << endl
117  << "If you do not see any output everything is fine" << endl;
118  while ( ! quit ) {
119 
120  //cout << "Writing value " << expval
121  // << " into interface as TestInt" << endl;
122  ti_writer->set_test_int( ti_writer->test_int() + 1 );
123  try {
124  ti_writer->write();
125  } catch (InterfaceWriteDeniedException &e) {
126  cout << "BUG: caught write denied exception" << endl;
127  e.print_trace();
128  }
129 
130  //cout << "Reading value from reader interface.. " << flush;
131  ti_reader->read();
132  int rval = ti_reader->test_int();
133  int wval = ti_writer->test_int();
134 
135  ti_reader->read_from_buffer(0);
136  int bval = ti_reader->test_int();
137 
138  if ( rval != wval ) {
139  cout << " failure, reader value is " << rval << ", writer has "
140  << wval << endl;
141  }
142 
143  if ( rval != bval + 1 ) {
144  cout << " failure, reader value is " << rval << ", buffer has "
145  << bval << endl;
146  }
147 
148  // could to copy_shared as well, but that is a little less predictable in
149  // the case of concurrent writers, hence we want people to copy and paste
150  // this version.
151  ti_reader->read();
152  ti_reader->copy_private_to_buffer(0);
153 
154  usleep(10);
155  }
156 
157  cout << "Tests done" << endl;
158 
159  bb->close(ti_reader);
160  bb->close(ti_writer);
161 
162  delete bb;
163 }
164 
165 
166 /// @endcond
void copy_private_to_buffer(unsigned int buffer)
Copy data from private memory to buffer.
Definition: interface.cpp:1307
Fawkes library namespace.
STL namespace.
Local BlackBoard.
Definition: local.h:44
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:500
void set_test_int(const int32_t new_test_int)
Set test_int value.
void read_from_buffer(unsigned int buffer)
Copy data from buffer to private memory.
Definition: interface.cpp:1327
Base class for exceptions in Fawkes.
Definition: exception.h:36
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:477
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
void resize_buffers(unsigned int num_buffers)
Resize buffer array.
Definition: interface.cpp:1241
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.
The BlackBoard abstract class.
Definition: blackboard.h:48
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.
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:33
const char * hash_printable() const
Get printable interface hash.
Definition: interface.cpp:304
virtual void close(Interface *interface)=0
Close interface.