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 __WISELIB_ALGORITHMS_BGU_CLUSTERING_MESSAGE_SERIALIZATION_H 00020 #define __WISELIB_ALGORITHMS_BGU_CLUSTERING_MESSAGE_SERIALIZATION_H 00021 00022 #include "algorithms/bgu_clustering/TopologyMessage.h" 00023 #include "util/serialization/serialization.h" 00024 #include "util/serialization/simple_types.h" 00025 #include "util/serialization/endian.h" 00026 00027 namespace wiselib 00028 { 00029 00030 template<typename OsModel_P, 00031 typename BlockData_P> 00032 class Serialization<OsModel_P, WISELIB_BIG_ENDIAN, BlockData_P, 00033 TopologyMessage::topology_message_header_t > 00034 { 00035 public: 00036 typedef OsModel_P OsModel; 00037 typedef BlockData_P BlockData; 00038 typedef TopologyMessage::topology_message_header_t Type; 00039 00040 typedef typename Type::message_id_t message_id_t; 00041 typedef typename Type::num_rec_t num_rec_t; 00042 typedef typename Type::node_id_t node_id_t; 00043 00044 typedef typename OsModel::size_t size_t; 00045 // -------------------------------------------------------------------- 00046 enum data_positions 00047 { 00048 MSG_ID_POS = 0, 00049 NUM_REC_POS = sizeof(message_id_t), 00050 NODE_ID_POS = sizeof(message_id_t) + sizeof(num_rec_t) 00051 }; 00052 // -------------------------------------------------------------------- 00053 static inline Type read( BlockData *target ) 00054 { 00055 Type x; 00056 read( target, x ); 00057 return x; 00058 } 00059 // -------------------------------------------------------------------- 00060 static inline void read( BlockData *target, Type& value ) 00061 { 00062 value.msgid = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, message_id_t>::read( 00063 target ); 00064 value.num_records = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, num_rec_t>::read( 00065 target + NUM_REC_POS ); 00066 value.sender = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, node_id_t>::read( 00067 target + NODE_ID_POS ); 00068 } 00069 // -------------------------------------------------------------------- 00070 static inline size_t write( BlockData *target, Type& value ) 00071 { 00072 size_t written_bytes = 0; 00073 written_bytes += Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, message_id_t>::write( 00074 target, value.msgid ); 00075 written_bytes += Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, num_rec_t>::write( 00076 target + NUM_REC_POS, value.num_records ); 00077 written_bytes += Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, node_id_t>::write( 00078 target + NODE_ID_POS, value.sender ); 00079 00080 return written_bytes; 00081 } 00082 }; 00083 // ----------------------------------------------------------------------- 00084 // ----------------------------------------------------------------------- 00085 // ----------------------------------------------------------------------- 00086 template<typename OsModel_P, 00087 typename BlockData_P> 00088 class Serialization<OsModel_P, WISELIB_LITTLE_ENDIAN, BlockData_P, 00089 TopologyMessage::topology_message_header_t > 00090 { 00091 public: 00092 typedef OsModel_P OsModel; 00093 typedef BlockData_P BlockData; 00094 typedef TopologyMessage::topology_message_header_t Type; 00095 00096 typedef typename Type::message_id_t message_id_t; 00097 typedef typename Type::num_rec_t num_rec_t; 00098 typedef typename Type::node_id_t node_id_t; 00099 00100 typedef typename OsModel::size_t size_t; 00101 // -------------------------------------------------------------------- 00102 enum data_positions 00103 { 00104 MSG_ID_POS = 0, 00105 NUM_REC_POS = sizeof(message_id_t), 00106 NODE_ID_POS = sizeof(message_id_t) + sizeof(num_rec_t) 00107 }; 00108 // -------------------------------------------------------------------- 00109 static inline Type read( BlockData *target ) 00110 { 00111 Type x; 00112 read( target, x ); 00113 return x; 00114 } 00115 // -------------------------------------------------------------------- 00116 static inline void read( BlockData *target, Type& value ) 00117 { 00118 value.msgid = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, message_id_t>::read( 00119 target ); 00120 value.num_records = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, num_rec_t>::read( 00121 target + NUM_REC_POS ); 00122 value.sender = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, node_id_t>::read( 00123 target + NODE_ID_POS ); 00124 } 00125 // -------------------------------------------------------------------- 00126 static inline size_t write( BlockData *target, Type& value ) 00127 { 00128 size_t written_bytes = 0; 00129 written_bytes += Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, message_id_t>::write( 00130 target, value.msgid ); 00131 written_bytes += Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, num_rec_t>::write( 00132 target + NUM_REC_POS, value.num_records ); 00133 written_bytes += Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, node_id_t>::write( 00134 target + NODE_ID_POS, value.sender ); 00135 00136 return written_bytes; 00137 } 00138 }; 00139 00140 // ----------------------------------------------------------------------- 00141 // ----------------------------------------------------------------------- 00142 // ----------------------------------------------------------------------- 00143 00144 template<typename OsModel_P, 00145 typename BlockData_P> 00146 class Serialization<OsModel_P, WISELIB_BIG_ENDIAN, BlockData_P, 00147 TopologyMessage::serializable_topology_record_t > 00148 { 00149 public: 00150 typedef OsModel_P OsModel; 00151 typedef BlockData_P BlockData; 00152 typedef TopologyMessage::serializable_topology_record_t Type; 00153 00154 typedef typename Type::node_id_t node_id_t; 00155 00156 typedef typename OsModel::size_t size_t; 00157 // -------------------------------------------------------------------- 00158 enum data_positions 00159 { 00160 NODE_ID_POS = 0, 00161 DISTANCE_POS = sizeof(node_id_t), 00162 IS_LEADER_POS = sizeof(node_id_t) + 1, 00163 LEADER_POS = sizeof(node_id_t) + 2, 00164 PARENT_POS = sizeof(node_id_t) + 2 + sizeof(node_id_t) 00165 }; 00166 // -------------------------------------------------------------------- 00167 static inline Type read( BlockData *target ) 00168 { 00169 Type x; 00170 read( target, x ); 00171 return x; 00172 } 00173 // -------------------------------------------------------------------- 00174 static inline void read( BlockData *target, Type& value ) 00175 { 00176 value.nodeid = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, node_id_t>::read( 00177 target ); 00178 value.distance = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, uint8_t>::read( 00179 target + DISTANCE_POS ); 00180 value.is_leader = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, bool>::read( 00181 target + IS_LEADER_POS ); 00182 value.leader = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, node_id_t>::read( 00183 target + LEADER_POS ); 00184 value.parent = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, node_id_t>::read( 00185 target + PARENT_POS ); 00186 } 00187 // -------------------------------------------------------------------- 00188 static inline size_t write( BlockData *target, Type& value ) 00189 { 00190 size_t written_bytes = 0; 00191 written_bytes += Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, node_id_t>::write( 00192 target, value.nodeid ); 00193 written_bytes += Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, uint8_t>::write( 00194 target + DISTANCE_POS, value.distance ); 00195 written_bytes += Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, bool>::write( 00196 target + IS_LEADER_POS, value.is_leader ); 00197 written_bytes += Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, node_id_t>::write( 00198 target + LEADER_POS, value.leader ); 00199 written_bytes += Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, node_id_t>::write( 00200 target + PARENT_POS, value.parent ); 00201 00202 return written_bytes; 00203 } 00204 }; 00205 // ----------------------------------------------------------------------- 00206 // ----------------------------------------------------------------------- 00207 // ----------------------------------------------------------------------- 00208 template<typename OsModel_P, 00209 typename BlockData_P> 00210 class Serialization<OsModel_P, WISELIB_LITTLE_ENDIAN, BlockData_P, 00211 TopologyMessage::serializable_topology_record_t > 00212 { 00213 public: 00214 typedef OsModel_P OsModel; 00215 typedef BlockData_P BlockData; 00216 typedef TopologyMessage::serializable_topology_record_t Type; 00217 00218 typedef typename Type::node_id_t node_id_t; 00219 00220 typedef typename OsModel::size_t size_t; 00221 // -------------------------------------------------------------------- 00222 enum data_positions 00223 { 00224 NODE_ID_POS = 0, 00225 DISTANCE_POS = sizeof(node_id_t), 00226 IS_LEADER_POS = sizeof(node_id_t) + 1, 00227 LEADER_POS = sizeof(node_id_t) + 2, 00228 PARENT_POS = sizeof(node_id_t) + 2 + sizeof(node_id_t) 00229 }; 00230 // -------------------------------------------------------------------- 00231 static inline Type read( BlockData *target ) 00232 { 00233 Type x; 00234 read( target, x ); 00235 return x; 00236 } 00237 // -------------------------------------------------------------------- 00238 static inline void read( BlockData *target, Type& value ) 00239 { 00240 value.nodeid = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, node_id_t>::read( 00241 target ); 00242 value.distance = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, uint8_t>::read( 00243 target + DISTANCE_POS ); 00244 value.is_leader = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, bool>::read( 00245 target + IS_LEADER_POS ); 00246 value.leader = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, node_id_t>::read( 00247 target + LEADER_POS ); 00248 value.parent = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, node_id_t>::read( 00249 target + PARENT_POS ); 00250 } 00251 // -------------------------------------------------------------------- 00252 static inline size_t write( BlockData *target, Type& value ) 00253 { 00254 size_t written_bytes = 0; 00255 written_bytes += Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, node_id_t>::write( 00256 target, value.nodeid ); 00257 written_bytes += Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, uint8_t>::write( 00258 target + DISTANCE_POS, value.distance ); 00259 written_bytes += Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, bool>::write( 00260 target + IS_LEADER_POS, value.is_leader ); 00261 written_bytes += Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, node_id_t>::write( 00262 target + LEADER_POS, value.leader ); 00263 written_bytes += Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, node_id_t>::write( 00264 target + PARENT_POS, value.parent ); 00265 00266 return written_bytes; 00267 } 00268 }; 00269 00270 } 00271 00272 #endif