Fawkes API Fawkes Development Version
blackboard_thread.cpp
1
2/***************************************************************************
3 * blackboard_thread.cpp - Fawkes Example Plugin BlackBoard Thread
4 *
5 * Created: Wed Jun 20 16:37:40 2007
6 * Copyright 2007-2008 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.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * Read the full text in the LICENSE.GPL file in the doc directory.
21 */
22
23#include <interfaces/TestInterface.h>
24#include <plugins/examples/basics/blackboard_thread.h>
25
26using namespace fawkes;
27
28/** @class ExampleBlackBoardThread <plugins/examples/basics/blackboard_thread.h>
29 * Simple demonstration for a thread using the BlackBoard.
30 *
31 * @author Tim Niemueller
32 */
33
34/** Constructor.
35 * @param reader set to true, to make this bb thread to open the test interface
36 * read-only, false to open it as a writer
37 */
39: Thread("ExampleBlackBoardThread", Thread::OPMODE_WAITFORWAKEUP),
41{
42 this->reader = reader;
43}
44
45/** Destructor. */
47{
48}
49
50void
52{
53 logger->log_debug(name(), "Closing test interface");
54 try {
55 blackboard->close(test_interface);
56 } catch (Exception &e) {
57 logger->log_error(name(), "Could not close kicker interface");
58 logger->log_error(name(), e);
59 }
60}
61
62/** Initialize thread.
63 * Here, the device and the BB-interface are opened.
64 */
65void
67{
68 logger->log_debug(name(), "Opening test interface");
69 try {
70 if (reader) {
71 test_interface = blackboard->open_for_reading<TestInterface>("Test");
72 } else {
73 test_interface = blackboard->open_for_writing<TestInterface>("Test");
74 }
75 } catch (Exception &e) {
76 e.append("Opening test interface for writing failed");
77 throw;
78 }
79}
80
81/** Thread loop.
82 * Parse messages from the interface and update values in the interface.
83 */
84void
86{
87 // nothin'
88}
virtual void finalize()
Finalize the thread.
virtual void loop()
Thread loop.
ExampleBlackBoardThread(bool reader)
Constructor.
virtual ~ExampleBlackBoardThread()
Destructor.
virtual void init()
Initialize thread.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
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.
Thread aspect to use blocked timing.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void append(const char *format,...) noexcept
Append messages to the message list.
Definition: exception.cpp:333
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:34
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
Fawkes library namespace.