blob: 8966e50102ae344d1e70f15e9a383622ad5c31d7 [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);
39 ~CcnxCharbuf();
40
41 // expose internal data structure, use with caution!!
42 ccn_charbuf *
43 getBuf() { return m_buf; }
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080044 static CcnxCharbufPtr Null;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080045
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080046 const unsigned char *
47 buf () const
48 { return m_buf->buf; }
49
50 size_t
51 length () const
52 { return m_buf->length; }
53
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080054protected:
55 ccn_charbuf *m_buf;
56};
57
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080058
59struct NameException:
60 virtual boost::exception, virtual exception {};
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080061
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080062class Name
63{
64public:
65 Name();
66 Name(const string &name);
67 Name(const vector<Bytes> &comps);
68 Name(const Name &other);
69 Name(const unsigned char *data, const ccn_indexbuf *comps);
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080070 Name (const unsigned char *buf, const size_t length);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080071 virtual ~Name() {}
72
73 CcnxCharbufPtr
74 toCcnxCharbuf() const;
75
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080076 operator CcnxCharbufPtr () const { return toCcnxCharbuf (); }
77
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080078 Name &
79 appendComp(const Bytes &comp);
80
81 Name &
82 appendComp(const string &compStr);
83
84 int
85 size() const {return m_comps.size();}
86
87 Bytes
88 getComp(int index) const;
89
90 // return string format of the comp
91 // if all characters are printable, simply returns the string
92 // if not, print the bytes in hex string format
93 string
94 getCompAsString(int index) const;
95
96 Name
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080097 getPartialName(int start, int n = -1) const;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080098
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080099 string
100 toString() const;
101
102 Name &
103 operator=(const Name &other);
104
105 bool
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800106 operator==(const string &str) const;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800107
108 bool
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800109 operator!=(const string &str) const;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800110
111 friend Name
112 operator+(const Name &n1, const Name &n2);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800113
114protected:
115 vector<Bytes> m_comps;
116};
117
118ostream&
119operator <<(ostream &os, const Name &name);
120
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800121bool
122operator ==(const Name &n1, const Name &n2);
123
124bool
125operator !=(const Name &n1, const Name &n2);
126
127bool
128operator <(const Name &n1, const Name &n2);
129
130
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800131struct InterestSelectorException:
132 virtual boost::exception, virtual exception {};
133
134class Selectors
135{
136public:
137 Selectors();
138 Selectors(const Selectors &other);
139 ~Selectors(){};
140
141 typedef enum
142 {
143 AOK_CS = 0x1,
144 AOK_NEW = 0x2,
145 AOK_DEFAULT = 0x3, // (AOK_CS | AOK_NEW)
146 AOK_STALE = 0x4,
147 AOK_EXPIRE = 0x10
148 } AOK;
149
150 typedef enum
151 {
152 LEFT = 0,
153 RIGHT = 1,
154 DEFAULT = 2
155 } CHILD_SELECTOR;
156
157 inline Selectors &
158 maxSuffixComps(int maxSCs) {m_maxSuffixComps = maxSCs; return *this;}
159
160 inline Selectors &
161 minSuffixComps(int minSCs) {m_minSuffixComps = minSCs; return *this;}
162
163 inline Selectors &
164 answerOriginKind(AOK kind) {m_answerOriginKind = kind; return *this;}
165
166 inline Selectors &
167 interestLifetime(int lifetime) {m_interestLifetime = lifetime; return *this;}
168
169 inline Selectors &
170 scope(int scope) {m_scope = scope; return *this;}
171
172 inline Selectors &
173 childSelector(CHILD_SELECTOR child) {m_childSelector = child; return *this;}
174
175 // this has no effect now
176 inline Selectors &
177 publisherPublicKeyDigest(const Bytes &digest) {m_publisherPublicKeyDigest = digest; return *this;}
178
179 CcnxCharbufPtr
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800180 toCcnxCharbuf() const;
181
182 bool
183 isEmpty() const;
184
185 bool
186 operator==(const Selectors &other);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800187
188private:
189 int m_maxSuffixComps;
190 int m_minSuffixComps;
191 AOK m_answerOriginKind;
192 double m_interestLifetime;
193 int m_scope;
194 CHILD_SELECTOR m_childSelector;
195 // not used now
196 Bytes m_publisherPublicKeyDigest;
197};
198
199} // Ccnx
200#endif