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 <plugins/examples/basics/blackboard_thread.h>
24 #include <interfaces/TestInterface.h>
25 
26 using namespace fawkes;
27 
28 
29 /** @class ExampleBlackBoardThread <plugins/examples/basics/blackboard_thread.h>
30  * Simple demonstration for a thread using the BlackBoard.
31  *
32  * @author Tim Niemueller
33  */
34 
35 
36 /** Constructor.
37  * @param reader set to true, to make this bb thread to open the test interface
38  * read-only, false to open it as a writer
39  */
41  : Thread("ExampleBlackBoardThread", Thread::OPMODE_WAITFORWAKEUP),
42  BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_THINK)
43 {
44  this->reader = reader;
45 }
46 
47 
48 /** Destructor. */
50 {
51 }
52 
53 
54 void
56 {
57  logger->log_debug(name(), "Closing test interface");
58  try {
59  blackboard->close(test_interface);
60  } catch (Exception &e) {
61  logger->log_error(name(), "Could not close kicker interface");
62  logger->log_error(name(), e);
63  }
64 }
65 
66 
67 /** Initialize thread.
68  * Here, the device and the BB-interface are opened.
69  */
70 void
72 {
73  logger->log_debug(name(), "Opening test interface");
74  try {
75  if ( reader ) {
76  test_interface = blackboard->open_for_reading<TestInterface>("Test");
77  } else {
78  test_interface = blackboard->open_for_writing<TestInterface>("Test");
79  }
80  } catch (Exception& e) {
81  e.append("Opening test interface for writing failed");
82  throw;
83  }
84 }
85 
86 /** Thread loop.
87  * Parse messages from the interface and update values in the interface.
88  */
89 void
91 {
92  // nothin'
93 }
virtual void init()
Initialize thread.
Fawkes library namespace.
virtual void loop()
Thread loop.
Thread class encapsulation of pthreads.
Definition: thread.h:42
virtual void finalize()
Finalize the thread.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
Thread aspect to use blocked timing.
Base class for exceptions in Fawkes.
Definition: exception.h:36
ExampleBlackBoardThread(bool reader)
Constructor.
const char * name() const
Get name of thread.
Definition: thread.h:95
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
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.
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:341
virtual ~ExampleBlackBoardThread()
Destructor.
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:33
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
virtual void close(Interface *interface)=0
Close interface.