Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 15 | #include "common.hpp" |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 16 | #include "certificate-cache-ttl.hpp" |
| 17 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 18 | using namespace std; |
| 19 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 20 | namespace ndn { |
| 21 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 22 | CertificateCacheTtl::CertificateCacheTtl(shared_ptr<boost::asio::io_service> io, |
| 23 | const time::seconds& defaultTtl) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 24 | : m_defaultTtl(defaultTtl) |
| 25 | , m_scheduler(*io) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 26 | { |
| 27 | } |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 28 | |
| 29 | CertificateCacheTtl::~CertificateCacheTtl() |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 30 | { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 34 | CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate) |
| 35 | { |
| 36 | time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ? |
| 37 | certificate->getFreshnessPeriod() : m_defaultTtl); |
| 38 | |
| 39 | Name trackerIndex = certificate->getName().getPrefix(-1); |
| 40 | EventTracker::iterator it = m_tracker.find(trackerIndex); |
| 41 | if (it != m_tracker.end()) |
| 42 | m_scheduler.cancelEvent(m_tracker[trackerIndex]); |
| 43 | |
| 44 | m_scheduler.scheduleEvent(time::seconds(0), |
| 45 | bind(&CertificateCacheTtl::insert, this, certificate)); |
| 46 | m_tracker[trackerIndex] = m_scheduler.scheduleEvent(expire, |
| 47 | bind(&CertificateCacheTtl::remove, |
| 48 | this, certificate->getName())); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 53 | { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 54 | Name name = certificate->getName().getPrefix(-1); |
| 55 | m_cache[name] = certificate; |
| 56 | } |
| 57 | |
| 58 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | CertificateCacheTtl::remove(const Name& certificateName) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 60 | { |
| 61 | Name name = certificateName.getPrefix(-1); |
| 62 | Cache::iterator it = m_cache.find(name); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 63 | if (it != m_cache.end()) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 64 | m_cache.erase(it); |
| 65 | } |
| 66 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 67 | shared_ptr<const IdentityCertificate> |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 68 | CertificateCacheTtl::getCertificate(const Name& certificateName) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 69 | { |
| 70 | Cache::iterator it = m_cache.find(certificateName); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 71 | if (it != m_cache.end()) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 72 | return it->second; |
| 73 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 74 | return shared_ptr<IdentityCertificate>(); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 77 | } // namespace ndn |