Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 1 | ## -*- 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 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame^] | 20 | def toWire (name): |
| 21 | buf = ns.network.Buffer (ns.ndnSIM.ndn.Wire.FromNameSize (name)) |
| 22 | ns.ndnSIM.ndn.Wire.FromName (buf.Begin (), name) |
| 23 | |
| 24 | output = bytearray (buf.GetSize ()) |
| 25 | buf.CopyData (output, buf.GetSize ()) |
| 26 | |
| 27 | return buf |
| 28 | |
| 29 | import ns.ndnSIM |
| 30 | import ns.network |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 31 | |
| 32 | from copy import copy |
| 33 | import time, struct, random |
| 34 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame^] | 35 | class Name (ns.ndnSIM.ndn.Name): |
| 36 | def __init__(self, name=None): |
| 37 | super (Name, self).__init__ (name) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 38 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame^] | 39 | @staticmethod |
| 40 | def fromWire (wire): |
| 41 | return ns.ndnSIM.ndn.Wire.ToName (wire) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame^] | 43 | @staticmethod |
| 44 | def toWire (name): |
| 45 | buf = ns.network.Buffer () |
| 46 | buf.AddToStart (ns.ndnSIM.ndn.Wire.FromNameSize (name)) |
| 47 | ns.ndnSIM.ndn.Wire.FromName (buf.Begin (), name) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame^] | 49 | output = bytearray (buf.GetSize ()) |
| 50 | buf.CopyData (output, buf.GetSize ()) |
| 51 | return output |
| 52 | |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 53 | def get_ccnb(self): |
| 54 | return _ndn.dump_charbuf(self.ccn_data) |
| 55 | |
| 56 | def __repr__(self): |
| 57 | global NAME_NORMAL, NAME_ANY |
| 58 | |
| 59 | if self.type == NAME_NORMAL: |
| 60 | return "ndn.Name('ccnx:" + _ndn.name_to_uri(self.ccn_data) + "')" |
| 61 | elif self.type == NAME_ANY: |
| 62 | return "ndn.Name(name_type=ndn.NAME_ANY)" |
| 63 | else: |
| 64 | raise ValueError("Name is of wrong type %d" % self.type) |
| 65 | |
| 66 | def __str__(self): |
| 67 | global NAME_NORMAL, NAME_ANY |
| 68 | |
| 69 | if self.type == NAME_NORMAL: |
| 70 | return _ndn.name_to_uri(self.ccn_data) |
| 71 | elif self.type == NAME_ANY: |
| 72 | return "<any>" |
| 73 | else: |
| 74 | raise ValueError("Name is of wrong type %d" % self.type) |
| 75 | |
| 76 | def __len__(self): |
| 77 | return len(self.components) |
| 78 | |
| 79 | def __add__(self, other): |
| 80 | return self.append(other) |
| 81 | |
| 82 | def __setattr__(self, name, value): |
| 83 | raise TypeError("can't modify immutable instance") |
| 84 | |
| 85 | __delattr__ = __setattr__ |
| 86 | |
| 87 | def __getattribute__(self, name): |
| 88 | if name == "ccn_data": |
| 89 | if object.__getattribute__(self, 'ccn_data_dirty'): |
| 90 | self._setattr('ccn_data', _ndn.name_comps_to_ccn(self.components)) |
| 91 | self._setattr('ccn_data_dirty', False) |
| 92 | return object.__getattribute__(self, name) |
| 93 | |
| 94 | def __getitem__(self, key): |
| 95 | if type(key) is int: |
| 96 | return self.components[key] |
| 97 | elif type(key) is slice: |
| 98 | return Name(self.components[key]) |
| 99 | else: |
| 100 | raise ValueError("Unknown __getitem__ type: %s" % type(key)) |
| 101 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame^] | 102 | # def __setitem__(self, key, value): |
| 103 | # self.components[key] = value |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame^] | 105 | # def __delitem__(self, key): |
| 106 | # del self.components[key] |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 107 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame^] | 108 | # def __len__(self): |
| 109 | # return len(self.components) |