blob: e04afaf8054e4e9de277ac87fd9975d6be35c8da [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"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -06008#include "core/logger.hpp"
Junxiao Shi16d1b7d2014-03-27 21:29:09 -07009#include "core/global-io.hpp"
Steve DiBenedetto5b433982014-01-29 17:14:27 -070010
11namespace nfd {
12
Steve DiBenedetto3970c892014-01-31 23:31:13 -070013NFD_LOG_INIT("InternalFace");
14
15InternalFace::InternalFace()
Junxiao Shi79494162014-04-02 18:25:11 -070016 : Face(FaceUri("internal://"), FaceUri("internal://"), true)
Steve DiBenedetto5b433982014-01-29 17:14:27 -070017{
Steve DiBenedetto5b433982014-01-29 17:14:27 -070018}
19
20void
21InternalFace::sendInterest(const Interest& interest)
22{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000023 onSendInterest(interest);
24
Junxiao Shi16d1b7d2014-03-27 21:29:09 -070025 // Invoke .processInterest a bit later,
26 // to avoid potential problems in forwarding pipelines.
27 getGlobalIoService().post(bind(&InternalFace::processInterest,
28 this, boost::cref(interest)));
29}
30
31void
32InternalFace::processInterest(const Interest& interest)
33{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070034 if (m_interestFilters.size() == 0)
35 {
36 NFD_LOG_DEBUG("no Interest filters to match against");
37 return;
38 }
39
Steve DiBenedetto3970c892014-01-31 23:31:13 -070040 const Name& interestName(interest.getName());
41 NFD_LOG_DEBUG("received Interest: " << interestName);
Steve DiBenedetto5b433982014-01-29 17:14:27 -070042
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070043 std::map<Name, OnInterest>::const_iterator filter =
44 m_interestFilters.lower_bound(interestName);
Steve DiBenedetto3970c892014-01-31 23:31:13 -070045
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070046 // lower_bound gives us the first Name that is
47 // an exact match OR ordered after interestName.
48 //
49 // If we reach the end of the map, then we need
50 // only check if the before-end element is a match.
51 //
52 // If we match an element, then the current
53 // position or the previous element are potential
54 // matches.
55 //
56 // If we hit begin, the element is either an exact
57 // match or there is no matching prefix in the map.
58
59
Steve DiBenedettobdedce92014-02-02 22:49:39 -070060 if (filter == m_interestFilters.end() && filter != m_interestFilters.begin())
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070061 {
62 // We hit the end, check if the previous element
63 // is a match
64 --filter;
65 if (filter->first.isPrefixOf(interestName))
Steve DiBenedetto3970c892014-01-31 23:31:13 -070066 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070067 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (before end match)");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070068 filter->second(interestName, interest);
69 }
70 else
71 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070072 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (before end)");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070073 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -070074 }
Steve DiBenedettobdedce92014-02-02 22:49:39 -070075 else if (filter->first == interestName)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070076 {
77 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (exact match)");
78 filter->second(interestName, interest);
79 }
80 else if (filter != m_interestFilters.begin())
81 {
82 // the element we found is canonically
83 // ordered after interestName.
84 // Check the previous element.
85 --filter;
86 if (filter->first.isPrefixOf(interestName))
87 {
88 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (previous match)");
89 filter->second(interestName, interest);
90 }
91 else
92 {
93 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (previous)");
94 }
95 }
96 else
97 {
98 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (begin)");
99 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700100 //Drop Interest
101}
102
103void
104InternalFace::sendData(const Data& data)
105{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000106 onSendData(data);
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700107}
108
109void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800110InternalFace::close()
111{
112 throw Error("Internal face cannot be closed");
113}
114
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800115void
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700116InternalFace::setInterestFilter(const Name& filter,
117 OnInterest onInterest)
118{
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700119 NFD_LOG_INFO("registering callback for " << filter);
120 m_interestFilters[filter] = onInterest;
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700121}
122
123void
124InternalFace::put(const Data& data)
125{
126 onReceiveData(data);
127}
128
129InternalFace::~InternalFace()
130{
131
132}
133
134} // namespace nfd