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 <blackboard/net/interface_listener.h>
25 #include <blackboard/net/messages.h>
26 
27 #include <blackboard/blackboard.h>
28 #include <interface/interface.h>
29 
30 #include <netcomm/fawkes/hub.h>
31 #include <netcomm/fawkes/message.h>
32 #include <netcomm/fawkes/component_ids.h>
33 #include <logging/liblogger.h>
34 
35 #include <cstdlib>
36 #include <cstring>
37 #include <cstdio>
38 #include <arpa/inet.h>
39 
40 namespace fawkes {
41 
42 /** @class BlackBoardNetHandlerInterfaceListener <blackboard/net/interface_listener.h>
43  * Interface listener for network handler.
44  * This class is used by the BlackBoardNetworkHandler to track interface changes and
45  * send out notifications timely.
46  * @author Tim Niemueller
47  */
48 
49 /** Constructor.
50  * @param blackboard local BlackBoard
51  * @param interface interface to care about
52  * @param hub Fawkes network hub to use to send messages
53  * @param clid client ID of the client which opened this interface
54  */
56  Interface *interface,
57  FawkesNetworkHub *hub,
58  unsigned int clid)
59  : BlackBoardInterfaceListener("NetIL/%s", interface->uid())
60 {
61  bbil_add_data_interface(interface);
62  bbil_add_reader_interface(interface);
63  bbil_add_writer_interface(interface);
64  if ( interface->is_writer() ) {
65  bbil_add_message_interface(interface);
66  }
67 
68  __blackboard = blackboard;
69  __interface = interface;
70  __fnh = hub;
71  __clid = clid;
72 
73  __blackboard->register_listener(this);
74 }
75 
76 
77 /** Destructor. */
79 {
80  __blackboard->unregister_listener(this);
81 }
82 
83 
84 void
86 {
87  // send out data changed notification
88  interface->read();
89 
90  size_t payload_size = sizeof(bb_idata_msg_t) + interface->datasize();
91  void *payload = malloc(payload_size);
92  bb_idata_msg_t *dm = (bb_idata_msg_t *)payload;
93  dm->serial = htonl(interface->serial());
94  dm->data_size = htonl(interface->datasize());
95  memcpy((char *)payload + sizeof(bb_idata_msg_t), interface->datachunk(),
96  interface->datasize());
97 
98  try {
99  __fnh->send(__clid, FAWKES_CID_BLACKBOARD, MSG_BB_DATA_CHANGED, payload, payload_size);
100  } catch (Exception &e) {
101  LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard data, exception follows");
103  }
104 }
105 
106 
107 bool
109  Message *message) throw()
110 {
111  // send out interface message
112  size_t payload_size = sizeof(bb_imessage_msg_t) + message->datasize();
113  void *payload = calloc(1, payload_size);
114  bb_imessage_msg_t *dm = (bb_imessage_msg_t *)payload;
115  dm->serial = htonl(interface->serial());
116  strncpy(dm->msg_type, message->type(), __INTERFACE_MESSAGE_TYPE_SIZE);
117  dm->data_size = htonl(message->datasize());
118  dm->msgid = htonl(message->id());
119  dm->hops = htonl(message->hops());
120  memcpy((char *)payload + sizeof(bb_imessage_msg_t), message->datachunk(),
121  message->datasize());
122 
123  try {
124  __fnh->send(__clid, FAWKES_CID_BLACKBOARD, MSG_BB_INTERFACE_MESSAGE, payload, payload_size);
125  } catch (Exception &e) {
126  LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard message, exception follows");
128  }
129 
130  // do not enqueue, we are fine with just sending
131  return false;
132 }
133 
134 
135 void
136 BlackBoardNetHandlerInterfaceListener::send_event_serial(Interface *interface,
137  unsigned int msg_id,
138  unsigned int event_serial)
139 {
141  esm->serial = htonl(interface->serial());
142  esm->event_serial = htonl(event_serial);
143 
144  try {
145  __fnh->send(__clid, FAWKES_CID_BLACKBOARD, msg_id, esm, sizeof(bb_ieventserial_msg_t));
146  } catch (Exception &e) {
147  LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard event serial, exception follows");
149  }
150 }
151 
152 
153 void
155  unsigned int instance_serial) throw()
156 {
157  send_event_serial(interface, MSG_BB_WRITER_ADDED, instance_serial);
158 }
159 
160 
161 void
163  unsigned int instance_serial) throw()
164 {
165  send_event_serial(interface, MSG_BB_WRITER_REMOVED, instance_serial);
166 }
167 
168 
169 void
171  unsigned int instance_serial) throw()
172 {
173  send_event_serial(interface, MSG_BB_READER_ADDED, instance_serial);
174 }
175 
176 
177 void
179  unsigned int instance_serial) throw()
180 {
181  send_event_serial(interface, MSG_BB_READER_REMOVED, instance_serial);
182 }
183 
184 } // end namespace fawkes
uint32_t msgid
message ID
Definition: messages.h:173
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:44
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:124
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:218
char msg_type[__INTERFACE_MESSAGE_TYPE_SIZE]
message type
Definition: messages.h:172
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
uint32_t event_serial
instance serial to unique identify instance that caused the event.
Definition: messages.h:126
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:190
Fawkes Network Hub.
Definition: hub.h:33
Base class for exceptions in Fawkes.
Definition: exception.h:36
unsigned short serial() const
Get instance serial of interface.
Definition: interface.cpp:697
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:125
virtual void bb_interface_data_changed(Interface *interface)
BlackBoard data changed notification.
uint32_t data_size
data for message
Definition: messages.h:175
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:162
Interface data message.
Definition: messages.h:160
Interface message.
Definition: messages.h:170
bool is_writer() const
Check if this is a writing instance.
Definition: interface.cpp:440
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:171
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:161
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:48
uint32_t data_size
size in bytes of the following data.
Definition: messages.h:162
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:174
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.