blob: 5552d0282064584a72852bef1405cbefc2b5c0a5 [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"
8
9namespace nfd {
10
Steve DiBenedetto3970c892014-01-31 23:31:13 -070011NFD_LOG_INIT("InternalFace");
12
13InternalFace::InternalFace()
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +000014 : Face(FaceUri("internal://"), true)
Steve DiBenedetto5b433982014-01-29 17:14:27 -070015{
Steve DiBenedetto5b433982014-01-29 17:14:27 -070016}
17
18void
19InternalFace::sendInterest(const Interest& interest)
20{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000021 onSendInterest(interest);
22
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070023 if (m_interestFilters.size() == 0)
24 {
25 NFD_LOG_DEBUG("no Interest filters to match against");
26 return;
27 }
28
Steve DiBenedetto3970c892014-01-31 23:31:13 -070029 const Name& interestName(interest.getName());
30 NFD_LOG_DEBUG("received Interest: " << interestName);
Steve DiBenedetto5b433982014-01-29 17:14:27 -070031
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070032 std::map<Name, OnInterest>::const_iterator filter =
33 m_interestFilters.lower_bound(interestName);
Steve DiBenedetto3970c892014-01-31 23:31:13 -070034
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070035 // lower_bound gives us the first Name that is
36 // an exact match OR ordered after interestName.
37 //
38 // If we reach the end of the map, then we need
39 // only check if the before-end element is a match.
40 //
41 // If we match an element, then the current
42 // position or the previous element are potential
43 // matches.
44 //
45 // If we hit begin, the element is either an exact
46 // match or there is no matching prefix in the map.
47
48
Steve DiBenedettobdedce92014-02-02 22:49:39 -070049 if (filter == m_interestFilters.end() && filter != m_interestFilters.begin())
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070050 {
51 // We hit the end, check if the previous element
52 // is a match
53 --filter;
54 if (filter->first.isPrefixOf(interestName))
Steve DiBenedetto3970c892014-01-31 23:31:13 -070055 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070056 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (before end match)");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070057 filter->second(interestName, interest);
58 }
59 else
60 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070061 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (before end)");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070062 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -070063 }
Steve DiBenedettobdedce92014-02-02 22:49:39 -070064 else if (filter->first == interestName)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070065 {
66 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (exact match)");
67 filter->second(interestName, interest);
68 }
69 else if (filter != m_interestFilters.begin())
70 {
71 // the element we found is canonically
72 // ordered after interestName.
73 // Check the previous element.
74 --filter;
75 if (filter->first.isPrefixOf(interestName))
76 {
77 NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (previous match)");
78 filter->second(interestName, interest);
79 }
80 else
81 {
82 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (previous)");
83 }
84 }
85 else
86 {
87 NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (begin)");
88 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -070089 //Drop Interest
90}
91
92void
93InternalFace::sendData(const Data& data)
94{
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000095 onSendData(data);
Steve DiBenedetto5b433982014-01-29 17:14:27 -070096}
97
98void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080099InternalFace::close()
100{
101 throw Error("Internal face cannot be closed");
102}
103
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800104void
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700105InternalFace::setInterestFilter(const Name& filter,
106 OnInterest onInterest)
107{
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700108 NFD_LOG_INFO("registering callback for " << filter);
109 m_interestFilters[filter] = onInterest;
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700110}
111
112void
113InternalFace::put(const Data& data)
114{
115 onReceiveData(data);
116}
117
118InternalFace::~InternalFace()
119{
120
121}
122
123} // namespace nfd