blob: d831bb5a34fea21ffd98ef1502733604766f3295 [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
Alexander Afanasyev39f53162013-07-18 16:24:31 -070022from Name import 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
Alexander Afanasyev39f53162013-07-18 16:24:31 -070027 def __init__(self,
28 name = None, scope = None, interestLifetime = None,
29 interest = None):
30 if interest:
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070031 if isinstance (interest, Interest):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070032 self._interest = interest._interest
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070033 elif isinstance (interest, ns.ndnSIM.ndn.Interest):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070034 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 Afanasyev5ba90362013-07-15 19:58:38 -070039
Alexander Afanasyev39f53162013-07-18 16:24:31 -070040 self.name = name
41 self.scope = scope
42 self.interestLifetime = interestLifetime
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070043
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070044 @staticmethod
45 def fromWire (wire):
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070046 return Interest (interest = ns.ndnSIM.ndn.Wire.ToInterestStr (wire))
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070047
48 def toWire (self):
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070049 return ns.ndnSIM.ndn.Wire.FromInterestStr (self._interest)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070050
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070051 def __getattr__ (self, name):
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070052 if name == "_interest":
53 return object.__getattr__ (self, name)
54
55 elif name == "name":
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070056 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 Afanasyev5ba90362013-07-15 19:58:38 -070063
64 def __setattr__(self, name, value):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070065 if name == "_interest":
66 return object.__setattr__ (self, name, value)
Alexander Afanasyevf6f9ef22013-07-26 11:34:00 -070067
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070068 elif name == "name":
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070069 if value is None:
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070070 return self._interest.SetName (ns.ndnSIM.ndn.Name ())
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070071 elif isinstance (value, Name):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070072 return self._interest.SetName (value._name)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070073 elif isinstance (value, ns.ndnSIM.ndn.Name):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070074 return self._interest.SetName (value)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070075 elif isinstance (value, str):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070076 return self._interest.SetName (ns.ndnSIM.ndn.Name (value))
77 else:
78 raise ValueError ("Invalid name parameter")
79 elif name == "scope":
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070080 if value is None:
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070081 return self._interest.SetScope (-1)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070082 elif isinstance (value, int):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070083 return self._interest.SetScope (value)
84 else:
85 raise ValueError ("Scope parameter should be int, [%s] supplied" % type (value))
86 elif name == "interestLifetime":
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070087 if value is None:
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070088 return self._interest.SetInterestLifetime (ns.core.Time ())
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070089 elif isinstance (value, float) or isinstance (value, int):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070090 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 Afanasyev5ba90362013-07-15 19:58:38 -070095
96 def __repr__(self):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070097 return "ndnSIM.Interest(%s)" % str (self._interest)