blob: ddafb65869709b736700bc0b5924d832fe36ffc5 [file] [log] [blame]
Alexander Afanasyev5ba90362013-07-15 19:58:38 -07001## -*- 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 Afanasyev7d0a61d2013-07-17 17:33:05 -070020import ns.ndnSIM
21import ns.core
22import Name
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070023
Alexander Afanasyev76b11572013-07-16 21:49:50 -070024class Interest (object):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070025 _interest = None
26
27 def __init__(self, name = None,
28 scope = None, interestLifetime = None):
29 self._interest = ns.ndnSIM.ndn.Interest ()
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070030
31 self.name = name
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070032 self.scope = scope
33 self.interestLifetime = interestLifetime
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070034
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070035 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 Afanasyev5ba90362013-07-15 19:58:38 -070044
45 def __setattr__(self, name, value):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070046 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 Afanasyev5ba90362013-07-15 19:58:38 -070075
76 def __repr__(self):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070077 return "ndnSIM.Interest(%s)" % str (self._interest)