Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "internal-face.hpp" |
| 8 | |
| 9 | namespace nfd { |
| 10 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 11 | NFD_LOG_INIT("InternalFace"); |
| 12 | |
| 13 | InternalFace::InternalFace() |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 14 | : Face(FaceUri("internal://"), true) |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 15 | { |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | void |
| 19 | InternalFace::sendInterest(const Interest& interest) |
| 20 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 21 | onSendInterest(interest); |
| 22 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 23 | if (m_interestFilters.size() == 0) |
| 24 | { |
| 25 | NFD_LOG_DEBUG("no Interest filters to match against"); |
| 26 | return; |
| 27 | } |
| 28 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 29 | const Name& interestName(interest.getName()); |
| 30 | NFD_LOG_DEBUG("received Interest: " << interestName); |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 31 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 32 | std::map<Name, OnInterest>::const_iterator filter = |
| 33 | m_interestFilters.lower_bound(interestName); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 34 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 35 | // lower_bound gives us the first Name that is |
| 36 | // an exact match OR ordered after interestName. |
| 37 | // |
| 38 | // If we reach the end of the map, then we need |
| 39 | // only check if the before-end element is a match. |
| 40 | // |
| 41 | // If we match an element, then the current |
| 42 | // position or the previous element are potential |
| 43 | // matches. |
| 44 | // |
| 45 | // If we hit begin, the element is either an exact |
| 46 | // match or there is no matching prefix in the map. |
| 47 | |
| 48 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 49 | if (filter == m_interestFilters.end() && filter != m_interestFilters.begin()) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 50 | { |
| 51 | // We hit the end, check if the previous element |
| 52 | // is a match |
| 53 | --filter; |
| 54 | if (filter->first.isPrefixOf(interestName)) |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 55 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 56 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (before end match)"); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 57 | filter->second(interestName, interest); |
| 58 | } |
| 59 | else |
| 60 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 61 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (before end)"); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 62 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 63 | } |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 64 | else if (filter->first == interestName) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 65 | { |
| 66 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (exact match)"); |
| 67 | filter->second(interestName, interest); |
| 68 | } |
| 69 | else if (filter != m_interestFilters.begin()) |
| 70 | { |
| 71 | // the element we found is canonically |
| 72 | // ordered after interestName. |
| 73 | // Check the previous element. |
| 74 | --filter; |
| 75 | if (filter->first.isPrefixOf(interestName)) |
| 76 | { |
| 77 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (previous match)"); |
| 78 | filter->second(interestName, interest); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (previous)"); |
| 83 | } |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (begin)"); |
| 88 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 89 | //Drop Interest |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | InternalFace::sendData(const Data& data) |
| 94 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 95 | onSendData(data); |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 99 | InternalFace::close() |
| 100 | { |
| 101 | throw Error("Internal face cannot be closed"); |
| 102 | } |
| 103 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 104 | void |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 105 | InternalFace::setInterestFilter(const Name& filter, |
| 106 | OnInterest onInterest) |
| 107 | { |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 108 | NFD_LOG_INFO("registering callback for " << filter); |
| 109 | m_interestFilters[filter] = onInterest; |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
| 113 | InternalFace::put(const Data& data) |
| 114 | { |
| 115 | onReceiveData(data); |
| 116 | } |
| 117 | |
| 118 | InternalFace::~InternalFace() |
| 119 | { |
| 120 | |
| 121 | } |
| 122 | |
| 123 | } // namespace nfd |