certificate-cache-ttl.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
25 
26 namespace ndn {
27 namespace security {
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 
38 {
39 }
40 
41 void
42 CertificateCacheTtl::insertCertificate(shared_ptr<const v1::IdentityCertificate> certificate)
43 {
44  m_io.dispatch([this, certificate] { this->insert(certificate); });
45 }
46 
47 shared_ptr<const v1::IdentityCertificate>
49 {
50  Cache::iterator it = m_cache.find(certificateName);
51  if (it != m_cache.end())
52  return it->second.first;
53  else
54  return shared_ptr<v1::IdentityCertificate>();
55 }
56 
57 void
59 {
60  m_io.dispatch([this] { this->removeAll(); });
61 }
62 
63 size_t
65 {
66  return m_cache.size();
67 }
68 
69 void
70 CertificateCacheTtl::insert(shared_ptr<const v1::IdentityCertificate> certificate)
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 
106 } // namespace security
107 } // namespace ndn
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
CertificateCacheTtl(boost::asio::io_service &io, const time::seconds &defaultTtl=time::seconds(3600))
virtual void insertCertificate(shared_ptr< const v1::IdentityCertificate > certificate)
Name abstraction to represent an absolute name.
Definition: name.hpp:46
virtual shared_ptr< const v1::IdentityCertificate > getCertificate(const Name &certificateNameWithoutVersion)
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:241