bugfixed erase and re-set of TTLs; changed time warp thresh from 24h to 10mins
[pingcheck] / src / dns / timetolive.cpp
CommitLineData
91fcc471
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on 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
24using boost::date_time::time_resolution_traits_adapted64_impl;
25using boost::posix_time::microsec_clock;
26using boost::posix_time::ptime;
27
28//-----------------------------------------------------------------------------
29// TimeToLive
30//-----------------------------------------------------------------------------
31
b0a693a0 32TimeToLive::TimeToLive( uint32_t ttl ) :
101be5ce
GMF
33 Ttl( ttl ),
34 TtlSetTime( microsec_clock::universal_time() )
35{
36}
37
38TimeToLive::~TimeToLive()
39{
40}
41
5a3f6189
GMF
42/**
43 * @return the original time-to-live value, as specified by the set method.
44 */
b0a693a0 45uint32_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 53void 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 64uint32_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
78bool TimeToLive::was_set_before(const ptime &threshold) const
79{
80 return TtlSetTime < threshold;
81}