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 | |
| 27 | import functools |
Alexander Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 28 | import traceback |
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): |
Alexander Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 102 | try: |
| 103 | deleteList.remove (object) |
| 104 | except: |
| 105 | pass |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 106 | |
| 107 | class 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 Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 114 | try: |
| 115 | if self.onData: |
| 116 | return self.onData (Interest (interest=interest), Data (data = data)) |
| 117 | except Exception, e: |
| 118 | traceback.print_exc() |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 119 | |
| 120 | def handleOnTimeout (self, interest): |
| 121 | ns.core.Simulator.ScheduleNow (removeFromDeleteList, self) |
Alexander Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 122 | try: |
| 123 | if self.onTimeout: |
| 124 | self.onTimeout (Interest (interest=interest)) |
| 125 | except Exception, e: |
| 126 | traceback.print_exc() |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 127 | |
| 128 | class 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 Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 133 | try: |
| 134 | if self.onInterest: |
| 135 | self.onInterest (Name (name = name), Interest (interest = interest)) |
| 136 | except Exception, e: |
| 137 | traceback.print_exc() |