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