blob: 7490471994a011df16529d7ca2fc8629a9698ba2 [file] [log] [blame]
Yingdi Yu7640cb32014-01-29 20:00:50 -08001/* -*- 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
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08008#include "common.hpp"
Yingdi Yu7640cb32014-01-29 20:00:50 -08009#include "certificate-cache-ttl.hpp"
10
Yingdi Yu7640cb32014-01-29 20:00:50 -080011using namespace std;
12
Yingdi Yufc40d872014-02-18 12:56:04 -080013namespace ndn {
14
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070015CertificateCacheTtl::CertificateCacheTtl(shared_ptr<boost::asio::io_service> io,
16 const time::seconds& defaultTtl)
Yingdi Yu7640cb32014-01-29 20:00:50 -080017 : m_defaultTtl(defaultTtl)
18 , m_scheduler(*io)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070019{
20}
Yingdi Yu7640cb32014-01-29 20:00:50 -080021
22CertificateCacheTtl::~CertificateCacheTtl()
Yingdi Yu7640cb32014-01-29 20:00:50 -080023{
Yingdi Yu7640cb32014-01-29 20:00:50 -080024}
25
26void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070027CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate)
28{
29 time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
30 certificate->getFreshnessPeriod() : m_defaultTtl);
31
32 Name trackerIndex = certificate->getName().getPrefix(-1);
33 EventTracker::iterator it = m_tracker.find(trackerIndex);
34 if (it != m_tracker.end())
35 m_scheduler.cancelEvent(m_tracker[trackerIndex]);
36
37 m_scheduler.scheduleEvent(time::seconds(0),
38 bind(&CertificateCacheTtl::insert, this, certificate));
39 m_tracker[trackerIndex] = m_scheduler.scheduleEvent(expire,
40 bind(&CertificateCacheTtl::remove,
41 this, certificate->getName()));
42}
43
44void
45CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070046{
Yingdi Yu7640cb32014-01-29 20:00:50 -080047 Name name = certificate->getName().getPrefix(-1);
48 m_cache[name] = certificate;
49}
50
51void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052CertificateCacheTtl::remove(const Name& certificateName)
Yingdi Yu7640cb32014-01-29 20:00:50 -080053{
54 Name name = certificateName.getPrefix(-1);
55 Cache::iterator it = m_cache.find(name);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070056 if (it != m_cache.end())
Yingdi Yu7640cb32014-01-29 20:00:50 -080057 m_cache.erase(it);
58}
59
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070060shared_ptr<const IdentityCertificate>
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061CertificateCacheTtl::getCertificate(const Name& certificateName)
Yingdi Yu7640cb32014-01-29 20:00:50 -080062{
63 Cache::iterator it = m_cache.find(certificateName);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070064 if (it != m_cache.end())
Yingdi Yu7640cb32014-01-29 20:00:50 -080065 return it->second;
66 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070067 return shared_ptr<IdentityCertificate>();
Yingdi Yu7640cb32014-01-29 20:00:50 -080068}
69
Yingdi Yufc40d872014-02-18 12:56:04 -080070} // namespace ndn