Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * net_interface_listener.cpp - BlackBoard interface listener for net handler 00004 * 00005 * Created: Tue Mar 04 17:53:32 2008 00006 * Copyright 2007-2008 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <blackboard/net/interface_listener.h> 00025 #include <blackboard/net/messages.h> 00026 00027 #include <blackboard/blackboard.h> 00028 #include <interface/interface.h> 00029 00030 #include <netcomm/fawkes/hub.h> 00031 #include <netcomm/fawkes/message.h> 00032 #include <netcomm/fawkes/component_ids.h> 00033 #include <logging/liblogger.h> 00034 00035 #include <cstdlib> 00036 #include <cstring> 00037 #include <cstdio> 00038 #include <arpa/inet.h> 00039 00040 namespace fawkes { 00041 00042 /** @class BlackBoardNetHandlerInterfaceListener <blackboard/net/interface_listener.h> 00043 * Interface listener for network handler. 00044 * This class is used by the BlackBoardNetworkHandler to track interface changes and 00045 * send out notifications timely. 00046 * @author Tim Niemueller 00047 */ 00048 00049 /** Constructor. 00050 * @param blackboard local BlackBoard 00051 * @param interface interface to care about 00052 * @param hub Fawkes network hub to use to send messages 00053 * @param clid client ID of the client which opened this interface 00054 */ 00055 BlackBoardNetHandlerInterfaceListener::BlackBoardNetHandlerInterfaceListener(BlackBoard *blackboard, 00056 Interface *interface, 00057 FawkesNetworkHub *hub, 00058 unsigned int clid) 00059 : BlackBoardInterfaceListener("NetIL/%s", interface->uid()) 00060 { 00061 bbil_add_data_interface(interface); 00062 bbil_add_reader_interface(interface); 00063 bbil_add_writer_interface(interface); 00064 if ( interface->is_writer() ) { 00065 bbil_add_message_interface(interface); 00066 } 00067 00068 __blackboard = blackboard; 00069 __interface = interface; 00070 __fnh = hub; 00071 __clid = clid; 00072 00073 __blackboard->register_listener(this); 00074 } 00075 00076 00077 /** Destructor. */ 00078 BlackBoardNetHandlerInterfaceListener::~BlackBoardNetHandlerInterfaceListener() 00079 { 00080 __blackboard->unregister_listener(this); 00081 } 00082 00083 00084 void 00085 BlackBoardNetHandlerInterfaceListener::bb_interface_data_changed(Interface *interface) throw() 00086 { 00087 // send out data changed notification 00088 interface->read(); 00089 00090 size_t payload_size = sizeof(bb_idata_msg_t) + interface->datasize(); 00091 void *payload = malloc(payload_size); 00092 bb_idata_msg_t *dm = (bb_idata_msg_t *)payload; 00093 dm->serial = htonl(interface->serial()); 00094 dm->data_size = htonl(interface->datasize()); 00095 memcpy((char *)payload + sizeof(bb_idata_msg_t), interface->datachunk(), 00096 interface->datasize()); 00097 00098 try { 00099 __fnh->send(__clid, FAWKES_CID_BLACKBOARD, MSG_BB_DATA_CHANGED, payload, payload_size); 00100 } catch (Exception &e) { 00101 LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard data, exception follows"); 00102 LibLogger::log_warn(bbil_name(), e); 00103 } 00104 } 00105 00106 00107 bool 00108 BlackBoardNetHandlerInterfaceListener::bb_interface_message_received(Interface *interface, 00109 Message *message) throw() 00110 { 00111 // send out interface message 00112 size_t payload_size = sizeof(bb_imessage_msg_t) + message->datasize(); 00113 void *payload = calloc(1, payload_size); 00114 bb_imessage_msg_t *dm = (bb_imessage_msg_t *)payload; 00115 dm->serial = htonl(interface->serial()); 00116 strncpy(dm->msg_type, message->type(), __INTERFACE_MESSAGE_TYPE_SIZE); 00117 dm->data_size = htonl(message->datasize()); 00118 dm->msgid = htonl(message->id()); 00119 dm->hops = htonl(message->hops()); 00120 memcpy((char *)payload + sizeof(bb_imessage_msg_t), message->datachunk(), 00121 message->datasize()); 00122 00123 try { 00124 __fnh->send(__clid, FAWKES_CID_BLACKBOARD, MSG_BB_INTERFACE_MESSAGE, payload, payload_size); 00125 } catch (Exception &e) { 00126 LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard message, exception follows"); 00127 LibLogger::log_warn(bbil_name(), e); 00128 } 00129 00130 // do not enqueue, we are fine with just sending 00131 return false; 00132 } 00133 00134 00135 void 00136 BlackBoardNetHandlerInterfaceListener::send_event_serial(Interface *interface, 00137 unsigned int msg_id, 00138 unsigned int event_serial) 00139 { 00140 bb_ieventserial_msg_t *esm = (bb_ieventserial_msg_t *)malloc(sizeof(bb_ieventserial_msg_t)); 00141 esm->serial = htonl(interface->serial()); 00142 esm->event_serial = htonl(event_serial); 00143 00144 try { 00145 __fnh->send(__clid, FAWKES_CID_BLACKBOARD, msg_id, esm, sizeof(bb_ieventserial_msg_t)); 00146 } catch (Exception &e) { 00147 LibLogger::log_warn(bbil_name(), "Failed to send BlackBoard event serial, exception follows"); 00148 LibLogger::log_warn(bbil_name(), e); 00149 } 00150 } 00151 00152 00153 void 00154 BlackBoardNetHandlerInterfaceListener::bb_interface_writer_added(Interface *interface, 00155 unsigned int instance_serial) throw() 00156 { 00157 send_event_serial(interface, MSG_BB_WRITER_ADDED, instance_serial); 00158 } 00159 00160 00161 void 00162 BlackBoardNetHandlerInterfaceListener::bb_interface_writer_removed(Interface *interface, 00163 unsigned int instance_serial) throw() 00164 { 00165 send_event_serial(interface, MSG_BB_WRITER_REMOVED, instance_serial); 00166 } 00167 00168 00169 void 00170 BlackBoardNetHandlerInterfaceListener::bb_interface_reader_added(Interface *interface, 00171 unsigned int instance_serial) throw() 00172 { 00173 send_event_serial(interface, MSG_BB_READER_ADDED, instance_serial); 00174 } 00175 00176 00177 void 00178 BlackBoardNetHandlerInterfaceListener::bb_interface_reader_removed(Interface *interface, 00179 unsigned int instance_serial) throw() 00180 { 00181 send_event_serial(interface, MSG_BB_READER_REMOVED, instance_serial); 00182 } 00183 00184 } // end namespace fawkes