Fawkes API  Fawkes Development Version
interface_observer.cpp
1 
2 /***************************************************************************
3  * interface_observer.cpp - BlackBoard interface observer for net handler
4  *
5  * Created: Wed Mar 02 17:05:29 2011
6  * Copyright 2007-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <blackboard/blackboard.h>
25 #include <blackboard/net/interface_observer.h>
26 #include <blackboard/net/messages.h>
27 #include <logging/liblogger.h>
28 #include <netcomm/fawkes/hub.h>
29 #include <netcomm/fawkes/component_ids.h>
30 
31 #include <cstdlib>
32 #include <cstring>
33 
34 namespace fawkes {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
39 /** @class BlackBoardNetHandlerInterfaceObserver <blackboard/net/interface_observer.h>
40  * Interface observer for blackboard network handler.
41  * This class is used by the BlackBoardNetworkHandler to track interface events (creation
42  * and destruction) and broadcast them to everybody listening.
43  * @author Tim Niemueller
44  */
45 
46 /** Constructor.
47  * @param blackboard local BlackBoard
48  * @param hub Fawkes network hub to use to send messages
49  */
51  FawkesNetworkHub *hub)
52 {
53  __blackboard = blackboard;
54  __fnh = hub;
55 
56  bbio_add_observed_create("*", "*");
57  bbio_add_observed_destroy("*", "*");
58 
59  __blackboard->register_observer(this);
60 }
61 
62 
63 /** Destructor. */
65 {
66  __blackboard->unregister_observer(this);
67 }
68 
69 
70 /** Broadcast event.
71  * @param msg_id message ID to use
72  * @param type interface type
73  * @param id interface ID
74  */
75 void
76 BlackBoardNetHandlerInterfaceObserver::send_event(unsigned int msg_id,
77  const char *type, const char *id)
78 {
79  bb_ievent_msg_t *esm = (bb_ievent_msg_t *)malloc(sizeof(bb_ievent_msg_t));
80  strncpy(esm->type, type, __INTERFACE_TYPE_SIZE);
81  strncpy(esm->id, id, __INTERFACE_ID_SIZE);
82 
83  try {
84  __fnh->broadcast(FAWKES_CID_BLACKBOARD, msg_id, esm, sizeof(bb_ievent_msg_t));
85  } catch (Exception &e) {
86  LibLogger::log_warn("BlackBoardNetHandlerInterfaceObserver",
87  "Failed to send BlackBoard event (%s), exception follows",
88  (msg_id == MSG_BB_INTERFACE_CREATED) ? "create" : "destroy");
89  LibLogger::log_warn("BlackBoardNetHandlerInterfaceObserver", e);
90  }
91 }
92 
93 void
95  const char *id) throw()
96 {
97  send_event(MSG_BB_INTERFACE_CREATED, type, id);
98 }
99 
100 
101 void
103  const char *id) throw()
104 {
105  send_event(MSG_BB_INTERFACE_DESTROYED, type, id);
106 }
107 
108 
109 } // end namespace fawkes
virtual void register_observer(BlackBoardInterfaceObserver *observer)
Register BB interface observer.
Definition: blackboard.cpp:230
char type[__INTERFACE_TYPE_SIZE]
interface type name
Definition: messages.h:106
BlackBoardNetHandlerInterfaceObserver(BlackBoard *blackboard, FawkesNetworkHub *hub)
Constructor.
Fawkes library namespace.
virtual void bb_interface_created(const char *type, const char *id)
BlackBoard interface created notification.
void bbio_add_observed_destroy(const char *type_pattern, const char *id_pattern="*")
Add interface destruction type to watch list.
Fawkes Network Hub.
Definition: hub.h:33
void bbio_add_observed_create(const char *type_pattern, const char *id_pattern="*")
Add interface creation type to watch list.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void unregister_observer(BlackBoardInterfaceObserver *observer)
Unregister BB interface observer.
Definition: blackboard.cpp:244
virtual void broadcast(FawkesNetworkMessage *msg)=0
Method to broadcast a message to all connected clients.
static void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: liblogger.cpp:162
Message for interface events.
Definition: messages.h:105
The BlackBoard abstract class.
Definition: blackboard.h:48
virtual void bb_interface_destroyed(const char *type, const char *id)
BlackBoard interface destroyed notification.
char id[__INTERFACE_ID_SIZE]
interface instance ID
Definition: messages.h:107