blob: abcd5a2aa39401b90bb0ef103e717bd9fedd62b2 [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
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