1 # -*- test-case-name: calculus.test.test_client -*-
3 from twisted.protocols import basic, policies
4 from twisted.internet import defer
8 class ClientTimeoutError(Exception):
13 class RemoteCalculationClient(object, basic.LineReceiver, policies.TimeoutMixin):
19 def lineReceived(self, line):
21 d = self.results.pop(0)
25 def timeoutConnection(self):
26 for d in self.results:
27 d.errback(ClientTimeoutError())
28 self.transport.loseConnection()
31 def _sendOperation(self, op, a, b):
33 self.results.append(d)
34 line = "%s %d %d" % (op, a, b)
36 self.setTimeout(self._timeOut)
41 return self._sendOperation("add", a, b)
44 def subtract(self, a, b):
45 return self._sendOperation("subtract", a, b)
48 def multiply(self, a, b):
49 return self._sendOperation("multiply", a, b)
52 def divide(self, a, b):
53 return self._sendOperation("divide", a, b)