blob: 2ab13c925be80d52d9fb238f1b29dfff01965b03 [file] [log] [blame]
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
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
26namespace ndn {
27
28CertificateCacheTtl::CertificateCacheTtl(boost::asio::io_service& io,
29 const time::seconds& defaultTtl/* = time::seconds(3600)*/)
30 : m_defaultTtl(defaultTtl)
31 , m_io(io)
32 , m_scheduler(m_io)
33{
34}
35
36CertificateCacheTtl::~CertificateCacheTtl()
37{
38}
39
40void
41CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate)
42{
43 m_io.dispatch([this, certificate] { this->insert(certificate); });
44}
45
46shared_ptr<const IdentityCertificate>
47CertificateCacheTtl::getCertificate(const Name& certificateName)
48{
49 Cache::iterator it = m_cache.find(certificateName);
50 if (it != m_cache.end())
51 return it->second.first;
52 else
53 return shared_ptr<IdentityCertificate>();
54}
55
56void
57CertificateCacheTtl::reset()
58{
59 m_io.dispatch([this] { this->removeAll(); });
60}
61
62size_t
63CertificateCacheTtl::getSize()
64{
65 return m_cache.size();
66}
67
68void
69CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
70{
71 time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
72 certificate->getFreshnessPeriod() : m_defaultTtl);
73
74 Name index = certificate->getName().getPrefix(-1);
75
76 Cache::iterator it = m_cache.find(index);
77 if (it != m_cache.end())
78 m_scheduler.cancelEvent(it->second.second);
79
80 EventId eventId = m_scheduler.scheduleEvent(expire,
81 bind(&CertificateCacheTtl::remove,
82 this, certificate->getName()));
83
84 m_cache[index] = std::make_pair(certificate, eventId);
85}
86
87void
88CertificateCacheTtl::remove(const Name& certificateName)
89{
90 Name name = certificateName.getPrefix(-1);
91 Cache::iterator it = m_cache.find(name);
92 if (it != m_cache.end())
93 m_cache.erase(it);
94}
95
96void
97CertificateCacheTtl::removeAll()
98{
99 for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); it++)
100 m_scheduler.cancelEvent(it->second.second);
101
102 m_cache.clear();
103}
104
105} // namespace ndn