Wiselib
wiselib.testing/external_interface/shawn/shawn_distance.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 __INTERNAL_INTERFACE_SHAWN_DISTANCE__
00020 #define __INTERNAL_INTERFACE_SHAWN_DISTANCE__
00021 
00022 #include "external_interface/shawn/shawn_types.h"
00023 #include "sys/node.h"
00024 #include "sys/processor.h"
00025 #include "sys/world.h"
00026 #include "sys/simulation/simulation_controller.h"
00027 #include "sys/node_distance_estimate.h"
00028 #include "sys/simulation/simulation_environment.h"
00029 #include "sys/distance_estimates/distance_estimate_keeper.h"
00030 #include "sys/misc/random/random_variable_keeper.h"
00031 #include "sys/misc/random/uniform_random_variable.h"
00032 #include "sys/taggings/node_reference_tag.h"
00033 #include <limits>
00034 
00035 namespace wiselib
00036 {
00037 
00038    template<typename OsModel_P,
00039             typename Radio_P = typename OsModel_P::Radio>
00040    class ShawnDistanceModel
00041    {
00042    public:
00043       typedef OsModel_P OsModel;
00044 
00045       typedef Radio_P Radio;
00046       typedef typename Radio::node_id_t node_id_t;
00047 
00048       typedef ShawnDistanceModel<OsModel> self_type;
00049       typedef self_type* self_pointer_t;
00050       // --------------------------------------------------------------------
00051       typedef double distance_t;
00052       typedef distance_t value_t;
00053       static value_t UNKNOWN_DISTANCE;
00054       // --------------------------------------------------------------------
00055       enum ErrorCodes
00056       {
00057          SUCCESS = OsModel::SUCCESS,
00058          ERR_UNSPEC = OsModel::ERR_UNSPEC
00059       };
00060       // --------------------------------------------------------------------
00061       enum
00062       {
00063          READY = OsModel::READY,
00064          NO_VALUE = OsModel::NO_VALUE,
00065          INACTIVE = OsModel::INACTIVE
00066       };
00067       // --------------------------------------------------------------------
00068       ShawnDistanceModel( ShawnOs& os )
00069          : os_(os)
00070       {
00071          const shawn::SimulationEnvironment& se = os_.proc->owner().world().simulation_controller().environment();
00072          std::string dist_est_name = se.required_string_param( "est_dist" );
00073          dist_est_ = os_.proc->owner().world().simulation_controller().distance_estimate_keeper().find( dist_est_name );
00074          assert( dist_est_ != 0 );
00075       }
00076       // --------------------------------------------------------------------
00077       int init( Radio& )
00078       {
00079          return SUCCESS;
00080       }
00081       // --------------------------------------------------------------------
00082       int state()
00083       {
00084          return READY;
00085       }
00086       // --------------------------------------------------------------------
00087       distance_t operator()( node_id_t to )
00088       {
00089          return distance( to );
00090       }
00091       // --------------------------------------------------------------------
00094       distance_t distance( node_id_t to )
00095       {
00096          distance_t distance;
00097          shawn::Node *src = &os_.proc->owner_w();
00098          shawn::Node *dest = os_.proc->owner_w().world_w().find_node_by_id_w( to );
00099          assert( dest != 0 );
00100          dist_est_->estimate_distance( *src, *dest, distance );
00101          // std::cout << "calculated distance: " << distance << std::endl;
00102 
00103          return distance;
00104       };
00105 
00106    private:
00107       ShawnOs& os()
00108       { return os_; }
00109       // --------------------------------------------------------------------
00110       ShawnOs& os_;
00111       shawn::ConstNodeDistanceEstimateHandle dist_est_;
00112    };
00113    // -----------------------------------------------------------------------
00114    // -----------------------------------------------------------------------
00115    // -----------------------------------------------------------------------
00116    template<typename OsModel_P,
00117             typename Radio_P>
00118    typename ShawnDistanceModel<OsModel_P, Radio_P>::value_t
00119       ShawnDistanceModel<OsModel_P, Radio_P>::UNKNOWN_DISTANCE =
00120            std::numeric_limits<typename ShawnDistanceModel<OsModel_P, Radio_P>::distance_t>::max();
00121 
00122 }
00123 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines