blob: 890fc7a82038aa495f3659369a4b263c13736f7a [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu0a312e52015-07-22 13:14:53 -07003 * Copyright (c) 2014-2015, Regents of the University of California.
Yingdi Yu77627ab2015-07-21 16:13:49 -07004 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07005 * 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 Yu77627ab2015-07-21 16:13:49 -07007 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07008 * 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 Yu77627ab2015-07-21 16:13:49 -070011 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070012 * 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 Yu77627ab2015-07-21 16:13:49 -070015 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070016 * 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 Yu77627ab2015-07-21 16:13:49 -070018 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070019 * @author Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yu77627ab2015-07-21 16:13:49 -070020 */
21
22#include "cert-publisher.hpp"
23
24namespace ndn {
25namespace pib {
26
27CertPublisher::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
39CertPublisher::~CertPublisher()
40{
41 for (const auto& it : m_registeredPrefixes)
42 m_face.unsetInterestFilter(it.second);
43}
44
45void
46CertPublisher::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
67void
68CertPublisher::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
91void
92CertPublisher::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
101void
102CertPublisher::startPublish(const Name& certName)
103{
104 m_responseCache.insert(*m_db.getCertificate(certName));
105 registerCertPrefix(certName);
106}
107
108void
109CertPublisher::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