Fawkes API  Fawkes Development Version
interface_observer.cpp
00001  
00002 /***************************************************************************
00003  *  interface_observer.cpp - BlackBoard interface observer for net handler
00004  *
00005  *  Created: Wed Mar 02 17:05:29 2011
00006  *  Copyright  2007-2011  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/blackboard.h>
00025 #include <blackboard/net/interface_observer.h>
00026 #include <blackboard/net/messages.h>
00027 #include <logging/liblogger.h>
00028 #include <netcomm/fawkes/hub.h>
00029 #include <netcomm/fawkes/component_ids.h>
00030 
00031 #include <cstdlib>
00032 #include <cstring>
00033 
00034 namespace fawkes {
00035 #if 0 /* just to make Emacs auto-indent happy */
00036 }
00037 #endif
00038 
00039 /** @class BlackBoardNetHandlerInterfaceObserver <blackboard/net/interface_observer.h>
00040  * Interface observer for blackboard network handler.
00041  * This class is used by the BlackBoardNetworkHandler to track interface events (creation
00042  * and destruction) and broadcast them to everybody listening.
00043  * @author Tim Niemueller
00044  */
00045 
00046 /** Constructor.
00047  * @param blackboard local BlackBoard
00048  * @param hub Fawkes network hub to use to send messages
00049  */
00050 BlackBoardNetHandlerInterfaceObserver::BlackBoardNetHandlerInterfaceObserver(BlackBoard *blackboard,
00051                                                                              FawkesNetworkHub *hub)
00052 {
00053   __blackboard = blackboard;
00054   __fnh = hub;
00055 
00056   bbio_add_observed_create("*", "*");
00057   bbio_add_observed_destroy("*", "*");
00058 
00059   __blackboard->register_observer(this);
00060 }
00061 
00062 
00063 /** Destructor. */
00064 BlackBoardNetHandlerInterfaceObserver::~BlackBoardNetHandlerInterfaceObserver()
00065 {
00066   __blackboard->unregister_observer(this);
00067 }
00068 
00069 
00070 /** Broadcast event.
00071  * @param msg_id message ID to use
00072  * @param type interface type
00073  * @param id interface ID
00074  */
00075 void
00076 BlackBoardNetHandlerInterfaceObserver::send_event(unsigned int msg_id,
00077                                                   const char *type, const char *id)
00078 {
00079   bb_ievent_msg_t *esm = (bb_ievent_msg_t *)malloc(sizeof(bb_ievent_msg_t));
00080   strncpy(esm->type, type, __INTERFACE_TYPE_SIZE);
00081   strncpy(esm->id, id, __INTERFACE_ID_SIZE);
00082 
00083   try {
00084     __fnh->broadcast(FAWKES_CID_BLACKBOARD, msg_id, esm, sizeof(bb_ievent_msg_t));  
00085   } catch (Exception &e) {
00086     LibLogger::log_warn("BlackBoardNetHandlerInterfaceObserver",
00087                         "Failed to send BlackBoard event (%s), exception follows",
00088                         (msg_id == MSG_BB_INTERFACE_CREATED) ? "create" : "destroy");
00089     LibLogger::log_warn("BlackBoardNetHandlerInterfaceObserver", e);
00090   }
00091 }
00092 
00093 void
00094 BlackBoardNetHandlerInterfaceObserver::bb_interface_created(const char *type,
00095                                                             const char *id) throw()
00096 {
00097   send_event(MSG_BB_INTERFACE_CREATED, type, id);
00098 }
00099 
00100 
00101 void
00102 BlackBoardNetHandlerInterfaceObserver::bb_interface_destroyed(const char *type,
00103                                                               const char *id) throw()
00104 {
00105   send_event(MSG_BB_INTERFACE_DESTROYED, type, id);
00106 }
00107 
00108 
00109 } // end namespace fawkes