NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
internal-face.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "internal-face.hpp"
27 #include "core/logger.hpp"
28 #include "core/scheduler.hpp"
29 
30 namespace nfd {
31 
32 NFD_LOG_INIT("InternalFace");
33 
35  : Face(FaceUri("internal://"), FaceUri("internal://"), true)
36 {
37 }
38 
39 void
40 InternalFace::sendInterest(const Interest& interest)
41 {
42  onSendInterest(interest);
43 
44  // Invoke .processInterest a bit later,
45  // to avoid potential problems in forwarding pipelines.
46  scheduler::schedule(time::seconds(0),
47  bind(&InternalFace::processInterest, this, interest.shared_from_this()));
48 }
49 
50 void
51 InternalFace::processInterest(const shared_ptr<const Interest>& interest)
52 {
53  if (m_interestFilters.size() == 0)
54  {
55  NFD_LOG_DEBUG("no Interest filters to match against");
56  return;
57  }
58 
59  const Name& interestName(interest->getName());
60  NFD_LOG_DEBUG("received Interest: " << interestName);
61 
62  std::map<Name, OnInterest>::const_iterator filter =
63  m_interestFilters.lower_bound(interestName);
64 
65  // lower_bound gives us the first Name that is
66  // an exact match OR ordered after interestName.
67  //
68  // If we reach the end of the map, then we need
69  // only check if the before-end element is a match.
70  //
71  // If we match an element, then the current
72  // position or the previous element are potential
73  // matches.
74  //
75  // If we hit begin, the element is either an exact
76  // match or there is no matching prefix in the map.
77 
78 
79  if (filter == m_interestFilters.end() && filter != m_interestFilters.begin())
80  {
81  // We hit the end, check if the previous element
82  // is a match
83  --filter;
84  if (filter->first.isPrefixOf(interestName))
85  {
86  NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (before end match)");
87  filter->second(interestName, *interest);
88  }
89  else
90  {
91  NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (before end)");
92  }
93  }
94  else if (filter->first == interestName)
95  {
96  NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (exact match)");
97  filter->second(interestName, *interest);
98  }
99  else if (filter != m_interestFilters.begin())
100  {
101  // the element we found is canonically
102  // ordered after interestName.
103  // Check the previous element.
104  --filter;
105  if (filter->first.isPrefixOf(interestName))
106  {
107  NFD_LOG_DEBUG("found Interest filter for " << filter->first << " (previous match)");
108  filter->second(interestName, *interest);
109  }
110  else
111  {
112  NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (previous)");
113  }
114  }
115  else
116  {
117  NFD_LOG_DEBUG("no Interest filter found for " << interestName << " (begin)");
118  }
119  //Drop Interest
120 }
121 
122 void
123 InternalFace::sendData(const Data& data)
124 {
125  onSendData(data);
126 }
127 
128 void
130 {
131  throw Error("Internal face cannot be closed");
132 }
133 
134 void
136  OnInterest onInterest)
137 {
138  NFD_LOG_INFO("registering callback for " << filter);
139  m_interestFilters[filter] = onInterest;
140 }
141 
142 void
143 InternalFace::put(const Data& data)
144 {
145  onReceiveData(data);
146 }
147 
149 {
150 
151 }
152 
153 } // namespace nfd
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:36
EventEmitter< Data > onReceiveData
fires when a Data is received
Definition: face.hpp:84
InternalFace-related error.
virtual void sendData(const Data &data)
send a Data
virtual void put(const Data &data)
EventEmitter< Interest > onSendInterest
fires when an Interest is sent out
Definition: face.hpp:87
represents a face
Definition: face.hpp:59
virtual void setInterestFilter(const Name &filter, OnInterest onInterest)
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:37
EventEmitter< Data > onSendData
fires when a Data is sent out
Definition: face.hpp:90
EventId schedule(const time::nanoseconds &after, const std::function< void()> &event)
schedule an event
Definition: scheduler.cpp:50
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
virtual void sendInterest(const Interest &interest)
send an Interest
function< void(const Name &, const Interest &)> OnInterest
Definition: app-face.hpp:35
virtual void close()
Close the face.