blob: 51af45b68528ea50518ee0e7db2a6fd70faf43d6 [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.
45 getGlobalIoService().post(bind(&InternalFace::processInterest,
46 this, boost::cref(interest)));
47}
48
49void
50InternalFace::processInterest(const Interest& interest)
51{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070052 if (m_interestFilters.size() == 0)
53 {
54 NFD_LOG_DEBUG("no Interest filters to match against");
55 return;
56 }
57
Steve DiBenedetto3970c892014-01-31 23:31:13 -070058 const Name& interestName(interest.getName());
59 NFD_LOG_DEBUG("received Interest: " << interestName);
Steve DiBenedetto5b433982014-01-29 17:14:27 -070060
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070061 std::map<Name, OnInterest>::const_iterator filter =
62 m_interestFilters.lower_bound(interestName);
Steve DiBenedetto3970c892014-01-31 23:31:13 -070063
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070064 // lower_bound gives us the first Name that is
65 // an exact match OR ordered after interestName.
66 //
67 // If we reach the end of the map, then we need
68 // only check if the before-end element is a match.
69 //
70 // If we match an element, then the current
71 // position or the previous element are potential
72 // matches.
73 //
74 // If we hit begin, the element is either an exact
75 // match or there is no matching prefix in the map.
76
77
Steve DiBenedettobdedce92014-02-02 22:49:39 -070078 if (filter == m_interestFilters.end() && filter != m_interestFilters.begin())
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070079 {
80 // We hit the end, check if the previous element
81 // is a match
82 --filter;
83 if (filter->first.isPrefixOf(interestName))
Steve DiBenedetto3970c892014-01-31 23:31:13 -070084 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070085 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (before end match)");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070086 filter->second(interestName, interest);
87 }
88 else
89 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070090 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (before end)");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070091 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -070092 }
Steve DiBenedettobdedce92014-02-02 22:49:39 -070093 else if (filter->first == interestName)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070094 {
95 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (exact match)");
96 filter->second(interestName, interest);
97 }
98 else if (filter != m_interestFilters.begin())
99 {
100 // the element we found is canonically
101 // ordered after interestName.
102 // Check the previous element.
103 --filter;
104 if (filter->first.isPrefixOf(interestName))
105 {
106 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (previous match)");
107 filter->second(interestName, interest);
108 }
109 else
110 {
111 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (previous)");
112 }
113 }
114 else
115 {
116 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (begin)");
117 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700118 //Drop Interest
119}
120
121void
122InternalFace::sendData(const Data& data)
123{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000124 onSendData(data);
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700125}
126
127void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800128InternalFace::close()
129{
130 throw Error("Internal face cannot be closed");
131}
132
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800133void
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700134InternalFace::setInterestFilter(const Name& filter,
135 OnInterest onInterest)
136{
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700137 NFD_LOG_INFO("registering callback for " << filter);
138 m_interestFilters[filter] = onInterest;
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700139}
140
141void
142InternalFace::put(const Data& data)
143{
144 onReceiveData(data);
145}
146
147InternalFace::~InternalFace()
148{
149
150}
151
152} // namespace nfd