Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame^] | 10 | namespace nfd { |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 11 | |
| 12 | Pit::Pit() |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | Pit::~Pit() |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | static inline bool |
| 21 | operator==(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 | |
| 29 | static inline bool |
| 30 | predicate_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 | |
| 43 | std::pair<shared_ptr<pit::Entry>, bool> |
| 44 | Pit::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 | |
| 56 | shared_ptr<pit::DataMatchResult> |
| 57 | Pit::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 | |
| 70 | void |
| 71 | Pit::remove(shared_ptr<pit::Entry> pitEntry) |
| 72 | { |
| 73 | m_table.remove(pitEntry); |
| 74 | } |
| 75 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame^] | 76 | } // namespace nfd |