Fawkes API  Fawkes Development Version
interface_listener.cpp
1 
2 /***************************************************************************
3  * net_interface_listener.cpp - BlackBoard interface listener for net handler
4  *
5  * Created: Tue Mar 04 17:53:32 2008
6  * Copyright 2007-2008 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 <arpa/inet.h>
25 #include <blackboard/blackboard.h>
26 #include <blackboard/net/interface_listener.h>
27 #include <blackboard/net/messages.h>
28 #include <interface/interface.h>
29 #include <logging/liblogger.h>
30 #include <netcomm/fawkes/component_ids.h>
31 #include <netcomm/fawkes/hub.h>
32 #include <netcomm/fawkes/message.h>
33 
34 #include <cstdio>
35 #include <cstdlib>
36 #include <cstring>
37 
38 namespace fawkes {
39 
40 /** @class BlackBoardNetHandlerInterfaceListener <blackboard/net/interface_listener.h>
41  * Interface listener for network handler.
42  * This class is used by the BlackBoardNetworkHandler to track interface changes and
43  * send out notifications timely.
44  * @author Tim Niemueller
45  */
46 
47 /** Constructor.
48  * @param blackboard local BlackBoard
49  * @param interface interface to care about
50  * @param hub Fawkes network hub to use to send messages
51  * @param clid client ID of the client which opened this interface
52  */
54  Interface * interface,
55  FawkesNetworkHub *hub,
56  unsigned int clid)
57 : BlackBoardInterfaceListener("NetIL/%s", interface->uid())
58 {
59  bbil_add_data_interface(interface);
60  bbil_add_reader_interface(interface);
61  bbil_add_writer_interface(interface);
62  if (interface->is_writer()) {
63  bbil_add_message_interface(interface);
64  }
65 
66  blackboard_ = blackboard;
67  interface_ = interface;
68  fnh_ = hub;
69  clid_ = clid;
70 
71  blackboard_->register_listener(this);
72 }
73 
74 /** Destructor. */
76 {
77  blackboard_->unregister_listener(this);
78 }
79 
80 void
82 {
83  // send out data refreshed notification
84  interface->read();
85 
86  size_t payload_size = sizeof(bb_idata_msg_t) + interface->datasize();
87  void * payload = malloc(payload_size);
88  bb_idata_msg_t *dm = (bb_idata_msg_t *)payload;
89  dm->serial = interface->serial();
90  dm->data_size = htonl(interface->datasize());
91  memcpy((char *)payload + sizeof(bb_idata_msg_t), interface->datachunk(), interface->datasize());
92 
93  try {
94  fnh_->send(clid_, FAWKES_CID_BLACKBOARD, MSG_BB_DATA_REFRESHED, payload, payload_size);
95  } catch (Exception &e) {
96  LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard data, exception follows");
97  LibLogger::log_warn(bbil_name(), e);
98  }
99 }
100 
101 void
103 {
104  // send out data changed notification
105  interface->read();
106 
107  size_t payload_size = sizeof(bb_idata_msg_t) + interface->datasize();
108  void * payload = malloc(payload_size);
109  bb_idata_msg_t *dm = (bb_idata_msg_t *)payload;
110  dm->serial = interface->serial();
111  dm->data_size = htonl(interface->datasize());
112  memcpy((char *)payload + sizeof(bb_idata_msg_t), interface->datachunk(), interface->datasize());
113 
114  try {
115  fnh_->send(clid_, FAWKES_CID_BLACKBOARD, MSG_BB_DATA_CHANGED, payload, payload_size);
116  } catch (Exception &e) {
117  LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard data, exception follows");
118  LibLogger::log_warn(bbil_name(), e);
119  }
120 }
121 
122 bool
124  Message * message) noexcept
125 {
126  // send out interface message
127  size_t payload_size = sizeof(bb_imessage_msg_t) + message->datasize();
128  void * payload = calloc(1, payload_size);
129  bb_imessage_msg_t *dm = (bb_imessage_msg_t *)payload;
130  dm->serial = interface->serial();
131  dm->source = message->source_id();
132  strncpy(dm->msg_type, message->type(), INTERFACE_MESSAGE_TYPE_SIZE_ - 1);
133  dm->data_size = htonl(message->datasize());
134  dm->msgid = htonl(message->id());
135  dm->hops = htonl(message->hops());
136  memcpy((char *)payload + sizeof(bb_imessage_msg_t), message->datachunk(), message->datasize());
137 
138  try {
139  fnh_->send(clid_, FAWKES_CID_BLACKBOARD, MSG_BB_INTERFACE_MESSAGE, payload, payload_size);
140  } catch (Exception &e) {
141  LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard message, exception follows");
142  LibLogger::log_warn(bbil_name(), e);
143  }
144 
145  // do not enqueue, we are fine with just sending
146  return false;
147 }
148 
149 void
150 BlackBoardNetHandlerInterfaceListener::send_event_serial(Interface * interface,
151  unsigned int msg_id,
152  Uuid event_serial)
153 {
155  esm->serial = interface->serial();
156  esm->event_serial = event_serial;
157 
158  try {
159  fnh_->send(clid_, FAWKES_CID_BLACKBOARD, msg_id, esm, sizeof(bb_ieventserial_msg_t));
160  } catch (Exception &e) {
161  LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard event serial, exception follows");
163  }
164 }
165 
166 void
168  Uuid instance_serial) noexcept
169 {
170  send_event_serial(interface, MSG_BB_WRITER_ADDED, instance_serial);
171 }
172 
173 void
175  Uuid instance_serial) noexcept
176 {
177  send_event_serial(interface, MSG_BB_WRITER_REMOVED, instance_serial);
178 }
179 
180 void
182  Uuid instance_serial) noexcept
183 {
184  send_event_serial(interface, MSG_BB_READER_ADDED, instance_serial);
185 }
186 
187 void
189  Uuid instance_serial) noexcept
190 {
191  send_event_serial(interface, MSG_BB_READER_REMOVED, instance_serial);
192 }
193 
194 } // end namespace fawkes
BlackBoard interface listener.
void bbil_add_reader_interface(Interface *interface)
Add an interface to the reader addition/removal watch list.
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
void bbil_add_writer_interface(Interface *interface)
Add an interface to the writer addition/removal watch list.
const char * bbil_name() const
Get BBIL name.
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.
BlackBoardNetHandlerInterfaceListener(BlackBoard *blackboard, Interface *interface, FawkesNetworkHub *hub, unsigned int clid)
Constructor.
virtual void bb_interface_reader_removed(Interface *interface, Uuid instance_serial) noexcept
A reading instance has been closed for a watched interface.
virtual void bb_interface_data_refreshed(Interface *interface) noexcept
BlackBoard data refreshed notification.
virtual bool bb_interface_message_received(Interface *interface, Message *message) noexcept
BlackBoard message received notification.
virtual void bb_interface_reader_added(Interface *interface, Uuid instance_serial) noexcept
A reading instance has been opened for a watched interface.
virtual void bb_interface_writer_added(Interface *interface, Uuid instance_serial) noexcept
A writing instance has been opened for a watched interface.
virtual void bb_interface_writer_removed(Interface *interface, Uuid instance_serial) noexcept
A writing instance has been closed for a watched interface.
virtual void bb_interface_data_changed(Interface *interface) noexcept
BlackBoard data changed notification.
The BlackBoard abstract class.
Definition: blackboard.h:46
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Definition: blackboard.cpp:212
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:185
Base class for exceptions in Fawkes.
Definition: exception.h:36
Fawkes Network Hub.
Definition: hub.h:34
virtual void send(FawkesNetworkMessage *msg)=0
Method to send a message to a specific client.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
bool is_writer() const
Check if this is a writing instance.
Definition: interface.cpp:445
Uuid serial() const
Get instance serial of interface.
Definition: interface.cpp:695
static void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: liblogger.cpp:156
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
A convenience class for universally unique identifiers (UUIDs).
Definition: uuid.h:29
Fawkes library namespace.
Interface data message.
Definition: messages.h:165
uint32_t data_size
size in bytes of the following data.
Definition: messages.h:167
Uuid serial
instance serial to unique identify this instance
Definition: messages.h:166
Message to identify an two interface instances.
Definition: messages.h:129
Uuid event_serial
instance serial to unique identify instance that caused the event.
Definition: messages.h:131
Uuid serial
instance serial to unique identify own instance
Definition: messages.h:130
Interface message.
Definition: messages.h:175
Uuid serial
interface instance serial
Definition: messages.h:176
uint32_t msgid
message ID
Definition: messages.h:179
Uuid source
serial of the original message source
Definition: messages.h:177
uint32_t data_size
data for message
Definition: messages.h:181
char msg_type[INTERFACE_MESSAGE_TYPE_SIZE_]
message type
Definition: messages.h:178
uint32_t hops
number of hops this message already passed
Definition: messages.h:180