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