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