Commit | Line | Data |
---|---|---|
91fcc471 TJ |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | */ | |
101be5ce GMF |
20 | #include "dns/timetolive.h" |
21 | ||
780b0bca | 22 | #include "boost_assert_handler.h" |
101be5ce GMF |
23 | |
24 | using boost::date_time::time_resolution_traits_adapted64_impl; | |
25 | using boost::posix_time::microsec_clock; | |
26 | using boost::posix_time::ptime; | |
27 | ||
28 | //----------------------------------------------------------------------------- | |
29 | // TimeToLive | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
b0a693a0 | 32 | TimeToLive::TimeToLive( uint32_t ttl ) : |
101be5ce GMF |
33 | Ttl( ttl ), |
34 | TtlSetTime( microsec_clock::universal_time() ) | |
35 | { | |
36 | } | |
37 | ||
38 | TimeToLive::~TimeToLive() | |
39 | { | |
40 | } | |
41 | ||
5a3f6189 GMF |
42 | /** |
43 | * @return the original time-to-live value, as specified by the set method. | |
44 | */ | |
b0a693a0 | 45 | uint32_t TimeToLive::get_value() const |
101be5ce GMF |
46 | { |
47 | return Ttl; | |
48 | } | |
49 | ||
5a3f6189 GMF |
50 | /** |
51 | * @param ttl the time-to-live value. | |
52 | */ | |
b0a693a0 | 53 | void TimeToLive::set_value( const uint32_t ttl ) |
101be5ce | 54 | { |
81cf5681 | 55 | BOOST_ASSERT( 0 <= ttl ); |
101be5ce GMF |
56 | |
57 | Ttl = ttl; | |
58 | TtlSetTime = microsec_clock::universal_time(); | |
59 | } | |
60 | ||
5a3f6189 GMF |
61 | /** |
62 | * @return the value of the time-to-live updated since the last set was called. | |
63 | */ | |
b0a693a0 | 64 | uint32_t TimeToLive::get_updated_value() const |
101be5ce GMF |
65 | { |
66 | ptime now = microsec_clock::universal_time(); | |
f2601e57 | 67 | uint32_t elapsed_seconds = static_cast<uint32_t>( |
101be5ce GMF |
68 | (now - TtlSetTime).total_seconds() |
69 | ); | |
101be5ce | 70 | |
8bff33b4 CH |
71 | uint32_t original_ttl = get_value(); |
72 | if (elapsed_seconds > original_ttl) | |
73 | return 0; | |
74 | else | |
75 | return original_ttl - elapsed_seconds; | |
101be5ce | 76 | } |
c98b5fcb CH |
77 | |
78 | bool TimeToLive::was_set_before(const ptime &threshold) const | |
79 | { | |
80 | return TtlSetTime < threshold; | |
81 | } |