blob: 8d92201cbb9bc25b7739a6a04959d81ed1833224 [file] [log] [blame]
Alexander Afanasyev39f53162013-07-18 16:24:31 -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 ns.ndnSIM
21from Name import Name
22
23class Data (object):
24 _data = None
25
26 def __init__(self,
27 name = None, content = None, signed_info = None,
28 data = None):
29 if data:
30 if type (data) is Data:
31 self._data = data._data
32 elif type (data) is ns.ndnSIM.ndn.ContentObject:
33 self._data = data
34 else:
35 raise TypeError ("Invalid type supplied for 'data' parameter [%s]" % type (data))
36 else:
37 self._data = ns.ndnSIM.ndn.ContentObject ()
38
39 self.name = name
40 self.content = content
41 self.signedInfo = signed_info or SignedInfo ()
42
43 def sign (self, key):
44 """There is no actual signing in ndnSIM for now, but we will assign signature bits based on key"""
45 self._data.SetSignature (key.fakeKey)
46
47 def verify_signature (self, key):
48 """There is no actual signing in ndnSIM for now, but we will check if signature matches the key"""
49 return self._data.GetSignature () == key.fakeKey
50
51 def __getattr__ (self, name):
52 if name == "signedInfo":
53 return object.__getattr__ (self, name)
54 elif name == "name":
55 return Name (self._data.GetName ())
56 elif name == "scope":
57 return self._data.GetScope ()
58 elif name == "interestLifetime":
59 return self._data.GetInterestLifetime ().ToDouble (ns.core.Time.S)
60 else:
61 return self._data.__getattribute__ (name)
62
63 def __setattr__(self, name, value):
64 if name == "_data":
65 return object.__setattr__ (self, name, value)
66 elif name == 'signedInfo':
67 if not value:
68 return
69 if type (value) is SignedInfo:
70 object.__setattr__ (self, name, value)
71
72 if value.timestamp:
73 pass
74 if value.freshnessSeconds:
75 pass
76 if value.keyLocator:
77 pass
78 else:
79 raise TypeError ("signedInfo can be assigned either None or SignedInfo object, [%s] supplied" % type (value))
80 elif name == "name":
81 if not value:
82 return self._data.SetName (ns.ndnSIM.ndn.Name ())
83 elif type (value) is Name:
84 return self._data.SetName (value._name)
85 elif type (value) is ns.ndnSIM.ndn.Name:
86 return self._data.SetName (value)
87 elif type (value) is str:
88 return self._data.SetName (ns.ndnSIM.ndn.Name (value))
89 else:
90 raise ValueError ("Invalid name parameter")
91 elif name == "content":
92 if not value:
93 pkt = ns.network.Packet ()
94 self._data.SetPayload (pkt)
95 else:
96 pkt = ns.network.Packet (bytes (value))
97 self._data.SetPayload (pkt)
98 else:
99 raise NameError ("Unknown attribute [%s]" % name)
100
101 def __repr__(self):
102 return "ndn.Data(%s)" % str (self._data)
103
104class SignedInfo (object):
105 def __init__(self, keyLocator = None, freshness = None, timestamp = None):
106
107 self.timestamp = timestamp
108 self.freshnessSeconds = freshness
109 self.keyLocator = keyLocator
110
111 def __repr__(self):
112 args = []
113
114 if self.keyLocator is not None:
115 args += ["keyLocator=%r" % self.keyLocator]
116 if self.freshnessSeconds is not None:
117 args += ["freshness=%r" % self.freshnessSeconds]
118 if self.timeStamp is not None:
119 args += ["timestamp=%r" % self.timestamp]
120
121 return "ndn.SignedInfo(%s)" % ", ".join(args)
122
123class ContentObject (Data):
124 """For backwards compatibility"""
125 pass
126