blob: 5165d2c081e19a57842e2bbc6953ccabaf2dd8be [file] [log] [blame]
Alexander Afanasyev39f53162013-07-18 16:24:31 -07001## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -07002#
Alexander Afanasyev39f53162013-07-18 16:24:31 -07003# Copyright (c) 2011-2013, Regents of the University of California
4# Alexander Afanasyev
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -07005#
Alexander Afanasyev39f53162013-07-18 16:24:31 -07006# GNU 3.0 license, See the LICENSE file for more information
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -07007#
Alexander Afanasyev39f53162013-07-18 16:24:31 -07008# Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -07009#
Alexander Afanasyev39f53162013-07-18 16:24:31 -070010
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
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070022from SignedInfo import SignedInfo
Alexander Afanasyev39f53162013-07-18 16:24:31 -070023
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070024class DataError (Exception):
25 pass
26
Alexander Afanasyev39f53162013-07-18 16:24:31 -070027class Data (object):
28 _data = None
29
30 def __init__(self,
31 name = None, content = None, signed_info = None,
32 data = None):
33 if data:
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070034 if isinstance (data, Data):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070035 self._data = data._data
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070036 elif isinstance (data, ns.ndnSIM.ndn.Data):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070037 self._data = data
38 else:
39 raise TypeError ("Invalid type supplied for 'data' parameter [%s]" % type (data))
40 else:
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070041 self._data = ns.ndnSIM.ndn.Data ()
Alexander Afanasyev39f53162013-07-18 16:24:31 -070042
43 self.name = name
44 self.content = content
45 self.signedInfo = signed_info or SignedInfo ()
46
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070047 @staticmethod
48 def fromWire (wire):
49 data = Data (data = ns.ndnSIM.ndn.Wire.ToData (wire))
50 # timestamp
51 data.signedInfo.freshnessSeconds = data._data.GetFreshness ()
52 if data._data.GetKeyLocator ():
53 data.keyLocator = Name (_name = data._data.GetKeyLocator ())
54
Alexander Afanasyev39f53162013-07-18 16:24:31 -070055 def sign (self, key):
56 """There is no actual signing in ndnSIM for now, but we will assign signature bits based on key"""
57 self._data.SetSignature (key.fakeKey)
58
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070059 def toWire (self):
60 if self._data.GetSignature () == 0:
61 raise DataError ("Data packet has not been signed, cannot create wire format")
62
63 return ns.ndnSIM.ndn.Wire.FromData (self._data)
64
Alexander Afanasyev39f53162013-07-18 16:24:31 -070065 def verify_signature (self, key):
66 """There is no actual signing in ndnSIM for now, but we will check if signature matches the key"""
67 return self._data.GetSignature () == key.fakeKey
68
69 def __getattr__ (self, name):
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070070 if name == "_data":
71 return object.__getattr__ (self, name)
72
73 elif name == "signedInfo":
Alexander Afanasyev39f53162013-07-18 16:24:31 -070074 return object.__getattr__ (self, name)
75 elif name == "name":
76 return Name (self._data.GetName ())
77 elif name == "scope":
78 return self._data.GetScope ()
79 elif name == "interestLifetime":
80 return self._data.GetInterestLifetime ().ToDouble (ns.core.Time.S)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070081 elif name == "content":
82 pkt = self._data.GetContent ()
83 return ns.ndnSIM.ndn.PacketToBuffer (pkt)
Alexander Afanasyev39f53162013-07-18 16:24:31 -070084 else:
85 return self._data.__getattribute__ (name)
86
87 def __setattr__(self, name, value):
88 if name == "_data":
89 return object.__setattr__ (self, name, value)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070090
Alexander Afanasyev39f53162013-07-18 16:24:31 -070091 elif name == 'signedInfo':
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070092 if value is None:
Alexander Afanasyev39f53162013-07-18 16:24:31 -070093 return
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070094 if isinstance (value, SignedInfo):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070095 object.__setattr__ (self, name, value)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070096
Alexander Afanasyev39f53162013-07-18 16:24:31 -070097 if value.timestamp:
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070098 # ?
Alexander Afanasyev39f53162013-07-18 16:24:31 -070099 pass
100 if value.freshnessSeconds:
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -0700101 self._data.SetFreshness (ns.core.Seconds (value))
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700102 if value.keyLocator:
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -0700103 self._data.SetKeyLocator (value._name)
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700104 else:
105 raise TypeError ("signedInfo can be assigned either None or SignedInfo object, [%s] supplied" % type (value))
106 elif name == "name":
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700107 if value is None:
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700108 return self._data.SetName (ns.ndnSIM.ndn.Name ())
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700109 elif isinstance (value, Name):
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700110 return self._data.SetName (value._name)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700111 elif isinstance (value, ns.ndnSIM.ndn.Name):
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700112 return self._data.SetName (value)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700113 elif isinstance (value, str):
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700114 return self._data.SetName (ns.ndnSIM.ndn.Name (value))
115 else:
116 raise ValueError ("Invalid name parameter")
117 elif name == "content":
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -0700118 if value is None:
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700119 pkt = ns.network.Packet ()
120 self._data.SetPayload (pkt)
121 else:
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -0700122 pkt = ns.ndnSIM.ndn.BufferToPacket (bytes (value))
Alexander Afanasyev39f53162013-07-18 16:24:31 -0700123 self._data.SetPayload (pkt)
124 else:
125 raise NameError ("Unknown attribute [%s]" % name)
126
127 def __repr__(self):
128 return "ndn.Data(%s)" % str (self._data)