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: |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 31 | if isinstance (interest, Interest): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 32 | self._interest = interest._interest |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 33 | elif isinstance (interest, ns.ndnSIM.ndn.Interest): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 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 | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 44 | @staticmethod |
| 45 | def fromWire (wire): |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 46 | return Interest (interest = ns.ndnSIM.ndn.Wire.ToInterestStr (wire)) |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 47 | |
| 48 | def toWire (self): |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 49 | return ns.ndnSIM.ndn.Wire.FromInterestStr (self._interest) |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 51 | def __getattr__ (self, name): |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 52 | if name == "_interest": |
| 53 | return object.__getattr__ (self, name) |
| 54 | |
| 55 | elif name == "name": |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 56 | return Name (self._interest.GetName ()) |
| 57 | elif name == "scope": |
| 58 | return self._interest.GetScope () |
| 59 | elif name == "interestLifetime": |
| 60 | return self._interest.GetInterestLifetime ().ToDouble (ns.core.Time.S) |
| 61 | else: |
| 62 | return self._interest.__getattribute__ (name) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 63 | |
| 64 | def __setattr__(self, name, value): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 65 | if name == "_interest": |
| 66 | return object.__setattr__ (self, name, value) |
Alexander Afanasyev | f6f9ef2 | 2013-07-26 11:34:00 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 68 | elif name == "name": |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 69 | if value is None: |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 70 | return self._interest.SetName (ns.ndnSIM.ndn.Name ()) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 71 | elif isinstance (value, Name): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 72 | return self._interest.SetName (value._name) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 73 | elif isinstance (value, ns.ndnSIM.ndn.Name): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 74 | return self._interest.SetName (value) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 75 | elif isinstance (value, str): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 76 | return self._interest.SetName (ns.ndnSIM.ndn.Name (value)) |
| 77 | else: |
| 78 | raise ValueError ("Invalid name parameter") |
| 79 | elif name == "scope": |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 80 | if value is None: |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 81 | return self._interest.SetScope (-1) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 82 | elif isinstance (value, int): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 83 | return self._interest.SetScope (value) |
| 84 | else: |
| 85 | raise ValueError ("Scope parameter should be int, [%s] supplied" % type (value)) |
| 86 | elif name == "interestLifetime": |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 87 | if value is None: |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 88 | return self._interest.SetInterestLifetime (ns.core.Time ()) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 89 | elif isinstance (value, float) or isinstance (value, int): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 90 | return self._interest.SetInterestLifetime (ns.core.Seconds (value)) |
| 91 | else: |
| 92 | raise ValueError ("interestLifetime parameter should be fload or int, [%s] supplied" % type (value)) |
| 93 | else: |
| 94 | raise ValueError ("Unknown or unsupported attribute [%s]" % name) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 95 | |
| 96 | def __repr__(self): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 97 | return "ndnSIM.Interest(%s)" % str (self._interest) |