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
34namespace 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 */
49{
50 blackboard_ = blackboard;
51 fnh_ = hub;
52
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 */
70void
71BlackBoardNetHandlerInterfaceObserver::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
89void
91 const char *id) noexcept
92{
93 send_event(MSG_BB_INTERFACE_CREATED, type, id);
94}
95
96void
98 const char *id) noexcept
99{
100 send_event(MSG_BB_INTERFACE_DESTROYED, type, id);
101}
102
103} // end namespace fawkes
void bbio_add_observed_destroy(const char *type_pattern, const char *id_pattern="*") noexcept
Add interface destruction type to watch list.
void bbio_add_observed_create(const char *type_pattern, const char *id_pattern="*") noexcept
Add interface creation type to watch list.
virtual void bb_interface_destroyed(const char *type, const char *id) noexcept
BlackBoard interface destroyed notification.
virtual void bb_interface_created(const char *type, const char *id) noexcept
BlackBoard interface created notification.
BlackBoardNetHandlerInterfaceObserver(BlackBoard *blackboard, FawkesNetworkHub *hub)
Constructor.
The BlackBoard abstract class.
Definition: blackboard.h:46
virtual void unregister_observer(BlackBoardInterfaceObserver *observer)
Unregister BB interface observer.
Definition: blackboard.cpp:240
virtual void register_observer(BlackBoardInterfaceObserver *observer)
Register BB interface observer.
Definition: blackboard.cpp:225
Fawkes Network Hub.
Definition: hub.h:34
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:156
Fawkes library namespace.
Message for interface events.
Definition: messages.h:110
char type[INTERFACE_TYPE_SIZE_]
interface type name
Definition: messages.h:111
char id[INTERFACE_ID_SIZE_]
interface instance ID
Definition: messages.h:112