blob: 9fad328a4157a98331af40a477f54de88e2b1d48 [file] [log] [blame]
Steve DiBenedetto5b433982014-01-29 17:14:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev7bbe80c2014-07-10 20:07:16 -07003 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Afanasyev7bbe80c2014-07-10 20:07:16 -070024 */
Steve DiBenedetto5b433982014-01-29 17:14:27 -070025
26#include "internal-face.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Junxiao Shi16d1b7d2014-03-27 21:29:09 -070028#include "core/global-io.hpp"
Steve DiBenedetto5b433982014-01-29 17:14:27 -070029
30namespace nfd {
31
Steve DiBenedetto3970c892014-01-31 23:31:13 -070032NFD_LOG_INIT("InternalFace");
33
34InternalFace::InternalFace()
Junxiao Shi79494162014-04-02 18:25:11 -070035 : Face(FaceUri("internal://"), FaceUri("internal://"), true)
Steve DiBenedetto5b433982014-01-29 17:14:27 -070036{
Steve DiBenedetto5b433982014-01-29 17:14:27 -070037}
38
39void
40InternalFace::sendInterest(const Interest& interest)
41{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000042 onSendInterest(interest);
43
Junxiao Shi16d1b7d2014-03-27 21:29:09 -070044 // Invoke .processInterest a bit later,
45 // to avoid potential problems in forwarding pipelines.
46 getGlobalIoService().post(bind(&InternalFace::processInterest,
Alexander Afanasyev7bbe80c2014-07-10 20:07:16 -070047 this, interest.shared_from_this()));
Junxiao Shi16d1b7d2014-03-27 21:29:09 -070048}
49
50void
Alexander Afanasyev7bbe80c2014-07-10 20:07:16 -070051InternalFace::processInterest(const shared_ptr<const Interest>& interest)
Junxiao Shi16d1b7d2014-03-27 21:29:09 -070052{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070053 if (m_interestFilters.size() == 0)
54 {
55 NFD_LOG_DEBUG("no Interest filters to match against");
56 return;
57 }
58
Alexander Afanasyev7bbe80c2014-07-10 20:07:16 -070059 const Name& interestName(interest->getName());
Steve DiBenedetto3970c892014-01-31 23:31:13 -070060 NFD_LOG_DEBUG("received Interest: " << interestName);
Steve DiBenedetto5b433982014-01-29 17:14:27 -070061
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070062 std::map<Name, OnInterest>::const_iterator filter =
63 m_interestFilters.lower_bound(interestName);
Steve DiBenedetto3970c892014-01-31 23:31:13 -070064
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070065 // 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 DiBenedettobdedce92014-02-02 22:49:39 -070079 if (filter == m_interestFilters.end() && filter != m_interestFilters.begin())
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070080 {
81 // We hit the end, check if the previous element
82 // is a match
83 --filter;
84 if (filter->first.isPrefixOf(interestName))
Steve DiBenedetto3970c892014-01-31 23:31:13 -070085 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070086 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (before end match)");
Alexander Afanasyev7bbe80c2014-07-10 20:07:16 -070087 filter->second(interestName, *interest);
Steve DiBenedetto3970c892014-01-31 23:31:13 -070088 }
89 else
90 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070091 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (before end)");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070092 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -070093 }
Steve DiBenedettobdedce92014-02-02 22:49:39 -070094 else if (filter->first == interestName)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070095 {
96 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (exact match)");
Alexander Afanasyev7bbe80c2014-07-10 20:07:16 -070097 filter->second(interestName, *interest);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070098 }
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 Afanasyev7bbe80c2014-07-10 20:07:16 -0700108 filter->second(interestName, *interest);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700109 }
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 DiBenedetto5b433982014-01-29 17:14:27 -0700119 //Drop Interest
120}
121
122void
123InternalFace::sendData(const Data& data)
124{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000125 onSendData(data);
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700126}
127
128void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800129InternalFace::close()
130{
131 throw Error("Internal face cannot be closed");
132}
133
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800134void
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700135InternalFace::setInterestFilter(const Name& filter,
136 OnInterest onInterest)
137{
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700138 NFD_LOG_INFO("registering callback for " << filter);
139 m_interestFilters[filter] = onInterest;
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700140}
141
142void
143InternalFace::put(const Data& data)
144{
145 onReceiveData(data);
146}
147
148InternalFace::~InternalFace()
149{
150
151}
152
153} // namespace nfd