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