blob: 98f92247ddcbe3bd2f856800707ae1b42ee232e7 [file] [log] [blame]
Guido van Rossum27b7c7e2013-10-17 13:40:50 -07001"""Abstract Transport class."""
2
3__all__ = ['ReadTransport', 'WriteTransport', 'Transport']
4
5
6class BaseTransport:
7 """Base ABC for transports."""
8
9 def __init__(self, extra=None):
10 if extra is None:
11 extra = {}
12 self._extra = extra
13
14 def get_extra_info(self, name, default=None):
15 """Get optional transport information."""
16 return self._extra.get(name, default)
17
18 def close(self):
Antoine Pitroudec43382013-11-23 12:30:00 +010019 """Close the transport.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070020
21 Buffered data will be flushed asynchronously. No more data
22 will be received. After all buffered data is flushed, the
23 protocol's connection_lost() method will (eventually) called
24 with None as its argument.
25 """
26 raise NotImplementedError
27
28
29class ReadTransport(BaseTransport):
30 """ABC for read-only transports."""
31
Guido van Rossum57497ad2013-10-18 07:58:20 -070032 def pause_reading(self):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070033 """Pause the receiving end.
34
35 No data will be passed to the protocol's data_received()
Guido van Rossum57497ad2013-10-18 07:58:20 -070036 method until resume_reading() is called.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070037 """
38 raise NotImplementedError
39
Guido van Rossum57497ad2013-10-18 07:58:20 -070040 def resume_reading(self):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070041 """Resume the receiving end.
42
43 Data received will once again be passed to the protocol's
44 data_received() method.
45 """
46 raise NotImplementedError
47
48
49class WriteTransport(BaseTransport):
50 """ABC for write-only transports."""
51
Guido van Rossum355491d2013-10-18 15:17:11 -070052 def set_write_buffer_limits(self, high=None, low=None):
53 """Set the high- and low-water limits for write flow control.
54
55 These two values control when to call the protocol's
56 pause_writing() and resume_writing() methods. If specified,
57 the low-water limit must be less than or equal to the
58 high-water limit. Neither value can be negative.
59
60 The defaults are implementation-specific. If only the
61 high-water limit is given, the low-water limit defaults to a
62 implementation-specific value less than or equal to the
63 high-water limit. Setting high to zero forces low to zero as
64 well, and causes pause_writing() to be called whenever the
65 buffer becomes non-empty. Setting low to zero causes
66 resume_writing() to be called only once the buffer is empty.
67 Use of zero for either limit is generally sub-optimal as it
68 reduces opportunities for doing I/O and computation
69 concurrently.
70 """
71 raise NotImplementedError
72
73 def get_write_buffer_size(self):
74 """Return the current size of the write buffer."""
75 raise NotImplementedError
76
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070077 def write(self, data):
78 """Write some data bytes to the transport.
79
80 This does not block; it buffers the data and arranges for it
81 to be sent out asynchronously.
82 """
83 raise NotImplementedError
84
85 def writelines(self, list_of_data):
86 """Write a list (or any iterable) of data bytes to the transport.
87
88 The default implementation just calls write() for each item in
89 the list/iterable.
90 """
91 for data in list_of_data:
92 self.write(data)
93
94 def write_eof(self):
Antoine Pitroudec43382013-11-23 12:30:00 +010095 """Close the write end after flushing buffered data.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070096
97 (This is like typing ^D into a UNIX program reading from stdin.)
98
99 Data may still be received.
100 """
101 raise NotImplementedError
102
103 def can_write_eof(self):
Antoine Pitroudec43382013-11-23 12:30:00 +0100104 """Return True if this transport supports write_eof(), False if not."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700105 raise NotImplementedError
106
107 def abort(self):
Guido van Rossum488b0da2013-11-23 11:51:09 -0800108 """Close the transport immediately.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700109
110 Buffered data will be lost. No more data will be received.
111 The protocol's connection_lost() method will (eventually) be
112 called with None as its argument.
113 """
114 raise NotImplementedError
115
116
117class Transport(ReadTransport, WriteTransport):
118 """ABC representing a bidirectional transport.
119
120 There may be several implementations, but typically, the user does
121 not implement new transports; rather, the platform provides some
122 useful transports that are implemented using the platform's best
123 practices.
124
125 The user never instantiates a transport directly; they call a
126 utility function, passing it a protocol factory and other
127 information necessary to create the transport and protocol. (E.g.
128 EventLoop.create_connection() or EventLoop.create_server().)
129
130 The utility function will asynchronously create a transport and a
131 protocol and hook them up by calling the protocol's
132 connection_made() method, passing it the transport.
133
134 The implementation here raises NotImplemented for every method
135 except writelines(), which calls write() in a loop.
136 """
137
138
139class DatagramTransport(BaseTransport):
140 """ABC for datagram (UDP) transports."""
141
142 def sendto(self, data, addr=None):
143 """Send data to the transport.
144
145 This does not block; it buffers the data and arranges for it
146 to be sent out asynchronously.
147 addr is target socket address.
148 If addr is None use target address pointed on transport creation.
149 """
150 raise NotImplementedError
151
152 def abort(self):
Antoine Pitroudec43382013-11-23 12:30:00 +0100153 """Close the transport immediately.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700154
155 Buffered data will be lost. No more data will be received.
156 The protocol's connection_lost() method will (eventually) be
157 called with None as its argument.
158 """
159 raise NotImplementedError
160
161
162class SubprocessTransport(BaseTransport):
163
164 def get_pid(self):
165 """Get subprocess id."""
166 raise NotImplementedError
167
168 def get_returncode(self):
169 """Get subprocess returncode.
170
171 See also
172 http://docs.python.org/3/library/subprocess#subprocess.Popen.returncode
173 """
174 raise NotImplementedError
175
176 def get_pipe_transport(self, fd):
177 """Get transport for pipe with number fd."""
178 raise NotImplementedError
179
180 def send_signal(self, signal):
181 """Send signal to subprocess.
182
183 See also:
184 docs.python.org/3/library/subprocess#subprocess.Popen.send_signal
185 """
186 raise NotImplementedError
187
188 def terminate(self):
189 """Stop the subprocess.
190
191 Alias for close() method.
192
193 On Posix OSs the method sends SIGTERM to the subprocess.
194 On Windows the Win32 API function TerminateProcess()
195 is called to stop the subprocess.
196
197 See also:
198 http://docs.python.org/3/library/subprocess#subprocess.Popen.terminate
199 """
200 raise NotImplementedError
201
202 def kill(self):
203 """Kill the subprocess.
204
205 On Posix OSs the function sends SIGKILL to the subprocess.
206 On Windows kill() is an alias for terminate().
207
208 See also:
209 http://docs.python.org/3/library/subprocess#subprocess.Popen.kill
210 """
211 raise NotImplementedError