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 
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 /** 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  */
55 LocalBlackBoard::LocalBlackBoard(size_t memsize, const char *magic_token, bool master)
56 {
57  memmgr_ = new BlackBoardMemoryManager(memsize, BLACKBOARD_VERSION, master);
58 
59  msgmgr_ = new BlackBoardMessageManager(notifier_);
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 
74  msgmgr_ = new BlackBoardMessageManager(notifier_);
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 
95 Interface *
96 LocalBlackBoard::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 
105 Interface *
106 LocalBlackBoard::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 
115 std::list<Interface *>
116 LocalBlackBoard::open_multiple_for_reading(const char *type_pattern,
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 
127 void
129 {
130  im_->close(interface);
131 }
132 
135 {
136  return im_->list_all();
137 }
138 
140 LocalBlackBoard::list(const char *type_pattern, const char *id_pattern)
141 {
142  return im_->list(type_pattern, id_pattern);
143 }
144 
145 bool
146 LocalBlackBoard::is_alive() const throw()
147 {
148  return true;
149 }
150 
151 bool
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  */
163 void
164 LocalBlackBoard::cleanup(const char *magic_token, bool use_lister)
165 {
166  BlackBoardSharedMemoryHeader *bbsh = new BlackBoardSharedMemoryHeader(BLACKBOARD_VERSION);
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  */
183 const BlackBoardMemoryManager *
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  */
193 void
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
fawkes::LocalBlackBoard::list_all
virtual InterfaceInfoList * list_all()
Definition: local.cpp:137
fawkes::BlackBoardSharedMemoryHeader
Definition: header.h:38
fawkes::BlackBoardInterfaceManager::list
InterfaceInfoList * list(const char *type_pattern, const char *id_pattern) const
Get a constrained list of interfaces.
Definition: interface_manager.cpp:578
fawkes::SharedMemory::erase_orphaned
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:1203
fawkes::BlackBoardInterfaceManager::open_for_reading
Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)
Open interface for reading.
Definition: interface_manager.cpp:250
fawkes::LocalBlackBoard::try_aliveness_restore
virtual bool try_aliveness_restore()
Definition: local.cpp:155
fawkes::BlackBoardInterfaceManager::open_for_writing
Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)
Open interface for writing.
Definition: interface_manager.cpp:415
fawkes::BlackBoardMemoryManager
Definition: memory_manager.h:62
fawkes::LocalBlackBoard::start_nethandler
virtual void start_nethandler(FawkesNetworkHub *hub)
Start network handler.
Definition: local.cpp:197
fawkes::BlackBoardInterfaceManager
Definition: interface_manager.h:49
fawkes::BlackBoard::notifier_
BlackBoardNotifier * notifier_
Notifier for BB events.
Definition: blackboard.h:114
fawkes::LocalBlackBoard::open_for_reading
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)
Definition: local.cpp:99
fawkes::LocalBlackBoard::cleanup
static void cleanup(const char *magic_token, bool use_lister=false)
Cleanup orphaned BlackBoard segments.
Definition: local.cpp:167
fawkes::BlackBoardNetworkHandler
Definition: handler.h:46
fawkes
fawkes::BlackBoardInterfaceManager::close
void close(Interface *interface)
Close interface.
Definition: interface_manager.cpp:488
fawkes::LocalBlackBoard::is_alive
virtual bool is_alive() const
Definition: local.cpp:149
fawkes::LocalBlackBoard::list
virtual InterfaceInfoList * list(const char *type_pattern, const char *id_pattern)
Definition: local.cpp:143
fawkes::BlackBoardInterfaceManager::open_multiple_for_reading
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.
Definition: interface_manager.cpp:328
fawkes::LocalBlackBoard::open_multiple_for_reading
virtual std::list< Interface * > open_multiple_for_reading(const char *type_pattern, const char *id_pattern="*", const char *owner=NULL)
Definition: local.cpp:119
fawkes::InterfaceInfoList
Definition: interface_info.h:79
fawkes::Interface
Definition: interface.h:77
fawkes::LocalBlackBoard::close
virtual void close(Interface *interface)
Definition: local.cpp:131
fawkes::BlackBoardInterfaceManager::list_all
InterfaceInfoList * list_all() const
Get a list of interfaces.
Definition: interface_manager.cpp:533
fawkes::BlackBoardMessageManager
Definition: message_manager.h:40
fawkes::BlackBoardSharedMemoryLister
Definition: lister.h:37
fawkes::LocalBlackBoard::~LocalBlackBoard
virtual ~LocalBlackBoard()
Destructor.
Definition: local.cpp:86
fawkes::Thread::start
void start(bool wait=true)
Call this method to start the thread.
Definition: thread.cpp:503
fawkes::Thread::cancel
void cancel()
Cancel a thread.
Definition: thread.cpp:650
fawkes::LocalBlackBoard::LocalBlackBoard
LocalBlackBoard(size_t memsize)
Heap Memory Constructor.
Definition: local.cpp:73
fawkes::FawkesNetworkHub
Definition: hub.h:37
fawkes::LocalBlackBoard::memory_manager
const BlackBoardMemoryManager * memory_manager() const
Get memory manager.
Definition: local.cpp:187
fawkes::LocalBlackBoard::open_for_writing
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)
Definition: local.cpp:109
fawkes::Thread::join
void join()
Join the thread.
Definition: thread.cpp:601
fawkes::Exception
Definition: exception.h:39