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; -*- |
| 2 | # |
| 3 | # Copyright (c) 2011-2013, Regents of the University of California |
| 4 | # Alexander Afanasyev |
| 5 | # |
| 6 | # GNU 3.0 license, See the LICENSE file for more information |
| 7 | # |
| 8 | # Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | # |
| 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 |
| 25 | |
| 26 | import functools |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 28 | class Face (ns.ndnSIM.ndn.ApiFace): |
| 29 | def __init__(self): |
| 30 | self.nodeId = ns.core.Simulator.GetContext () |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 31 | self.node = ns.network.NodeList.GetNode (self.nodeId) |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 32 | super(Face, self).__init__ (self.node) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 33 | |
| 34 | def connect (self): |
| 35 | pass |
| 36 | |
| 37 | def disconnect (self): |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 38 | self.Shutdown () |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 39 | |
| 40 | def defer_verification (self, deferVerification = True): |
| 41 | pass |
| 42 | |
| 43 | def expressInterestSimple (self, name, onData, onTimeout, template = None): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 44 | """ |
| 45 | onData: void <interest, name> |
| 46 | onTimeout: void <interest> |
| 47 | """ |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 49 | interest = Interest (interest = template) |
| 50 | interest.name = name |
| 51 | |
| 52 | class OnDataConvert: |
| 53 | def __init__ (self, onData): |
| 54 | self.onData = onData |
| 55 | def __call__ (self, interest, data): |
| 56 | if self.onData: |
| 57 | return self.onData (Interest (interest=interest), Data (data = data)) |
| 58 | |
| 59 | class OnTimeoutConvert: |
| 60 | def __init__ (self, onTimeout): |
| 61 | self.onTimeout = onTimeout |
| 62 | def __call__ (self, interest): |
| 63 | if self.onTimeout: |
| 64 | self.onTimeout (Interest (interest=interest)) |
| 65 | |
| 66 | self.ExpressInterest (interest, OnDataConvert (onData), OnTimeoutConvert (onTimeout)) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 67 | |
| 68 | def setInterestFilterSimple (self, name, onInterest, flags = None): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 69 | """ |
| 70 | onInterest: void <name, interest> |
| 71 | """ |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 72 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 73 | class OnInterestConvert: |
| 74 | def __init__ (self, onInterest): |
| 75 | self.onInterest = onInterest |
| 76 | def __call__ (self, name, interest): |
| 77 | if self.onInterest: |
| 78 | self.onInterest (Name (name = name), Interest (interest = interest)) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 79 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 80 | self.SetInterestFilter (name, OnInterestConvert (onInterest)) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 82 | def clearInterestFilter (self, name): |
| 83 | if type (name) is Name: |
| 84 | self.ClearInterestFilter (name._name) |
| 85 | elif type (name) is ns.ndnSIM.ndn.Name: |
| 86 | self.ClearInterestFilter (name) |
| 87 | else: |
| 88 | raise TypeError ("Wrong type for 'name' parameter [%s]" % type (name)) |
| 89 | |
| 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 | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 94 | self.Put (data) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 95 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 96 | class EventLoop(object): |
| 97 | def execute (self, event): |
| 98 | ns.core.Simulator.ScheduleNow (event) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 99 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 100 | def run (self, timeoutMs): |
| 101 | ns.core.Simulator.Stop (ns.core.MilliSeconds (timeoutMs)) |
| 102 | ns.core.Simulator.Run () |
| 103 | ns.core.Simulator.Destroy () |