blob: 807fee796dd60dc291ddf9981b61404d53921723 [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 Afanasyevf0bda2f2013-08-12 16:38:13 -070030class Face (object):
31 deleteList = []
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070032
Alexander Afanasyev76b11572013-07-16 21:49:50 -070033 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 Afanasyevf0bda2f2013-08-12 16:38:13 -070036 self._face = ns.ndnSIM.ndn.ApiFace (self.node)
37 # super(Face, self).__init__ (self.node)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070038
39 def connect (self):
40 pass
41
42 def disconnect (self):
Alexander Afanasyevf0bda2f2013-08-12 16:38:13 -070043 self._face.Shutdown ()
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070044
45 def defer_verification (self, deferVerification = True):
46 pass
47
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070048
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070049 def expressInterest (self, name, onData, onTimeout, template = None):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070050 """
51 onData: void <interest, name>
52 onTimeout: void <interest>
53 """
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070054
Alexander Afanasyev39f53162013-07-18 16:24:31 -070055 interest = Interest (interest = template)
56 interest.name = name
57
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070058 converter = ExpressInterestConverter (onData, onTimeout)
Alexander Afanasyevf0bda2f2013-08-12 16:38:13 -070059 self._face.ExpressInterest (interest._interest, converter.handleOnData, converter.handleOnTimeout)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070060
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070061 def setInterestFilter (self, name, onInterest, flags = None):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070062 """
63 onInterest: void <name, interest>
64 """
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070065
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070066 if isinstance (name, Name):
67 name = name._name
68 elif isinstance (name, ns.ndnSIM.ndn.Name):
69 pass
70 else:
71 raise TypeError ("Wrong type for 'name' parameter [%s]" % type (name))
72
Alexander Afanasyevf0bda2f2013-08-12 16:38:13 -070073 self._face.SetInterestFilter (name, OnInterestConvert (onInterest))
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070074
Alexander Afanasyev39f53162013-07-18 16:24:31 -070075 def clearInterestFilter (self, name):
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070076 if isinstance (name, Name):
77 name = name._name
78 elif isinstance (name, ns.ndnSIM.ndn.Name):
79 pass
Alexander Afanasyev39f53162013-07-18 16:24:31 -070080 else:
81 raise TypeError ("Wrong type for 'name' parameter [%s]" % type (name))
82
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070083 # @bug: memory leak, deleteList need to remove previosly set callback... but how?
Alexander Afanasyevf0bda2f2013-08-12 16:38:13 -070084 self._face.ClearInterestFilter (name)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070085
Alexander Afanasyev39f53162013-07-18 16:24:31 -070086 def get (self, name, template = None, timeoutms = 3000):
87 raise NotImplementedError ("NS-3 simulation cannot have syncrhonous operations")
88
89 def put (self, data):
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070090 if isinstance (data, Data):
Alexander Afanasyevf0bda2f2013-08-12 16:38:13 -070091 self._face.Put (data._data)
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070092 elif isinstance (data, ns.ndnSIM.ndn.Data):
Alexander Afanasyevf0bda2f2013-08-12 16:38:13 -070093 self._face.Put (data)
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070094 else:
95 raise TypeError ("Unsupported type to publish data [%s]" % type (data))
96
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070097class ExpressInterestConverter:
98 def __init__ (self, onData, onTimeout):
99 self.onData = onData
100 self.onTimeout = onTimeout
101
102 def handleOnData (self, interest, data):
Alexander Afanasyev36f10532013-08-08 11:25:14 -0700103 try:
104 if self.onData:
105 return self.onData (Interest (interest=interest), Data (data = data))
106 except Exception, e:
107 traceback.print_exc()
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -0700108
109 def handleOnTimeout (self, interest):
Alexander Afanasyev36f10532013-08-08 11:25:14 -0700110 try:
111 if self.onTimeout:
112 self.onTimeout (Interest (interest=interest))
113 except Exception, e:
114 traceback.print_exc()
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -0700115
116class OnInterestConvert (object):
117 def __init__ (self, onInterest):
118 self.onInterest = onInterest
Alexander Afanasyevf0bda2f2013-08-12 16:38:13 -0700119
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -0700120 def __call__ (self, name, interest):
Alexander Afanasyev36f10532013-08-08 11:25:14 -0700121 try:
122 if self.onInterest:
123 self.onInterest (Name (name = name), Interest (interest = interest))
124 except Exception, e:
125 traceback.print_exc()