| 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 | */ |
| 20 | #ifndef TIME_TO_LIVE_H |
| 21 | #define TIME_TO_LIVE_H |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <boost/asio.hpp> |
| 26 | #include <boost/serialization/access.hpp> |
| 27 | #include <boost/serialization/nvp.hpp> |
| 28 | #include <boost/serialization/split_member.hpp> |
| 29 | #include <boost/serialization/string.hpp> |
| 30 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 31 | |
| 32 | // forward declaration |
| 33 | class HostAddress; |
| 34 | |
| 35 | //----------------------------------------------------------------------------- |
| 36 | // TimeToLive |
| 37 | //----------------------------------------------------------------------------- |
| 38 | |
| 39 | class TimeToLive |
| 40 | { |
| 41 | public: |
| 42 | TimeToLive( uint32_t ttl = 0 ); |
| 43 | ~TimeToLive(); |
| 44 | |
| 45 | uint32_t get_value() const; |
| 46 | void set_value( const uint32_t ttl ); |
| 47 | |
| 48 | uint32_t get_updated_value() const; |
| 49 | |
| 50 | bool was_set_before(const boost::posix_time::ptime &threshold) const; |
| 51 | |
| 52 | private: |
| 53 | /// the numeric time-to-live |
| 54 | uint32_t Ttl; |
| 55 | /// the time when the time-to-live was set, so it is possible to know the |
| 56 | /// elapsed time |
| 57 | boost::posix_time::ptime TtlSetTime; |
| 58 | |
| 59 | // serialization |
| 60 | friend class boost::serialization::access; |
| 61 | template<class Archive> |
| 62 | void save(Archive & ar, const unsigned int version) const |
| 63 | { |
| 64 | std::string ttl_creation_time = boost::posix_time::to_iso_string( |
| 65 | TtlSetTime); |
| 66 | ar & BOOST_SERIALIZATION_NVP(Ttl); |
| 67 | ar & BOOST_SERIALIZATION_NVP(ttl_creation_time); |
| 68 | } |
| 69 | |
| 70 | template<class Archive> |
| 71 | void load(Archive & ar, const unsigned int version) |
| 72 | { |
| 73 | std::string ttl_creation_time; |
| 74 | ar & BOOST_SERIALIZATION_NVP(Ttl); |
| 75 | ar & BOOST_SERIALIZATION_NVP(ttl_creation_time); |
| 76 | |
| 77 | // now convert to Ip and Ttl |
| 78 | TtlSetTime = boost::posix_time::from_iso_string(ttl_creation_time); |
| 79 | } |
| 80 | |
| 81 | BOOST_SERIALIZATION_SPLIT_MEMBER() |
| 82 | }; |
| 83 | |
| 84 | #endif // TIME_TO_LIVE_H |