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 changed 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 = htonl(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_CHANGED, 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 bool
103  Message * message) throw()
104 {
105  // send out interface message
106  size_t payload_size = sizeof(bb_imessage_msg_t) + message->datasize();
107  void * payload = calloc(1, payload_size);
108  bb_imessage_msg_t *dm = (bb_imessage_msg_t *)payload;
109  dm->serial = htonl(interface->serial());
110  strncpy(dm->msg_type, message->type(), INTERFACE_MESSAGE_TYPE_SIZE_ - 1);
111  dm->data_size = htonl(message->datasize());
112  dm->msgid = htonl(message->id());
113  dm->hops = htonl(message->hops());
114  memcpy((char *)payload + sizeof(bb_imessage_msg_t), message->datachunk(), message->datasize());
115 
116  try {
117  fnh_->send(clid_, FAWKES_CID_BLACKBOARD, MSG_BB_INTERFACE_MESSAGE, payload, payload_size);
118  } catch (Exception &e) {
119  LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard message, exception follows");
120  LibLogger::log_warn(bbil_name(), e);
121  }
122 
123  // do not enqueue, we are fine with just sending
124  return false;
125 }
126 
127 void
128 BlackBoardNetHandlerInterfaceListener::send_event_serial(Interface * interface,
129  unsigned int msg_id,
130  unsigned int event_serial)
131 {
133  esm->serial = htonl(interface->serial());
134  esm->event_serial = htonl(event_serial);
135 
136  try {
137  fnh_->send(clid_, FAWKES_CID_BLACKBOARD, msg_id, esm, sizeof(bb_ieventserial_msg_t));
138  } catch (Exception &e) {
139  LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard event serial, exception follows");
141  }
142 }
143 
144 void
146  Interface * interface,
147  unsigned int instance_serial) throw()
148 {
149  send_event_serial(interface, MSG_BB_WRITER_ADDED, instance_serial);
150 }
151 
152 void
154  Interface * interface,
155  unsigned int instance_serial) throw()
156 {
157  send_event_serial(interface, MSG_BB_WRITER_REMOVED, instance_serial);
158 }
159 
160 void
162  Interface * interface,
163  unsigned int instance_serial) throw()
164 {
165  send_event_serial(interface, MSG_BB_READER_ADDED, instance_serial);
166 }
167 
168 void
170  Interface * interface,
171  unsigned int instance_serial) throw()
172 {
173  send_event_serial(interface, MSG_BB_READER_REMOVED, instance_serial);
174 }
175 
176 } // end namespace fawkes
uint32_t msgid
message ID
Definition: messages.h:177
virtual bool bb_interface_message_received(Interface *interface, Message *message)
BlackBoard message received notification.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:41
virtual void bb_interface_reader_added(Interface *interface, unsigned int instance_serial)
A reading instance has been opened for a watched interface.
Message to identify an two interface instances.
Definition: messages.h:127
Fawkes library namespace.
void bbil_add_writer_interface(Interface *interface)
Add an interface to the writer addition/removal watch list.
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Definition: blackboard.cpp:212
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:78
uint32_t event_serial
instance serial to unique identify instance that caused the event.
Definition: messages.h:130
char msg_type[INTERFACE_MESSAGE_TYPE_SIZE_]
message type
Definition: messages.h:176
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:185
Fawkes Network Hub.
Definition: hub.h:33
Base class for exceptions in Fawkes.
Definition: exception.h:35
unsigned short serial() const
Get instance serial of interface.
Definition: interface.cpp:683
virtual void send(FawkesNetworkMessage *msg)=0
Method to send a message to a specific client.
uint32_t serial
instance serial to unique identify own instance
Definition: messages.h:129
virtual void bb_interface_data_changed(Interface *interface)
BlackBoard data changed notification.
uint32_t data_size
data for message
Definition: messages.h:179
virtual void bb_interface_reader_removed(Interface *interface, unsigned int instance_serial)
A reading instance has been closed for a watched interface.
static void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: liblogger.cpp:156
Interface data message.
Definition: messages.h:163
Interface message.
Definition: messages.h:173
bool is_writer() const
Check if this is a writing instance.
Definition: interface.cpp:438
void bbil_add_reader_interface(Interface *interface)
Add an interface to the reader addition/removal watch list.
uint32_t serial
interface instance serial
Definition: messages.h:175
virtual void bb_interface_writer_removed(Interface *interface, unsigned int instance_serial)
A writing instance has been closed for a watched interface.
uint32_t serial
instance serial to unique identify this instance
Definition: messages.h:165
virtual void bb_interface_writer_added(Interface *interface, unsigned int instance_serial)
A writing instance has been opened for a watched interface.
const char * bbil_name() const
Get BBIL name.
The BlackBoard abstract class.
Definition: blackboard.h:45
uint32_t data_size
size in bytes of the following data.
Definition: messages.h:166
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
BlackBoardNetHandlerInterfaceListener(BlackBoard *blackboard, Interface *interface, FawkesNetworkHub *hub, unsigned int clid)
Constructor.
BlackBoard interface listener.
uint32_t hops
number of hops this message already passed
Definition: messages.h:178
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.