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