blob: 616fe36da879031f27b19b548135d119433412e3 [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)
Alexander Afanasyeva47ed4f2015-08-19 23:05:27 -070031 , m_scheduler(io)
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -080032{
33}
34
35CertificateCacheTtl::~CertificateCacheTtl()
36{
37}
38
39void
40CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate)
41{
Alexander Afanasyeva47ed4f2015-08-19 23:05:27 -070042 m_scheduler.scheduleEvent(time::seconds(0), [this, certificate] { this->insert(certificate); });
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -080043}
44
45shared_ptr<const IdentityCertificate>
46CertificateCacheTtl::getCertificate(const Name& certificateName)
47{
48 Cache::iterator it = m_cache.find(certificateName);
49 if (it != m_cache.end())
50 return it->second.first;
51 else
52 return shared_ptr<IdentityCertificate>();
53}
54
55void
56CertificateCacheTtl::reset()
57{
Alexander Afanasyeva47ed4f2015-08-19 23:05:27 -070058 m_scheduler.scheduleEvent(time::seconds(0), [this] { this->removeAll(); });
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -080059}
60
61size_t
62CertificateCacheTtl::getSize()
63{
64 return m_cache.size();
65}
66
67void
68CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
69{
70 time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
71 certificate->getFreshnessPeriod() : m_defaultTtl);
72
73 Name index = certificate->getName().getPrefix(-1);
74
75 Cache::iterator it = m_cache.find(index);
76 if (it != m_cache.end())
77 m_scheduler.cancelEvent(it->second.second);
78
79 EventId eventId = m_scheduler.scheduleEvent(expire,
80 bind(&CertificateCacheTtl::remove,
81 this, certificate->getName()));
82
83 m_cache[index] = std::make_pair(certificate, eventId);
84}
85
86void
87CertificateCacheTtl::remove(const Name& certificateName)
88{
89 Name name = certificateName.getPrefix(-1);
90 Cache::iterator it = m_cache.find(name);
91 if (it != m_cache.end())
92 m_cache.erase(it);
93}
94
95void
96CertificateCacheTtl::removeAll()
97{
98 for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); it++)
99 m_scheduler.cancelEvent(it->second.second);
100
101 m_cache.clear();
102}
103
104} // namespace ndn