schedule.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_SCHEDULE_HPP
24 #define NDN_SCHEDULE_HPP
25 
26 #include <vector>
27 #include <ndn-cpp/c/errors.h>
28 #include <ndn-cpp/util/blob.hpp>
29 #include "repetitive-interval.hpp"
30 
31 struct ndn_TlvEncoder;
32 struct ndn_TlvDecoder;
33 
34 namespace ndn {
35 
43 class Schedule {
44 public:
45  class Result {
46  public:
47  Result(bool isPositive, const Interval& interval)
48  {
49  this->isPositive = isPositive;
50  this->interval = interval;
51  }
52 
53  bool isPositive;
54  Interval interval;
55  };
56 
61  {
62  }
63 
64  // Note: RepetitiveInterval is immutable, so we can use the default shallow
65  // copy constructor.
66 
73  Schedule&
74  addWhiteInterval(const ptr_lib::shared_ptr<RepetitiveInterval>& repetitiveInterval)
75  {
76  // RepetitiveInterval is immutable, so we don't need to make a copy.
77  sortedSetAdd(whiteIntervalList_, repetitiveInterval);
78  return *this;
79  }
80 
87  Schedule&
88  addBlackInterval(const ptr_lib::shared_ptr<RepetitiveInterval>& repetitiveInterval)
89  {
90  // RepetitiveInterval is immutable, so we don't need to make a copy.
91  sortedSetAdd(blackIntervalList_, repetitiveInterval);
92  return *this;
93  }
94 
106  Result
108 
113  Blob
114  wireEncode() const;
115 
121  void
122  wireDecode(const uint8_t *input, size_t inputLength);
123 
128  void
129  wireDecode(const std::vector<uint8_t>& input)
130  {
131  wireDecode(&input[0], input.size());
132  }
133 
138  void
139  wireDecode(const Blob& input)
140  {
141  wireDecode(input.buf(), input.size());
142  }
143 
144  static MillisecondsSince1970
145  fromIsoString(const std::string& dateString);
146 
147  static std::string
148  toIsoString(MillisecondsSince1970 msSince1970);
149 
150 private:
157  static void
158  sortedSetAdd
159  (std::vector<ptr_lib::shared_ptr<RepetitiveInterval> >& list,
160  const ptr_lib::shared_ptr<RepetitiveInterval>& element);
161 
170  static ndn_Error
171  encodeRepetitiveIntervalValue(const void *context, ndn_TlvEncoder *encoder);
172 
181  static ndn_Error
182  encodeRepetitiveIntervalListValue(const void *context, ndn_TlvEncoder *encoder);
183 
191  static ndn_Error
192  encodeScheduleValue(const void *context, ndn_TlvEncoder *encoder);
193 
200  static ptr_lib::shared_ptr<RepetitiveInterval>
201  decodeRepetitiveInterval(ndn_TlvDecoder *decoder);
202 
212  static void
213  calculateIntervalResult
214  (const std::vector<ptr_lib::shared_ptr<RepetitiveInterval> >& list,
215  MillisecondsSince1970 timeStamp, Interval& positiveResult,
216  Interval& negativeResult);
217 
218  std::vector<ptr_lib::shared_ptr<RepetitiveInterval> > whiteIntervalList_;
219  std::vector<ptr_lib::shared_ptr<RepetitiveInterval> > blackIntervalList_;
220  static const uint64_t MILLISECONDS_IN_DAY = 24 * 3600 * 1000;
221 };
222 
223 }
224 
225 #endif
Schedule()
Create a Schedule with empty whiteIntervalList and blackIntervalList.
Definition: schedule.hpp:60
Blob wireEncode() const
Encode this Schedule.
Definition: schedule.cpp:80
An Interval defines a time duration which contains a start timestamp and an end timestamp.
Definition: interval.hpp:36
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
void wireDecode(const std::vector< uint8_t > &input)
Decode the input and update this Schedule.
Definition: schedule.hpp:129
Schedule is used to manage the times when a member can access data using two sets of RepetitiveInterv...
Definition: schedule.hpp:43
Schedule & addBlackInterval(const ptr_lib::shared_ptr< RepetitiveInterval > &repetitiveInterval)
Add the repetitiveInterval to the blackIntervalList.
Definition: schedule.hpp:88
void wireDecode(const uint8_t *input, size_t inputLength)
Decode the input and update this Schedule.
Definition: schedule.cpp:94
Copyright (C) 2014-2016 Regents of the University of California.
Definition: tlv-encoder.h:39
A Blob holds a pointer to an immutable byte array implemented as const std::vector.
Definition: blob.hpp:42
const uint8_t * buf() const
Return a const pointer to the first byte of the immutable byte array, or 0 if the pointer is null...
Definition: blob.hpp:159
Definition: schedule.hpp:45
size_t size() const
Return the length of the immutable byte array.
Definition: blob.hpp:147
Result getCoveringInterval(MillisecondsSince1970 timeStamp) const
Get the interval that covers the time stamp.
Definition: schedule.cpp:36
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:117
void wireDecode(const Blob &input)
Decode the input and update this Schedule.
Definition: schedule.hpp:139
Copyright (C) 2014-2016 Regents of the University of California.
Definition: tlv-decoder.h:34
Schedule & addWhiteInterval(const ptr_lib::shared_ptr< RepetitiveInterval > &repetitiveInterval)
Add the repetitiveInterval to the whiteIntervalList.
Definition: schedule.hpp:74