Fawkes API Fawkes Development Version
local.cpp
1
2/***************************************************************************
3 * local.cpp - Local BlackBoard
4 *
5 * Created: Sat Sep 16 17:11:13 2006 (on train to Cologne)
6 * Copyright 2006-2015 Tim Niemueller [www.niemueller.de]
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version. A runtime exception applies to
13 * this software (see LICENSE.GPL_WRE file mentioned below for details).
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_WRE file in the doc directory.
21 */
22
23#include <blackboard/bbconfig.h>
24#include <blackboard/internal/interface_manager.h>
25#include <blackboard/internal/memory_manager.h>
26#include <blackboard/internal/message_manager.h>
27#include <blackboard/internal/notifier.h>
28#include <blackboard/local.h>
29#include <blackboard/net/handler.h>
30
31// for -C: bb_cleanup
32#include <blackboard/shmem/header.h>
33#include <blackboard/shmem/lister.h>
34#include <utils/ipc/shm.h>
35
36#include <cstring>
37#include <string>
38
39namespace fawkes {
40
41/** @class LocalBlackBoard <blackboard/local.h>
42 * Local BlackBoard.
43 *
44 * @see Interface
45 * @see Message
46 *
47 * @author Tim Niemueller
48 */
49
50/** Shared Memory Constructor.
51 * @param memsize size of memory in bytes
52 * @param magic_token magic token used for shared memory segment
53 * @param master true to operate in master mode, false otherwise
54 */
55LocalBlackBoard::LocalBlackBoard(size_t memsize, const char *magic_token, bool master)
56{
57 memmgr_ = new BlackBoardMemoryManager(memsize, BLACKBOARD_VERSION, master);
58
60 im_ = new BlackBoardInterfaceManager(memmgr_, msgmgr_, notifier_);
61
62 msgmgr_->set_interface_manager(im_);
63
64 nethandler_ = NULL;
65}
66
67/** Heap Memory Constructor.
68 * @param memsize size of memory in bytes
69 */
71{
72 memmgr_ = new BlackBoardMemoryManager(memsize);
73
75 im_ = new BlackBoardInterfaceManager(memmgr_, msgmgr_, notifier_);
76
77 msgmgr_->set_interface_manager(im_);
78
79 nethandler_ = NULL;
80}
81
82/** Destructor. */
84{
85 if (nethandler_) {
86 nethandler_->cancel();
87 nethandler_->join();
88 delete nethandler_;
89 }
90 delete im_;
91 delete msgmgr_;
92 delete memmgr_;
93}
94
96LocalBlackBoard::open_for_reading(const char *type, const char *identifier, const char *owner)
97{
98 try {
99 return im_->open_for_reading(type, identifier, owner);
100 } catch (Exception &e) {
101 throw;
102 }
103}
104
105Interface *
106LocalBlackBoard::open_for_writing(const char *type, const char *identifier, const char *owner)
107{
108 try {
109 return im_->open_for_writing(type, identifier, owner);
110 } catch (Exception &e) {
111 throw;
112 }
113}
114
115std::list<Interface *>
117 const char *id_pattern,
118 const char *owner)
119{
120 try {
121 return im_->open_multiple_for_reading(type_pattern, id_pattern, owner);
122 } catch (Exception &e) {
123 throw;
124 }
125}
126
127void
129{
130 im_->close(interface);
131}
132
135{
136 return im_->list_all();
137}
138
140LocalBlackBoard::list(const char *type_pattern, const char *id_pattern)
141{
142 return im_->list(type_pattern, id_pattern);
143}
144
145bool
147{
148 return true;
149}
150
151bool
153{
154 return true;
155}
156
157/** Cleanup orphaned BlackBoard segments.
158 * This erase orphaned shared memory segments that belonged to a
159 * BlackBoard.
160 * @param magic_token magic token of shared memory segments
161 * @param use_lister true to use a lister with console output
162 */
163void
164LocalBlackBoard::cleanup(const char *magic_token, bool use_lister)
165{
166 BlackBoardSharedMemoryHeader *bbsh = new BlackBoardSharedMemoryHeader(BLACKBOARD_VERSION);
167 BlackBoardSharedMemoryLister *bblister = NULL;
168 if (use_lister) {
169 bblister = new BlackBoardSharedMemoryLister();
170 }
171 SharedMemory::erase_orphaned(magic_token, bbsh, bblister);
172 delete bblister;
173 delete bbsh;
174}
175
176/** Get memory manager.
177 * CAUTION: This is NOT meant to be used in your application.
178 * This returns a pointer to the used memory manager. The return type
179 * is declared const. Use this only for debugging purposes to output info about
180 * the BlackBoard memory.
181 * @return const pointer to memory manager
182 */
185{
186 return memmgr_;
187}
188
189/** Start network handler.
190 * This will start the network handler thread and register it with the given hub.
191 * @param hub hub to use and to register with
192 */
193void
195{
196 if (nethandler_) {
197 throw Exception("BlackBoardNetworkHandler already started");
198 }
199 nethandler_ = new BlackBoardNetworkHandler(this, hub);
200 nethandler_->start();
201}
202
203} // end namespace fawkes
BlackBoard interface manager.
InterfaceInfoList * list_all() const
Get a list of interfaces.
std::list< Interface * > open_multiple_for_reading(const char *type_pattern, const char *id_pattern="*", const char *owner=NULL)
Open all interfaces of the given type for reading.
InterfaceInfoList * list(const char *type_pattern, const char *id_pattern) const
Get a constrained list of interfaces.
Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)
Open interface for writing.
void close(Interface *interface)
Close interface.
Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)
Open interface for reading.
BlackBoard memory manager.
BlackBoard message manager.
BlackBoard Network Handler.
Definition: handler.h:44
BlackBoard Shared Memory Header.
Definition: header.h:35
BlackBoard shared memory lister.
Definition: lister.h:34
BlackBoardNotifier * notifier_
Notifier for BB events.
Definition: blackboard.h:111
Base class for exceptions in Fawkes.
Definition: exception.h:36
Fawkes Network Hub.
Definition: hub.h:34
Interface information list.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
static void cleanup(const char *magic_token, bool use_lister=false)
Cleanup orphaned BlackBoard segments.
Definition: local.cpp:164
LocalBlackBoard(size_t memsize)
Heap Memory Constructor.
Definition: local.cpp:70
virtual InterfaceInfoList * list(const char *type_pattern, const char *id_pattern)
Get list of interfaces matching type and ID patterns.
Definition: local.cpp:140
virtual std::list< Interface * > open_multiple_for_reading(const char *type_pattern, const char *id_pattern="*", const char *owner=NULL)
Open multiple interfaces for reading.
Definition: local.cpp:116
virtual bool is_alive() const noexcept
Check if the BlackBoard is still alive.
Definition: local.cpp:146
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)
Open interface for reading.
Definition: local.cpp:96
virtual void close(Interface *interface)
Close interface.
Definition: local.cpp:128
const BlackBoardMemoryManager * memory_manager() const
Get memory manager.
Definition: local.cpp:184
virtual bool try_aliveness_restore() noexcept
Try to restore the aliveness of the BlackBoard instance.
Definition: local.cpp:152
virtual InterfaceInfoList * list_all()
Get list of all currently existing interfaces.
Definition: local.cpp:134
virtual ~LocalBlackBoard()
Destructor.
Definition: local.cpp:83
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)
Open interface for writing.
Definition: local.cpp:106
virtual void start_nethandler(FawkesNetworkHub *hub)
Start network handler.
Definition: local.cpp:194
static void erase_orphaned(const char *magic_token, SharedMemoryHeader *header, SharedMemoryLister *lister=0, const char *registry_name=0)
Erase orphaned (attach count = 0) shared memory segments of a given type.
Definition: shm.cpp:1199
void start(bool wait=true)
Call this method to start the thread.
Definition: thread.cpp:499
void join()
Join the thread.
Definition: thread.cpp:597
void cancel()
Cancel a thread.
Definition: thread.cpp:646
Fawkes library namespace.