blob: 3a905fa035ba4a5859d10ec540113fdaa8e0f1c1 [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
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070027import time
Alexander Afanasyev39f53162013-07-18 16:24:31 -070028import functools
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):
102 deleteList.remove (object)
103
104class ExpressInterestConverter:
105 def __init__ (self, onData, onTimeout):
106 self.onData = onData
107 self.onTimeout = onTimeout
108
109 def handleOnData (self, interest, data):
110 ns.core.Simulator.ScheduleNow (removeFromDeleteList, self)
111 if self.onData:
112 return self.onData (Interest (interest=interest), Data (data = data))
113
114 def handleOnTimeout (self, interest):
115 ns.core.Simulator.ScheduleNow (removeFromDeleteList, self)
116 if self.onTimeout:
117 self.onTimeout (Interest (interest=interest))
118
119class OnInterestConvert (object):
120 def __init__ (self, onInterest):
121 self.onInterest = onInterest
122 def __call__ (self, name, interest):
123 ns.core.Simulator.ScheduleNow (removeFromDeleteList, self)
124 if self.onInterest:
125 self.onInterest (Name (name = name), Interest (interest = interest))