Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 1 | ## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 2 | # |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 3 | # Copyright (c) 2011-2013, Regents of the University of California |
| 4 | # Alexander Afanasyev |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 5 | # |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 6 | # GNU 3.0 license, See the LICENSE file for more information |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 7 | # |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 8 | # Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 9 | # |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 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 | |
| 20 | import ns.core |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 21 | import ns.network |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 22 | import ns.ndnSIM |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 23 | from Data import Data |
| 24 | from Interest import Interest |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 25 | from Name import Name |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame^] | 27 | import time |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 28 | import functools |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame^] | 30 | deleteList = [] |
| 31 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 32 | class Face (ns.ndnSIM.ndn.ApiFace): |
| 33 | def __init__(self): |
| 34 | self.nodeId = ns.core.Simulator.GetContext () |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 35 | self.node = ns.network.NodeList.GetNode (self.nodeId) |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 36 | super(Face, self).__init__ (self.node) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 37 | |
| 38 | def connect (self): |
| 39 | pass |
| 40 | |
| 41 | def disconnect (self): |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 42 | self.Shutdown () |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 43 | |
| 44 | def defer_verification (self, deferVerification = True): |
| 45 | pass |
| 46 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame^] | 47 | |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 48 | def expressInterest (self, name, onData, onTimeout, template = None): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 49 | """ |
| 50 | onData: void <interest, name> |
| 51 | onTimeout: void <interest> |
| 52 | """ |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 53 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 54 | interest = Interest (interest = template) |
| 55 | interest.name = name |
| 56 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame^] | 57 | converter = ExpressInterestConverter (onData, onTimeout) |
| 58 | deleteList.append (converter) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame^] | 60 | self.ExpressInterest (interest._interest, converter.handleOnData, converter.handleOnTimeout) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 61 | |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 62 | def setInterestFilter (self, name, onInterest, flags = None): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 63 | """ |
| 64 | onInterest: void <name, interest> |
| 65 | """ |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 66 | |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 67 | 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 Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame^] | 74 | converter = OnInterestConvert (onInterest) |
| 75 | deleteList.append (converter) |
| 76 | |
| 77 | self.SetInterestFilter (name, converter) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 79 | def clearInterestFilter (self, name): |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 80 | if isinstance (name, Name): |
| 81 | name = name._name |
| 82 | elif isinstance (name, ns.ndnSIM.ndn.Name): |
| 83 | pass |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 84 | else: |
| 85 | raise TypeError ("Wrong type for 'name' parameter [%s]" % type (name)) |
| 86 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame^] | 87 | # @bug: memory leak, deleteList need to remove previosly set callback... but how? |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 88 | self.ClearInterestFilter (name) |
| 89 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 90 | 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 Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame^] | 94 | 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 | |
| 101 | def removeFromDeleteList (object): |
| 102 | deleteList.remove (object) |
| 103 | |
| 104 | class 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 | |
| 119 | class 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)) |