Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 22 | */ |
| 23 | |
| 24 | #include "certificate-cache-ttl.hpp" |
| 25 | |
| 26 | namespace ndn { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 27 | namespace security { |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 28 | |
| 29 | CertificateCacheTtl::CertificateCacheTtl(boost::asio::io_service& io, |
| 30 | const time::seconds& defaultTtl/* = time::seconds(3600)*/) |
| 31 | : m_defaultTtl(defaultTtl) |
| 32 | , m_io(io) |
| 33 | , m_scheduler(m_io) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | CertificateCacheTtl::~CertificateCacheTtl() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 42 | CertificateCacheTtl::insertCertificate(shared_ptr<const v1::IdentityCertificate> certificate) |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 43 | { |
| 44 | m_io.dispatch([this, certificate] { this->insert(certificate); }); |
| 45 | } |
| 46 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 47 | shared_ptr<const v1::IdentityCertificate> |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 48 | CertificateCacheTtl::getCertificate(const Name& certificateName) |
| 49 | { |
| 50 | Cache::iterator it = m_cache.find(certificateName); |
| 51 | if (it != m_cache.end()) |
| 52 | return it->second.first; |
| 53 | else |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 54 | return shared_ptr<v1::IdentityCertificate>(); |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void |
| 58 | CertificateCacheTtl::reset() |
| 59 | { |
| 60 | m_io.dispatch([this] { this->removeAll(); }); |
| 61 | } |
| 62 | |
| 63 | size_t |
| 64 | CertificateCacheTtl::getSize() |
| 65 | { |
| 66 | return m_cache.size(); |
| 67 | } |
| 68 | |
| 69 | void |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 70 | CertificateCacheTtl::insert(shared_ptr<const v1::IdentityCertificate> certificate) |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 71 | { |
| 72 | time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ? |
| 73 | certificate->getFreshnessPeriod() : m_defaultTtl); |
| 74 | |
| 75 | Name index = certificate->getName().getPrefix(-1); |
| 76 | |
| 77 | Cache::iterator it = m_cache.find(index); |
| 78 | if (it != m_cache.end()) |
| 79 | m_scheduler.cancelEvent(it->second.second); |
| 80 | |
| 81 | EventId eventId = m_scheduler.scheduleEvent(expire, |
| 82 | bind(&CertificateCacheTtl::remove, |
| 83 | this, certificate->getName())); |
| 84 | |
| 85 | m_cache[index] = std::make_pair(certificate, eventId); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | CertificateCacheTtl::remove(const Name& certificateName) |
| 90 | { |
| 91 | Name name = certificateName.getPrefix(-1); |
| 92 | Cache::iterator it = m_cache.find(name); |
| 93 | if (it != m_cache.end()) |
| 94 | m_cache.erase(it); |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | CertificateCacheTtl::removeAll() |
| 99 | { |
| 100 | for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); it++) |
| 101 | m_scheduler.cancelEvent(it->second.second); |
| 102 | |
| 103 | m_cache.clear(); |
| 104 | } |
| 105 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 106 | } // namespace security |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 107 | } // namespace ndn |