blob: 8b8be4a0cf4afd3eaaf590d7ee85cb559105e129 [file] [log] [blame]
Alexander Afanasyevd09871f2013-01-04 22:36:37 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080022#ifndef CCNX_NAME_H
23#define CCNX_NAME_H
24#include <boost/shared_ptr.hpp>
25#include "ccnx-common.h"
26
27namespace Ccnx {
28
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080029class CcnxCharbuf;
30typedef boost::shared_ptr<CcnxCharbuf> CcnxCharbufPtr;
31
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080032// This class is mostly used in CcnxWrapper; users should not be directly using this class
33// The main purpose of this class to is avoid manually create and destroy charbuf everytime
34class CcnxCharbuf
35{
36public:
37 CcnxCharbuf();
38 CcnxCharbuf(ccn_charbuf *buf);
Zhenkai Zhub0adefe2013-01-04 21:56:38 -080039 CcnxCharbuf(const CcnxCharbuf &other);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080040 ~CcnxCharbuf();
41
42 // expose internal data structure, use with caution!!
43 ccn_charbuf *
44 getBuf() { return m_buf; }
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080045 static CcnxCharbufPtr Null;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080046
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080047 const unsigned char *
48 buf () const
49 { return m_buf->buf; }
50
51 size_t
52 length () const
53 { return m_buf->length; }
54
Zhenkai Zhub0adefe2013-01-04 21:56:38 -080055private:
56 void init(ccn_charbuf *buf);
57
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080058protected:
59 ccn_charbuf *m_buf;
60};
61
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080062
63struct NameException:
64 virtual boost::exception, virtual exception {};
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080065
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080066class Name
67{
68public:
69 Name();
70 Name(const string &name);
71 Name(const vector<Bytes> &comps);
72 Name(const Name &other);
73 Name(const unsigned char *data, const ccn_indexbuf *comps);
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080074 Name (const unsigned char *buf, const size_t length);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080075 virtual ~Name() {}
76
77 CcnxCharbufPtr
78 toCcnxCharbuf() const;
79
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080080 operator CcnxCharbufPtr () const { return toCcnxCharbuf (); }
81
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080082 Name &
83 appendComp(const Bytes &comp);
84
85 Name &
86 appendComp(const string &compStr);
87
88 int
89 size() const {return m_comps.size();}
90
91 Bytes
92 getComp(int index) const;
93
94 // return string format of the comp
95 // if all characters are printable, simply returns the string
96 // if not, print the bytes in hex string format
97 string
98 getCompAsString(int index) const;
99
100 Name
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800101 getPartialName(int start, int n = -1) const;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800102
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800103 string
104 toString() const;
105
106 Name &
107 operator=(const Name &other);
108
109 bool
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800110 operator==(const string &str) const;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800111
112 bool
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800113 operator!=(const string &str) const;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800114
115 friend Name
116 operator+(const Name &n1, const Name &n2);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800117
118protected:
119 vector<Bytes> m_comps;
120};
121
122ostream&
123operator <<(ostream &os, const Name &name);
124
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800125bool
126operator ==(const Name &n1, const Name &n2);
127
128bool
129operator !=(const Name &n1, const Name &n2);
130
131bool
132operator <(const Name &n1, const Name &n2);
133
134
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800135struct InterestSelectorException:
136 virtual boost::exception, virtual exception {};
137
138class Selectors
139{
140public:
141 Selectors();
142 Selectors(const Selectors &other);
143 ~Selectors(){};
144
145 typedef enum
146 {
147 AOK_CS = 0x1,
148 AOK_NEW = 0x2,
149 AOK_DEFAULT = 0x3, // (AOK_CS | AOK_NEW)
150 AOK_STALE = 0x4,
151 AOK_EXPIRE = 0x10
152 } AOK;
153
154 typedef enum
155 {
156 LEFT = 0,
157 RIGHT = 1,
158 DEFAULT = 2
159 } CHILD_SELECTOR;
160
161 inline Selectors &
162 maxSuffixComps(int maxSCs) {m_maxSuffixComps = maxSCs; return *this;}
163
164 inline Selectors &
165 minSuffixComps(int minSCs) {m_minSuffixComps = minSCs; return *this;}
166
167 inline Selectors &
168 answerOriginKind(AOK kind) {m_answerOriginKind = kind; return *this;}
169
170 inline Selectors &
171 interestLifetime(int lifetime) {m_interestLifetime = lifetime; return *this;}
172
173 inline Selectors &
174 scope(int scope) {m_scope = scope; return *this;}
175
176 inline Selectors &
177 childSelector(CHILD_SELECTOR child) {m_childSelector = child; return *this;}
178
179 // this has no effect now
180 inline Selectors &
181 publisherPublicKeyDigest(const Bytes &digest) {m_publisherPublicKeyDigest = digest; return *this;}
182
183 CcnxCharbufPtr
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800184 toCcnxCharbuf() const;
185
186 bool
187 isEmpty() const;
188
189 bool
190 operator==(const Selectors &other);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800191
192private:
193 int m_maxSuffixComps;
194 int m_minSuffixComps;
195 AOK m_answerOriginKind;
196 double m_interestLifetime;
197 int m_scope;
198 CHILD_SELECTOR m_childSelector;
199 // not used now
200 Bytes m_publisherPublicKeyDigest;
201};
202
203} // Ccnx
204#endif