blob: 61865694a7e0237bf0292e69f1ae6b1c79d69399 [file] [log] [blame]
Alexander Afanasyev5ba90362013-07-15 19:58:38 -07001## -*- 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
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
25
26import functools
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070027
Alexander Afanasyev76b11572013-07-16 21:49:50 -070028class Face (ns.ndnSIM.ndn.ApiFace):
29 def __init__(self):
30 self.nodeId = ns.core.Simulator.GetContext ()
Alexander Afanasyev39f53162013-07-18 16:24:31 -070031 self.node = ns.network.NodeList.GetNode (self.nodeId)
Alexander Afanasyev76b11572013-07-16 21:49:50 -070032 super(Face, self).__init__ (self.node)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070033
34 def connect (self):
35 pass
36
37 def disconnect (self):
Alexander Afanasyev76b11572013-07-16 21:49:50 -070038 self.Shutdown ()
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070039
40 def defer_verification (self, deferVerification = True):
41 pass
42
43 def expressInterestSimple (self, name, onData, onTimeout, template = None):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070044 """
45 onData: void <interest, name>
46 onTimeout: void <interest>
47 """
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070048
Alexander Afanasyev39f53162013-07-18 16:24:31 -070049 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 Afanasyev5ba90362013-07-15 19:58:38 -070067
68 def setInterestFilterSimple (self, name, onInterest, flags = None):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070069 """
70 onInterest: void <name, interest>
71 """
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070072
Alexander Afanasyev39f53162013-07-18 16:24:31 -070073 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 Afanasyev5ba90362013-07-15 19:58:38 -070079
Alexander Afanasyev39f53162013-07-18 16:24:31 -070080 self.SetInterestFilter (name, OnInterestConvert (onInterest))
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070081
Alexander Afanasyev39f53162013-07-18 16:24:31 -070082 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 Afanasyev76b11572013-07-16 21:49:50 -070094 self.Put (data)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070095
Alexander Afanasyev76b11572013-07-16 21:49:50 -070096class EventLoop(object):
97 def execute (self, event):
98 ns.core.Simulator.ScheduleNow (event)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070099
Alexander Afanasyev76b11572013-07-16 21:49:50 -0700100 def run (self, timeoutMs):
101 ns.core.Simulator.Stop (ns.core.MilliSeconds (timeoutMs))
102 ns.core.Simulator.Run ()
103 ns.core.Simulator.Destroy ()