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