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