blob: de77009fab25ef38b989c381a3b5244a00a4cdcc [file] [log] [blame]
Steve DiBenedetto5b433982014-01-29 17:14:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedetto5b433982014-01-29 17:14:27 -070024
25#include "internal-face.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060026#include "core/logger.hpp"
Junxiao Shi16d1b7d2014-03-27 21:29:09 -070027#include "core/global-io.hpp"
Steve DiBenedetto5b433982014-01-29 17:14:27 -070028
29namespace nfd {
30
Steve DiBenedetto3970c892014-01-31 23:31:13 -070031NFD_LOG_INIT("InternalFace");
32
33InternalFace::InternalFace()
Junxiao Shi79494162014-04-02 18:25:11 -070034 : Face(FaceUri("internal://"), FaceUri("internal://"), true)
Steve DiBenedetto5b433982014-01-29 17:14:27 -070035{
Steve DiBenedetto5b433982014-01-29 17:14:27 -070036}
37
38void
39InternalFace::sendInterest(const Interest& interest)
40{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000041 onSendInterest(interest);
42
Junxiao Shi16d1b7d2014-03-27 21:29:09 -070043 // Invoke .processInterest a bit later,
44 // to avoid potential problems in forwarding pipelines.
Junxiao Shi72c3e042014-04-08 15:02:37 -070045 weak_ptr<const Interest> interestWeak = interest.shared_from_this();
Junxiao Shi16d1b7d2014-03-27 21:29:09 -070046 getGlobalIoService().post(bind(&InternalFace::processInterest,
Junxiao Shi72c3e042014-04-08 15:02:37 -070047 this, interestWeak));
Junxiao Shi16d1b7d2014-03-27 21:29:09 -070048}
49
50void
Junxiao Shi72c3e042014-04-08 15:02:37 -070051InternalFace::processInterest(weak_ptr<const Interest> interestWeak)
Junxiao Shi16d1b7d2014-03-27 21:29:09 -070052{
Junxiao Shi72c3e042014-04-08 15:02:37 -070053 shared_ptr<const Interest> interestPtr = interestWeak.lock();
54 if (!static_cast<bool>(interestPtr)) {
55 // Interest is gone because it's satisfied and removed from PIT
56 NFD_LOG_DEBUG("processInterest: Interest is gone");
57 return;
58 }
59 const Interest& interest = *interestPtr;
60
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070061 if (m_interestFilters.size() == 0)
62 {
63 NFD_LOG_DEBUG("no Interest filters to match against");
64 return;
65 }
66
Steve DiBenedetto3970c892014-01-31 23:31:13 -070067 const Name& interestName(interest.getName());
68 NFD_LOG_DEBUG("received Interest: " << interestName);
Steve DiBenedetto5b433982014-01-29 17:14:27 -070069
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070070 std::map<Name, OnInterest>::const_iterator filter =
71 m_interestFilters.lower_bound(interestName);
Steve DiBenedetto3970c892014-01-31 23:31:13 -070072
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070073 // lower_bound gives us the first Name that is
74 // an exact match OR ordered after interestName.
75 //
76 // If we reach the end of the map, then we need
77 // only check if the before-end element is a match.
78 //
79 // If we match an element, then the current
80 // position or the previous element are potential
81 // matches.
82 //
83 // If we hit begin, the element is either an exact
84 // match or there is no matching prefix in the map.
85
86
Steve DiBenedettobdedce92014-02-02 22:49:39 -070087 if (filter == m_interestFilters.end() && filter != m_interestFilters.begin())
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070088 {
89 // We hit the end, check if the previous element
90 // is a match
91 --filter;
92 if (filter->first.isPrefixOf(interestName))
Steve DiBenedetto3970c892014-01-31 23:31:13 -070093 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070094 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (before end match)");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070095 filter->second(interestName, interest);
96 }
97 else
98 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070099 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (before end)");
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700100 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700101 }
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700102 else if (filter->first == interestName)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700103 {
104 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (exact match)");
105 filter->second(interestName, interest);
106 }
107 else if (filter != m_interestFilters.begin())
108 {
109 // the element we found is canonically
110 // ordered after interestName.
111 // Check the previous element.
112 --filter;
113 if (filter->first.isPrefixOf(interestName))
114 {
115 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (previous match)");
116 filter->second(interestName, interest);
117 }
118 else
119 {
120 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (previous)");
121 }
122 }
123 else
124 {
125 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (begin)");
126 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700127 //Drop Interest
128}
129
130void
131InternalFace::sendData(const Data& data)
132{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000133 onSendData(data);
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700134}
135
136void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800137InternalFace::close()
138{
139 throw Error("Internal face cannot be closed");
140}
141
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800142void
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700143InternalFace::setInterestFilter(const Name& filter,
144 OnInterest onInterest)
145{
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700146 NFD_LOG_INFO("registering callback for " << filter);
147 m_interestFilters[filter] = onInterest;
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700148}
149
150void
151InternalFace::put(const Data& data)
152{
153 onReceiveData(data);
154}
155
156InternalFace::~InternalFace()
157{
158
159}
160
161} // namespace nfd