Alexander Afanasyev | 360f41f | 2016-12-25 13:20:21 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
| 6 | * |
| 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #ifndef CHRONOSHARE_CORE_RANDOM_INTERVAL_GENERATOR_HPP |
| 22 | #define CHRONOSHARE_CORE_RANDOM_INTERVAL_GENERATOR_HPP |
| 23 | |
| 24 | #include "interval-generator.hpp" |
| 25 | |
| 26 | #include <boost/date_time/posix_time/posix_time_types.hpp> |
| 27 | #include <boost/random/mersenne_twister.hpp> |
| 28 | #include <boost/random/uniform_real.hpp> |
| 29 | #include <boost/random/variate_generator.hpp> |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace chronoshare { |
| 33 | |
| 34 | // generates intervals with uniform distribution |
| 35 | class RandomIntervalGenerator : public IntervalGenerator |
| 36 | { |
| 37 | public: |
| 38 | enum class Direction { |
| 39 | UP = 1, |
| 40 | DOWN = 2, |
| 41 | EVEN = 3 |
| 42 | }; |
| 43 | |
| 44 | public: |
| 45 | // percent is random-range/interval; e.g. if interval is 10 and you wish the random-range to be 2 |
| 46 | // e.g. 9 ~ 11, percent = 0.2 |
| 47 | // direction shifts the random range; e.g. in the above example, UP would produce a range of |
| 48 | // 10 ~ 12, DOWN of 8 ~ 10, and EVEN of 9 ~ 11 |
| 49 | RandomIntervalGenerator(double interval, double percent, Direction direction = Direction::EVEN) |
| 50 | // : m_rng(time(NULL)) |
| 51 | : m_rng(static_cast<int>( |
| 52 | boost::posix_time::microsec_clock::local_time().time_of_day().total_nanoseconds())), |
| 53 | m_dist(0.0, fractional(percent)), |
| 54 | m_random(m_rng, m_dist), |
| 55 | m_direction(direction), |
| 56 | m_percent(percent), |
| 57 | m_interval(interval) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | virtual ~RandomIntervalGenerator() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | virtual double |
| 66 | nextInterval() |
| 67 | { |
| 68 | double percent = m_random(); |
| 69 | double interval = m_interval; |
| 70 | switch (m_direction) { |
| 71 | case Direction::UP: |
| 72 | interval = m_interval * (1.0 + percent); |
| 73 | break; |
| 74 | case Direction::DOWN: |
| 75 | interval = m_interval * (1.0 - percent); |
| 76 | break; |
| 77 | case Direction::EVEN: |
| 78 | interval = m_interval * (1.0 - m_percent / 2.0 + percent); |
| 79 | break; |
| 80 | default: |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | return interval; |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | inline double |
| 89 | fractional(double x) |
| 90 | { |
| 91 | double dummy; |
| 92 | return std::abs<double>(modf(x, &dummy)); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | typedef boost::mt19937 RNG_TYPE; |
| 97 | RNG_TYPE m_rng; |
| 98 | boost::uniform_real<> m_dist; |
| 99 | boost::variate_generator<RNG_TYPE&, boost::uniform_real<>> m_random; |
| 100 | Direction m_direction; |
| 101 | double m_percent; |
| 102 | double m_interval; |
| 103 | }; |
| 104 | |
| 105 | } // chronoshare |
| 106 | } // ndn |
| 107 | |
| 108 | #endif // CHRONOSHARE_CORE_RANDOM_INTERVAL_GENERATOR_HPP |