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