Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 8 | #include "common.hpp" |
| 9 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 10 | #include "certificate-cache-ttl.hpp" |
| 11 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 12 | #include "../util/logging.hpp" |
| 13 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 14 | INIT_LOGGER("ndn.CertificateCacheTtl") |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 15 | |
| 16 | using namespace std; |
| 17 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 18 | namespace ndn { |
| 19 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 20 | CertificateCacheTtl::CertificateCacheTtl(shared_ptr<boost::asio::io_service> io, const time::seconds& defaultTtl) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 21 | : m_defaultTtl(defaultTtl) |
| 22 | , m_scheduler(*io) |
| 23 | {} |
| 24 | |
| 25 | CertificateCacheTtl::~CertificateCacheTtl() |
| 26 | {} |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 27 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 28 | void |
| 29 | CertificateCacheTtl::insertCertificate(ptr_lib::shared_ptr<const IdentityCertificate> certificate) |
| 30 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 31 | time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ? |
| 32 | certificate->getFreshnessPeriod() : m_defaultTtl); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 33 | |
| 34 | Name trackerIndex = certificate->getName().getPrefix(-1); |
| 35 | EventTracker::iterator it = m_tracker.find(trackerIndex); |
| 36 | if(it != m_tracker.end()) |
| 37 | m_scheduler.cancelEvent(m_tracker[trackerIndex]); |
| 38 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 39 | m_scheduler.scheduleEvent(time::seconds(0), bind(&CertificateCacheTtl::insert, this, certificate)); |
| 40 | m_tracker[trackerIndex] = m_scheduler.scheduleEvent(expire, bind(&CertificateCacheTtl::remove, |
| 41 | this, certificate->getName())); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 42 | |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | CertificateCacheTtl::insert(ptr_lib::shared_ptr<const IdentityCertificate> certificate) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 47 | { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 48 | Name name = certificate->getName().getPrefix(-1); |
| 49 | m_cache[name] = certificate; |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | CertificateCacheTtl::remove(const Name &certificateName) |
| 54 | { |
| 55 | Name name = certificateName.getPrefix(-1); |
| 56 | Cache::iterator it = m_cache.find(name); |
| 57 | if(it != m_cache.end()) |
| 58 | m_cache.erase(it); |
| 59 | } |
| 60 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 61 | ptr_lib::shared_ptr<const IdentityCertificate> |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 62 | CertificateCacheTtl::getCertificate(const Name & certificateName) |
| 63 | { |
| 64 | Cache::iterator it = m_cache.find(certificateName); |
| 65 | if(it != m_cache.end()) |
| 66 | return it->second; |
| 67 | else |
| 68 | return ptr_lib::shared_ptr<IdentityCertificate>(); |
| 69 | } |
| 70 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 71 | } // namespace ndn |