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 | #include "schedule.hpp" |
| 23 | #include "tlv.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 26 | #include <ndn-cxx/util/concepts.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace gep { |
| 30 | |
Zhiyi Zhang | 2f9efa6 | 2015-11-23 19:02:28 +0800 | [diff] [blame] | 31 | /** |
| 32 | * @brief Helper functon to calculate black interval results or white interval results |
| 33 | * @p list The RepetitiveInterval list, which can be white list or the black list |
| 34 | * @p tp The timestamp |
| 35 | * @p positiveR The positive result |
| 36 | * @p negativeR The negative result |
| 37 | */ |
| 38 | static void |
| 39 | calIntervalResult(const std::set<RepetitiveInterval>& list, const TimeStamp& ts, |
| 40 | Interval& positiveR, Interval& negativeR) |
| 41 | { |
| 42 | Interval tempInterval; |
| 43 | bool isPositive; |
| 44 | |
| 45 | for (const RepetitiveInterval& element : list) { |
| 46 | std::tie(isPositive, tempInterval) = element.getInterval(ts); |
| 47 | if (isPositive == true) { |
| 48 | positiveR || tempInterval; |
| 49 | } |
| 50 | else { |
| 51 | if (!negativeR.isValid()) |
| 52 | negativeR = tempInterval; |
| 53 | else |
| 54 | negativeR && tempInterval; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 59 | BOOST_CONCEPT_ASSERT((WireEncodable<Schedule>)); |
| 60 | BOOST_CONCEPT_ASSERT((WireDecodable<Schedule>)); |
| 61 | |
| 62 | Schedule::Schedule() = default; |
| 63 | |
| 64 | Schedule::Schedule(const Block& block) |
| 65 | { |
| 66 | wireDecode(block); |
| 67 | } |
| 68 | |
| 69 | template<encoding::Tag TAG> |
| 70 | size_t |
| 71 | Schedule::wireEncode(EncodingImpl<TAG>& encoder) const |
| 72 | { |
| 73 | size_t totalLength = 0; |
| 74 | size_t blackLength = 0; |
| 75 | size_t whiteLength = 0; |
| 76 | |
| 77 | // encode the blackIntervalList as an embed TLV structure |
Yingdi Yu | d263c8a | 2016-03-09 16:53:19 -0800 | [diff] [blame^] | 78 | for (auto it = m_blackIntervalList.rbegin(); it != m_blackIntervalList.rend(); it++) { |
| 79 | blackLength += encoder.prependBlock(it->wireEncode()); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 80 | } |
| 81 | blackLength += encoder.prependVarNumber(blackLength); |
| 82 | blackLength += encoder.prependVarNumber(tlv::BlackIntervalList); |
| 83 | |
| 84 | // encode the whiteIntervalList as an embed TLV structure |
Yingdi Yu | d263c8a | 2016-03-09 16:53:19 -0800 | [diff] [blame^] | 85 | for (auto it = m_whiteIntervalList.rbegin(); it != m_whiteIntervalList.rend(); it++) { |
| 86 | whiteLength += encoder.prependBlock(it->wireEncode()); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 87 | } |
| 88 | whiteLength += encoder.prependVarNumber(whiteLength); |
| 89 | whiteLength += encoder.prependVarNumber(tlv::WhiteIntervalList); |
| 90 | |
| 91 | totalLength = whiteLength + blackLength; |
| 92 | totalLength += encoder.prependVarNumber(totalLength); |
| 93 | totalLength += encoder.prependVarNumber(tlv::Schedule); |
| 94 | |
| 95 | return totalLength; |
| 96 | } |
| 97 | |
| 98 | const Block& |
| 99 | Schedule::wireEncode() const |
| 100 | { |
| 101 | if (m_wire.hasWire()) |
| 102 | return m_wire; |
| 103 | |
| 104 | EncodingEstimator estimator; |
| 105 | size_t estimatedSize = wireEncode(estimator); |
| 106 | |
| 107 | EncodingBuffer buffer(estimatedSize, 0); |
| 108 | wireEncode(buffer); |
| 109 | |
| 110 | this->m_wire = buffer.block(); |
| 111 | return m_wire; |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | Schedule::wireDecode(const Block& wire) |
| 116 | { |
| 117 | if (wire.type() != tlv::Schedule) |
| 118 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding RepetitiveInterval")); |
| 119 | |
| 120 | m_wire = wire; |
| 121 | m_wire.parse(); |
| 122 | |
| 123 | if (m_wire.elements_size() != 2) |
| 124 | BOOST_THROW_EXCEPTION(tlv::Error("RepetitiveInterval tlv does not have two sub-TLVs")); |
| 125 | |
| 126 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 127 | |
| 128 | if (it != m_wire.elements_end() && it->type() == tlv::WhiteIntervalList) { |
| 129 | it->parse(); |
| 130 | Block::element_const_iterator tempIt = it->elements_begin(); |
| 131 | while (tempIt != it->elements_end() && tempIt->type() == tlv::RepetitiveInterval) { |
| 132 | m_whiteIntervalList.insert(RepetitiveInterval(*tempIt)); |
| 133 | tempIt++; |
| 134 | } |
| 135 | it++; |
| 136 | } |
| 137 | else |
| 138 | BOOST_THROW_EXCEPTION(tlv::Error("The first element must be WhiteIntervalList")); |
| 139 | |
| 140 | if (it != m_wire.elements_end() && it->type() == tlv::BlackIntervalList) { |
| 141 | it->parse(); |
| 142 | Block::element_const_iterator tempIt = it->elements_begin(); |
| 143 | while (tempIt != it->elements_end() && tempIt->type() == tlv::RepetitiveInterval) { |
| 144 | m_blackIntervalList.insert(RepetitiveInterval(*tempIt)); |
| 145 | tempIt++; |
| 146 | } |
| 147 | it++; |
| 148 | } |
| 149 | else |
| 150 | BOOST_THROW_EXCEPTION(tlv::Error("The second element must be BlackIntervalList")); |
| 151 | } |
| 152 | |
| 153 | Schedule& |
| 154 | Schedule::addWhiteInterval(const RepetitiveInterval& repetitiveInterval) |
| 155 | { |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 156 | m_wire.reset(); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 157 | m_whiteIntervalList.insert(repetitiveInterval); |
| 158 | return *this; |
| 159 | } |
| 160 | |
| 161 | Schedule& |
| 162 | Schedule::addBlackInterval(const RepetitiveInterval& repetitiveInterval) |
| 163 | { |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 164 | m_wire.reset(); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 165 | m_blackIntervalList.insert(repetitiveInterval); |
| 166 | return *this; |
| 167 | } |
| 168 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 169 | std::tuple<bool, Interval> |
Zhiyi Zhang | 2f9efa6 | 2015-11-23 19:02:28 +0800 | [diff] [blame] | 170 | Schedule::getCoveringInterval(const TimeStamp& ts) const |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 171 | { |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 172 | Interval blackPositiveResult(true); |
| 173 | Interval whitePositiveResult(true); |
| 174 | |
| 175 | Interval blackNegativeResult; |
| 176 | Interval whiteNegativeResult; |
| 177 | |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 178 | // get the blackResult |
Zhiyi Zhang | 2f9efa6 | 2015-11-23 19:02:28 +0800 | [diff] [blame] | 179 | calIntervalResult(m_blackIntervalList, ts, |
| 180 | blackPositiveResult, blackNegativeResult); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 181 | |
Zhiyi Zhang | 2f9efa6 | 2015-11-23 19:02:28 +0800 | [diff] [blame] | 182 | // if black positive result is not empty, the result must be false |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 183 | if (!blackPositiveResult.isEmpty()) |
| 184 | return std::make_tuple(false, blackPositiveResult); |
| 185 | |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 186 | // get the whiteResult |
Zhiyi Zhang | 2f9efa6 | 2015-11-23 19:02:28 +0800 | [diff] [blame] | 187 | calIntervalResult(m_whiteIntervalList, ts, |
| 188 | whitePositiveResult, whiteNegativeResult); |
| 189 | |
| 190 | if (whitePositiveResult.isEmpty() && !whiteNegativeResult.isValid()) { |
| 191 | // there is no white interval covering the timestamp |
| 192 | // return false and a 24-hour interval |
| 193 | return std::make_tuple(false, Interval(TimeStamp(ts.date(), boost::posix_time::hours(0)), |
| 194 | TimeStamp(ts.date(), boost::posix_time::hours(24)))); |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Zhiyi Zhang | 2f9efa6 | 2015-11-23 19:02:28 +0800 | [diff] [blame] | 197 | if (!whitePositiveResult.isEmpty()) { |
| 198 | // there is white interval covering the timestamp |
| 199 | // return ture and calculate the intersection |
| 200 | if (blackNegativeResult.isValid()) |
| 201 | return std::make_tuple(true, whitePositiveResult && blackNegativeResult); |
| 202 | else |
| 203 | return std::make_tuple(true, whitePositiveResult); |
| 204 | } |
| 205 | else { |
| 206 | // there is no white interval covering the timestamp |
| 207 | // return false |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 208 | return std::make_tuple(false, whiteNegativeResult); |
Zhiyi Zhang | 2f9efa6 | 2015-11-23 19:02:28 +0800 | [diff] [blame] | 209 | } |
Zhiyi Zhang | cea58d5 | 2015-08-26 10:19:56 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | } // namespace gep |
| 213 | } // namespace ndn |