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());