Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | bb058c0 | 2018-02-15 22:49:24 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng 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. |
| 11 | * |
| 12 | * repo-ng 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. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 18 | */ |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 19 | |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 20 | #include "read-handle.hpp" |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 21 | #include "repo.hpp" |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 22 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 23 | #include <ndn-cxx/util/logger.hpp> |
| 24 | |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 25 | namespace repo { |
| 26 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 27 | NDN_LOG_INIT(repo.ReadHandle); |
| 28 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 29 | ReadHandle::ReadHandle(Face& face, RepoStorage& storageHandle, size_t prefixSubsetLength) |
| 30 | : m_prefixSubsetLength(prefixSubsetLength) |
| 31 | , m_face(face) |
| 32 | , m_storageHandle(storageHandle) |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 33 | { |
Alexander Afanasyev | bb058c0 | 2018-02-15 22:49:24 +0000 | [diff] [blame] | 34 | connectAutoListen(); |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void |
| 38 | ReadHandle::connectAutoListen() |
| 39 | { |
| 40 | // Connect a RepoStorage's signals to the read handle |
| 41 | if (m_prefixSubsetLength != RepoConfig::DISABLED_SUBSET_LENGTH) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 42 | afterDataInsertionConnection = m_storageHandle.afterDataInsertion.connect( |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 43 | [this] (const Name& prefix) { |
| 44 | onDataInserted(prefix); |
| 45 | }); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 46 | afterDataDeletionConnection = m_storageHandle.afterDataDeletion.connect( |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 47 | [this] (const Name& prefix) { |
| 48 | onDataDeleted(prefix); |
| 49 | }); |
| 50 | } |
| 51 | } |
| 52 | |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 53 | void |
| 54 | ReadHandle::onInterest(const Name& prefix, const Interest& interest) |
| 55 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 56 | NDN_LOG_DEBUG("Received Interest " << interest.getName()); |
| 57 | std::shared_ptr<ndn::Data> data = m_storageHandle.readData(interest); |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 58 | if (data != nullptr) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 59 | NDN_LOG_DEBUG("Put Data: " << *data); |
| 60 | m_face.put(*data); |
| 61 | } |
| 62 | else { |
| 63 | NDN_LOG_DEBUG("No data for " << interest.getName()); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 64 | } |
Wentao Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 68 | ReadHandle::onRegisterFailed(const Name& prefix, const std::string& reason) |
| 69 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 70 | NDN_LOG_ERROR("ERROR: Failed to register prefix in local hub's daemon"); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 71 | m_face.shutdown(); |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void |
| 75 | ReadHandle::listen(const Name& prefix) |
| 76 | { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 77 | ndn::InterestFilter filter(prefix); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 78 | m_face.setInterestFilter(filter, |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 79 | std::bind(&ReadHandle::onInterest, this, _1, _2), |
| 80 | std::bind(&ReadHandle::onRegisterFailed, this, _1, _2)); |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 83 | void |
| 84 | ReadHandle::onDataDeleted(const Name& name) |
| 85 | { |
| 86 | // We add one here to account for the implicit digest at the end, |
| 87 | // which is what we get from the underlying storage when deleting. |
| 88 | Name prefix = name.getPrefix(-(m_prefixSubsetLength + 1)); |
| 89 | auto check = m_insertedDataPrefixes.find(prefix); |
| 90 | if (check != m_insertedDataPrefixes.end()) { |
| 91 | if (--(check->second.useCount) <= 0) { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 92 | m_face.unsetInterestFilter(check->second.prefixId); |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 93 | m_insertedDataPrefixes.erase(prefix); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | ReadHandle::onDataInserted(const Name& name) |
| 100 | { |
| 101 | // Note: We want to save the prefix that we register exactly, not the |
| 102 | // name that provoked the registration |
| 103 | Name prefixToRegister = name.getPrefix(-m_prefixSubsetLength); |
| 104 | ndn::InterestFilter filter(prefixToRegister); |
| 105 | auto check = m_insertedDataPrefixes.find(prefixToRegister); |
| 106 | if (check == m_insertedDataPrefixes.end()) { |
| 107 | // Because of stack lifetime problems, we assume here that the |
| 108 | // prefix registration will be successful, and we add the registered |
| 109 | // prefix to our list. This is because, if we fail, we shut |
| 110 | // everything down, anyway. If registration failures are ever |
| 111 | // considered to be recoverable, we would need to make this |
| 112 | // atomic. |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 113 | const ndn::RegisteredPrefixId* prefixId = m_face.setInterestFilter(filter, |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 114 | [this] (const ndn::InterestFilter& filter, const Interest& interest) { |
| 115 | // Implicit conversion to Name of filter |
| 116 | onInterest(filter, interest); |
| 117 | }, |
Davide Pesavento | 8cc7b91 | 2017-11-16 13:15:24 -0500 | [diff] [blame] | 118 | [] (const Name&) {}, |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 119 | [this] (const Name& prefix, const std::string& reason) { |
| 120 | onRegisterFailed(prefix, reason); |
| 121 | }); |
| 122 | RegisteredDataPrefix registeredPrefix{prefixId, 1}; |
| 123 | // Newly registered prefix |
| 124 | m_insertedDataPrefixes.emplace(std::make_pair(prefixToRegister, registeredPrefix)); |
| 125 | } |
| 126 | else { |
| 127 | check->second.useCount++; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | } // namespace repo |