blob: f5482a31de04f8b5e0b2646c62c0e502cd1782dd [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
8#include "certificate-cache-ttl.hpp"
9
10#include <iostream>
11
12#include "../util/logging.hpp"
13
14
15INIT_LOGGER("CertificateCacheTtl")
16
17using namespace std;
18
19namespace ndn
20{
21CertificateCacheTtl::CertificateCacheTtl(shared_ptr<boost::asio::io_service> io, int defaultTtl)
22 : m_defaultTtl(defaultTtl)
23 , m_scheduler(*io)
24{}
25
26CertificateCacheTtl::~CertificateCacheTtl()
27{}
28
29void
30CertificateCacheTtl::insertCertificate(ptr_lib::shared_ptr<const IdentityCertificate> certificate)
31{
32 time::Duration expire = (certificate->getFreshnessPeriod() >= 0 ? time::milliseconds(certificate->getFreshnessPeriod()) : time::seconds(m_defaultTtl));
33
34 Name trackerIndex = certificate->getName().getPrefix(-1);
35 EventTracker::iterator it = m_tracker.find(trackerIndex);
36 if(it != m_tracker.end())
37 m_scheduler.cancelEvent(m_tracker[trackerIndex]);
38
39 m_scheduler.scheduleEvent(time::seconds(0), bind(&CertificateCacheTtl::insert, this, certificate));
40 m_tracker[trackerIndex] = m_scheduler.scheduleEvent(expire, bind(&CertificateCacheTtl::remove, this, certificate->getName()));
41
42}
43
44void
45CertificateCacheTtl::insert(ptr_lib::shared_ptr<const IdentityCertificate> certificate)
46{
47 Name name = certificate->getName().getPrefix(-1);
48 m_cache[name] = certificate;
49}
50
51void
52CertificateCacheTtl::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
60ptr_lib::shared_ptr<const IdentityCertificate>
61CertificateCacheTtl::getCertificate(const Name & certificateName)
62{
63 Cache::iterator it = m_cache.find(certificateName);
64 if(it != m_cache.end())
65 return it->second;
66 else
67 return ptr_lib::shared_ptr<IdentityCertificate>();
68}
69
70}//ndn
71
72