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/local.h>
24 #include <blackboard/bbconfig.h>
25 #include <blackboard/internal/message_manager.h>
26 #include <blackboard/internal/memory_manager.h>
27 #include <blackboard/internal/interface_manager.h>
28 #include <blackboard/internal/notifier.h>
29 #include <blackboard/net/handler.h>
30 
31 // for -C: bb_cleanup
32 #include <utils/ipc/shm.h>
33 #include <blackboard/shmem/header.h>
34 #include <blackboard/shmem/lister.h>
35 
36 #include <string>
37 #include <cstring>
38 
39 namespace 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 
51 /** Shared Memory Constructor.
52  * @param memsize size of memory in bytes
53  * @param magic_token magic token used for shared memory segment
54  * @param master true to operate in master mode, false otherwise
55  */
57  const char *magic_token, bool master)
58 {
59  __memmgr = new BlackBoardMemoryManager(memsize, BLACKBOARD_VERSION, master);
60 
61  __msgmgr = new BlackBoardMessageManager(__notifier);
62  __im = new BlackBoardInterfaceManager(__memmgr, __msgmgr, __notifier);
63 
64  __msgmgr->set_interface_manager(__im);
65 
66  __nethandler = NULL;
67 }
68 
69 
70 /** Heap Memory Constructor.
71  * @param memsize size of memory in bytes
72  */
74 {
75  __memmgr = new BlackBoardMemoryManager(memsize);
76 
77  __msgmgr = new BlackBoardMessageManager(__notifier);
78  __im = new BlackBoardInterfaceManager(__memmgr, __msgmgr, __notifier);
79 
80  __msgmgr->set_interface_manager(__im);
81 
82  __nethandler = NULL;
83 }
84 
85 
86 /** Destructor. */
88 {
89  if ( __nethandler ) {
90  __nethandler->cancel();
91  __nethandler->join();
92  delete __nethandler;
93  }
94  delete __im;
95  delete __msgmgr;
96  delete __memmgr;
97 }
98 
99 
100 Interface *
101 LocalBlackBoard::open_for_reading(const char *type, const char *identifier, const char *owner)
102 {
103  try {
104  return __im->open_for_reading(type, identifier, owner);
105  } catch (Exception &e) {
106  throw;
107  }
108 }
109 
110 
111 Interface *
112 LocalBlackBoard::open_for_writing(const char *type, const char *identifier, const char *owner)
113 {
114  try {
115  return __im->open_for_writing(type, identifier, owner);
116  } catch (Exception &e) {
117  throw;
118  }
119 }
120 
121 
122 std::list<Interface *>
124  const char *id_pattern,
125  const char *owner)
126 {
127  try {
128  return __im->open_multiple_for_reading(type_pattern, id_pattern, owner);
129  } catch (Exception &e) {
130  throw;
131  }
132 }
133 
134 
135 void
137 {
138  __im->close(interface);
139 }
140 
141 
144 {
145  return __im->list_all();
146 }
147 
148 
150 LocalBlackBoard::list(const char *type_pattern, const char *id_pattern)
151 {
152  return __im->list(type_pattern, id_pattern);
153 }
154 
155 
156 bool
158 {
159  return true;
160 }
161 
162 
163 bool
165 {
166  return true;
167 }
168 
169 
170 /** Cleanup orphaned BlackBoard segments.
171  * This erase orphaned shared memory segments that belonged to a
172  * BlackBoard.
173  * @param magic_token magic token of shared memory segments
174  * @param use_lister true to use a lister with console output
175  */
176 void
177 LocalBlackBoard::cleanup(const char *magic_token, bool use_lister)
178 {
179  BlackBoardSharedMemoryHeader *bbsh = new BlackBoardSharedMemoryHeader( BLACKBOARD_VERSION );
180  BlackBoardSharedMemoryLister *bblister = NULL;
181  if ( use_lister ) {
182  bblister = new BlackBoardSharedMemoryLister();
183  }
184  SharedMemory::erase_orphaned(magic_token, bbsh, bblister);
185  delete bblister;
186  delete bbsh;
187 }
188 
189 
190 /** Get memory manager.
191  * CAUTION: This is NOT meant to be used in your application.
192  * This returns a pointer to the used memory manager. The return type
193  * is declared const. Use this only for debugging purposes to output info about
194  * the BlackBoard memory.
195  * @return const pointer to memory manager
196  */
199 {
200  return __memmgr;
201 }
202 
203 
204 /** Start network handler.
205  * This will start the network handler thread and register it with the given hub.
206  * @param hub hub to use and to register with
207  */
208 void
210 {
211  if ( __nethandler ) {
212  throw Exception("BlackBoardNetworkHandler already started");
213  }
214  __nethandler = new BlackBoardNetworkHandler(this, hub);
215  __nethandler->start();
216 }
217 
218 } // end namespace fawkes
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:1163
LocalBlackBoard(size_t memsize)
Heap Memory Constructor.
Definition: local.cpp:73
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)
Open interface for writing.
Definition: local.cpp:112
void close(Interface *interface)
Close interface.
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:123
static void cleanup(const char *magic_token, bool use_lister=false)
Cleanup orphaned BlackBoard segments.
Definition: local.cpp:177
virtual InterfaceInfoList * list_all()
Get list of all currently existing interfaces.
Definition: local.cpp:143
Fawkes library namespace.
BlackBoard memory manager.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)
Open interface for reading.
Definition: local.cpp:101
BlackBoard shared memory lister.
Definition: lister.h:33
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.
Fawkes Network Hub.
Definition: hub.h:33
virtual void start_nethandler(FawkesNetworkHub *hub)
Start network handler.
Definition: local.cpp:209
Interface information list.
virtual InterfaceInfoList * list(const char *type_pattern, const char *id_pattern)
Get list of interfaces matching type and ID patterns.
Definition: local.cpp:150
Base class for exceptions in Fawkes.
Definition: exception.h:36
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.
virtual void close(Interface *interface)
Close interface.
Definition: local.cpp:136
virtual bool is_alive() const
Check if the BlackBoard is still alive.
Definition: local.cpp:157
BlackBoardNotifier * __notifier
Notifier for BB events.
Definition: blackboard.h:122
void cancel()
Cancel a thread.
Definition: thread.cpp:651
BlackBoard Shared Memory Header.
Definition: header.h:34
BlackBoard message manager.
const BlackBoardMemoryManager * memory_manager() const
Get memory manager.
Definition: local.cpp:198
Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)
Open interface for reading.
void join()
Join the thread.
Definition: thread.cpp:610
InterfaceInfoList * list_all() const
Get a list of interfaces.
BlackBoard Network Handler.
Definition: handler.h:45
virtual ~LocalBlackBoard()
Destructor.
Definition: local.cpp:87
BlackBoard interface manager.
virtual bool try_aliveness_restore()
Try to restore the aliveness of the BlackBoard instance.
Definition: local.cpp:164
void start(bool wait=true)
Call this method to start the thread.
Definition: thread.cpp:511