From a196211836efb555aede4fce4d161e7d327ca719 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Fri, 4 Apr 2008 13:03:24 +0200 Subject: * host/mex: - added support for asynchronous operations. --- host/mex/mex/node.py | 28 ++++++++++++++++++++++++++++ host/mex/test/test.py | 7 ++++++- 2 files changed, 34 insertions(+), 1 deletion(-) (limited to 'host/mex') diff --git a/host/mex/mex/node.py b/host/mex/mex/node.py index aa980c61..c51d7c79 100644 --- a/host/mex/mex/node.py +++ b/host/mex/mex/node.py @@ -60,6 +60,30 @@ class Node: msg = self.recv () self.dispatch (msg) + def wait_async (self, date = None): + """Asynchronous version of wait. This should not be called again + until sync return True.""" + self.async_waited = date + synced = self.sync () + assert not synced + + def sync (self): + """To be called after read or wait_async. Return True if the waited + date is reached or signal the Hub our waiting status.""" + if self.date == self.async_waited: + return True + else: + idle = Msg (mex.IDLE) + if self.async_waited != None: + idle.push ('L', self.async_waited) + self.send (idle) + + def read (self): + """Used for asynchronous operations. Handle incoming data. The sync + method should be called after this one returns.""" + msg = self.recv () + self.dispatch (msg) + def send (self, msg): """Send a message.""" data = msg.data () @@ -101,6 +125,10 @@ class Node: self.socket.close () self.socket = None + def fileno (self): + """Return socket fileno () for asynchronous operations.""" + return self.socket.fileno () + def recv (self): """Receive one message.""" head = self.socket.recv (calcsize (mex.HEADER_FMT)) diff --git a/host/mex/test/test.py b/host/mex/test/test.py index 48484486..bdb705a2 100644 --- a/host/mex/test/test.py +++ b/host/mex/test/test.py @@ -5,6 +5,7 @@ from mex.hub import Hub from mex.node import Node from mex.msg import Msg from mex.forked import Forked +import select def log (x): print x @@ -37,7 +38,11 @@ def c2 (): r = n.request (m) assert r.mtype == 0x82 assert r.pop ('B') == (43,) - n.wait (42) + n.wait_async (42) + while not n.sync (): + fds = select.select ((n, ), (), ())[0] + for i in fds: + i.read () n.wait () f2 = Forked (c2) -- cgit v1.2.3