blob: 4e9b4408ed1ba11164b536bd0ff09f90ce60d49e [file] [log] [blame]
Zhenkai Zhua6472e02013-01-06 14:33:37 -08001#include "ccnx-selectors.h"
2#include "ccnx-common.h"
3#include <boost/lexical_cast.hpp>
4
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -08005using namespace std;
6
Zhenkai Zhua6472e02013-01-06 14:33:37 -08007namespace Ccnx {
8
9Selectors::Selectors()
10 : m_maxSuffixComps(-1)
11 , m_minSuffixComps(-1)
12 , m_answerOriginKind(AOK_DEFAULT)
13 , m_interestLifetime(-1.0)
Alexander Afanasyevefb12d72013-03-02 21:24:54 -080014 , m_scope(NO_SCOPE)
Zhenkai Zhua6472e02013-01-06 14:33:37 -080015 , m_childSelector(DEFAULT)
16{
17}
18
19Selectors::Selectors(const Selectors &other)
20{
21 m_maxSuffixComps = other.m_maxSuffixComps;
22 m_minSuffixComps = other.m_minSuffixComps;
23 m_answerOriginKind = other.m_answerOriginKind;
24 m_interestLifetime = other.m_interestLifetime;
25 m_scope = other.m_scope;
26 m_childSelector = other.m_childSelector;
27 m_publisherPublicKeyDigest = other.m_publisherPublicKeyDigest;
28}
29
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080030Selectors::Selectors(const ccn_parsed_interest *pi)
31 : m_maxSuffixComps(-1)
32 , m_minSuffixComps(-1)
33 , m_answerOriginKind(AOK_DEFAULT)
34 , m_interestLifetime(-1.0)
Alexander Afanasyevefb12d72013-03-02 21:24:54 -080035 , m_scope(NO_SCOPE)
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080036 , m_childSelector(DEFAULT)
37{
38 if (pi != NULL)
39 {
40 m_maxSuffixComps = pi->max_suffix_comps;
41 m_minSuffixComps = pi->min_suffix_comps;
42 switch(pi->orderpref)
43 {
44 case 0: m_childSelector = LEFT; break;
45 case 1: m_childSelector = RIGHT; break;
46 default: break;
47 }
48 switch(pi->answerfrom)
49 {
50 case 0x1: m_answerOriginKind = AOK_CS; break;
51 case 0x2: m_answerOriginKind = AOK_NEW; break;
52 case 0x3: m_answerOriginKind = AOK_DEFAULT; break;
53 case 0x4: m_answerOriginKind = AOK_STALE; break;
54 case 0x10: m_answerOriginKind = AOK_EXPIRE; break;
55 default: break;
56 }
Alexander Afanasyevefb12d72013-03-02 21:24:54 -080057 m_scope = static_cast<Scope> (pi->scope);
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080058 // scope and interest lifetime do not really matter to receiving application, it's only meaningful to routers
59 }
60}
61
Zhenkai Zhua6472e02013-01-06 14:33:37 -080062bool
63Selectors::operator == (const Selectors &other)
64{
65 return m_maxSuffixComps == other.m_maxSuffixComps
66 && m_minSuffixComps == other.m_minSuffixComps
67 && m_answerOriginKind == other.m_answerOriginKind
68 && (m_interestLifetime - other.m_interestLifetime) < 10e-4
69 && m_scope == other.m_scope
70 && m_childSelector == other.m_childSelector;
71}
72
73bool
74Selectors::isEmpty() const
75{
76 return m_maxSuffixComps == -1
77 && m_minSuffixComps == -1
78 && m_answerOriginKind == AOK_DEFAULT
79 && (m_interestLifetime - (-1.0)) < 10e-4
Alexander Afanasyevefb12d72013-03-02 21:24:54 -080080 && m_scope == NO_SCOPE
Zhenkai Zhua6472e02013-01-06 14:33:37 -080081 && m_childSelector == DEFAULT;
82}
83
84
85CcnxCharbufPtr
86Selectors::toCcnxCharbuf() const
87{
88 if (isEmpty())
89 {
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080090 return CcnxCharbufPtr ();
Zhenkai Zhua6472e02013-01-06 14:33:37 -080091 }
92 CcnxCharbufPtr ptr(new CcnxCharbuf());
93 ccn_charbuf *cbuf = ptr->getBuf();
94 ccn_charbuf_append_tt(cbuf, CCN_DTAG_Interest, CCN_DTAG);
95 ccn_charbuf_append_tt(cbuf, CCN_DTAG_Name, CCN_DTAG);
96 ccn_charbuf_append_closer(cbuf); // </Name>
97
98 if (m_maxSuffixComps < m_minSuffixComps)
99 {
100 boost::throw_exception(InterestSelectorException() << error_info_str("MaxSuffixComps = " + boost::lexical_cast<string>(m_maxSuffixComps) + " is smaller than MinSuffixComps = " + boost::lexical_cast<string>(m_minSuffixComps)));
101 }
102
Zhenkai Zhua6472e02013-01-06 14:33:37 -0800103 if (m_minSuffixComps > 0)
104 {
105 ccnb_tagged_putf(cbuf, CCN_DTAG_MinSuffixComponents, "%d", m_minSuffixComps);
106 }
107
Alexander Afanasyevd012a0d2013-03-14 14:53:09 -0700108 if (m_maxSuffixComps > 0)
109 {
110 ccnb_tagged_putf(cbuf, CCN_DTAG_MaxSuffixComponents, "%d", m_maxSuffixComps);
111 }
112
113 // publisher digest
114
115 // exclude
116
117 if (m_childSelector != DEFAULT)
118 {
119 ccnb_tagged_putf(cbuf, CCN_DTAG_ChildSelector, "%d", (int)m_childSelector);
120 }
121
Zhenkai Zhua6472e02013-01-06 14:33:37 -0800122 if (m_answerOriginKind != AOK_DEFAULT)
123 {
124 // it was not using "ccnb_tagged_putf" in ccnx c code, no idea why
125 ccn_charbuf_append_tt(cbuf, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
126 ccnb_append_number(cbuf, m_answerOriginKind);
127 ccn_charbuf_append_closer(cbuf); // <AnswerOriginKind>
128 }
129
Alexander Afanasyevefb12d72013-03-02 21:24:54 -0800130 if (m_scope != NO_SCOPE)
Zhenkai Zhua6472e02013-01-06 14:33:37 -0800131 {
132 ccnb_tagged_putf(cbuf, CCN_DTAG_Scope, "%d", m_scope);
133 }
134
135 if (m_interestLifetime > 0.0)
136 {
137 // Ccnx timestamp unit is weird 1/4096 second
138 // this is from their code
139 unsigned lifetime = 4096 * (m_interestLifetime + 1.0/8192.0);
140 if (lifetime == 0 || lifetime > (30 << 12))
141 {
142 boost::throw_exception(InterestSelectorException() << error_info_str("Ccnx requires 0 < lifetime < 30.0. lifetime= " + boost::lexical_cast<string>(m_interestLifetime)));
143 }
144 unsigned char buf[3] = {0};
145 for (int i = sizeof(buf) - 1; i >= 0; i--, lifetime >>= 8)
146 {
147 buf[i] = lifetime & 0xff;
148 }
149 ccnb_append_tagged_blob(cbuf, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
150 }
151
Zhenkai Zhua6472e02013-01-06 14:33:37 -0800152 ccn_charbuf_append_closer(cbuf); // </Interest>
153
154 return ptr;
155}
156
157} // Ccnx