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