All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
certificate-cache-ttl.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
8 #include "common.hpp"
10 
11 using namespace std;
12 
13 namespace ndn {
14 
15 CertificateCacheTtl::CertificateCacheTtl(shared_ptr<boost::asio::io_service> io,
16  const time::seconds& defaultTtl)
17  : m_defaultTtl(defaultTtl)
18  , m_scheduler(*io)
19 {
20 }
21 
23 {
24 }
25 
26 void
27 CertificateCacheTtl::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 
44 void
45 CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
46 {
47  Name name = certificate->getName().getPrefix(-1);
48  m_cache[name] = certificate;
49 }
50 
51 void
52 CertificateCacheTtl::remove(const Name& certificateName)
53 {
54  Name name = certificateName.getPrefix(-1);
55  Cache::iterator it = m_cache.find(name);
56  if (it != m_cache.end())
57  m_cache.erase(it);
58 }
59 
60 shared_ptr<const IdentityCertificate>
62 {
63  Cache::iterator it = m_cache.find(certificateName);
64  if (it != m_cache.end())
65  return it->second;
66  else
67  return shared_ptr<IdentityCertificate>();
68 }
69 
70 } // namespace ndn
virtual void insertCertificate(shared_ptr< const IdentityCertificate > certificate)
Name getPrefix(int nComponents) const
Return a new Name with the first nComponents components of this Name.
Definition: name.hpp:240
EventId scheduleEvent(const time::nanoseconds &after, const Event &event)
Schedule one time event after the specified delay.
Definition: scheduler.cpp:86
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
virtual shared_ptr< const IdentityCertificate > getCertificate(const Name &certificateNameWithoutVersion)
void cancelEvent(const EventId &eventId)
Cancel scheduled event.
Definition: scheduler.cpp:115