blob: 4ea644e3f9eadfd21d8c94d22185c0dee0d47126 [file] [log] [blame]
Yingdi Yu7640cb32014-01-29 20:00:50 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu7640cb32014-01-29 20:00:50 -080013 */
14
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080015#include "common.hpp"
Yingdi Yu7640cb32014-01-29 20:00:50 -080016#include "certificate-cache-ttl.hpp"
17
Yingdi Yu7640cb32014-01-29 20:00:50 -080018using namespace std;
19
Yingdi Yufc40d872014-02-18 12:56:04 -080020namespace ndn {
21
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070022CertificateCacheTtl::CertificateCacheTtl(shared_ptr<boost::asio::io_service> io,
23 const time::seconds& defaultTtl)
Yingdi Yu7640cb32014-01-29 20:00:50 -080024 : m_defaultTtl(defaultTtl)
25 , m_scheduler(*io)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070026{
27}
Yingdi Yu7640cb32014-01-29 20:00:50 -080028
29CertificateCacheTtl::~CertificateCacheTtl()
Yingdi Yu7640cb32014-01-29 20:00:50 -080030{
Yingdi Yu7640cb32014-01-29 20:00:50 -080031}
32
33void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070034CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate)
35{
36 time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
37 certificate->getFreshnessPeriod() : m_defaultTtl);
38
39 Name trackerIndex = certificate->getName().getPrefix(-1);
40 EventTracker::iterator it = m_tracker.find(trackerIndex);
41 if (it != m_tracker.end())
42 m_scheduler.cancelEvent(m_tracker[trackerIndex]);
43
44 m_scheduler.scheduleEvent(time::seconds(0),
45 bind(&CertificateCacheTtl::insert, this, certificate));
46 m_tracker[trackerIndex] = m_scheduler.scheduleEvent(expire,
47 bind(&CertificateCacheTtl::remove,
48 this, certificate->getName()));
49}
50
51void
52CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053{
Yingdi Yu7640cb32014-01-29 20:00:50 -080054 Name name = certificate->getName().getPrefix(-1);
55 m_cache[name] = certificate;
56}
57
58void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070059CertificateCacheTtl::remove(const Name& certificateName)
Yingdi Yu7640cb32014-01-29 20:00:50 -080060{
61 Name name = certificateName.getPrefix(-1);
62 Cache::iterator it = m_cache.find(name);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070063 if (it != m_cache.end())
Yingdi Yu7640cb32014-01-29 20:00:50 -080064 m_cache.erase(it);
65}
66
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070067shared_ptr<const IdentityCertificate>
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070068CertificateCacheTtl::getCertificate(const Name& certificateName)
Yingdi Yu7640cb32014-01-29 20:00:50 -080069{
70 Cache::iterator it = m_cache.find(certificateName);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070071 if (it != m_cache.end())
Yingdi Yu7640cb32014-01-29 20:00:50 -080072 return it->second;
73 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070074 return shared_ptr<IdentityCertificate>();
Yingdi Yu7640cb32014-01-29 20:00:50 -080075}
76
Yingdi Yufc40d872014-02-18 12:56:04 -080077} // namespace ndn