blob: fe8faf34f29b94fedcbe44c4cff946f7a6b69614 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07005 * See COPYING for copyright and distribution information.
Jeff Thompsonb7f95562013-07-03 18:36:42 -07006 */
7
Jeff Thompsonfe556862013-07-09 13:52:55 -07008#include <stdexcept>
Jeff Thompson25b4e612013-10-10 16:03:24 -07009#include <ndn-cpp/common.hpp>
10#include <ndn-cpp/interest.hpp>
11#include "c/interest.h"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070012
13using namespace std;
14
15namespace ndn {
16
Jeff Thompson0050abe2013-09-17 12:50:25 -070017void
Jeff Thompsonf62f9f22013-11-26 17:22:54 -080018Exclude::Entry::get(struct ndn_ExcludeEntry& excludeEntryStruct) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070019{
20 excludeEntryStruct.type = type_;
21 if (type_ == ndn_Exclude_COMPONENT)
22 component_.get(excludeEntryStruct.component);
23}
24
25void
Jeff Thompson0050abe2013-09-17 12:50:25 -070026Exclude::get(struct ndn_Exclude& excludeStruct) const
Jeff Thompsonfe556862013-07-09 13:52:55 -070027{
28 if (excludeStruct.maxEntries < entries_.size())
29 throw runtime_error("excludeStruct.maxEntries must be >= this exclude getEntryCount()");
30
31 excludeStruct.nEntries = entries_.size();
Jeff Thompson97223af2013-09-24 17:01:27 -070032 for (size_t i = 0; i < excludeStruct.nEntries; ++i)
Jeff Thompsonfe556862013-07-09 13:52:55 -070033 entries_[i].get(excludeStruct.entries[i]);
34}
35
Jeff Thompson0050abe2013-09-17 12:50:25 -070036void
37Exclude::set(const struct ndn_Exclude& excludeStruct)
Jeff Thompsonfe556862013-07-09 13:52:55 -070038{
39 entries_.clear();
Jeff Thompson97223af2013-09-24 17:01:27 -070040 for (size_t i = 0; i < excludeStruct.nEntries; ++i) {
Jeff Thompsonfe556862013-07-09 13:52:55 -070041 ndn_ExcludeEntry *entry = &excludeStruct.entries[i];
42
43 if (entry->type == ndn_Exclude_COMPONENT)
Jeff Thompson3a715632013-10-31 11:36:35 -070044 appendComponent(entry->component.value.value, entry->component.value.length);
Jeff Thompsonfe556862013-07-09 13:52:55 -070045 else if (entry->type == ndn_Exclude_ANY)
Jeff Thompson3a715632013-10-31 11:36:35 -070046 appendAny();
Jeff Thompsonfe556862013-07-09 13:52:55 -070047 else
48 throw runtime_error("unrecognized ndn_ExcludeType");
49 }
50}
51
Jeff Thompson0050abe2013-09-17 12:50:25 -070052string
53Exclude::toUri() const
Jeff Thompson37527d62013-08-21 11:15:54 -070054{
Jeff Thompsone589c3f2013-10-12 17:30:50 -070055 if (entries_.size() == 0)
56 return "";
Jeff Thompson37527d62013-08-21 11:15:54 -070057
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 Thompson9bdb3b22013-09-12 12:42:13 -070066 Name::toEscapedString(*entries_[i].getComponent().getValue(), result);
Jeff Thompson37527d62013-08-21 11:15:54 -070067 }
68
69 return result.str();
70}
71
Jeff Thompson0050abe2013-09-17 12:50:25 -070072void
73Interest::set(const struct ndn_Interest& interestStruct)
Jeff Thompsonb7f95562013-07-03 18:36:42 -070074{
75 name_.set(interestStruct.name);
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070076 minSuffixComponents_ = interestStruct.minSuffixComponents;
77 maxSuffixComponents_ = interestStruct.maxSuffixComponents;
78
79 publisherPublicKeyDigest_.set(interestStruct.publisherPublicKeyDigest);
Jeff Thompsonfe556862013-07-09 13:52:55 -070080
81 exclude_.set(interestStruct.exclude);
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070082 childSelector_ = interestStruct.childSelector;
83 answerOriginKind_ = interestStruct.answerOriginKind;
84 scope_ = interestStruct.scope;
85 interestLifetimeMilliseconds_ = interestStruct.interestLifetimeMilliseconds;
Jeff Thompson93034532013-10-08 11:52:43 -070086 nonce_ = Blob(interestStruct.nonce);
Jeff Thompsonb7f95562013-07-03 18:36:42 -070087}
Jeff Thompsond9e278c2013-07-08 15:20:13 -070088
Jeff Thompson0050abe2013-09-17 12:50:25 -070089void
90Interest::get(struct ndn_Interest& interestStruct) const
Jeff Thompsond9e278c2013-07-08 15:20:13 -070091{
92 name_.get(interestStruct.name);
Jeff Thompsond9e278c2013-07-08 15:20:13 -070093 interestStruct.minSuffixComponents = minSuffixComponents_;
Jeff Thompsonf2e5e282013-07-08 15:26:16 -070094 interestStruct.maxSuffixComponents = maxSuffixComponents_;
Jeff Thompson8238d002013-07-10 11:56:49 -070095 publisherPublicKeyDigest_.get(interestStruct.publisherPublicKeyDigest);
Jeff Thompsonfe556862013-07-09 13:52:55 -070096 exclude_.get(interestStruct.exclude);
Jeff Thompsond9e278c2013-07-08 15:20:13 -070097 interestStruct.childSelector = childSelector_;
98 interestStruct.answerOriginKind = answerOriginKind_;
99 interestStruct.scope = scope_;
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700100 interestStruct.interestLifetimeMilliseconds = interestLifetimeMilliseconds_;
Jeff Thompson93034532013-10-08 11:52:43 -0700101 nonce_.get(interestStruct.nonce);
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700102}
Jeff Thompson13e280b2013-12-03 13:12:23 -0800103
104string
105Interest::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 Thompsonb7f95562013-07-03 18:36:42 -0700141
Jeff Thompson13e280b2013-12-03 13:12:23 -0800142 return result.str();
143}
144
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700145}
146