Wiselib
|
00001 /*************************************************************************** 00002 ** This file is part of the generic algorithm library Wiselib. ** 00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project. ** 00004 ** ** 00005 ** The Wiselib is free software: you can redistribute it and/or modify ** 00006 ** it under the terms of the GNU Lesser General Public License as ** 00007 ** published by the Free Software Foundation, either version 3 of the ** 00008 ** License, or (at your option) any later version. ** 00009 ** ** 00010 ** The Wiselib is distributed in the hope that it will be useful, ** 00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00013 ** GNU Lesser General Public License for more details. ** 00014 ** ** 00015 ** You should have received a copy of the GNU Lesser General Public ** 00016 ** License along with the Wiselib. ** 00017 ** If not, see <http://www.gnu.org/licenses/>. ** 00018 ***************************************************************************/ 00019 #ifndef __UTIL_METRICS_PACKET_SNIFFER_RADIO_H 00020 #define __UTIL_METRICS_PACKET_SNIFFER_RADIO_H 00021 00022 #include "util/delegates/delegate.hpp" 00023 #include "util/serialization/simple_types.h" 00024 #include "util/pstl/map_static_vector.h" 00025 00026 00027 namespace wiselib { 00028 00034 template<typename OsModel_P, 00035 typename Statistics_Map_P, 00036 typename Radio_P = typename OsModel_P::Radio, 00037 typename Debug_P = typename OsModel_P::Debug> 00038 class PacketSnifferRadioModel { 00039 public: 00040 typedef OsModel_P OsModel; 00041 typedef Radio_P Radio; 00042 typedef Debug_P Debug; 00043 typedef Statistics_Map_P Statistics_map; 00044 typedef PacketSnifferRadioModel <OsModel, Statistics_map, Radio, Debug> self_type; 00045 typedef self_type* self_pointer_t; 00046 00047 typedef typename Radio::node_id_t node_id_t; 00048 typedef typename Radio::size_t size_t; 00049 typedef typename Radio::block_data_t block_data_t; 00050 typedef typename Radio::message_id_t message_id_t; 00051 00052 typedef typename Radio::radio_delegate_t radio_delegate_t; 00053 // -------------------------------------------------------------------- 00054 enum SpecialNodeIds { 00055 BROADCAST_ADDRESS = Radio::BROADCAST_ADDRESS, 00056 NULL_NODE_ID = Radio::NULL_NODE_ID 00057 }; 00058 // -------------------------------------------------------------------- 00059 enum Restrictions { 00060 MAX_MESSAGE_LENGTH = Radio::MAX_MESSAGE_LENGTH 00061 }; 00062 // -------------------------------------------------------------------- 00063 void 00064 send(node_id_t id, size_t len, block_data_t *data) 00065 { 00066 00067 radio().send( id, len, data ); 00068 } 00069 // -------------------------------------------------------------------- 00070 void init( Radio& radio,Debug& debug,message_id_t sniffer_filter ) 00071 { 00072 radio_ = &radio; 00073 debug_ = &debug; 00074 sniffer_filter_ = sniffer_filter; 00075 sniffer_filter_set_ = true; 00076 } 00077 // -------------------------------------------------------------------- 00078 void init( Radio& radio,Debug& debug) 00079 { 00080 radio_ = &radio; 00081 debug_ = &debug; 00082 remove_filter(); 00083 } 00084 // -------------------------------------------------------------------- 00085 void destruct() 00086 {} 00087 // -------------------------------------------------------------------- 00088 void enable_radio() 00089 { 00090 callback_id_ = radio().template reg_recv_callback<self_type, &self_type::receive_radio_message>(this); 00091 radio().enable_radio(); 00092 } 00093 // -------------------------------------------------------------------- 00094 void disable_radio() 00095 { 00096 radio().unreg_recv_callback( callback_id_ ); 00097 radio().disable_radio(); 00098 } 00099 // -------------------------------------------------------------------- 00100 node_id_t id() 00101 { 00102 return radio().id(); 00103 } 00104 // -------------------------------------------------------------------- 00105 template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)> 00106 int reg_recv_callback( T *obj_pnt ) 00107 { 00108 callback_ = radio_delegate_t::template from_method<T, TMethod>( obj_pnt ); 00109 return -1; 00110 } 00111 // -------------------------------------------------------------------- 00112 void unreg_recv_callback( int idx ) 00113 { 00114 radio().unreg_recv_callback( idx ); 00115 } 00116 // -------------------------------------------------------------------- 00117 void 00118 receive_radio_message( 00119 typename Radio::node_id_t source, 00120 typename Radio::size_t length, 00121 typename Radio::block_data_t *buf) 00122 { 00123 message_id_t message_id = read<OsModel, block_data_t, message_id_t>( buf ); 00124 if(!sniffer_filter_set_ || message_id == sniffer_filter_) 00125 { 00126 if ( stat_map.find(source) == stat_map.end() ) 00127 stat_map[source] = 0; 00128 stat_map[source]++; 00129 debug().debug("SNIFFER: Node %d received message from node %d of type %d received %d messages from this node\n", 00130 id(),source, message_id, stat_map[source]); 00131 } 00132 if(callback_) 00133 callback_( source, length, buf ); 00134 } 00135 // -------------------------------------------------------------------- 00136 void set_sniffer_filter(message_id_t type) 00137 { 00138 sniffer_filter_ = type; 00139 sniffer_filter_set_ = true; 00140 } 00141 // -------------------------------------------------------------------- 00142 void remove_filter() 00143 { 00144 sniffer_filter_set_ = false; 00145 } 00146 private: 00147 Radio& radio() 00148 { return *radio_; } 00149 00150 Debug& debug() 00151 { return *debug_; } 00152 00153 Debug* debug_; 00154 00155 Radio *radio_; 00156 00157 message_id_t sniffer_filter_; 00158 00159 bool sniffer_filter_set_; 00160 00161 Statistics_map stat_map; 00162 00163 radio_delegate_t callback_; 00164 00165 int callback_id_; 00166 00167 }; 00168 00169 } 00170 00171 #endif