Skip to content

Commit

Permalink
Method run() moved from Port to Protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Vasiliev committed Jan 2, 2010
1 parent 4e097fe commit 0d1c090
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 33 deletions.
40 changes: 10 additions & 30 deletions src/erlport/erlproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@
class Protocol(object):
"""Erlang port protocol."""

def connected(self, port):
"""Port connected."""
self.port = port

def disconnected(self, reason):
"""Port disconnected."""
def run(self, port):
"""Run processing loop."""
while True:
try:
message = port.read()
except EOFError:
break
self.handle(port, message)

def handle(self, message):
def handle(self, port, message):
"""Handle incoming message."""
if not (isinstance(message, Atom)
or isinstance(message, tuple) and len(message) > 0):
Expand All @@ -70,7 +72,7 @@ def handle(self, message):
except TypeError:
# Easy way to check correct number of arguments
response = Atom("error"), Atom("function_clause")
self.port.write(response)
port.write(response)


class Port(object):
Expand All @@ -82,8 +84,6 @@ class Port(object):
4: ">I",
}

_running = False

def __init__(self, packet=1, use_stdio=False, descriptors=None):
self._format = self._formats.get(packet)
if self._format is None:
Expand All @@ -97,26 +97,6 @@ def __init__(self, packet=1, use_stdio=False, descriptors=None):
else:
self.in_d, self.out_d = 3, 4

def run(self, proto):
"""Run processing loop."""
if self._running:
raise RuntimeError("already running")
self._running = True
proto.connected(self)
try:
try:
while True:
message = self.read()
proto.handle(message)
except EOFError, why:
proto.disconnected(why)
except Exception, why:
proto.disconnected(why)
raise
finally:
self._running = False
self.close()

def _read_data(self, length):
data = ""
while len(data) != length:
Expand Down
4 changes: 1 addition & 3 deletions src/erlport/tests/erlproto.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ Setup test modules, classes and functions:
...
... port = Port(packet, descriptors=(r, w))
... port2 = Port(packet, descriptors=(in_d, out_d))
... proto.connected(port)
... port2.write((Atom("test"), u"value"))
... message = port.read()
... proto.handle(message)
... proto.handle(port, message)
... result = port2.read()
... proto.disconnected(EOFError())
... return result

Test protocol with different options:
Expand Down

0 comments on commit 0d1c090

Please sign in to comment.