Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
| 9 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 24 | |
| 25 | #include "internal-face.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 26 | #include "core/logger.hpp" |
Junxiao Shi | 16d1b7d | 2014-03-27 21:29:09 -0700 | [diff] [blame] | 27 | #include "core/global-io.hpp" |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 31 | NFD_LOG_INIT("InternalFace"); |
| 32 | |
| 33 | InternalFace::InternalFace() |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 34 | : Face(FaceUri("internal://"), FaceUri("internal://"), true) |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 35 | { |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void |
| 39 | InternalFace::sendInterest(const Interest& interest) |
| 40 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 41 | onSendInterest(interest); |
| 42 | |
Junxiao Shi | 16d1b7d | 2014-03-27 21:29:09 -0700 | [diff] [blame] | 43 | // Invoke .processInterest a bit later, |
| 44 | // to avoid potential problems in forwarding pipelines. |
Junxiao Shi | 72c3e04 | 2014-04-08 15:02:37 -0700 | [diff] [blame] | 45 | weak_ptr<const Interest> interestWeak = interest.shared_from_this(); |
Junxiao Shi | 16d1b7d | 2014-03-27 21:29:09 -0700 | [diff] [blame] | 46 | getGlobalIoService().post(bind(&InternalFace::processInterest, |
Junxiao Shi | 72c3e04 | 2014-04-08 15:02:37 -0700 | [diff] [blame] | 47 | this, interestWeak)); |
Junxiao Shi | 16d1b7d | 2014-03-27 21:29:09 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
Junxiao Shi | 72c3e04 | 2014-04-08 15:02:37 -0700 | [diff] [blame] | 51 | InternalFace::processInterest(weak_ptr<const Interest> interestWeak) |
Junxiao Shi | 16d1b7d | 2014-03-27 21:29:09 -0700 | [diff] [blame] | 52 | { |
Junxiao Shi | 72c3e04 | 2014-04-08 15:02:37 -0700 | [diff] [blame] | 53 | shared_ptr<const Interest> interestPtr = interestWeak.lock(); |
| 54 | if (!static_cast<bool>(interestPtr)) { |
| 55 | // Interest is gone because it's satisfied and removed from PIT |
| 56 | NFD_LOG_DEBUG("processInterest: Interest is gone"); |
| 57 | return; |
| 58 | } |
| 59 | const Interest& interest = *interestPtr; |
| 60 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 61 | if (m_interestFilters.size() == 0) |
| 62 | { |
| 63 | NFD_LOG_DEBUG("no Interest filters to match against"); |
| 64 | return; |
| 65 | } |
| 66 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 67 | const Name& interestName(interest.getName()); |
| 68 | NFD_LOG_DEBUG("received Interest: " << interestName); |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 69 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 70 | std::map<Name, OnInterest>::const_iterator filter = |
| 71 | m_interestFilters.lower_bound(interestName); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 72 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 73 | // lower_bound gives us the first Name that is |
| 74 | // an exact match OR ordered after interestName. |
| 75 | // |
| 76 | // If we reach the end of the map, then we need |
| 77 | // only check if the before-end element is a match. |
| 78 | // |
| 79 | // If we match an element, then the current |
| 80 | // position or the previous element are potential |
| 81 | // matches. |
| 82 | // |
| 83 | // If we hit begin, the element is either an exact |
| 84 | // match or there is no matching prefix in the map. |
| 85 | |
| 86 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 87 | if (filter == m_interestFilters.end() && filter != m_interestFilters.begin()) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 88 | { |
| 89 | // We hit the end, check if the previous element |
| 90 | // is a match |
| 91 | --filter; |
| 92 | if (filter->first.isPrefixOf(interestName)) |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 93 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 94 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (before end match)"); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 95 | filter->second(interestName, interest); |
| 96 | } |
| 97 | else |
| 98 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 99 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (before end)"); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 100 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 101 | } |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 102 | else if (filter->first == interestName) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 103 | { |
| 104 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (exact match)"); |
| 105 | filter->second(interestName, interest); |
| 106 | } |
| 107 | else if (filter != m_interestFilters.begin()) |
| 108 | { |
| 109 | // the element we found is canonically |
| 110 | // ordered after interestName. |
| 111 | // Check the previous element. |
| 112 | --filter; |
| 113 | if (filter->first.isPrefixOf(interestName)) |
| 114 | { |
| 115 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (previous match)"); |
| 116 | filter->second(interestName, interest); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (previous)"); |
| 121 | } |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (begin)"); |
| 126 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 127 | //Drop Interest |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | InternalFace::sendData(const Data& data) |
| 132 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 133 | onSendData(data); |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 137 | InternalFace::close() |
| 138 | { |
| 139 | throw Error("Internal face cannot be closed"); |
| 140 | } |
| 141 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 142 | void |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 143 | InternalFace::setInterestFilter(const Name& filter, |
| 144 | OnInterest onInterest) |
| 145 | { |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 146 | NFD_LOG_INFO("registering callback for " << filter); |
| 147 | m_interestFilters[filter] = onInterest; |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void |
| 151 | InternalFace::put(const Data& data) |
| 152 | { |
| 153 | onReceiveData(data); |
| 154 | } |
| 155 | |
| 156 | InternalFace::~InternalFace() |
| 157 | { |
| 158 | |
| 159 | } |
| 160 | |
| 161 | } // namespace nfd |