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