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() |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 14 | { |
| 15 | |
| 16 | } |
| 17 | |
| 18 | void |
| 19 | InternalFace::sendInterest(const Interest& interest) |
| 20 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 21 | if (m_interestFilters.size() == 0) |
| 22 | { |
| 23 | NFD_LOG_DEBUG("no Interest filters to match against"); |
| 24 | return; |
| 25 | } |
| 26 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 27 | const Name& interestName(interest.getName()); |
| 28 | NFD_LOG_DEBUG("received Interest: " << interestName); |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 29 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 30 | std::map<Name, OnInterest>::const_iterator filter = |
| 31 | m_interestFilters.lower_bound(interestName); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 32 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 33 | // lower_bound gives us the first Name that is |
| 34 | // an exact match OR ordered after interestName. |
| 35 | // |
| 36 | // If we reach the end of the map, then we need |
| 37 | // only check if the before-end element is a match. |
| 38 | // |
| 39 | // If we match an element, then the current |
| 40 | // position or the previous element are potential |
| 41 | // matches. |
| 42 | // |
| 43 | // If we hit begin, the element is either an exact |
| 44 | // match or there is no matching prefix in the map. |
| 45 | |
| 46 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame^] | 47 | if (filter == m_interestFilters.end() && filter != m_interestFilters.begin()) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 48 | { |
| 49 | // We hit the end, check if the previous element |
| 50 | // is a match |
| 51 | --filter; |
| 52 | if (filter->first.isPrefixOf(interestName)) |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 53 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 54 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (before end match)"); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 55 | filter->second(interestName, interest); |
| 56 | } |
| 57 | else |
| 58 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 59 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (before end)"); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 60 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 61 | } |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame^] | 62 | else if (filter->first == interestName) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 63 | { |
| 64 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (exact match)"); |
| 65 | filter->second(interestName, interest); |
| 66 | } |
| 67 | else if (filter != m_interestFilters.begin()) |
| 68 | { |
| 69 | // the element we found is canonically |
| 70 | // ordered after interestName. |
| 71 | // Check the previous element. |
| 72 | --filter; |
| 73 | if (filter->first.isPrefixOf(interestName)) |
| 74 | { |
| 75 | NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (previous match)"); |
| 76 | filter->second(interestName, interest); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (previous)"); |
| 81 | } |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (begin)"); |
| 86 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 87 | //Drop Interest |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | InternalFace::sendData(const Data& data) |
| 92 | { |
| 93 | |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | InternalFace::setInterestFilter(const Name& filter, |
| 98 | OnInterest onInterest) |
| 99 | { |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 100 | NFD_LOG_INFO("registering callback for " << filter); |
| 101 | m_interestFilters[filter] = onInterest; |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
| 105 | InternalFace::put(const Data& data) |
| 106 | { |
| 107 | onReceiveData(data); |
| 108 | } |
| 109 | |
| 110 | InternalFace::~InternalFace() |
| 111 | { |
| 112 | |
| 113 | } |
| 114 | |
| 115 | } // namespace nfd |