blob: d63550fc69180e67ca7c85aea96da063f21f2d29 [file] [log] [blame]
Alexander Afanasyev5ba90362013-07-15 19:58:38 -07001## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -07002#
Alexander Afanasyev5ba90362013-07-15 19:58:38 -07003# Copyright (c) 2011-2013, Regents of the University of California
4# Alexander Afanasyev
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -07005#
Alexander Afanasyev5ba90362013-07-15 19:58:38 -07006# GNU 3.0 license, See the LICENSE file for more information
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -07007#
Alexander Afanasyev5ba90362013-07-15 19:58:38 -07008# Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -07009#
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070010
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
20import ns.core
Alexander Afanasyev39f53162013-07-18 16:24:31 -070021import ns.network
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070022import ns.ndnSIM
Alexander Afanasyev39f53162013-07-18 16:24:31 -070023from Data import Data
24from Interest import Interest
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070025from Name import Name
Alexander Afanasyev39f53162013-07-18 16:24:31 -070026
27import functools
Alexander Afanasyev36f10532013-08-08 11:25:14 -070028import traceback
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070029
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070030deleteList = []
31
Alexander Afanasyev76b11572013-07-16 21:49:50 -070032class Face (ns.ndnSIM.ndn.ApiFace):
33 def __init__(self):
34 self.nodeId = ns.core.Simulator.GetContext ()
Alexander Afanasyev39f53162013-07-18 16:24:31 -070035 self.node = ns.network.NodeList.GetNode (self.nodeId)
Alexander Afanasyev76b11572013-07-16 21:49:50 -070036 super(Face, self).__init__ (self.node)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070037
38 def connect (self):
39 pass
40
41 def disconnect (self):
Alexander Afanasyev76b11572013-07-16 21:49:50 -070042 self.Shutdown ()
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070043
44 def defer_verification (self, deferVerification = True):
45 pass
46
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070047
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070048 def expressInterest (self, name, onData, onTimeout, template = None):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070049 """
50 onData: void <interest, name>
51 onTimeout: void <interest>
52 """
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070053
Alexander Afanasyev39f53162013-07-18 16:24:31 -070054 interest = Interest (interest = template)
55 interest.name = name
56
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070057 converter = ExpressInterestConverter (onData, onTimeout)
58 deleteList.append (converter)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070059
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070060 self.ExpressInterest (interest._interest, converter.handleOnData, converter.handleOnTimeout)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070061
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070062 def setInterestFilter (self, name, onInterest, flags = None):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070063 """
64 onInterest: void <name, interest>
65 """
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070066
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070067 if isinstance (name, Name):
68 name = name._name
69 elif isinstance (name, ns.ndnSIM.ndn.Name):
70 pass
71 else:
72 raise TypeError ("Wrong type for 'name' parameter [%s]" % type (name))
73
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070074 converter = OnInterestConvert (onInterest)
75 deleteList.append (converter)
76
77 self.SetInterestFilter (name, converter)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070078
Alexander Afanasyev39f53162013-07-18 16:24:31 -070079 def clearInterestFilter (self, name):
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070080 if isinstance (name, Name):
81 name = name._name
82 elif isinstance (name, ns.ndnSIM.ndn.Name):
83 pass
Alexander Afanasyev39f53162013-07-18 16:24:31 -070084 else:
85 raise TypeError ("Wrong type for 'name' parameter [%s]" % type (name))
86
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070087 # @bug: memory leak, deleteList need to remove previosly set callback... but how?
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070088 self.ClearInterestFilter (name)
89
Alexander Afanasyev39f53162013-07-18 16:24:31 -070090 def get (self, name, template = None, timeoutms = 3000):
91 raise NotImplementedError ("NS-3 simulation cannot have syncrhonous operations")
92
93 def put (self, data):
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070094 if isinstance (data, Data):
95 self.Put (data._data)
96 elif isinstance (data, ns.ndnSIM.ndn.Data):
97 self.Put (data)
98 else:
99 raise TypeError ("Unsupported type to publish data [%s]" % type (data))
100
101def removeFromDeleteList (object):
Alexander Afanasyev36f10532013-08-08 11:25:14 -0700102 try:
103 deleteList.remove (object)
104 except:
105 pass
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -0700106
107class ExpressInterestConverter:
108 def __init__ (self, onData, onTimeout):
109 self.onData = onData
110 self.onTimeout = onTimeout
111
112 def handleOnData (self, interest, data):
113 ns.core.Simulator.ScheduleNow (removeFromDeleteList, self)
Alexander Afanasyev36f10532013-08-08 11:25:14 -0700114 try:
115 if self.onData:
116 return self.onData (Interest (interest=interest), Data (data = data))
117 except Exception, e:
118 traceback.print_exc()
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -0700119
120 def handleOnTimeout (self, interest):
121 ns.core.Simulator.ScheduleNow (removeFromDeleteList, self)
Alexander Afanasyev36f10532013-08-08 11:25:14 -0700122 try:
123 if self.onTimeout:
124 self.onTimeout (Interest (interest=interest))
125 except Exception, e:
126 traceback.print_exc()
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -0700127
128class OnInterestConvert (object):
129 def __init__ (self, onInterest):
130 self.onInterest = onInterest
131 def __call__ (self, name, interest):
132 ns.core.Simulator.ScheduleNow (removeFromDeleteList, self)
Alexander Afanasyev36f10532013-08-08 11:25:14 -0700133 try:
134 if self.onInterest:
135 self.onInterest (Name (name = name), Interest (interest = interest))
136 except Exception, e:
137 traceback.print_exc()