NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
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_scheduler(io)
33 {
34 }
35 
37 {
38 }
39 
40 void
41 CertificateCacheTtl::insertCertificate(shared_ptr<const v1::IdentityCertificate> certificate)
42 {
43  m_scheduler.scheduleEvent(time::seconds(0), [this, certificate] { this->insert(certificate); });
44 }
45 
46 shared_ptr<const v1::IdentityCertificate>
48 {
49  Cache::iterator it = m_cache.find(certificateName);
50  if (it != m_cache.end())
51  return it->second.first;
52  else
53  return shared_ptr<v1::IdentityCertificate>();
54 }
55 
56 void
58 {
59  m_scheduler.scheduleEvent(time::seconds(0), [this] { this->removeAll(); });
60 }
61 
62 size_t
64 {
65  return m_cache.size();
66 }
67 
68 void
69 CertificateCacheTtl::insert(shared_ptr<const v1::IdentityCertificate> certificate)
70 {
71  time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
72  certificate->getFreshnessPeriod() : m_defaultTtl);
73 
74  Name index = certificate->getName().getPrefix(-1);
75 
76  Cache::iterator it = m_cache.find(index);
77  if (it != m_cache.end())
78  m_scheduler.cancelEvent(it->second.second);
79 
80  EventId eventId = m_scheduler.scheduleEvent(expire,
81  bind(&CertificateCacheTtl::remove,
82  this, certificate->getName()));
83 
84  m_cache[index] = std::make_pair(certificate, eventId);
85 }
86 
87 void
88 CertificateCacheTtl::remove(const Name& certificateName)
89 {
90  Name name = certificateName.getPrefix(-1);
91  Cache::iterator it = m_cache.find(name);
92  if (it != m_cache.end())
93  m_cache.erase(it);
94 }
95 
96 void
97 CertificateCacheTtl::removeAll()
98 {
99  for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); it++)
100  m_scheduler.cancelEvent(it->second.second);
101 
102  m_cache.clear();
103 }
104 
105 } // namespace security
106 } // namespace ndn
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:241
Copyright (c) 2011-2015 Regents of the University of California.
CertificateCacheTtl(boost::asio::io_service &io, const time::seconds &defaultTtl=time::seconds(3600))
EventId scheduleEvent(const time::nanoseconds &after, const Event &event)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:58
virtual void insertCertificate(shared_ptr< const v1::IdentityCertificate > certificate)
void cancelEvent(const EventId &eventId)
Cancel a scheduled event.
Definition: scheduler.cpp:79
Table::const_iterator iterator
Definition: cs-internal.hpp:41
std::shared_ptr< ns3::EventId > EventId
Definition: scheduler.hpp:39
Name abstraction to represent an absolute name.
Definition: name.hpp:46
virtual shared_ptr< const v1::IdentityCertificate > getCertificate(const Name &certificateNameWithoutVersion)