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 | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 20 | import ns.ndnSIM |
| 21 | import ns.core |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 22 | from Name import Name |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 24 | class Interest (object): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 25 | _interest = None |
| 26 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 27 | def __init__(self, |
| 28 | name = None, scope = None, interestLifetime = None, |
| 29 | interest = None): |
| 30 | if interest: |
| 31 | if type (interest) is Interest: |
| 32 | self._interest = interest._interest |
| 33 | elif type (interest) is ns.ndnSIM.ndn.Interest: |
| 34 | self._interest = interest |
| 35 | else: |
| 36 | raise TypeError ("Invalid type supplied for 'interest' parameter [%s]" % type (interest)) |
| 37 | else: |
| 38 | self._interest = ns.ndnSIM.ndn.Interest () |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 40 | self.name = name |
| 41 | self.scope = scope |
| 42 | self.interestLifetime = interestLifetime |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 44 | def __getattr__ (self, name): |
| 45 | if name == "name": |
| 46 | return Name (self._interest.GetName ()) |
| 47 | elif name == "scope": |
| 48 | return self._interest.GetScope () |
| 49 | elif name == "interestLifetime": |
| 50 | return self._interest.GetInterestLifetime ().ToDouble (ns.core.Time.S) |
| 51 | else: |
| 52 | return self._interest.__getattribute__ (name) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 53 | |
| 54 | def __setattr__(self, name, value): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 55 | if name == "_interest": |
| 56 | return object.__setattr__ (self, name, value) |
| 57 | elif name == "name": |
| 58 | if not value: |
| 59 | return self._interest.SetName (ns.ndnSIM.ndn.Name ()) |
| 60 | elif type (value) is Name: |
| 61 | return self._interest.SetName (value._name) |
| 62 | elif type (value) is ns.ndnSIM.ndn.Name: |
| 63 | return self._interest.SetName (value) |
| 64 | elif type (value) is str: |
| 65 | return self._interest.SetName (ns.ndnSIM.ndn.Name (value)) |
| 66 | else: |
| 67 | raise ValueError ("Invalid name parameter") |
| 68 | elif name == "scope": |
| 69 | if not value: |
| 70 | return self._interest.SetScope (-1) |
| 71 | elif type (value) is int: |
| 72 | return self._interest.SetScope (value) |
| 73 | else: |
| 74 | raise ValueError ("Scope parameter should be int, [%s] supplied" % type (value)) |
| 75 | elif name == "interestLifetime": |
| 76 | if not value: |
| 77 | return self._interest.SetInterestLifetime (ns.core.Time ()) |
| 78 | elif type (value) is float or type (value) is int: |
| 79 | return self._interest.SetInterestLifetime (ns.core.Seconds (value)) |
| 80 | else: |
| 81 | raise ValueError ("interestLifetime parameter should be fload or int, [%s] supplied" % type (value)) |
| 82 | else: |
| 83 | raise ValueError ("Unknown or unsupported attribute [%s]" % name) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 84 | |
| 85 | def __repr__(self): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 86 | return "ndnSIM.Interest(%s)" % str (self._interest) |