security: Clean up ValidatorConfig when it is re-loaded.

Refs: #1492

Change-Id: I203a57c0cf18c3c5993abfa9c3c7ca9decbed6c2
diff --git a/src/security/certificate-cache-ttl.hpp b/src/security/certificate-cache-ttl.hpp
index a86ee5e..4cf20e2 100644
--- a/src/security/certificate-cache-ttl.hpp
+++ b/src/security/certificate-cache-ttl.hpp
@@ -24,35 +24,116 @@
 class CertificateCacheTtl : public CertificateCache
 {
 public:
-  CertificateCacheTtl(shared_ptr<boost::asio::io_service> io,
-                      const time::seconds& defaultTtl = time::seconds(3600));
+  CertificateCacheTtl(boost::asio::io_service& io,
+                      const time::seconds& defaultTtl = time::seconds(3600))
+    : m_defaultTtl(defaultTtl)
+    , m_scheduler(io)
+  {
+  }
 
   virtual
-  ~CertificateCacheTtl();
+  ~CertificateCacheTtl()
+  {
+  }
 
-  virtual void
+  virtual inline void
   insertCertificate(shared_ptr<const IdentityCertificate> certificate);
 
-  virtual shared_ptr<const IdentityCertificate>
+  virtual inline shared_ptr<const IdentityCertificate>
   getCertificate(const Name& certificateNameWithoutVersion);
 
+  virtual inline void
+  reset();
+
+  virtual inline size_t
+  getSize();
+
 private:
-  void
+  inline void
   insert(shared_ptr<const IdentityCertificate> certificate);
 
-  void
+  inline void
   remove(const Name& certificateName);
 
+  inline void
+  removeAll();
+
 protected:
-  typedef std::map<Name, shared_ptr<const IdentityCertificate> > Cache;
-  typedef std::map<Name, EventId> EventTracker;
+  typedef std::map<Name, std::pair<shared_ptr<const IdentityCertificate>, EventId> > Cache;
 
   time::seconds m_defaultTtl;
   Cache m_cache;
-  EventTracker m_tracker;
   Scheduler m_scheduler;
 };
 
+inline void
+CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate)
+{
+  m_scheduler.scheduleEvent(time::seconds(0),
+                            bind(&CertificateCacheTtl::insert, this, certificate));
+}
+
+inline shared_ptr<const IdentityCertificate>
+CertificateCacheTtl::getCertificate(const Name& certificateName)
+{
+  Cache::iterator it = m_cache.find(certificateName);
+  if (it != m_cache.end())
+    return it->second.first;
+  else
+    return shared_ptr<IdentityCertificate>();
+}
+
+inline void
+CertificateCacheTtl::reset()
+{
+  m_scheduler.scheduleEvent(time::seconds(0),
+                            bind(&CertificateCacheTtl::removeAll, this));
+}
+
+inline size_t
+CertificateCacheTtl::getSize()
+{
+  return m_cache.size();
+}
+
+inline void
+CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
+{
+  time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
+                               certificate->getFreshnessPeriod() : m_defaultTtl);
+
+  Name index = certificate->getName().getPrefix(-1);
+
+  Cache::iterator it = m_cache.find(index);
+  if (it != m_cache.end())
+    m_scheduler.cancelEvent(it->second.second);
+
+  EventId eventId = m_scheduler.scheduleEvent(expire,
+                                              bind(&CertificateCacheTtl::remove,
+                                                   this, certificate->getName()));
+
+  m_cache[index] = std::make_pair(certificate, eventId);
+}
+
+inline void
+CertificateCacheTtl::remove(const Name& certificateName)
+{
+  Name name = certificateName.getPrefix(-1);
+  Cache::iterator it = m_cache.find(name);
+  if (it != m_cache.end())
+    m_cache.erase(it);
+}
+
+inline void
+CertificateCacheTtl::removeAll()
+{
+  for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); it++)
+    m_scheduler.cancelEvent(it->second.second);
+
+  m_cache.clear();
+}
+
+
 } // namespace ndn
 
 #endif //NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP