blob: 7ebee0140138765c83d09e83f51035ba900752b2 [file] [log] [blame]
Alexander Afanasyev5ba90362013-07-15 19:58:38 -07001## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2#
3# Copyright (c) 2011-2013, Regents of the University of California
4# Alexander Afanasyev
5#
6# GNU 3.0 license, See the LICENSE file for more information
7#
8# Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
9#
10
11#
12# Based on PyCCN code, copyrighted and licensed as follows
13#
14# Copyright (c) 2011-2013, Regents of the University of California
15# BSD license, See the COPYING file for more information
16# Written by: Derek Kulinski <takeda@takeda.tk>
17# Jeff Burke <jburke@ucla.edu>
18#
19
20import ndn.Name
21
22class AOKType(utils.Flag):
23 _prefix = "ndn"
24
25AOK_NONE = AOKType.new_flag('AOK_NONE', 0x0)
26AOK_CS = AOKType.new_flag('AOK_CS', 0x1) # Answer from content store
27AOK_NEW = AOKType.new_flag('AOK_NEW', 0x2) # OK to produce new content
28AOK_STALE = AOKType.new_flag('AOK_STALE', 0x4) # OK to answer with stale data
29AOK_EXPIRE = AOKType.new_flag('AOK_EXPIRE', 0x10) # Mark as stale (requires scope 0)
30
31AOK_DEFAULT = AOK_CS | AOK_NEW
32
33CHILD_SELECTOR_LEFT = 0
34CHILD_SELECTOR_RIGHT = 1
35
36class Interest(object):
37 def __init__(self, name = None, minSuffixComponents = None,
38 maxSuffixComponents = None, publisherPublicKeyDigest = None,
39 exclude = None, childSelector = None, answerOriginKind = None,
40 scope = None, interestLifetime = None, nonce = None):
41
42 self.name = name
43
44 #
45 # Not supported at the moment
46 #
47 # self.minSuffixComponents = minSuffixComponents # default 0
48 # self.maxSuffixComponents = maxSuffixComponents # default infinity
49 # self.publisherPublicKeyDigest = publisherPublicKeyDigest # SHA256 hash
50 # self.exclude = exclude
51 # self.childSelector = childSelector
52 # self.answerOriginKind = answerOriginKind
53
54 self.scope = scope
55 self.interestLifetime = interestLifetime
56 self.nonce = nonce
57
58 # wire
59 self.wire_dirty = True
60 self.wire = None # backing charbuf
61
62 def __setattr__(self, name, value):
63 if name != "wire_dirty":
64 self.wire_dirty = True
65 object.__setattr__(self, name, value)
66
67 def __getattribute__(self, name):
68 if name == "wire":
69 # force refresh if components changed
70 if object.__getattribute__(self, 'name') and self.name.wire_dirty:
71 self.wire_dirty = True
72 elif object.__getattribute__(self, 'exclude') and self.exclude.wire_dirty:
73 self.wire_dirty = True
74
75 if object.__getattribute__(self, 'wire_dirty'):
76 self.wire = _ndn.Interest_obj_to_ccn(self)
77 self.wire_dirty = False
78 return object.__getattribute__(self, name)
79
80 def __str__(self):
81 res = []
82 res.append("name: %s" % self.name)
83 res.append("minSuffixComponents: %s" % self.minSuffixComponents)
84 res.append("maxSuffixComponents: %s" % self.maxSuffixComponents)
85 res.append("publisherPublicKeyDigest: %r" % self.publisherPublicKeyDigest)
86 res.append("exclude:\n%s" % self.exclude)
87 res.append("childSelector: %s" % self.childSelector)
88 res.append("answerOriginKind: %s" % self.answerOriginKind)
89 res.append("scope: %s" % self.scope)
90 res.append("interestLifetime: %s" % self.interestLifetime)
91 res.append("nonce: %r" % self.nonce)
92 return "\n".join(res)
93
94 def __repr__(self):
95 args = []
96
97 if self.name is not None:
98 args += ["name=%r" % self.name]
99 if self.minSuffixComponents is not None:
100 args += ["minSuffixComponents=%r" % self.minSuffixComponents]
101 if self.maxSuffixComponents is not None:
102 args += ["maxSuffixComponents=%r" % self.maxSuffixComponents]
103 if self.publisherPublicKeyDigest is not None:
104 args += ["publisherPublicKeyDigest=%r" % self.publisherPublicKeyDigest]
105 if self.exclude is not None:
106 args += ["exclude=%r" % self.exclude]
107 if self.childSelector is not None:
108 args += ["childSelector=%r" % self.childSelector]
109 if self.answerOriginKind is not None:
110 args += ["answerOriginKind=%r" % self.answerOriginKind]
111 if self.scope is not None:
112 args += ["scope=%r" % self.scope]
113 if self.interestLifetime is not None:
114 args += ["interestLifetime=%r" % self.interestLifetime]
115 if self.nonce is not None:
116 args += ["nonce=%r" % self.nonce]
117
118 return "ndn.Interest(%s)" % ", ".join(args)
119
120 def get_aok_value(self):
121 global AOK_DEFAULT
122
123 return AOK_DEFAULT if not self.answerOriginKind else self.answerOriginKind
124
125 def matches_name(self, name):
126 i_name = self.name.components
127 o_name = name.components
128
129 # requested name is longer than ours
130 if len(i_name) > len(o_name):
131 return False
132
133 # at least one of given components don't match
134 if not all(i == j for i, j in zip(i_name, o_name)):
135 return False
136
137 return True
138
139# # Bloom filters will be deprecated, so we do not support them.
140# class ExclusionFilter(object):
141# def __init__(self):
142# self.components = []
143
144# # py-ndn
145# self.wire_dirty = False
146# self.wire = None # backing charbuf
147
148# def reset(self):
149# self.components = []
150
151# def add_names(self, names):
152# self.wire_dirty = True
153# self.components.extend(sorted(names))
154
155# def add_name(self, name):
156# if not type(name) is Name.Name:
157# raise TypeError("Name type required")
158
159# self.wire_dirty = True
160# self.components.append(name)
161
162# def add_any(self):
163# self.components.append(Name.Name(name_type = Name.NAME_ANY))
164
165# def __setattr__(self, name, value):
166# if name != "wire_dirty":
167# self.wire_dirty = True
168# object.__setattr__(self, name, value)
169
170# def __getattribute__(self, name):
171# if name == "wire":
172# if object.__getattribute__(self, 'wire_dirty'):
173# self.wire = _ndn.ExclusionFilter_names_to_ccn(
174# self.components)
175# self.wire_dirty = False
176# return object.__getattribute__(self, name)
177
178# def __str__(self):
179# comps = []
180# for n in self.components:
181# comps.append(str(n))
182# return str(comps)