Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
local.cpp
1 
2 /***************************************************************************
3  * blackboard.cpp - BlackBoard plugin
4  *
5  * Generated: Sat Sep 16 17:11:13 2006 (on train to Cologne)
6  * Copyright 2006-2007 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 #include <blackboard/local.h>
25 #include <blackboard/bbconfig.h>
26 #include <blackboard/internal/message_manager.h>
27 #include <blackboard/internal/memory_manager.h>
28 #include <blackboard/internal/interface_manager.h>
29 #include <blackboard/internal/notifier.h>
30 #include <blackboard/net/handler.h>
31 
32 // for -C: bb_cleanup
33 #include <utils/ipc/shm.h>
34 #include <blackboard/shmem/header.h>
35 #include <blackboard/shmem/lister.h>
36 
37 #include <string>
38 #include <cstring>
39 
40 namespace fawkes {
41 
42 /** @class LocalBlackBoard <blackboard/local.h>
43  * Local BlackBoard.
44  *
45  * @see Interface
46  * @see Message
47  *
48  * @author Tim Niemueller
49  */
50 
51 
52 /** Shared Memory Constructor.
53  * @param memsize size of memory in bytes
54  * @param magic_token magic token used for shared memory segment
55  * @param master true to operate in master mode, false otherwise
56  */
58  const char *magic_token, bool master)
59 {
60  __memmgr = new BlackBoardMemoryManager(memsize, BLACKBOARD_VERSION, master);
61 
62  __msgmgr = new BlackBoardMessageManager(__notifier);
63  __im = new BlackBoardInterfaceManager(__memmgr, __msgmgr, __notifier);
64 
65  __msgmgr->set_interface_manager(__im);
66 
67  __nethandler = NULL;
68 }
69 
70 
71 /** Heap Memory Constructor.
72  * @param memsize size of memory in bytes
73  */
75 {
76  __memmgr = new BlackBoardMemoryManager(memsize);
77 
78  __msgmgr = new BlackBoardMessageManager(__notifier);
79  __im = new BlackBoardInterfaceManager(__memmgr, __msgmgr, __notifier);
80 
81  __msgmgr->set_interface_manager(__im);
82 
83  __nethandler = NULL;
84 }
85 
86 
87 /** Destructor. */
89 {
90  if ( __nethandler ) {
91  __nethandler->cancel();
92  __nethandler->join();
93  delete __nethandler;
94  }
95  delete __im;
96  delete __msgmgr;
97  delete __memmgr;
98 }
99 
100 
101 Interface *
102 LocalBlackBoard::open_for_reading(const char *type, const char *identifier)
103 {
104  try {
105  return __im->open_for_reading(type, identifier);
106  } catch (Exception &e) {
107  throw;
108  }
109 }
110 
111 
112 Interface *
113 LocalBlackBoard::open_for_writing(const char *type, const char *identifier)
114 {
115  try {
116  return __im->open_for_writing(type, identifier);
117  } catch (Exception &e) {
118  throw;
119  }
120 }
121 
122 
123 std::list<Interface *>
125  const char *id_pattern)
126 {
127  try {
128  return __im->open_multiple_for_reading(type_pattern, id_pattern);
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:74
void close(Interface *interface)
Close interface.
Interface * open_for_reading(const char *interface_type, const char *identifier)
Open interface for reading.
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
BlackBoard memory manager.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier)
Open interface for writing.
Definition: local.cpp:113
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
BlackBoard shared memory lister.
Definition: lister.h:33
virtual bool is_alive() const
Check if the BlackBoard is still alive.
Definition: local.cpp:157
std::list< Interface * > open_multiple_for_reading(const char *type_pattern, const char *id_pattern="*")
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.
const BlackBoardMemoryManager * memory_manager() const
Get memory manager.
Definition: local.cpp:198
Fawkes Network Hub.
Definition: hub.h:33
InterfaceInfoList * list_all() const
Get a list of interfaces.
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
virtual Interface * open_for_reading(const char *interface_type, const char *identifier)
Open interface for reading.
Definition: local.cpp:102
virtual void close(Interface *interface)
Close interface.
Definition: local.cpp:136
BlackBoardNotifier * __notifier
Notifier for BB events.
Definition: blackboard.h:102
void cancel()
Cancel a thread.
Definition: thread.cpp:640
BlackBoard Shared Memory Header.
Definition: header.h:34
Interface * open_for_writing(const char *interface_type, const char *identifier)
Open interface for writing.
BlackBoard message manager.
void join()
Join the thread.
Definition: thread.cpp:599
BlackBoard Network Handler.
Definition: handler.h:45
virtual ~LocalBlackBoard()
Destructor.
Definition: local.cpp:88
virtual std::list< Interface * > open_multiple_for_reading(const char *type_pattern, const char *id_pattern="*")
Open multiple interfaces for reading.
Definition: local.cpp:124
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:507