Wiselib
wiselib.testing/algorithms/routing/static/static_routing_message.h
Go to the documentation of this file.
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 __STATIC_ROUTING_ALGORITHM_MSG_H__
00020 #define __STATIC_ROUTING_ALGORITHM_MSG_H__
00021 
00022 #include "util/serialization/simple_types.h"
00023 
00024 namespace wiselib
00025 {
00026 
00027    template<typename OsModel_P,
00028             typename Radio_P>
00029    class StaticRoutingMessage
00030    {
00031    public:
00032       typedef OsModel_P OsModel;
00033       typedef Radio_P Radio;
00034       typedef typename Radio::node_id_t node_id_t;
00035       typedef typename Radio::block_data_t block_data_t;
00036       typedef typename Radio::size_t size_t;
00037       typedef typename Radio::message_id_t message_id_t;
00038       // --------------------------------------------------------------------
00039       inline StaticRoutingMessage();
00040             // --------------------------------------------------------------------
00041       inline message_id_t msg_id()
00042       { return read<OsModel, block_data_t, message_id_t>( buffer ); };
00043       // --------------------------------------------------------------------
00044       inline void set_msg_id( message_id_t id )
00045       { write<OsModel, block_data_t, message_id_t>( buffer, id ); }
00046       // --------------------------------------------------------------------
00047       inline node_id_t source_id()
00048       { return read<OsModel, block_data_t, node_id_t>(buffer + SOURCE_ID_POS); }
00049       // --------------------------------------------------------------------
00050       inline void set_source_id( node_id_t id )
00051       { write<OsModel, block_data_t, node_id_t>(buffer + SOURCE_ID_POS, id); }
00052       // --------------------------------------------------------------------
00053       inline node_id_t destination()
00054       { return read<OsModel, block_data_t, node_id_t>(buffer + DESTINATION_POS); }
00055       // --------------------------------------------------------------------
00056       inline void set_destination( node_id_t dest )
00057       { write<OsModel, block_data_t, node_id_t>(buffer + DESTINATION_POS, dest); }
00058       // --------------------------------------------------------------------
00059       inline size_t payload_size()
00060       { return read<OsModel, block_data_t, size_t>(buffer + PAYLOAD_POS); }
00061       // --------------------------------------------------------------------
00062       inline block_data_t* payload()
00063       { return buffer + PAYLOAD_POS + sizeof(size_t); }
00064       // --------------------------------------------------------------------
00065       inline void set_payload( size_t len, block_data_t *buf )
00066       {
00067          write<OsModel, block_data_t, size_t>(buffer + PAYLOAD_POS, len);
00068          memcpy( buffer + PAYLOAD_POS + sizeof(size_t), buf, len);
00069       }
00070       // --------------------------------------------------------------------
00071       inline size_t buffer_size()
00072       { return PAYLOAD_POS + sizeof(size_t) + payload_size(); }
00073 
00074    
00075       enum data_positions
00076       {
00077          SOURCE_ID_POS = sizeof(message_id_t),
00078          DESTINATION_POS = SOURCE_ID_POS + sizeof(node_id_t),
00079          PAYLOAD_POS = DESTINATION_POS + sizeof(node_id_t)
00080       };
00081 
00082 private:
00083       block_data_t buffer[Radio::MAX_MESSAGE_LENGTH];
00084    };
00085    // -----------------------------------------------------------------------
00086    template<typename OsModel_P,
00087             typename Radio_P>
00088    StaticRoutingMessage<OsModel_P, Radio_P>::
00089    StaticRoutingMessage()
00090    {
00091       set_msg_id( 0 );
00092       set_source_id( 0 );
00093       size_t len = 0;
00094       write<OsModel, block_data_t, size_t>(buffer + PAYLOAD_POS, len);
00095    }
00096 
00097 }
00098 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines