blob: 8dea43816155685028c3fdf711b2086f10a00b5b [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
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070014INIT_LOGGER("ndn.CertificateCacheTtl");
Yingdi Yu7640cb32014-01-29 20:00:50 -080015
16using namespace std;
17
Yingdi Yufc40d872014-02-18 12:56:04 -080018namespace ndn {
19
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070020CertificateCacheTtl::CertificateCacheTtl(shared_ptr<boost::asio::io_service> io, const time::seconds& defaultTtl)
Yingdi Yu7640cb32014-01-29 20:00:50 -080021 : m_defaultTtl(defaultTtl)
22 , m_scheduler(*io)
23{}
24
25CertificateCacheTtl::~CertificateCacheTtl()
26{}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070027
Yingdi Yu7640cb32014-01-29 20:00:50 -080028void
29CertificateCacheTtl::insertCertificate(ptr_lib::shared_ptr<const IdentityCertificate> certificate)
30{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070031 time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
32 certificate->getFreshnessPeriod() : m_defaultTtl);
Yingdi Yu7640cb32014-01-29 20:00:50 -080033
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
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070039 m_scheduler.scheduleEvent(time::seconds(0), bind(&CertificateCacheTtl::insert, this, certificate));
40 m_tracker[trackerIndex] = m_scheduler.scheduleEvent(expire, bind(&CertificateCacheTtl::remove,
41 this, certificate->getName()));
Yingdi Yu7640cb32014-01-29 20:00:50 -080042
43}
44
45void
46CertificateCacheTtl::insert(ptr_lib::shared_ptr<const IdentityCertificate> certificate)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070047{
Yingdi Yu7640cb32014-01-29 20:00:50 -080048 Name name = certificate->getName().getPrefix(-1);
49 m_cache[name] = certificate;
50}
51
52void
53CertificateCacheTtl::remove(const Name &certificateName)
54{
55 Name name = certificateName.getPrefix(-1);
56 Cache::iterator it = m_cache.find(name);
57 if(it != m_cache.end())
58 m_cache.erase(it);
59}
60
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061ptr_lib::shared_ptr<const IdentityCertificate>
Yingdi Yu7640cb32014-01-29 20:00:50 -080062CertificateCacheTtl::getCertificate(const Name & certificateName)
63{
64 Cache::iterator it = m_cache.find(certificateName);
65 if(it != m_cache.end())
66 return it->second;
67 else
68 return ptr_lib::shared_ptr<IdentityCertificate>();
69}
70
Yingdi Yufc40d872014-02-18 12:56:04 -080071} // namespace ndn