blob: 81bd2de7aba1f1459c6b3cb15d2c10c2e72229fa [file] [log] [blame]
Steve DiBenedetto5b433982014-01-29 17:14:27 -07001/* -*- 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 Shi16d1b7d2014-03-27 21:29:09 -07008#include "core/global-io.hpp"
Steve DiBenedetto5b433982014-01-29 17:14:27 -07009
10namespace nfd {
11
Steve DiBenedetto3970c892014-01-31 23:31:13 -070012NFD_LOG_INIT("InternalFace");
13
14InternalFace::InternalFace()
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +000015 : Face(FaceUri("internal://"), true)
Steve DiBenedetto5b433982014-01-29 17:14:27 -070016{
Steve DiBenedetto5b433982014-01-29 17:14:27 -070017}
18
19void
20InternalFace::sendInterest(const Interest& interest)
21{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000022 onSendInterest(interest);
23
Junxiao Shi16d1b7d2014-03-27 21:29:09 -070024 // 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
30void
31InternalFace::processInterest(const Interest& interest)
32{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070033 if (m_interestFilters.size() == 0)
34 {
35 NFD_LOG_DEBUG("no Interest filters to match against");
36 return;
37 }
38
Steve DiBenedetto3970c892014-01-31 23:31:13 -070039 const Name& interestName(interest.getName());
40 NFD_LOG_DEBUG("received Interest: " << interestName);
Steve DiBenedetto5b433982014-01-29 17:14:27 -070041
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070042 std::map<Name, OnInterest>::const_iterator filter =
43 m_interestFilters.lower_bound(interestName);
Steve DiBenedetto3970c892014-01-31 23:31:13 -070044
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070045 // 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 DiBenedettobdedce92014-02-02 22:49:39 -070059 if (filter == m_interestFilters.end() && filter != m_interestFilters.begin())
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070060 {
61 // We hit the end, check if the previous element
62 // is a match
63 --filter;
64 if (filter->first.isPrefixOf(interestName))
Steve DiBenedetto3970c892014-01-31 23:31:13 -070065 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070066 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (before end match)");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070067 filter->second(interestName, interest);
68 }
69 else
70 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070071 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (before end)");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070072 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -070073 }
Steve DiBenedettobdedce92014-02-02 22:49:39 -070074 else if (filter->first == interestName)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070075 {
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 DiBenedetto5b433982014-01-29 17:14:27 -070099 //Drop Interest
100}
101
102void
103InternalFace::sendData(const Data& data)
104{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000105 onSendData(data);
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700106}
107
108void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800109InternalFace::close()
110{
111 throw Error("Internal face cannot be closed");
112}
113
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800114void
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700115InternalFace::setInterestFilter(const Name& filter,
116 OnInterest onInterest)
117{
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700118 NFD_LOG_INFO("registering callback for " << filter);
119 m_interestFilters[filter] = onInterest;
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700120}
121
122void
123InternalFace::put(const Data& data)
124{
125 onReceiveData(data);
126}
127
128InternalFace::~InternalFace()
129{
130
131}
132
133} // namespace nfd