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" |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 9 | #include "certificate-cache-ttl.hpp" |
| 10 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 11 | using namespace std; |
| 12 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 13 | namespace ndn { |
| 14 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 15 | CertificateCacheTtl::CertificateCacheTtl(shared_ptr<boost::asio::io_service> io, |
| 16 | const time::seconds& defaultTtl) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 17 | : m_defaultTtl(defaultTtl) |
| 18 | , m_scheduler(*io) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 19 | { |
| 20 | } |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 21 | |
| 22 | CertificateCacheTtl::~CertificateCacheTtl() |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 23 | { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 27 | CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate) |
| 28 | { |
| 29 | time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ? |
| 30 | certificate->getFreshnessPeriod() : m_defaultTtl); |
| 31 | |
| 32 | Name trackerIndex = certificate->getName().getPrefix(-1); |
| 33 | EventTracker::iterator it = m_tracker.find(trackerIndex); |
| 34 | if (it != m_tracker.end()) |
| 35 | m_scheduler.cancelEvent(m_tracker[trackerIndex]); |
| 36 | |
| 37 | m_scheduler.scheduleEvent(time::seconds(0), |
| 38 | bind(&CertificateCacheTtl::insert, this, certificate)); |
| 39 | m_tracker[trackerIndex] = m_scheduler.scheduleEvent(expire, |
| 40 | bind(&CertificateCacheTtl::remove, |
| 41 | this, certificate->getName())); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 46 | { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 47 | Name name = certificate->getName().getPrefix(-1); |
| 48 | m_cache[name] = certificate; |
| 49 | } |
| 50 | |
| 51 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 52 | CertificateCacheTtl::remove(const Name& certificateName) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 53 | { |
| 54 | Name name = certificateName.getPrefix(-1); |
| 55 | Cache::iterator it = m_cache.find(name); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 56 | if (it != m_cache.end()) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 57 | m_cache.erase(it); |
| 58 | } |
| 59 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 60 | shared_ptr<const IdentityCertificate> |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 61 | CertificateCacheTtl::getCertificate(const Name& certificateName) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 62 | { |
| 63 | Cache::iterator it = m_cache.find(certificateName); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 64 | if (it != m_cache.end()) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 65 | return it->second; |
| 66 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 67 | return shared_ptr<IdentityCertificate>(); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 70 | } // namespace ndn |