interface_observer.cpp

00001  
00002 /***************************************************************************
00003  *  interface_observer.cpp - BlackBoard interface observer
00004  *
00005  *  Created: Fri Jan 25 18:26:12 2008
00006  *  Copyright  2007-2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <blackboard/interface_observer.h>
00025 #include <interface/interface.h>
00026 #include <cstdlib>
00027 #include <cstring>
00028 
00029 namespace fawkes {
00030 
00031 /** @class BlackBoardInterfaceObserver <blackboard/interface_observer.h>
00032  * BlackBoard interface observer.
00033  * Derive this class if you want to be notified of specific BlackBoard 
00034  * events that are not tied to particular instances of interfaces like
00035  * create and destroy operations.
00036  *
00037  * The bb_interface_* methods are called during the appropriate operation. The
00038  * operation that you carry out in this event handler really has to be damn fast,
00039  * or the performance of the whole system will suffer severely. For this reason use
00040  * this notification facility only rarely and only register for the appropriate
00041  * events.
00042  *
00043  * This class provides the basic infrastructure that can be used to build
00044  * your own observer. During the life time of your observer you
00045  * first add all the interfaces to the appropriate structures that you want
00046  * to listen for and add the interface types where you want to be notified
00047  * of creation events.
00048  *
00049  * The interface created event is raised whenever an interface of a type that
00050  * you registered for is created. The destroyed event is raised if the an interface
00051  * is irrecoverable deleted from the BlackBoard. This happens when the last
00052  * reader or writer closes the interface. That means neither a writer nor any
00053  * reader has a particular interface still opened.
00054  *
00055  * Here is a simple life cycle of a BlackBoard interface observer:
00056  * First you add all the interface types that you want to observe with calls to
00057  * bbio_add_interface_create_type() and bbio_add_interface_destroy_type(). Then
00058  * you register the observer with InterfaceManager::register_observer(). From then
00059  * on you are notified of the events. Afterwards you unregister your observer
00060  * to no longer receive events.
00061  *
00062  * @author Tim Niemueller
00063  * @see BlackBoardInterfaceManager::register_observer()
00064  * @see BlackBoardInterfaceManager::unregister_observer()
00065  */
00066 
00067 /** Empty constructor. */
00068 BlackBoardInterfaceObserver::BlackBoardInterfaceObserver()
00069 {
00070 }
00071 
00072 /** Destructor. */
00073 BlackBoardInterfaceObserver::~BlackBoardInterfaceObserver()
00074 {
00075   __bbio_observed_create.clear();
00076   __bbio_observed_destroy.clear();
00077 }
00078 
00079 
00080 /** BlackBoard interface created notification.
00081  * This is called whenever an interface is created for a type that you registered
00082  * for.
00083  * @param type type of the interface. If you want to store this make a copy as it
00084  * is not guaranteed that the supplied string exists for longer than the duration
00085  * of the method call
00086  * @param id ID of the newly created interface. If you want to store this make a
00087  * copy as it is not guaranteed that the supplied string exists for longer than
00088  * the duration of the method call
00089  */
00090 void
00091 BlackBoardInterfaceObserver::bb_interface_created(const char *type, const char *id) throw()
00092 {
00093 }
00094 
00095 
00096 /** BlackBoard interface destroyed notification.
00097  * This is called whenever an interface is destroyed for a type that you registered
00098  * for.
00099  * @param type type of the interface. If you want to store this make a copy as it
00100  * is not guaranteed that the supplied string exists for longer than the duration
00101  * of the method call
00102  * @param id ID of the newly created interface. If you want to store this make a
00103  * copy as it is not guaranteed that the supplied string exists for longer than
00104  * the duration of the method call
00105  */
00106 void
00107 BlackBoardInterfaceObserver::bb_interface_destroyed(const char *type, const char *id) throw()
00108 {
00109 }
00110 
00111 
00112 /** Add interface creation type to watch list.
00113  * With this you add an interface type to the watch list. For any type on this list
00114  * you will be notified if an interface is created.
00115  * @param type type to watch
00116  * @param id_pattern pattern of interface IDs to open, supports wildcards similar
00117  * to filenames (*, ?, []), see "man fnmatch" for all supported.
00118  */
00119 void
00120 BlackBoardInterfaceObserver::bbio_add_observed_create(const char *type, const char *id_pattern) throw()
00121 {
00122   __bbio_observed_create.lock();
00123   __bbio_observed_create[type].push_back(id_pattern);
00124   __bbio_observed_create[type].sort();
00125   __bbio_observed_create[type].unique();
00126   __bbio_observed_create.unlock();
00127 }
00128 
00129 
00130 /** Add interface destruction type to watch list.
00131  * With this you add an interface type to the watch list. For any type on this list
00132  * you will be notified if an interface is destroyed.
00133  * @param type type to watch
00134  * @param id_pattern pattern of interface IDs to open, supports wildcards similar
00135  * to filenames (*, ?, []), see "man fnmatch" for all supported.
00136  */
00137 void
00138 BlackBoardInterfaceObserver::bbio_add_observed_destroy(const char *type, const char *id_pattern) throw()
00139 {
00140   __bbio_observed_destroy.lock();
00141   __bbio_observed_destroy[type].push_back(id_pattern);
00142   __bbio_observed_destroy[type].sort();
00143   __bbio_observed_destroy[type].unique();
00144   __bbio_observed_destroy.unlock();
00145 }
00146 
00147 
00148 /** Get interface creation type watch list.
00149  * @return interface type watch list
00150  */
00151 BlackBoardInterfaceObserver::ObservedInterfaceLockMap *
00152 BlackBoardInterfaceObserver::bbio_get_observed_create() throw()
00153 {
00154   return &__bbio_observed_create;
00155 }
00156 
00157 
00158 /** Get interface destriction type watch list.
00159  * @return interface type watch list
00160  */
00161 BlackBoardInterfaceObserver::ObservedInterfaceLockMap *
00162 BlackBoardInterfaceObserver::bbio_get_observed_destroy() throw()
00163 {
00164   return &__bbio_observed_destroy;
00165 }
00166 
00167 
00168 } // end namespace fawkes

Generated on Tue Feb 22 13:32:25 2011 for Fawkes API by  doxygen 1.4.7