Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 4 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 7 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 11 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 15 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 18 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 19 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "cert-publisher.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace pib { |
| 26 | |
| 27 | CertPublisher::CertPublisher(Face& face, PibDb& db) |
| 28 | : m_face(face) |
| 29 | , m_db(db) |
| 30 | { |
| 31 | startPublishAll(); |
| 32 | |
| 33 | m_certDeletedConnection = |
| 34 | m_db.certificateDeleted.connect(bind(&CertPublisher::stopPublish, this, _1)); |
| 35 | m_certInsertedConnection = |
| 36 | m_db.certificateInserted.connect(bind(&CertPublisher::startPublish, this, _1)); |
| 37 | } |
| 38 | |
| 39 | CertPublisher::~CertPublisher() |
| 40 | { |
| 41 | for (const auto& it : m_registeredPrefixes) |
| 42 | m_face.unsetInterestFilter(it.second); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | CertPublisher::startPublishAll() |
| 47 | { |
| 48 | // For now we have to register the prefix of certificates separately. |
| 49 | // The reason is that certificates do not share the same prefix that |
| 50 | // can aggregate the certificates publishing without attracting interests |
| 51 | // for non-PIB data. |
| 52 | |
| 53 | std::vector<Name> identities = m_db.listIdentities(); |
| 54 | |
| 55 | for (const auto& identity : identities) { |
| 56 | std::vector<Name> keyNames = m_db.listKeyNamesOfIdentity(identity); |
| 57 | |
| 58 | for (const auto& key : keyNames) { |
| 59 | std::vector<Name> certNames = m_db.listCertNamesOfKey(key); |
| 60 | |
| 61 | for (const auto& certName : certNames) |
| 62 | startPublish(certName); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | CertPublisher::registerCertPrefix(const Name& certName) |
| 69 | { |
| 70 | BOOST_ASSERT(!certName.empty()); |
| 71 | |
| 72 | const Name& prefix = certName.getPrefix(-1); |
| 73 | |
| 74 | if (m_registeredPrefixes.find(prefix) == m_registeredPrefixes.end()) { |
| 75 | m_registeredPrefixes[prefix] = |
| 76 | m_face.setInterestFilter(certName.getPrefix(-1), |
| 77 | bind(&CertPublisher::processInterest, this, _1, _2), |
| 78 | [] (const Name& name) {}, |
| 79 | [=] (const Name& name, const std::string& msg) { |
| 80 | if (m_registeredPrefixes.erase(prefix) == 0) { |
| 81 | // registration no longer needed - certificates deleted |
| 82 | return; |
| 83 | } |
| 84 | // retry |
| 85 | registerCertPrefix(certName); |
| 86 | }); |
| 87 | |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | CertPublisher::processInterest(const InterestFilter& interestFilter, |
| 93 | const Interest& interest) |
| 94 | { |
| 95 | shared_ptr<const Data> certificate = m_responseCache.find(interest); |
| 96 | if (certificate != nullptr) { |
| 97 | m_face.put(*certificate); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | CertPublisher::startPublish(const Name& certName) |
| 103 | { |
| 104 | m_responseCache.insert(*m_db.getCertificate(certName)); |
| 105 | registerCertPrefix(certName); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | CertPublisher::stopPublish(const Name& certName) |
| 110 | { |
| 111 | BOOST_ASSERT(!certName.empty()); |
| 112 | |
| 113 | m_responseCache.erase(certName); |
| 114 | |
| 115 | // clear the listener if this is the only cert using the prefix |
| 116 | const Name& prefix = certName.getPrefix(-1); // strip version component |
| 117 | |
| 118 | if (m_responseCache.find(prefix) != nullptr) |
| 119 | return; |
| 120 | |
| 121 | auto it = m_registeredPrefixes.find(prefix); |
| 122 | BOOST_ASSERT(it != m_registeredPrefixes.end()); |
| 123 | m_face.unsetInterestFilter(it->second); |
| 124 | m_registeredPrefixes.erase(it); |
| 125 | } |
| 126 | |
| 127 | } // namespace pib |
| 128 | } // namespace ndn |