| 1 | import os, sys, time |
|---|
| 2 | |
|---|
| 3 | from twisted.internet import defer, reactor, stdio, interfaces |
|---|
| 4 | from twisted.protocols import basic |
|---|
| 5 | from twisted.python import usage |
|---|
| 6 | from zope.interface import implements |
|---|
| 7 | |
|---|
| 8 | class TestReader (basic.LineReceiver): |
|---|
| 9 | from os import linesep as delimiter |
|---|
| 10 | implements(interfaces.IHalfCloseableProtocol) |
|---|
| 11 | |
|---|
| 12 | def __init__ (self): |
|---|
| 13 | pass |
|---|
| 14 | |
|---|
| 15 | def lineReceived (self, line): |
|---|
| 16 | self.transport.write(line + "\n") |
|---|
| 17 | |
|---|
| 18 | def stop (self): |
|---|
| 19 | print >>sys.stderr, "stop" |
|---|
| 20 | self.transport.loseConnection() |
|---|
| 21 | |
|---|
| 22 | def readConnectionLost (self): |
|---|
| 23 | print >>sys.stderr, "readConnectionLost" |
|---|
| 24 | reactor.callLater(1, self.transport.write, "stopping\n") |
|---|
| 25 | reactor.callLater(2, self.stop) |
|---|
| 26 | |
|---|
| 27 | def writeConnectionLost(self): |
|---|
| 28 | print >>sys.stderr, "writeConnectionLost" |
|---|
| 29 | pass |
|---|
| 30 | |
|---|
| 31 | def connectionLost(self, reason): |
|---|
| 32 | print >>sys.stderr, "connectionLost" |
|---|
| 33 | reactor.callWhenRunning(reactor.stop) |
|---|
| 34 | |
|---|
| 35 | stdio.StandardIO(TestReader()) |
|---|
| 36 | reactor.run() |
|---|
| 37 | |
|---|