Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 8 | #include <stdexcept> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 9 | #include <ndn-cpp/common.hpp> |
| 10 | #include <ndn-cpp/interest.hpp> |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 11 | |
| 12 | using namespace std; |
| 13 | |
| 14 | namespace ndn { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 15 | |
| 16 | bool |
| 17 | Interest::matchesName(const Name &name) const |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 18 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 19 | if (!name_.isPrefixOf(name)) |
| 20 | return false; |
| 21 | |
| 22 | if (minSuffixComponents_ >= 0 && |
| 23 | // Add 1 for the implicit digest. |
| 24 | !(name.size() + 1 - name_.size() >= minSuffixComponents_)) |
| 25 | return false; |
| 26 | |
| 27 | if (maxSuffixComponents_ >= 0 && |
| 28 | // Add 1 for the implicit digest. |
| 29 | !(name.size() + 1 - name_.size() <= maxSuffixComponents_)) |
| 30 | return false; |
| 31 | |
| 32 | if (!exclude_.empty() && name.size() > name_.size() && |
| 33 | exclude_.isExcluded(name[name_.size()])) |
| 34 | return false; |
| 35 | |
| 36 | return true; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 39 | std::ostream & |
| 40 | operator << (std::ostream &os, const Interest &interest) |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 41 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 42 | os << interest.getName(); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 44 | char delim = '?'; |
| 45 | |
| 46 | if (interest.getMinSuffixComponents() >= 0) { |
| 47 | os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents(); |
| 48 | delim = '&'; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 49 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 50 | if (interest.getMaxSuffixComponents() >= 0) { |
| 51 | os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents(); |
| 52 | delim = '&'; |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 53 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 54 | if (interest.getChildSelector() >= 0) { |
| 55 | os << delim << "ndn.ChildSelector=" << interest.getChildSelector(); |
| 56 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 57 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 58 | if (interest.getMustBeFresh()) { |
| 59 | os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh(); |
| 60 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 61 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 62 | if (interest.getScope() >= 0) { |
| 63 | os << delim << "ndn.Scope=" << interest.getScope(); |
| 64 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 65 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 66 | if (interest.getInterestLifetime() >= 0) { |
| 67 | os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime(); |
| 68 | delim = '&'; |
| 69 | } |
| 70 | |
| 71 | if (interest.getNonce() > 0) { |
| 72 | os << delim << "ndn.Nonce=" << interest.getNonce(); |
| 73 | delim = '&'; |
| 74 | } |
| 75 | if (!interest.getExclude().empty()) { |
| 76 | os << delim << "ndn.Exclude=" << interest.getExclude(); |
| 77 | delim = '&'; |
| 78 | } |
| 79 | |
| 80 | return os; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 83 | const Block& |
| 84 | Interest::wireEncode() const |
| 85 | { |
| 86 | if (wire_.hasWire()) |
| 87 | return wire_; |
| 88 | |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 89 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 90 | // Name |
| 91 | // Selectors? |
| 92 | // Nonce |
| 93 | // Scope? |
| 94 | // InterestLifetime? |
| 95 | |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 96 | wire_ = Block(Tlv::Interest); |
| 97 | wire_.push_back(getName().wireEncode()); |
| 98 | |
| 99 | // selectors |
| 100 | { |
| 101 | Block selectors(Tlv::Selectors); |
| 102 | |
| 103 | if (getMinSuffixComponents() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 104 | selectors.push_back |
| 105 | (nonNegativeIntegerBlock(Tlv::MinSuffixComponents, getMinSuffixComponents())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 106 | } |
| 107 | if (getMaxSuffixComponents() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 108 | selectors.push_back |
| 109 | (nonNegativeIntegerBlock(Tlv::MaxSuffixComponents, getMaxSuffixComponents())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 110 | } |
| 111 | if (!getExclude().empty()) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 112 | selectors.push_back |
| 113 | (getExclude().wireEncode()); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 114 | } |
| 115 | if (getChildSelector() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 116 | selectors.push_back |
| 117 | (nonNegativeIntegerBlock(Tlv::ChildSelector, getChildSelector())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 118 | } |
| 119 | if (getMustBeFresh()) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 120 | selectors.push_back |
| 121 | (booleanBlock(Tlv::MustBeFresh)); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | selectors.encode(); |
| 125 | wire_.push_back(selectors); |
| 126 | } |
| 127 | |
| 128 | // Nonce |
| 129 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 130 | wire_.push_back |
| 131 | (nonNegativeIntegerBlock(Tlv::Nonce, getNonce())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | if (getScope() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 135 | wire_.push_back |
| 136 | (nonNegativeIntegerBlock(Tlv::Scope, getScope())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 137 | } |
| 138 | if (getInterestLifetime() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 139 | wire_.push_back |
| 140 | (nonNegativeIntegerBlock(Tlv::InterestLifetime, getInterestLifetime())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | wire_.encode(); |
| 144 | return wire_; |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | Interest::wireDecode(const Block &wire) |
| 149 | { |
| 150 | wire_ = wire; |
| 151 | wire_.parse(); |
| 152 | |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 153 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 154 | // Name |
| 155 | // Selectors? |
| 156 | // Nonce |
| 157 | // Scope? |
| 158 | // InterestLifetime? |
| 159 | |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 160 | // Name |
| 161 | name_.wireDecode(wire_.get(Tlv::Name)); |
| 162 | |
| 163 | // Selectors |
| 164 | Block::element_iterator selectors = wire_.find(Tlv::Selectors); |
| 165 | if (selectors != wire_.getAll().end()) |
| 166 | { |
| 167 | selectors->parse(); |
| 168 | |
| 169 | // MinSuffixComponents |
| 170 | Block::element_iterator val = selectors->find(Tlv::MinSuffixComponents); |
| 171 | if (val != selectors->getAll().end()) |
| 172 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 173 | minSuffixComponents_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // MaxSuffixComponents |
| 177 | val = selectors->find(Tlv::MaxSuffixComponents); |
| 178 | if (val != selectors->getAll().end()) |
| 179 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 180 | maxSuffixComponents_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Exclude |
| 184 | val = selectors->find(Tlv::Exclude); |
| 185 | if (val != selectors->getAll().end()) |
| 186 | { |
| 187 | exclude_.wireDecode(*val); |
| 188 | } |
| 189 | |
| 190 | // ChildSelector |
| 191 | val = selectors->find(Tlv::ChildSelector); |
| 192 | if (val != selectors->getAll().end()) |
| 193 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 194 | childSelector_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | //MustBeFresh aka AnswerOriginKind |
| 198 | val = selectors->find(Tlv::MustBeFresh); |
| 199 | if (val != selectors->getAll().end()) |
| 200 | { |
| 201 | mustBeFresh_ = true; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Nonce |
| 206 | Block::element_iterator val = wire_.find(Tlv::Nonce); |
| 207 | if (val != wire_.getAll().end()) |
| 208 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 209 | nonce_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Scope |
| 213 | val = wire_.find(Tlv::Scope); |
| 214 | if (val != wire_.getAll().end()) |
| 215 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 216 | scope_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // InterestLifetime |
| 220 | val = wire_.find(Tlv::InterestLifetime); |
| 221 | if (val != wire_.getAll().end()) |
| 222 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame^] | 223 | interestLifetime_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 228 | } |