OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2007 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 | |
5 try: | |
6 from cStringIO import StringIO | |
7 except ImportError: | |
8 from StringIO import StringIO | |
9 | |
10 from twisted.protocols import basic | |
11 from twisted.internet import error | |
12 | |
13 | |
14 class LineSendingProtocol(basic.LineReceiver): | |
15 lostConn = False | |
16 | |
17 def __init__(self, lines, start = True): | |
18 self.lines = lines[:] | |
19 self.response = [] | |
20 self.start = start | |
21 | |
22 def connectionMade(self): | |
23 if self.start: | |
24 map(self.sendLine, self.lines) | |
25 | |
26 def lineReceived(self, line): | |
27 if not self.start: | |
28 map(self.sendLine, self.lines) | |
29 self.lines = [] | |
30 self.response.append(line) | |
31 | |
32 def connectionLost(self, reason): | |
33 self.lostConn = True | |
34 | |
35 | |
36 class FakeDatagramTransport: | |
37 noAddr = object() | |
38 | |
39 def __init__(self): | |
40 self.written = [] | |
41 | |
42 def write(self, packet, addr=noAddr): | |
43 self.written.append((packet, addr)) | |
44 | |
45 | |
46 class StringTransport: | |
47 disconnecting = 0 | |
48 | |
49 hostAddr = None | |
50 peerAddr = None | |
51 | |
52 def __init__(self, hostAddress=None, peerAddress=None): | |
53 self.clear() | |
54 if hostAddress is not None: | |
55 self.hostAddr = hostAddress | |
56 if peerAddress is not None: | |
57 self.peerAddr = peerAddress | |
58 self.connected = True | |
59 | |
60 def clear(self): | |
61 self.io = StringIO() | |
62 | |
63 def value(self): | |
64 return self.io.getvalue() | |
65 | |
66 def write(self, data): | |
67 if isinstance(data, unicode): # no, really, I mean it | |
68 raise TypeError("Data must not be unicode") | |
69 self.io.write(data) | |
70 | |
71 def writeSequence(self, data): | |
72 self.io.write(''.join(data)) | |
73 | |
74 def loseConnection(self): | |
75 pass | |
76 | |
77 def getPeer(self): | |
78 if self.peerAddr is None: | |
79 return ('StringIO', repr(self.io)) | |
80 return self.peerAddr | |
81 | |
82 def getHost(self): | |
83 if self.hostAddr is None: | |
84 return ('StringIO', repr(self.io)) | |
85 return self.hostAddr | |
86 | |
87 | |
88 class StringTransportWithDisconnection(StringTransport): | |
89 def loseConnection(self): | |
90 if self.connected: | |
91 self.connected = False | |
92 self.protocol.connectionLost(error.ConnectionDone("Bye.")) | |
93 | |
OLD | NEW |