Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN). |
| 6 | * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors. |
| 7 | * |
| 8 | * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Zhiyi Zhang <dreamerbarrychang@gmail.com> |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_GEP_REPETITIVE_INTERVAL_HPP |
| 23 | #define NDN_GEP_REPETITIVE_INTERVAL_HPP |
| 24 | |
| 25 | #include "common.hpp" |
| 26 | #include "interval.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace gep { |
| 30 | |
| 31 | ///@brief An advanced interval which can have a repeat pattern and repeat unit |
| 32 | class RepetitiveInterval |
| 33 | { |
| 34 | public: |
| 35 | enum class RepeatUnit{ |
| 36 | NONE = 0, |
| 37 | DAY = 1, |
| 38 | MONTH = 2, |
| 39 | YEAR = 3 |
| 40 | }; |
| 41 | |
| 42 | public: |
| 43 | RepetitiveInterval(); |
| 44 | |
| 45 | explicit |
| 46 | RepetitiveInterval(const Block& block); |
| 47 | |
| 48 | /** |
| 49 | * @brief Construction to create an object |
| 50 | * @pre @p startDate <= @p endDate |
| 51 | * @pre @p intervalStartHour and @p intervalEndHour can be [0, 24] |
| 52 | * @pre @p intervalStartHour < @p intervalEndHour |
| 53 | * @pre when @p unit = NONE, then @p startDate == @p endDate |
| 54 | */ |
| 55 | RepetitiveInterval(const TimeStamp& startDate, |
| 56 | const TimeStamp& endDate, |
| 57 | size_t intervalStartHour, |
| 58 | size_t intervalEndHour, |
| 59 | size_t nRepeats = 0, |
| 60 | RepeatUnit unit = RepeatUnit::NONE); |
| 61 | |
| 62 | template<encoding::Tag TAG> |
| 63 | size_t |
| 64 | wireEncode(EncodingImpl<TAG>& encoder) const; |
| 65 | |
| 66 | const Block& |
| 67 | wireEncode() const; |
| 68 | |
| 69 | void |
| 70 | wireDecode(const Block& wire); |
| 71 | |
| 72 | /** |
| 73 | * @brief Get get an interval that @p tp falls in |
| 74 | * |
| 75 | * @parameter tp A timestamp |
| 76 | * |
| 77 | * @return bool If the repetitive interval covers the @p tp, return true, otherwise false |
| 78 | * @return Interval Return the interval which @tp falls in |
| 79 | */ |
| 80 | std::tuple<bool, Interval> |
| 81 | getInterval(const TimeStamp& tp) const; |
| 82 | |
| 83 | /** |
| 84 | * @brief To store in std::set, class have to implement operator < |
| 85 | * |
| 86 | * @parameter interval Interval which will be compared with |
| 87 | */ |
| 88 | bool |
| 89 | operator<(const RepetitiveInterval& interval) const; |
| 90 | |
| 91 | const TimeStamp& |
| 92 | getStartDate() const |
| 93 | { |
| 94 | return m_startDate; |
| 95 | } |
| 96 | |
| 97 | const TimeStamp& |
| 98 | getEndDate() const |
| 99 | { |
| 100 | return m_endDate; |
| 101 | } |
| 102 | |
| 103 | size_t |
| 104 | getIntervalStartHour() const |
| 105 | { |
| 106 | return m_intervalStartHour; |
| 107 | } |
| 108 | |
| 109 | size_t |
| 110 | getIntervalEndHour() const |
| 111 | { |
| 112 | return m_intervalEndHour; |
| 113 | } |
| 114 | |
| 115 | size_t |
| 116 | getNRepeats() const |
| 117 | { |
| 118 | return m_nRepeats; |
| 119 | } |
| 120 | |
| 121 | RepeatUnit |
| 122 | getRepeatUnit() const |
| 123 | { |
| 124 | return m_unit; |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | ///@brief Check if there is any interval in the date of timestamp |
| 129 | bool |
| 130 | hasIntervalOnDate(const TimeStamp& tp) const; |
| 131 | |
| 132 | TimeStamp m_startDate; |
| 133 | TimeStamp m_endDate; |
| 134 | size_t m_intervalStartHour; |
| 135 | size_t m_intervalEndHour; |
| 136 | size_t m_nRepeats; |
| 137 | RepeatUnit m_unit; |
| 138 | |
| 139 | mutable Block m_wire; |
| 140 | }; |
| 141 | |
| 142 | } // namespace gep |
| 143 | } // namespace ndn |
| 144 | |
| 145 | #endif // NDN_GEP_REPETITIVE_INTERVAL_HPP |