Zhenkai Zhu | a6472e0 | 2013-01-06 14:33:37 -0800 | [diff] [blame] | 1 | #include "ccnx-selectors.h" |
| 2 | #include "ccnx-common.h" |
| 3 | #include <boost/lexical_cast.hpp> |
| 4 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 5 | using namespace std; |
| 6 | |
Zhenkai Zhu | a6472e0 | 2013-01-06 14:33:37 -0800 | [diff] [blame] | 7 | namespace Ccnx { |
| 8 | |
| 9 | Selectors::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 | |
| 19 | Selectors::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 Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 30 | Selectors::Selectors(const ccn_parsed_interest *pi) |
| 31 | : m_maxSuffixComps(-1) |
| 32 | , m_minSuffixComps(-1) |
| 33 | , m_answerOriginKind(AOK_DEFAULT) |
| 34 | , m_interestLifetime(-1.0) |
| 35 | , m_scope(-1) |
| 36 | , 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 | } |
| 57 | m_scope = pi->scope; |
| 58 | // scope and interest lifetime do not really matter to receiving application, it's only meaningful to routers |
| 59 | } |
| 60 | } |
| 61 | |
Zhenkai Zhu | a6472e0 | 2013-01-06 14:33:37 -0800 | [diff] [blame] | 62 | bool |
| 63 | Selectors::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 | |
| 73 | bool |
| 74 | Selectors::isEmpty() const |
| 75 | { |
| 76 | return m_maxSuffixComps == -1 |
| 77 | && m_minSuffixComps == -1 |
| 78 | && m_answerOriginKind == AOK_DEFAULT |
| 79 | && (m_interestLifetime - (-1.0)) < 10e-4 |
| 80 | && m_scope == -1 |
| 81 | && m_childSelector == DEFAULT; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | CcnxCharbufPtr |
| 86 | Selectors::toCcnxCharbuf() const |
| 87 | { |
| 88 | if (isEmpty()) |
| 89 | { |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 90 | return CcnxCharbufPtr (); |
Zhenkai Zhu | a6472e0 | 2013-01-06 14:33:37 -0800 | [diff] [blame] | 91 | } |
| 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 | |
| 103 | if (m_maxSuffixComps > 0) |
| 104 | { |
| 105 | ccnb_tagged_putf(cbuf, CCN_DTAG_MaxSuffixComponents, "%d", m_maxSuffixComps); |
| 106 | } |
| 107 | |
| 108 | if (m_minSuffixComps > 0) |
| 109 | { |
| 110 | ccnb_tagged_putf(cbuf, CCN_DTAG_MinSuffixComponents, "%d", m_minSuffixComps); |
| 111 | } |
| 112 | |
| 113 | if (m_answerOriginKind != AOK_DEFAULT) |
| 114 | { |
| 115 | // it was not using "ccnb_tagged_putf" in ccnx c code, no idea why |
| 116 | ccn_charbuf_append_tt(cbuf, CCN_DTAG_AnswerOriginKind, CCN_DTAG); |
| 117 | ccnb_append_number(cbuf, m_answerOriginKind); |
| 118 | ccn_charbuf_append_closer(cbuf); // <AnswerOriginKind> |
| 119 | } |
| 120 | |
| 121 | if (m_scope != -1) |
| 122 | { |
| 123 | ccnb_tagged_putf(cbuf, CCN_DTAG_Scope, "%d", m_scope); |
| 124 | } |
| 125 | |
| 126 | if (m_interestLifetime > 0.0) |
| 127 | { |
| 128 | // Ccnx timestamp unit is weird 1/4096 second |
| 129 | // this is from their code |
| 130 | unsigned lifetime = 4096 * (m_interestLifetime + 1.0/8192.0); |
| 131 | if (lifetime == 0 || lifetime > (30 << 12)) |
| 132 | { |
| 133 | boost::throw_exception(InterestSelectorException() << error_info_str("Ccnx requires 0 < lifetime < 30.0. lifetime= " + boost::lexical_cast<string>(m_interestLifetime))); |
| 134 | } |
| 135 | unsigned char buf[3] = {0}; |
| 136 | for (int i = sizeof(buf) - 1; i >= 0; i--, lifetime >>= 8) |
| 137 | { |
| 138 | buf[i] = lifetime & 0xff; |
| 139 | } |
| 140 | ccnb_append_tagged_blob(cbuf, CCN_DTAG_InterestLifetime, buf, sizeof(buf)); |
| 141 | } |
| 142 | |
| 143 | if (m_childSelector != DEFAULT) |
| 144 | { |
| 145 | ccnb_tagged_putf(cbuf, CCN_DTAG_ChildSelector, "%d", m_childSelector); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | ccn_charbuf_append_closer(cbuf); // </Interest> |
| 150 | |
| 151 | return ptr; |
| 152 | } |
| 153 | |
| 154 | } // Ccnx |