24 #include <blackboard/internal/interface_manager.h>
26 #include <blackboard/blackboard.h>
27 #include <blackboard/internal/memory_manager.h>
28 #include <blackboard/internal/message_manager.h>
29 #include <blackboard/exceptions.h>
30 #include <blackboard/internal/interface_mem_header.h>
31 #include <blackboard/interface_listener.h>
32 #include <blackboard/interface_observer.h>
33 #include <blackboard/internal/instance_factory.h>
34 #include <blackboard/internal/notifier.h>
36 #include <interface/interface.h>
37 #include <interface/interface_info.h>
39 #include <core/threading/mutex.h>
40 #include <core/threading/refc_rwlock.h>
41 #include <core/exceptions/system.h>
42 #include <utils/system/dynamic_module/module.h>
72 notifier = bb_notifier;
78 writer_interfaces.clear();
87 delete instance_factory;
102 BlackBoardInterfaceManager::new_interface_instance(
const char *type,
const char *identifier)
106 iface->set_instance_serial(next_instance_serial());
107 iface->set_mediators(
this, msgmgr);
120 BlackBoardInterfaceManager::delete_interface_instance(Interface *interface)
132 BlackBoardInterfaceManager::find_interface_in_memory(
const char *type,
const char *identifier)
134 interface_header_t *ih;
135 BlackBoardMemoryManager::ChunkIterator cit;
136 for ( cit = memmgr->
begin(); cit != memmgr->
end(); ++cit ) {
137 ih = (interface_header_t *)*cit;
138 if ( (strncmp(ih->type, type, __INTERFACE_TYPE_SIZE) == 0) &&
139 (strncmp(ih->id, identifier, __INTERFACE_ID_SIZE) == 0)
154 BlackBoardInterfaceManager::next_mem_serial()
156 unsigned int serial = 1;
157 interface_header_t *ih;
158 BlackBoardMemoryManager::ChunkIterator cit;
159 for ( cit = memmgr->
begin(); cit != memmgr->
end(); ++cit ) {
160 ih = (interface_header_t *)*cit;
161 if ( ih->serial >= serial ) {
162 serial = ih->serial + 1;
174 BlackBoardInterfaceManager::next_instance_serial()
178 return instance_serial++;
180 throw BBNotMasterException(
"Instance serial can only be requested by BB Master");
196 BlackBoardInterfaceManager::create_interface(
const char *type,
const char *identifier,
197 Interface* &interface,
void* &ptr)
199 interface_header_t *ih;
202 interface = new_interface_instance(type, identifier);
204 ptr = memmgr->alloc_nolock(interface->datasize() +
sizeof(interface_header_t));
205 ih = (interface_header_t *)ptr;
206 }
catch (OutOfMemoryException &e) {
207 e.append(
"BlackBoardInterfaceManager::createInterface: interface of type %s could not be created", type);
212 memset(ptr, 0, interface->datasize() +
sizeof(interface_header_t));
214 strncpy(ih->type, type, __INTERFACE_TYPE_SIZE);
215 strncpy(ih->id, identifier, __INTERFACE_ID_SIZE);
216 memcpy(ih->hash, interface->hash(), __INTERFACE_HASH_SIZE);
219 ih->serial = next_mem_serial();
220 ih->flag_writer_active = 0;
222 rwlocks[ih->serial] =
new RefCountRWLock();
224 interface->set_memory(ih->serial, ptr, (
char *)ptr +
sizeof(interface_header_t));
244 bool created =
false;
248 ptr = find_interface_in_memory(type, identifier);
253 iface = new_interface_instance(type, identifier);
255 if ( (iface->
hash_size() != __INTERFACE_HASH_SIZE ) ||
256 (memcmp(iface->
hash(), ih->
hash, __INTERFACE_HASH_SIZE) != 0) ) {
260 rwlocks[ih->
serial]->ref();
263 create_interface(type, identifier, iface, ptr);
267 iface->set_readwrite(
false, rwlocks[ih->
serial]);
280 if (iface) delete_interface_instance(iface);
301 std::list<Interface *>
303 const char *id_pattern)
308 std::list<Interface *> rv;
315 for ( cit = memmgr->
begin(); cit != memmgr->
end(); ++cit ) {
320 char type[__INTERFACE_TYPE_SIZE + 1];
321 char id[__INTERFACE_ID_SIZE + 1];
322 type[__INTERFACE_TYPE_SIZE] = 0;
323 id[__INTERFACE_TYPE_SIZE] = 0;
324 strncpy(type, ih->
type, __INTERFACE_TYPE_SIZE);
325 strncpy(
id, ih->
id, __INTERFACE_ID_SIZE);
327 if ((fnmatch(type_pattern, type, 0) == FNM_NOMATCH) ||
328 (fnmatch(id_pattern,
id, 0) == FNM_NOMATCH) ) {
334 iface = new_interface_instance(ih->
type, ih->
id);
337 if ( (iface->
hash_size() != __INTERFACE_HASH_SIZE ) ||
338 (memcmp(iface->
hash(), ih->
hash, __INTERFACE_HASH_SIZE) != 0) ) {
342 rwlocks[ih->
serial]->ref();
344 iface->set_readwrite(
false, rwlocks[ih->
serial]);
354 for (std::list<Interface *>::iterator j = rv.begin(); j != rv.end(); ++j) {
360 if (iface) delete_interface_instance( iface );
361 for (std::list<Interface *>::iterator i = rv.begin(); i != rv.end(); ++i) {
362 delete_interface_instance(*i);
394 bool created =
false;
397 ptr = find_interface_in_memory(type, identifier);
406 iface = new_interface_instance(type, identifier);
407 if ( (iface->
hash_size() != __INTERFACE_HASH_SIZE ) ||
408 (memcmp(iface->
hash(), ih->
hash, __INTERFACE_HASH_SIZE) != 0) ) {
412 rwlocks[ih->
serial]->ref();
415 create_interface(type, identifier, iface, ptr);
419 iface->set_readwrite(
true, rwlocks[ih->
serial]);
424 writer_interfaces[ih->
serial] = iface;
433 if (iface) delete_interface_instance(iface);
449 if ( interface == NULL )
return;
451 bool destroyed =
false;
455 bool killed_writer = interface->__write_access;
458 if ( interface->__write_access ) {
459 writer_interfaces.erase( interface->__mem_serial );
461 memmgr->
free( interface->__mem_real_ptr );
464 if ( interface->__write_access ) {
466 writer_interfaces.erase( interface->__mem_serial );
483 delete_interface_instance( interface );
500 for ( cit = memmgr->
begin(); cit != memmgr->
end(); ++cit ) {
523 const char *id_pattern)
const
530 for ( cit = memmgr->
begin(); cit != memmgr->
end(); ++cit ) {
532 char type[__INTERFACE_TYPE_SIZE + 1];
533 char id[__INTERFACE_ID_SIZE + 1];
535 type[__INTERFACE_TYPE_SIZE] = 0;
536 id[__INTERFACE_ID_SIZE] = 0;
537 strncpy(type, ih->
type, __INTERFACE_TYPE_SIZE);
538 strncpy(
id, ih->
id, __INTERFACE_ID_SIZE);
539 if ((fnmatch(type_pattern, type, FNM_NOESCAPE) == 0) &&
540 (fnmatch(id_pattern,
id, FNM_NOESCAPE) == 0))
560 BlackBoardInterfaceManager::writer_for_mem_serial(
unsigned int mem_serial)
562 if ( writer_interfaces.find(mem_serial) != writer_interfaces.end() ) {
563 return writer_interfaces[mem_serial];
565 throw BlackBoardNoWritingInstanceException();
580 return (writer_interfaces.find(interface->__mem_serial) != writer_interfaces.end());
void notify_of_reader_added(const Interface *interface, unsigned int event_instance_serial)
Notify that reader has been added.
virtual unsigned int num_readers(const Interface *interface) const
Get number of readers.
Interface * new_interface_instance(const char *type, const char *identifier)
Creates a new interface instance.
BlackBoard instance factory.
ChunkIterator begin()
Get first element for chunk iteration.
unsigned short serial() const
Get instance serial of interface.
void notify_of_interface_created(const char *type, const char *id)
Notify that an interface has been created.
void close(Interface *interface)
Close interface.
Interface * open_for_reading(const char *interface_type, const char *identifier)
Open interface for reading.
void notify_of_writer_added(const Interface *interface, unsigned int event_instance_serial)
Notify that writer has been added.
size_t hash_size() const
Get size of interface hash.
Fawkes library namespace.
ChunkIterator end()
Get end of chunk list.
void unlock()
Unlock the mutex.
BlackBoard memory manager.
virtual void notify_of_data_change(const Interface *interface)
Notify of data change.
const unsigned char * hash() const
Get interface hash.
bool is_master() const
Check if this BB memory manager is the master.
void notify_of_writer_removed(const Interface *interface, unsigned int event_instance_serial)
Notify that writer has been removed.
void notify_of_data_change(const Interface *interface)
Notify of data change.
Base class for all Fawkes BlackBoard interfaces.
virtual ~BlackBoardInterfaceManager()
Destructor.
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.
InterfaceInfoList * list_all() const
Get a list of interfaces.
Interface information list.
BlackBoardInterfaceManager(BlackBoardMemoryManager *bb_memmgr, BlackBoardMessageManager *bb_msgmgr, BlackBoardNotifier *bb_notifier)
Constructor.
Base class for exceptions in Fawkes.
void delete_interface_instance(Interface *interface)
Destroy an interface instance.
void notify_of_interface_destroyed(const char *type, const char *id)
Notify that an interface has been destroyed.
Thrown if versions do not match.
Iterator for memory chunks.
void unlock()
Unlock memory.
Interface * open_for_writing(const char *interface_type, const char *identifier)
Open interface for writing.
BlackBoard message manager.
void free(void *chunk_ptr)
Free a memory chunk.
void append(const char *type, const char *id, const unsigned char *hash, unsigned int serial, bool has_writer, unsigned int num_readers)
Append an interface info.
void lock()
Lock this mutex.
Thrown if a writer is already active on an interface that writing has been requested for...
Mutex mutual exclusion lock.
void notify_of_reader_removed(const Interface *interface, unsigned int event_instance_serial)
Notify that reader has been removed.
virtual bool exists_writer(const Interface *interface) const
Check if a writer exists for the given interface.