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. |
| 45 | getGlobalIoService().post(bind(&InternalFace::processInterest, |
| 46 | this, boost::cref(interest))); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | InternalFace::processInterest(const Interest& interest) |
| 51 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 52 | if (m_interestFilters.size() == 0) |
| 53 | { |
| 54 | NFD_LOG_DEBUG("no Interest filters to match against"); |
| 55 | return; |
| 56 | } |
| 57 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 58 | const Name& interestName(interest.getName()); |
| 59 | NFD_LOG_DEBUG("received Interest: " << interestName); |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 60 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 61 | std::map<Name, OnInterest>::const_iterator filter = |
| 62 | m_interestFilters.lower_bound(interestName); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 63 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 64 | // lower_bound gives us the first Name that is |
| 65 | // an exact match OR ordered after interestName. |
| 66 | // |
| 67 | // If we reach the end of the map, then we need |
| 68 | // only check if the before-end element is a match. |
| 69 | // |
| 70 | // If we match an element, then the current |
| 71 | // position or the previous element are potential |
| 72 | // matches. |
| 73 | // |
| 74 | // If we hit begin, the element is either an exact |
| 75 | // match or there is no matching prefix in the map. |
| 76 | |
| 77 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 78 | if (filter == m_interestFilters.end() && filter != m_interestFilters.begin()) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 79 | { |
| 80 | // We hit the end, check if the previous element |
| 81 | // is a match |
| 82 | --filter; |
| 83 | if (filter->first.isPrefixOf(interestName)) |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 84 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 85 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (before end match)"); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 86 | filter->second(interestName, interest); |
| 87 | } |
| 88 | else |
| 89 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 90 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (before end)"); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 91 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 92 | } |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 93 | else if (filter->first == interestName) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 94 | { |
| 95 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (exact match)"); |
| 96 | filter->second(interestName, interest); |
| 97 | } |
| 98 | else if (filter != m_interestFilters.begin()) |
| 99 | { |
| 100 | // the element we found is canonically |
| 101 | // ordered after interestName. |
| 102 | // Check the previous element. |
| 103 | --filter; |
| 104 | if (filter->first.isPrefixOf(interestName)) |
| 105 | { |
| 106 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (previous match)"); |
| 107 | filter->second(interestName, interest); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (previous)"); |
| 112 | } |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (begin)"); |
| 117 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 118 | //Drop Interest |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | InternalFace::sendData(const Data& data) |
| 123 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 124 | onSendData(data); |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 128 | InternalFace::close() |
| 129 | { |
| 130 | throw Error("Internal face cannot be closed"); |
| 131 | } |
| 132 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 133 | void |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 134 | InternalFace::setInterestFilter(const Name& filter, |
| 135 | OnInterest onInterest) |
| 136 | { |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 137 | NFD_LOG_INFO("registering callback for " << filter); |
| 138 | m_interestFilters[filter] = onInterest; |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void |
| 142 | InternalFace::put(const Data& data) |
| 143 | { |
| 144 | onReceiveData(data); |
| 145 | } |
| 146 | |
| 147 | InternalFace::~InternalFace() |
| 148 | { |
| 149 | |
| 150 | } |
| 151 | |
| 152 | } // namespace nfd |