blob: b349cc1b4b618fe1a956f86eac9102949ad25d2e [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)
14 , m_scope(-1)
15 , 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
30bool
31Selectors::operator == (const Selectors &other)
32{
33 return m_maxSuffixComps == other.m_maxSuffixComps
34 && m_minSuffixComps == other.m_minSuffixComps
35 && m_answerOriginKind == other.m_answerOriginKind
36 && (m_interestLifetime - other.m_interestLifetime) < 10e-4
37 && m_scope == other.m_scope
38 && m_childSelector == other.m_childSelector;
39}
40
41bool
42Selectors::isEmpty() const
43{
44 return m_maxSuffixComps == -1
45 && m_minSuffixComps == -1
46 && m_answerOriginKind == AOK_DEFAULT
47 && (m_interestLifetime - (-1.0)) < 10e-4
48 && m_scope == -1
49 && m_childSelector == DEFAULT;
50}
51
52
53CcnxCharbufPtr
54Selectors::toCcnxCharbuf() const
55{
56 if (isEmpty())
57 {
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080058 return CcnxCharbufPtr ();
Zhenkai Zhua6472e02013-01-06 14:33:37 -080059 }
60 CcnxCharbufPtr ptr(new CcnxCharbuf());
61 ccn_charbuf *cbuf = ptr->getBuf();
62 ccn_charbuf_append_tt(cbuf, CCN_DTAG_Interest, CCN_DTAG);
63 ccn_charbuf_append_tt(cbuf, CCN_DTAG_Name, CCN_DTAG);
64 ccn_charbuf_append_closer(cbuf); // </Name>
65
66 if (m_maxSuffixComps < m_minSuffixComps)
67 {
68 boost::throw_exception(InterestSelectorException() << error_info_str("MaxSuffixComps = " + boost::lexical_cast<string>(m_maxSuffixComps) + " is smaller than MinSuffixComps = " + boost::lexical_cast<string>(m_minSuffixComps)));
69 }
70
71 if (m_maxSuffixComps > 0)
72 {
73 ccnb_tagged_putf(cbuf, CCN_DTAG_MaxSuffixComponents, "%d", m_maxSuffixComps);
74 }
75
76 if (m_minSuffixComps > 0)
77 {
78 ccnb_tagged_putf(cbuf, CCN_DTAG_MinSuffixComponents, "%d", m_minSuffixComps);
79 }
80
81 if (m_answerOriginKind != AOK_DEFAULT)
82 {
83 // it was not using "ccnb_tagged_putf" in ccnx c code, no idea why
84 ccn_charbuf_append_tt(cbuf, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
85 ccnb_append_number(cbuf, m_answerOriginKind);
86 ccn_charbuf_append_closer(cbuf); // <AnswerOriginKind>
87 }
88
89 if (m_scope != -1)
90 {
91 ccnb_tagged_putf(cbuf, CCN_DTAG_Scope, "%d", m_scope);
92 }
93
94 if (m_interestLifetime > 0.0)
95 {
96 // Ccnx timestamp unit is weird 1/4096 second
97 // this is from their code
98 unsigned lifetime = 4096 * (m_interestLifetime + 1.0/8192.0);
99 if (lifetime == 0 || lifetime > (30 << 12))
100 {
101 boost::throw_exception(InterestSelectorException() << error_info_str("Ccnx requires 0 < lifetime < 30.0. lifetime= " + boost::lexical_cast<string>(m_interestLifetime)));
102 }
103 unsigned char buf[3] = {0};
104 for (int i = sizeof(buf) - 1; i >= 0; i--, lifetime >>= 8)
105 {
106 buf[i] = lifetime & 0xff;
107 }
108 ccnb_append_tagged_blob(cbuf, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
109 }
110
111 if (m_childSelector != DEFAULT)
112 {
113 ccnb_tagged_putf(cbuf, CCN_DTAG_ChildSelector, "%d", m_childSelector);
114 }
115
116
117 ccn_charbuf_append_closer(cbuf); // </Interest>
118
119 return ptr;
120}
121
122} // Ccnx