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 | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 30 | class Face (object): |
| 31 | deleteList = [] |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 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 | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 36 | self._face = ns.ndnSIM.ndn.ApiFace (self.node) |
| 37 | # super(Face, self).__init__ (self.node) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 38 | |
| 39 | def connect (self): |
| 40 | pass |
| 41 | |
| 42 | def disconnect (self): |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 43 | self._face.Shutdown () |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 44 | |
| 45 | def defer_verification (self, deferVerification = True): |
| 46 | pass |
| 47 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 49 | def expressInterest (self, name, onData, onTimeout, template = None): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 50 | """ |
| 51 | onData: void <interest, name> |
| 52 | onTimeout: void <interest> |
| 53 | """ |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 55 | interest = Interest (interest = template) |
| 56 | interest.name = name |
| 57 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 58 | converter = ExpressInterestConverter (onData, onTimeout) |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 59 | self._face.ExpressInterest (interest._interest, converter.handleOnData, converter.handleOnTimeout) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 61 | def setInterestFilter (self, name, onInterest, flags = None): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 62 | """ |
| 63 | onInterest: void <name, interest> |
| 64 | """ |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 66 | 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 Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 73 | self._face.SetInterestFilter (name, OnInterestConvert (onInterest)) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 74 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 75 | def clearInterestFilter (self, name): |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 76 | if isinstance (name, Name): |
| 77 | name = name._name |
| 78 | elif isinstance (name, ns.ndnSIM.ndn.Name): |
| 79 | pass |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 80 | else: |
| 81 | raise TypeError ("Wrong type for 'name' parameter [%s]" % type (name)) |
| 82 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 83 | # @bug: memory leak, deleteList need to remove previosly set callback... but how? |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 84 | self._face.ClearInterestFilter (name) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 86 | 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 Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 90 | if isinstance (data, Data): |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 91 | self._face.Put (data._data) |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 92 | elif isinstance (data, ns.ndnSIM.ndn.Data): |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 93 | self._face.Put (data) |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 94 | else: |
| 95 | raise TypeError ("Unsupported type to publish data [%s]" % type (data)) |
| 96 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 97 | class ExpressInterestConverter: |
| 98 | def __init__ (self, onData, onTimeout): |
| 99 | self.onData = onData |
| 100 | self.onTimeout = onTimeout |
| 101 | |
| 102 | def handleOnData (self, interest, data): |
Alexander Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 103 | try: |
| 104 | if self.onData: |
| 105 | return self.onData (Interest (interest=interest), Data (data = data)) |
| 106 | except Exception, e: |
| 107 | traceback.print_exc() |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 108 | |
| 109 | def handleOnTimeout (self, interest): |
Alexander Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 110 | try: |
| 111 | if self.onTimeout: |
| 112 | self.onTimeout (Interest (interest=interest)) |
| 113 | except Exception, e: |
| 114 | traceback.print_exc() |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 115 | |
| 116 | class OnInterestConvert (object): |
| 117 | def __init__ (self, onInterest): |
| 118 | self.onInterest = onInterest |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 120 | def __call__ (self, name, interest): |
Alexander Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 121 | try: |
| 122 | if self.onInterest: |
| 123 | self.onInterest (Name (name = name), Interest (interest = interest)) |
| 124 | except Exception, e: |
| 125 | traceback.print_exc() |