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/component_ids.h>
29 #include <netcomm/fawkes/hub.h>
30 
31 #include <cstdlib>
32 #include <cstring>
33 
34 namespace fawkes {
35 
36 /** @class BlackBoardNetHandlerInterfaceObserver <blackboard/net/interface_observer.h>
37  * Interface observer for blackboard network handler.
38  * This class is used by the BlackBoardNetworkHandler to track interface events (creation
39  * and destruction) and broadcast them to everybody listening.
40  * @author Tim Niemueller
41  */
42 
43 /** Constructor.
44  * @param blackboard local BlackBoard
45  * @param hub Fawkes network hub to use to send messages
46  */
48  FawkesNetworkHub *hub)
49 {
50  blackboard_ = blackboard;
51  fnh_ = hub;
52 
53  bbio_add_observed_create("*", "*");
54  bbio_add_observed_destroy("*", "*");
55 
56  blackboard_->register_observer(this);
57 }
58 
59 /** Destructor. */
61 {
62  blackboard_->unregister_observer(this);
63 }
64 
65 /** Broadcast event.
66  * @param msg_id message ID to use
67  * @param type interface type
68  * @param id interface ID
69  */
70 void
71 BlackBoardNetHandlerInterfaceObserver::send_event(unsigned int msg_id,
72  const char * type,
73  const char * id)
74 {
75  bb_ievent_msg_t *esm = (bb_ievent_msg_t *)malloc(sizeof(bb_ievent_msg_t));
76  strncpy(esm->type, type, INTERFACE_TYPE_SIZE_ - 1);
77  strncpy(esm->id, id, INTERFACE_ID_SIZE_ - 1);
78 
79  try {
80  fnh_->broadcast(FAWKES_CID_BLACKBOARD, msg_id, esm, sizeof(bb_ievent_msg_t));
81  } catch (Exception &e) {
82  LibLogger::log_warn("BlackBoardNetHandlerInterfaceObserver",
83  "Failed to send BlackBoard event (%s), exception follows",
84  (msg_id == MSG_BB_INTERFACE_CREATED) ? "create" : "destroy");
85  LibLogger::log_warn("BlackBoardNetHandlerInterfaceObserver", e);
86  }
87 }
88 
89 void
91  const char *id) throw()
92 {
93  send_event(MSG_BB_INTERFACE_CREATED, type, id);
94 }
95 
96 void
98  const char *id) throw()
99 {
100  send_event(MSG_BB_INTERFACE_DESTROYED, type, id);
101 }
102 
103 } // end namespace fawkes
fawkes::BlackBoardNetHandlerInterfaceObserver::BlackBoardNetHandlerInterfaceObserver
BlackBoardNetHandlerInterfaceObserver(BlackBoard *blackboard, FawkesNetworkHub *hub)
Constructor.
Definition: interface_observer.cpp:51
fawkes::BlackBoardInterfaceObserver::bbio_add_observed_destroy
void bbio_add_observed_destroy(const char *type_pattern, const char *id_pattern="*")
Add interface destruction type to watch list.
Definition: interface_observer.cpp:142
fawkes::bb_ievent_msg_t::type
char type[INTERFACE_TYPE_SIZE_]
interface type name
Definition: messages.h:114
fawkes::BlackBoardNetHandlerInterfaceObserver::~BlackBoardNetHandlerInterfaceObserver
virtual ~BlackBoardNetHandlerInterfaceObserver()
Destructor.
Definition: interface_observer.cpp:64
fawkes::BlackBoardInterfaceObserver::bbio_add_observed_create
void bbio_add_observed_create(const char *type_pattern, const char *id_pattern="*")
Add interface creation type to watch list.
Definition: interface_observer.cpp:123
fawkes::bb_ievent_msg_t
Message for interface events.
Definition: messages.h:112
fawkes::BlackBoardNetHandlerInterfaceObserver::bb_interface_created
virtual void bb_interface_created(const char *type, const char *id)
BlackBoard interface created notification.
Definition: interface_observer.cpp:94
fawkes::LibLogger::log_warn
static void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: liblogger.cpp:160
fawkes
fawkes::BlackBoard::register_observer
virtual void register_observer(BlackBoardInterfaceObserver *observer)
Register BB interface observer.
Definition: blackboard.cpp:228
fawkes::FawkesNetworkHub::broadcast
virtual void broadcast(FawkesNetworkMessage *msg)=0
fawkes::BlackBoardNetHandlerInterfaceObserver::bb_interface_destroyed
virtual void bb_interface_destroyed(const char *type, const char *id)
BlackBoard interface destroyed notification.
Definition: interface_observer.cpp:101
fawkes::BlackBoard::unregister_observer
virtual void unregister_observer(BlackBoardInterfaceObserver *observer)
Unregister BB interface observer.
Definition: blackboard.cpp:243
fawkes::bb_ievent_msg_t::id
char id[INTERFACE_ID_SIZE_]
interface instance ID
Definition: messages.h:115
fawkes::Exception
Definition: exception.h:39