blob: 2d016ca59b8a99edcff57a9b0fc751f18232428d [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-entry.hpp"
8#include <algorithm>
9
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080010namespace nfd {
Junxiao Shicbba04c2014-01-26 14:21:22 -070011namespace pit {
12
13Entry::Entry(const Interest& interest)
14 : m_interest(interest)
15{
16}
17
18const Name&
19Entry::getName() const
20{
21 return m_interest.getName();
22}
23
24const InRecordCollection&
25Entry::getInRecords() const
26{
27 return m_inRecords;
28}
29
30const OutRecordCollection&
31Entry::getOutRecords() const
32{
33 return m_outRecords;
34}
35
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070036static inline bool
37predicate_FaceRecord_Face(const FaceRecord& faceRecord, shared_ptr<Face> face)
38{
39 return faceRecord.getFace() == face;
40}
41
42static inline bool
43predicate_FaceRecord_ne_Face_and_unexpired(const FaceRecord& faceRecord,
44 shared_ptr<Face> face, time::Point now)
45{
46 return faceRecord.getFace() != face && faceRecord.getExpiry() >= now;
47}
48
49bool
50Entry::canForwardTo(shared_ptr<Face> face) const
51{
52 OutRecordCollection::const_iterator outIt = std::find_if(
53 m_outRecords.begin(), m_outRecords.end(),
54 bind(&predicate_FaceRecord_Face, _1, face));
55 bool hasUnexpiredOutRecord = outIt != m_outRecords.end() &&
56 outIt->getExpiry() >= time::now();
57 if (hasUnexpiredOutRecord) {
58 return false;
59 }
60
61 InRecordCollection::const_iterator inIt = std::find_if(
62 m_inRecords.begin(), m_inRecords.end(),
63 bind(&predicate_FaceRecord_ne_Face_and_unexpired, _1, face, time::now()));
64 bool hasUnexpiredOtherInRecord = inIt != m_inRecords.end();
65 return hasUnexpiredOtherInRecord;
66}
67
Junxiao Shicbba04c2014-01-26 14:21:22 -070068bool
Junxiao Shid3c792f2014-01-30 00:46:13 -070069Entry::addNonce(uint32_t nonce)
Junxiao Shicbba04c2014-01-26 14:21:22 -070070{
Junxiao Shid3c792f2014-01-30 00:46:13 -070071 std::pair<std::set<uint32_t>::iterator, bool> insertResult =
72 m_nonces.insert(nonce);
73
74 return insertResult.second;
Junxiao Shicbba04c2014-01-26 14:21:22 -070075}
76
Junxiao Shicbba04c2014-01-26 14:21:22 -070077InRecordCollection::iterator
78Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest)
79{
80 InRecordCollection::iterator it = std::find_if(m_inRecords.begin(),
81 m_inRecords.end(), bind(&predicate_FaceRecord_Face, _1, face));
82 if (it == m_inRecords.end()) {
83 m_inRecords.push_front(InRecord(face));
84 it = m_inRecords.begin();
85 }
86
87 it->update(interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -070088 return it;
89}
90
91void
92Entry::deleteInRecords()
93{
94 m_inRecords.clear();
95}
96
97OutRecordCollection::iterator
98Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest)
99{
100 OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
101 m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face));
102 if (it == m_outRecords.end()) {
103 m_outRecords.push_front(OutRecord(face));
104 it = m_outRecords.begin();
105 }
106
107 it->update(interest);
108 m_nonces.insert(interest.getNonce());
109 return it;
110}
111
112void
113Entry::deleteOutRecord(shared_ptr<Face> face)
114{
115 OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
116 m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face));
117 if (it != m_outRecords.end()) {
118 m_outRecords.erase(it);
119 }
120}
121
122
123} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800124} // namespace nfd