PyNDN: Initial changes of PyNDN emulation in ndnSIM
The biggest problem so far, which I cannot solve at all, is exception
handling. NDN.cxx throws many exceptions when something bad happens,
with are not handled by python bindings
Refs #1008 (http://redmine.named-data.net/issues/1008)
Refs #1011 (http://redmine.named-data.net/issues/1011)
diff --git a/PyNDN/Face.py b/PyNDN/Face.py
index 54803db..ebfeb5d 100644
--- a/PyNDN/Face.py
+++ b/PyNDN/Face.py
@@ -20,140 +20,51 @@
import ns.core
import ns.ndnSIM
-# from ns3.ndnSIM import ApiFace, Interest, Name
-
-class Face (object):
- def __init__(self, node):
- self._face = ns.ndnSIM.ndn.ApiFace (node)
+class Face (ns.ndnSIM.ndn.ApiFace):
+ def __init__(self):
+ self.nodeId = ns.core.Simulator.GetContext ()
+ self.node = ns.core.NodeList.GetNode (self.nodeId)
+ super(Face, self).__init__ (self.node)
def connect (self):
pass
def disconnect (self):
- pass
+ self.Shutdown ()
def defer_verification (self, deferVerification = True):
pass
def expressInterestSimple (self, name, onData, onTimeout, template = None):
-
- pass
- # class TrivialExpressClosure (Closure.Closure):
- # __slots__ = ["_baseName", "_onData", "_onTimeout"];
+ if template:
+ interest = ns.ndnSIM.ndn.Interest (template)
+ else:
+ interest = ns.ndnSIM.ndn.Interest ()
- # def __init__ (self, baseName, onData, onTimeout):
- # self._baseName = baseName
- # self._onData = onData
- # self._onTimeout = onTimeout
-
- # def upcall(self, kind, upcallInfo):
- # if (kind == Closure.UPCALL_CONTENT or
- # kind == Closure.UPCALL_CONTENT_UNVERIFIED or
- # kind == Closure.UPCALL_CONTENT_UNVERIFIED or
- # kind == Closure.UPCALL_CONTENT_KEYMISSING or
- # kind == Closure.UPCALL_CONTENT_RAW):
- # return self._onData (self._baseName, upcallInfo.Interest, upcallInfo.ContentObject, kind)
- # elif (kind == Closure.UPCALL_INTEREST_TIMED_OUT):
- # return self._onTimeout (self._baseName, upcallInfo.Interest)
- # return Closure.RESULT_OK
-
- # trivial_closure = TrivialExpressClosure (name, onData, onTimeout)
- # self.expressInterest (name, trivial_closure, template)
+ interest.SetName (name)
+ self.ExpressInterest (interest, onData, onTimeout)
def setInterestFilterSimple (self, name, onInterest, flags = None):
- pass
- # class TrivialFilterClosure (Closure.Closure):
- # # __slots__ = ["_baseName", "_onInterest"];
-
- # def __init__ (self, baseName, onInterest):
- # self._baseName = baseName
- # self._onInterest = onInterest
-
- # def upcall(self, kind, upcallInfo):
- # if (kind == Closure.UPCALL_INTEREST):
- # return self._onInterest (self._baseName, upcallInfo.Interest)
- # return Closure.RESULT_OK
-
- # trivial_closure = TrivialFilterClosure (name, onInterest)
- # self.setInterestFilter (name, trivial_closure, flags)
+ self.SetInterestFilter (name, onInterest)
def clearInterestFilter(self, name):
- pass
+ self.ClearInterestFilter (name)
- # Blocking!
def get(self, name, template = None, timeoutms = 3000):
- # self._acquire_lock("get")
- # try:
- # return _ndn.get(self, name, template, timeoutms)
- # finally:
- # self._release_lock("get")
+ raise ("NS-3 simulation cannot have syncrhonous operations")
- # def put(self, contentObject):
- # self._acquire_lock("put")
- # try:
- # return _ndn.put(self, contentObject)
- # finally:
- # self._release_lock("put")
+ def put(self, data):
+ self.Put (data)
@staticmethod
- def getDefaultKey():
- pass
- # return _ndn.get_default_key()
+ def getDefaultKey ():
+ return None
-# class EventLoop(object):
-# def __init__(self, *handles):
-# self.running = False
-# self.fds = {}
-# for handle in handles:
-# self.fds[handle.fileno()] = handle
-# self.eventLock = threading.Lock ()
-# self.events = []
+class EventLoop(object):
+ def execute (self, event):
+ ns.core.Simulator.ScheduleNow (event)
-# def execute (self, event):
-# self.eventLock.acquire ()
-# self.events.append (event)
-# self.eventLock.release ()
-
-# def run_scheduled(self):
-# wait = {}
-# for fd, handle in zip(self.fds.keys(), self.fds.values()):
-# wait[fd] = handle.process_scheduled()
-# return wait[sorted(wait, key=wait.get)[0]] / 1000.0
-
-# def run_once(self):
-# fd_read = self.fds.values()
-# fd_write = []
-# for handle in self.fds.values():
-# if handle.output_is_pending():
-# fd_write.append(handle)
-
-# timeout = min(self.run_scheduled(), 1.000)
-
-# res = select.select(fd_read, fd_write, [], timeout)
-
-# handles = set(res[0]).union(res[1])
-# for handle in handles:
-# handle.run(0)
-
-# def run(self):
-# self.running = True
-# while self.running:
-# try:
-# self.eventLock.acquire ()
-# for event in self.events:
-# event ()
-# self.events = []
-# self.eventLock.release ()
-
-# self.run_once()
-# except select.error, e:
-# if e[0] == 4:
-# continue
-# else:
-# raise
-# self.running = False
-
-# def stop(self):
-# self.running = False
-# for fd, handle in zip(self.fds.keys(), self.fds.values()):
-# handle.disconnect ()
+ def run (self, timeoutMs):
+ ns.core.Simulator.Stop (ns.core.MilliSeconds (timeoutMs))
+ ns.core.Simulator.Run ()
+ ns.core.Simulator.Destroy ()