blob: 7742266a4f3f474d00bd29f6069ebe466103791b [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -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 "pit.hpp"
8#include <algorithm>
9
10namespace ndn {
11
12Pit::Pit()
13{
14}
15
16Pit::~Pit()
17{
18}
19
20static inline bool
21operator==(const Exclude& a, const Exclude& b)
22{
23 const Block& aBlock = a.wireEncode();
24 const Block& bBlock = b.wireEncode();
25 return aBlock.size() == bBlock.size() &&
26 0 == memcmp(aBlock.wire(), bBlock.wire(), aBlock.size());
27}
28
29static inline bool
30predicate_PitEntry_similar_Interest(shared_ptr<pit::Entry> entry,
31 const Interest& interest)
32{
33 const Interest& pi = entry->getInterest();
34 return pi.getName().equals(interest.getName()) &&
35 pi.getMinSuffixComponents() == interest.getMinSuffixComponents() &&
36 pi.getMaxSuffixComponents() == interest.getMaxSuffixComponents() &&
37 // TODO PublisherPublicKeyLocator (ndn-cpp-dev #1157)
38 pi.getExclude() == interest.getExclude() &&
39 pi.getChildSelector() == interest.getChildSelector() &&
40 pi.getMustBeFresh() == interest.getMustBeFresh();
41}
42
43std::pair<shared_ptr<pit::Entry>, bool>
44Pit::insert(const Interest& interest)
45{
46 std::list<shared_ptr<pit::Entry> >::iterator it = std::find_if(
47 m_table.begin(), m_table.end(),
48 bind(&predicate_PitEntry_similar_Interest, _1, interest));
49 if (it != m_table.end()) return std::make_pair(*it, false);
50
51 shared_ptr<pit::Entry> entry = make_shared<pit::Entry>(interest);
52 m_table.push_back(entry);
53 return std::make_pair(entry, true);
54}
55
56shared_ptr<pit::DataMatchResult>
57Pit::findAllDataMatches(const Data& data) const
58{
59 shared_ptr<pit::DataMatchResult> result = make_shared<pit::DataMatchResult>();
60 for (std::list<shared_ptr<pit::Entry> >::const_iterator it = m_table.begin();
61 it != m_table.end(); ++it) {
62 shared_ptr<pit::Entry> entry = *it;
63 if (entry->getInterest().matchesName(data.getName())) {
64 result->push_back(entry);
65 }
66 }
67 return result;
68}
69
70void
71Pit::remove(shared_ptr<pit::Entry> pitEntry)
72{
73 m_table.remove(pitEntry);
74}
75
76} // namespace ndn