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> |
| 11 | #include "c/interest.h" |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 12 | |
| 13 | using namespace std; |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 17 | void |
Jeff Thompson | f62f9f2 | 2013-11-26 17:22:54 -0800 | [diff] [blame] | 18 | Exclude::Entry::get(struct ndn_ExcludeEntry& excludeEntryStruct) const |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 19 | { |
| 20 | excludeEntryStruct.type = type_; |
| 21 | if (type_ == ndn_Exclude_COMPONENT) |
| 22 | component_.get(excludeEntryStruct.component); |
| 23 | } |
| 24 | |
| 25 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 26 | Exclude::get(struct ndn_Exclude& excludeStruct) const |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 27 | { |
| 28 | if (excludeStruct.maxEntries < entries_.size()) |
| 29 | throw runtime_error("excludeStruct.maxEntries must be >= this exclude getEntryCount()"); |
| 30 | |
| 31 | excludeStruct.nEntries = entries_.size(); |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 32 | for (size_t i = 0; i < excludeStruct.nEntries; ++i) |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 33 | entries_[i].get(excludeStruct.entries[i]); |
| 34 | } |
| 35 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 36 | void |
| 37 | Exclude::set(const struct ndn_Exclude& excludeStruct) |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 38 | { |
| 39 | entries_.clear(); |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 40 | for (size_t i = 0; i < excludeStruct.nEntries; ++i) { |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 41 | ndn_ExcludeEntry *entry = &excludeStruct.entries[i]; |
| 42 | |
| 43 | if (entry->type == ndn_Exclude_COMPONENT) |
Jeff Thompson | 3a71563 | 2013-10-31 11:36:35 -0700 | [diff] [blame] | 44 | appendComponent(entry->component.value.value, entry->component.value.length); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 45 | else if (entry->type == ndn_Exclude_ANY) |
Jeff Thompson | 3a71563 | 2013-10-31 11:36:35 -0700 | [diff] [blame] | 46 | appendAny(); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 47 | else |
| 48 | throw runtime_error("unrecognized ndn_ExcludeType"); |
| 49 | } |
| 50 | } |
| 51 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 52 | string |
| 53 | Exclude::toUri() const |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 54 | { |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 55 | if (entries_.size() == 0) |
| 56 | return ""; |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 57 | |
| 58 | ostringstream result; |
| 59 | for (unsigned i = 0; i < entries_.size(); ++i) { |
| 60 | if (i > 0) |
| 61 | result << ","; |
| 62 | |
| 63 | if (entries_[i].getType() == ndn_Exclude_ANY) |
| 64 | result << "*"; |
| 65 | else |
Jeff Thompson | 9bdb3b2 | 2013-09-12 12:42:13 -0700 | [diff] [blame] | 66 | Name::toEscapedString(*entries_[i].getComponent().getValue(), result); |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return result.str(); |
| 70 | } |
| 71 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 72 | void |
| 73 | Interest::set(const struct ndn_Interest& interestStruct) |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 74 | { |
| 75 | name_.set(interestStruct.name); |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 76 | minSuffixComponents_ = interestStruct.minSuffixComponents; |
| 77 | maxSuffixComponents_ = interestStruct.maxSuffixComponents; |
| 78 | |
| 79 | publisherPublicKeyDigest_.set(interestStruct.publisherPublicKeyDigest); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 80 | |
| 81 | exclude_.set(interestStruct.exclude); |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 82 | childSelector_ = interestStruct.childSelector; |
| 83 | answerOriginKind_ = interestStruct.answerOriginKind; |
| 84 | scope_ = interestStruct.scope; |
| 85 | interestLifetimeMilliseconds_ = interestStruct.interestLifetimeMilliseconds; |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 86 | nonce_ = Blob(interestStruct.nonce); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 87 | } |
Jeff Thompson | d9e278c | 2013-07-08 15:20:13 -0700 | [diff] [blame] | 88 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 89 | void |
| 90 | Interest::get(struct ndn_Interest& interestStruct) const |
Jeff Thompson | d9e278c | 2013-07-08 15:20:13 -0700 | [diff] [blame] | 91 | { |
| 92 | name_.get(interestStruct.name); |
Jeff Thompson | d9e278c | 2013-07-08 15:20:13 -0700 | [diff] [blame] | 93 | interestStruct.minSuffixComponents = minSuffixComponents_; |
Jeff Thompson | f2e5e28 | 2013-07-08 15:26:16 -0700 | [diff] [blame] | 94 | interestStruct.maxSuffixComponents = maxSuffixComponents_; |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 95 | publisherPublicKeyDigest_.get(interestStruct.publisherPublicKeyDigest); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 96 | exclude_.get(interestStruct.exclude); |
Jeff Thompson | d9e278c | 2013-07-08 15:20:13 -0700 | [diff] [blame] | 97 | interestStruct.childSelector = childSelector_; |
| 98 | interestStruct.answerOriginKind = answerOriginKind_; |
| 99 | interestStruct.scope = scope_; |
Jeff Thompson | 5a5e8b7 | 2013-07-11 14:28:03 -0700 | [diff] [blame] | 100 | interestStruct.interestLifetimeMilliseconds = interestLifetimeMilliseconds_; |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 101 | nonce_.get(interestStruct.nonce); |
Jeff Thompson | d9e278c | 2013-07-08 15:20:13 -0700 | [diff] [blame] | 102 | } |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 103 | |
| 104 | string |
| 105 | Interest::toUri() const |
| 106 | { |
| 107 | ostringstream selectors; |
| 108 | |
| 109 | if (minSuffixComponents_ >= 0) |
| 110 | selectors << "&ndn.MinSuffixComponents=" << minSuffixComponents_; |
| 111 | if (maxSuffixComponents_ >= 0) |
| 112 | selectors << "&ndn.MaxSuffixComponents=" << maxSuffixComponents_; |
| 113 | if (childSelector_ >= 0) |
| 114 | selectors << "&ndn.ChildSelector=" << childSelector_; |
| 115 | if (answerOriginKind_ >= 0) |
| 116 | selectors << "&ndn.AnswerOriginKind=" << answerOriginKind_; |
| 117 | if (scope_ >= 0) |
| 118 | selectors << "&ndn.Scope=" << scope_; |
| 119 | if (interestLifetimeMilliseconds_ >= 0) |
| 120 | selectors << "&ndn.InterestLifetime=" << interestLifetimeMilliseconds_; |
| 121 | if (publisherPublicKeyDigest_.getPublisherPublicKeyDigest().size() > 0) { |
| 122 | selectors << "&ndn.PublisherPublicKeyDigest="; |
| 123 | Name::toEscapedString(*publisherPublicKeyDigest_.getPublisherPublicKeyDigest(), selectors); |
| 124 | } |
| 125 | if (nonce_.size() > 0) { |
| 126 | selectors << "&ndn.Nonce="; |
| 127 | Name::toEscapedString(*nonce_, selectors); |
| 128 | } |
| 129 | if (exclude_.size() > 0) |
| 130 | selectors << "&ndn.Exclude=" << exclude_.toUri(); |
| 131 | |
| 132 | ostringstream result; |
| 133 | |
| 134 | result << name_.toUri(); |
| 135 | string selectorsString(selectors.str()); |
| 136 | if (selectorsString.size() > 0) { |
| 137 | // Replace the first & with ?. |
| 138 | result << "?"; |
| 139 | result.write(&selectorsString[1], selectorsString.size() - 1); |
| 140 | } |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 141 | |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 142 | return result.str(); |
| 143 | } |
| 144 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 145 | } |
| 146 | |