2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
1 |
# Copyright (C) 2006 Canonical Ltd
|
2 |
#
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
2018.5.19
by Andrew Bennetts
Add docstrings to all the new modules, and a few other places. |
17 |
"""The 'medium' layer for the smart servers and clients.
|
18 |
||
19 |
"Medium" here is the noun meaning "a means of transmission", not the adjective
|
|
20 |
for "the quality between big and small."
|
|
21 |
||
22 |
Media carry the bytes of the requests somehow (e.g. via TCP, wrapped in HTTP, or
|
|
23 |
over SSH), and pass them to and from the protocol logic. See the overview in
|
|
24 |
bzrlib/transport/smart/__init__.py.
|
|
25 |
"""
|
|
26 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
27 |
import os |
28 |
import socket |
|
2018.5.162
by Andrew Bennetts
Add some missing _ensure_real calls, and a missing import. |
29 |
import sys |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
30 |
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
31 |
from bzrlib import ( |
32 |
errors, |
|
3118.2.1
by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall. |
33 |
osutils, |
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
34 |
symbol_versioning, |
35 |
)
|
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
36 |
from bzrlib.smart.protocol import ( |
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
37 |
REQUEST_VERSION_TWO, |
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
38 |
SmartServerRequestProtocolOne, |
39 |
SmartServerRequestProtocolTwo, |
|
40 |
)
|
|
3066.2.1
by John Arbash Meinel
We don't require paramiko for bzr+ssh. |
41 |
from bzrlib.transport import ssh |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
42 |
|
2018.5.17
by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler. |
43 |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
44 |
class SmartServerStreamMedium(object): |
45 |
"""Handles smart commands coming over a stream.
|
|
46 |
||
47 |
The stream may be a pipe connected to sshd, or a tcp socket, or an
|
|
48 |
in-process fifo for testing.
|
|
49 |
||
50 |
One instance is created for each connected client; it can serve multiple
|
|
51 |
requests in the lifetime of the connection.
|
|
52 |
||
53 |
The server passes requests through to an underlying backing transport,
|
|
54 |
which will typically be a LocalTransport looking at the server's filesystem.
|
|
55 |
"""
|
|
56 |
||
57 |
def __init__(self, backing_transport): |
|
58 |
"""Construct new server.
|
|
59 |
||
60 |
:param backing_transport: Transport for the directory served.
|
|
61 |
"""
|
|
62 |
# backing_transport could be passed to serve instead of __init__
|
|
63 |
self.backing_transport = backing_transport |
|
64 |
self.finished = False |
|
65 |
||
66 |
def serve(self): |
|
67 |
"""Serve requests until the client disconnects."""
|
|
68 |
# Keep a reference to stderr because the sys module's globals get set to
|
|
69 |
# None during interpreter shutdown.
|
|
70 |
from sys import stderr |
|
71 |
try: |
|
72 |
while not self.finished: |
|
2432.2.3
by Andrew Bennetts
Merge from bzr.dev. |
73 |
server_protocol = self._build_protocol() |
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
74 |
self._serve_one_request(server_protocol) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
75 |
except Exception, e: |
76 |
stderr.write("%s terminating on exception %s\n" % (self, e)) |
|
77 |
raise
|
|
78 |
||
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
79 |
def _build_protocol(self): |
2432.2.8
by Andrew Bennetts
NEWS entry, greatly improved docstring in bzrlib.smart. |
80 |
"""Identifies the version of the incoming request, and returns an
|
81 |
a protocol object that can interpret it.
|
|
82 |
||
83 |
If more bytes than the version prefix of the request are read, they will
|
|
84 |
be fed into the protocol before it is returned.
|
|
85 |
||
86 |
:returns: a SmartServerRequestProtocol.
|
|
87 |
"""
|
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
88 |
# Identify the protocol version.
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
89 |
bytes = self._get_line() |
90 |
if bytes.startswith(REQUEST_VERSION_TWO): |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
91 |
protocol_class = SmartServerRequestProtocolTwo |
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
92 |
bytes = bytes[len(REQUEST_VERSION_TWO):] |
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
93 |
else: |
94 |
protocol_class = SmartServerRequestProtocolOne |
|
95 |
protocol = protocol_class(self.backing_transport, self._write_out) |
|
96 |
protocol.accept_bytes(bytes) |
|
97 |
return protocol |
|
98 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
99 |
def _serve_one_request(self, protocol): |
100 |
"""Read one request from input, process, send back a response.
|
|
101 |
|
|
102 |
:param protocol: a SmartServerRequestProtocol.
|
|
103 |
"""
|
|
104 |
try: |
|
105 |
self._serve_one_request_unguarded(protocol) |
|
106 |
except KeyboardInterrupt: |
|
107 |
raise
|
|
108 |
except Exception, e: |
|
109 |
self.terminate_due_to_error() |
|
110 |
||
111 |
def terminate_due_to_error(self): |
|
112 |
"""Called when an unhandled exception from the protocol occurs."""
|
|
113 |
raise NotImplementedError(self.terminate_due_to_error) |
|
114 |
||
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
115 |
def _get_bytes(self, desired_count): |
116 |
"""Get some bytes from the medium.
|
|
117 |
||
118 |
:param desired_count: number of bytes we want to read.
|
|
119 |
"""
|
|
120 |
raise NotImplementedError(self._get_bytes) |
|
121 |
||
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
122 |
def _get_line(self): |
123 |
"""Read bytes from this request's response until a newline byte.
|
|
124 |
|
|
125 |
This isn't particularly efficient, so should only be used when the
|
|
126 |
expected size of the line is quite short.
|
|
127 |
||
128 |
:returns: a string of bytes ending in a newline (byte 0x0A).
|
|
129 |
"""
|
|
130 |
# XXX: this duplicates SmartClientRequestProtocolOne._recv_tuple
|
|
131 |
line = '' |
|
132 |
while not line or line[-1] != '\n': |
|
133 |
new_char = self._get_bytes(1) |
|
134 |
line += new_char |
|
135 |
if new_char == '': |
|
136 |
# Ran out of bytes before receiving a complete line.
|
|
137 |
break
|
|
138 |
return line |
|
139 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
140 |
|
141 |
class SmartServerSocketStreamMedium(SmartServerStreamMedium): |
|
142 |
||
143 |
def __init__(self, sock, backing_transport): |
|
144 |
"""Constructor.
|
|
145 |
||
146 |
:param sock: the socket the server will read from. It will be put
|
|
147 |
into blocking mode.
|
|
148 |
"""
|
|
149 |
SmartServerStreamMedium.__init__(self, backing_transport) |
|
150 |
self.push_back = '' |
|
151 |
sock.setblocking(True) |
|
152 |
self.socket = sock |
|
153 |
||
154 |
def _serve_one_request_unguarded(self, protocol): |
|
155 |
while protocol.next_read_size(): |
|
156 |
if self.push_back: |
|
157 |
protocol.accept_bytes(self.push_back) |
|
158 |
self.push_back = '' |
|
159 |
else: |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
160 |
bytes = self._get_bytes(4096) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
161 |
if bytes == '': |
162 |
self.finished = True |
|
163 |
return
|
|
164 |
protocol.accept_bytes(bytes) |
|
165 |
||
166 |
self.push_back = protocol.excess_buffer |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
167 |
|
168 |
def _get_bytes(self, desired_count): |
|
169 |
# We ignore the desired_count because on sockets it's more efficient to
|
|
170 |
# read 4k at a time.
|
|
171 |
return self.socket.recv(4096) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
172 |
|
173 |
def terminate_due_to_error(self): |
|
174 |
"""Called when an unhandled exception from the protocol occurs."""
|
|
175 |
# TODO: This should log to a server log file, but no such thing
|
|
176 |
# exists yet. Andrew Bennetts 2006-09-29.
|
|
177 |
self.socket.close() |
|
178 |
self.finished = True |
|
179 |
||
180 |
def _write_out(self, bytes): |
|
3118.2.1
by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall. |
181 |
osutils.send_all(self.socket, bytes) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
182 |
|
183 |
||
184 |
class SmartServerPipeStreamMedium(SmartServerStreamMedium): |
|
185 |
||
186 |
def __init__(self, in_file, out_file, backing_transport): |
|
187 |
"""Construct new server.
|
|
188 |
||
189 |
:param in_file: Python file from which requests can be read.
|
|
190 |
:param out_file: Python file to write responses.
|
|
191 |
:param backing_transport: Transport for the directory served.
|
|
192 |
"""
|
|
193 |
SmartServerStreamMedium.__init__(self, backing_transport) |
|
2018.5.161
by Andrew Bennetts
Reinstate forcing binary mode on windows in SmartServerStreamMedium. |
194 |
if sys.platform == 'win32': |
195 |
# force binary mode for files
|
|
196 |
import msvcrt |
|
197 |
for f in (in_file, out_file): |
|
198 |
fileno = getattr(f, 'fileno', None) |
|
199 |
if fileno: |
|
200 |
msvcrt.setmode(fileno(), os.O_BINARY) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
201 |
self._in = in_file |
202 |
self._out = out_file |
|
203 |
||
204 |
def _serve_one_request_unguarded(self, protocol): |
|
205 |
while True: |
|
206 |
bytes_to_read = protocol.next_read_size() |
|
207 |
if bytes_to_read == 0: |
|
208 |
# Finished serving this request.
|
|
209 |
self._out.flush() |
|
210 |
return
|
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
211 |
bytes = self._get_bytes(bytes_to_read) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
212 |
if bytes == '': |
213 |
# Connection has been closed.
|
|
214 |
self.finished = True |
|
215 |
self._out.flush() |
|
216 |
return
|
|
217 |
protocol.accept_bytes(bytes) |
|
218 |
||
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
219 |
def _get_bytes(self, desired_count): |
220 |
return self._in.read(desired_count) |
|
221 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
222 |
def terminate_due_to_error(self): |
223 |
# TODO: This should log to a server log file, but no such thing
|
|
224 |
# exists yet. Andrew Bennetts 2006-09-29.
|
|
225 |
self._out.close() |
|
226 |
self.finished = True |
|
227 |
||
228 |
def _write_out(self, bytes): |
|
229 |
self._out.write(bytes) |
|
230 |
||
231 |
||
232 |
class SmartClientMediumRequest(object): |
|
233 |
"""A request on a SmartClientMedium.
|
|
234 |
||
235 |
Each request allows bytes to be provided to it via accept_bytes, and then
|
|
236 |
the response bytes to be read via read_bytes.
|
|
237 |
||
238 |
For instance:
|
|
239 |
request.accept_bytes('123')
|
|
240 |
request.finished_writing()
|
|
241 |
result = request.read_bytes(3)
|
|
242 |
request.finished_reading()
|
|
243 |
||
244 |
It is up to the individual SmartClientMedium whether multiple concurrent
|
|
245 |
requests can exist. See SmartClientMedium.get_request to obtain instances
|
|
246 |
of SmartClientMediumRequest, and the concrete Medium you are using for
|
|
247 |
details on concurrency and pipelining.
|
|
248 |
"""
|
|
249 |
||
250 |
def __init__(self, medium): |
|
251 |
"""Construct a SmartClientMediumRequest for the medium medium."""
|
|
252 |
self._medium = medium |
|
253 |
# we track state by constants - we may want to use the same
|
|
254 |
# pattern as BodyReader if it gets more complex.
|
|
255 |
# valid states are: "writing", "reading", "done"
|
|
256 |
self._state = "writing" |
|
257 |
||
258 |
def accept_bytes(self, bytes): |
|
259 |
"""Accept bytes for inclusion in this request.
|
|
260 |
||
261 |
This method may not be be called after finished_writing() has been
|
|
262 |
called. It depends upon the Medium whether or not the bytes will be
|
|
263 |
immediately transmitted. Message based Mediums will tend to buffer the
|
|
264 |
bytes until finished_writing() is called.
|
|
265 |
||
266 |
:param bytes: A bytestring.
|
|
267 |
"""
|
|
268 |
if self._state != "writing": |
|
269 |
raise errors.WritingCompleted(self) |
|
270 |
self._accept_bytes(bytes) |
|
271 |
||
272 |
def _accept_bytes(self, bytes): |
|
273 |
"""Helper for accept_bytes.
|
|
274 |
||
275 |
Accept_bytes checks the state of the request to determing if bytes
|
|
276 |
should be accepted. After that it hands off to _accept_bytes to do the
|
|
277 |
actual acceptance.
|
|
278 |
"""
|
|
279 |
raise NotImplementedError(self._accept_bytes) |
|
280 |
||
281 |
def finished_reading(self): |
|
282 |
"""Inform the request that all desired data has been read.
|
|
283 |
||
284 |
This will remove the request from the pipeline for its medium (if the
|
|
285 |
medium supports pipelining) and any further calls to methods on the
|
|
286 |
request will raise ReadingCompleted.
|
|
287 |
"""
|
|
288 |
if self._state == "writing": |
|
289 |
raise errors.WritingNotComplete(self) |
|
290 |
if self._state != "reading": |
|
291 |
raise errors.ReadingCompleted(self) |
|
292 |
self._state = "done" |
|
293 |
self._finished_reading() |
|
294 |
||
295 |
def _finished_reading(self): |
|
296 |
"""Helper for finished_reading.
|
|
297 |
||
298 |
finished_reading checks the state of the request to determine if
|
|
299 |
finished_reading is allowed, and if it is hands off to _finished_reading
|
|
300 |
to perform the action.
|
|
301 |
"""
|
|
302 |
raise NotImplementedError(self._finished_reading) |
|
303 |
||
304 |
def finished_writing(self): |
|
305 |
"""Finish the writing phase of this request.
|
|
306 |
||
307 |
This will flush all pending data for this request along the medium.
|
|
308 |
After calling finished_writing, you may not call accept_bytes anymore.
|
|
309 |
"""
|
|
310 |
if self._state != "writing": |
|
311 |
raise errors.WritingCompleted(self) |
|
312 |
self._state = "reading" |
|
313 |
self._finished_writing() |
|
314 |
||
315 |
def _finished_writing(self): |
|
316 |
"""Helper for finished_writing.
|
|
317 |
||
318 |
finished_writing checks the state of the request to determine if
|
|
319 |
finished_writing is allowed, and if it is hands off to _finished_writing
|
|
320 |
to perform the action.
|
|
321 |
"""
|
|
322 |
raise NotImplementedError(self._finished_writing) |
|
323 |
||
324 |
def read_bytes(self, count): |
|
325 |
"""Read bytes from this requests response.
|
|
326 |
||
327 |
This method will block and wait for count bytes to be read. It may not
|
|
328 |
be invoked until finished_writing() has been called - this is to ensure
|
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
329 |
a message-based approach to requests, for compatibility with message
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
330 |
based mediums like HTTP.
|
331 |
"""
|
|
332 |
if self._state == "writing": |
|
333 |
raise errors.WritingNotComplete(self) |
|
334 |
if self._state != "reading": |
|
335 |
raise errors.ReadingCompleted(self) |
|
336 |
return self._read_bytes(count) |
|
337 |
||
338 |
def _read_bytes(self, count): |
|
339 |
"""Helper for read_bytes.
|
|
340 |
||
341 |
read_bytes checks the state of the request to determing if bytes
|
|
342 |
should be read. After that it hands off to _read_bytes to do the
|
|
343 |
actual read.
|
|
344 |
"""
|
|
345 |
raise NotImplementedError(self._read_bytes) |
|
346 |
||
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
347 |
def read_line(self): |
348 |
"""Read bytes from this request's response until a newline byte.
|
|
349 |
|
|
350 |
This isn't particularly efficient, so should only be used when the
|
|
351 |
expected size of the line is quite short.
|
|
352 |
||
353 |
:returns: a string of bytes ending in a newline (byte 0x0A).
|
|
354 |
"""
|
|
355 |
# XXX: this duplicates SmartClientRequestProtocolOne._recv_tuple
|
|
356 |
line = '' |
|
357 |
while not line or line[-1] != '\n': |
|
358 |
new_char = self.read_bytes(1) |
|
359 |
line += new_char |
|
360 |
if new_char == '': |
|
361 |
raise errors.SmartProtocolError( |
|
362 |
'unexpected end of file reading from server') |
|
363 |
return line |
|
364 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
365 |
|
366 |
class SmartClientMedium(object): |
|
367 |
"""Smart client is a medium for sending smart protocol requests over."""
|
|
368 |
||
369 |
def disconnect(self): |
|
370 |
"""If this medium maintains a persistent connection, close it.
|
|
371 |
|
|
372 |
The default implementation does nothing.
|
|
373 |
"""
|
|
374 |
||
375 |
||
376 |
class SmartClientStreamMedium(SmartClientMedium): |
|
377 |
"""Stream based medium common class.
|
|
378 |
||
379 |
SmartClientStreamMediums operate on a stream. All subclasses use a common
|
|
380 |
SmartClientStreamMediumRequest for their requests, and should implement
|
|
381 |
_accept_bytes and _read_bytes to allow the request objects to send and
|
|
382 |
receive bytes.
|
|
383 |
"""
|
|
384 |
||
385 |
def __init__(self): |
|
386 |
self._current_request = None |
|
387 |
||
388 |
def accept_bytes(self, bytes): |
|
389 |
self._accept_bytes(bytes) |
|
390 |
||
391 |
def __del__(self): |
|
392 |
"""The SmartClientStreamMedium knows how to close the stream when it is
|
|
393 |
finished with it.
|
|
394 |
"""
|
|
395 |
self.disconnect() |
|
396 |
||
397 |
def _flush(self): |
|
398 |
"""Flush the output stream.
|
|
399 |
|
|
400 |
This method is used by the SmartClientStreamMediumRequest to ensure that
|
|
401 |
all data for a request is sent, to avoid long timeouts or deadlocks.
|
|
402 |
"""
|
|
403 |
raise NotImplementedError(self._flush) |
|
404 |
||
405 |
def get_request(self): |
|
406 |
"""See SmartClientMedium.get_request().
|
|
407 |
||
408 |
SmartClientStreamMedium always returns a SmartClientStreamMediumRequest
|
|
409 |
for get_request.
|
|
410 |
"""
|
|
411 |
return SmartClientStreamMediumRequest(self) |
|
412 |
||
413 |
def read_bytes(self, count): |
|
414 |
return self._read_bytes(count) |
|
415 |
||
416 |
||
417 |
class SmartSimplePipesClientMedium(SmartClientStreamMedium): |
|
418 |
"""A client medium using simple pipes.
|
|
419 |
|
|
420 |
This client does not manage the pipes: it assumes they will always be open.
|
|
421 |
"""
|
|
422 |
||
423 |
def __init__(self, readable_pipe, writeable_pipe): |
|
424 |
SmartClientStreamMedium.__init__(self) |
|
425 |
self._readable_pipe = readable_pipe |
|
426 |
self._writeable_pipe = writeable_pipe |
|
427 |
||
428 |
def _accept_bytes(self, bytes): |
|
429 |
"""See SmartClientStreamMedium.accept_bytes."""
|
|
430 |
self._writeable_pipe.write(bytes) |
|
431 |
||
432 |
def _flush(self): |
|
433 |
"""See SmartClientStreamMedium._flush()."""
|
|
434 |
self._writeable_pipe.flush() |
|
435 |
||
436 |
def _read_bytes(self, count): |
|
437 |
"""See SmartClientStreamMedium._read_bytes."""
|
|
438 |
return self._readable_pipe.read(count) |
|
439 |
||
440 |
||
441 |
class SmartSSHClientMedium(SmartClientStreamMedium): |
|
442 |
"""A client medium using SSH."""
|
|
443 |
||
444 |
def __init__(self, host, port=None, username=None, password=None, |
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
445 |
vendor=None, bzr_remote_path=None): |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
446 |
"""Creates a client that will connect on the first use.
|
447 |
|
|
448 |
:param vendor: An optional override for the ssh vendor to use. See
|
|
449 |
bzrlib.transport.ssh for details on ssh vendors.
|
|
450 |
"""
|
|
451 |
SmartClientStreamMedium.__init__(self) |
|
452 |
self._connected = False |
|
453 |
self._host = host |
|
454 |
self._password = password |
|
455 |
self._port = port |
|
456 |
self._username = username |
|
457 |
self._read_from = None |
|
458 |
self._ssh_connection = None |
|
459 |
self._vendor = vendor |
|
460 |
self._write_to = None |
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
461 |
self._bzr_remote_path = bzr_remote_path |
462 |
if self._bzr_remote_path is None: |
|
463 |
symbol_versioning.warn( |
|
464 |
'bzr_remote_path is required as of bzr 0.92', |
|
465 |
DeprecationWarning, stacklevel=2) |
|
466 |
self._bzr_remote_path = os.environ.get('BZR_REMOTE_PATH', 'bzr') |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
467 |
|
468 |
def _accept_bytes(self, bytes): |
|
469 |
"""See SmartClientStreamMedium.accept_bytes."""
|
|
470 |
self._ensure_connection() |
|
471 |
self._write_to.write(bytes) |
|
472 |
||
473 |
def disconnect(self): |
|
474 |
"""See SmartClientMedium.disconnect()."""
|
|
475 |
if not self._connected: |
|
476 |
return
|
|
477 |
self._read_from.close() |
|
478 |
self._write_to.close() |
|
479 |
self._ssh_connection.close() |
|
480 |
self._connected = False |
|
481 |
||
482 |
def _ensure_connection(self): |
|
483 |
"""Connect this medium if not already connected."""
|
|
484 |
if self._connected: |
|
485 |
return
|
|
486 |
if self._vendor is None: |
|
487 |
vendor = ssh._get_ssh_vendor() |
|
488 |
else: |
|
489 |
vendor = self._vendor |
|
490 |
self._ssh_connection = vendor.connect_ssh(self._username, |
|
491 |
self._password, self._host, self._port, |
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
492 |
command=[self._bzr_remote_path, 'serve', '--inet', |
493 |
'--directory=/', '--allow-writes']) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
494 |
self._read_from, self._write_to = \ |
495 |
self._ssh_connection.get_filelike_channels() |
|
496 |
self._connected = True |
|
497 |
||
498 |
def _flush(self): |
|
499 |
"""See SmartClientStreamMedium._flush()."""
|
|
500 |
self._write_to.flush() |
|
501 |
||
502 |
def _read_bytes(self, count): |
|
503 |
"""See SmartClientStreamMedium.read_bytes."""
|
|
504 |
if not self._connected: |
|
505 |
raise errors.MediumNotConnected(self) |
|
506 |
return self._read_from.read(count) |
|
507 |
||
508 |
||
3004.2.1
by Vincent Ladeuil
Fix 150860 by leaving port as user specified it. |
509 |
# Port 4155 is the default port for bzr://, registered with IANA.
|
510 |
BZR_DEFAULT_INTERFACE = '0.0.0.0' |
|
511 |
BZR_DEFAULT_PORT = 4155 |
|
512 |
||
513 |
||
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
514 |
class SmartTCPClientMedium(SmartClientStreamMedium): |
515 |
"""A client medium using TCP."""
|
|
516 |
||
517 |
def __init__(self, host, port): |
|
518 |
"""Creates a client that will connect on the first use."""
|
|
519 |
SmartClientStreamMedium.__init__(self) |
|
520 |
self._connected = False |
|
521 |
self._host = host |
|
522 |
self._port = port |
|
523 |
self._socket = None |
|
524 |
||
525 |
def _accept_bytes(self, bytes): |
|
526 |
"""See SmartClientMedium.accept_bytes."""
|
|
527 |
self._ensure_connection() |
|
3118.2.1
by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall. |
528 |
osutils.send_all(self._socket, bytes) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
529 |
|
530 |
def disconnect(self): |
|
531 |
"""See SmartClientMedium.disconnect()."""
|
|
532 |
if not self._connected: |
|
533 |
return
|
|
534 |
self._socket.close() |
|
535 |
self._socket = None |
|
536 |
self._connected = False |
|
537 |
||
538 |
def _ensure_connection(self): |
|
539 |
"""Connect this medium if not already connected."""
|
|
540 |
if self._connected: |
|
541 |
return
|
|
542 |
self._socket = socket.socket() |
|
543 |
self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) |
|
3004.2.1
by Vincent Ladeuil
Fix 150860 by leaving port as user specified it. |
544 |
if self._port is None: |
545 |
port = BZR_DEFAULT_PORT |
|
546 |
else: |
|
547 |
port = int(self._port) |
|
548 |
result = self._socket.connect_ex((self._host, port)) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
549 |
if result: |
550 |
raise errors.ConnectionError("failed to connect to %s:%d: %s" % |
|
3004.2.1
by Vincent Ladeuil
Fix 150860 by leaving port as user specified it. |
551 |
(self._host, port, os.strerror(result))) |
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
552 |
self._connected = True |
553 |
||
554 |
def _flush(self): |
|
555 |
"""See SmartClientStreamMedium._flush().
|
|
556 |
|
|
557 |
For TCP we do no flushing. We may want to turn off TCP_NODELAY and
|
|
558 |
add a means to do a flush, but that can be done in the future.
|
|
559 |
"""
|
|
560 |
||
561 |
def _read_bytes(self, count): |
|
562 |
"""See SmartClientMedium.read_bytes."""
|
|
563 |
if not self._connected: |
|
564 |
raise errors.MediumNotConnected(self) |
|
565 |
return self._socket.recv(count) |
|
566 |
||
567 |
||
568 |
class SmartClientStreamMediumRequest(SmartClientMediumRequest): |
|
569 |
"""A SmartClientMediumRequest that works with an SmartClientStreamMedium."""
|
|
570 |
||
571 |
def __init__(self, medium): |
|
572 |
SmartClientMediumRequest.__init__(self, medium) |
|
573 |
# check that we are safe concurrency wise. If some streams start
|
|
574 |
# allowing concurrent requests - i.e. via multiplexing - then this
|
|
575 |
# assert should be moved to SmartClientStreamMedium.get_request,
|
|
576 |
# and the setting/unsetting of _current_request likewise moved into
|
|
577 |
# that class : but its unneeded overhead for now. RBC 20060922
|
|
578 |
if self._medium._current_request is not None: |
|
579 |
raise errors.TooManyConcurrentRequests(self._medium) |
|
580 |
self._medium._current_request = self |
|
581 |
||
582 |
def _accept_bytes(self, bytes): |
|
583 |
"""See SmartClientMediumRequest._accept_bytes.
|
|
584 |
|
|
585 |
This forwards to self._medium._accept_bytes because we are operating
|
|
586 |
on the mediums stream.
|
|
587 |
"""
|
|
588 |
self._medium._accept_bytes(bytes) |
|
589 |
||
590 |
def _finished_reading(self): |
|
591 |
"""See SmartClientMediumRequest._finished_reading.
|
|
592 |
||
593 |
This clears the _current_request on self._medium to allow a new
|
|
594 |
request to be created.
|
|
595 |
"""
|
|
596 |
assert self._medium._current_request is self |
|
597 |
self._medium._current_request = None |
|
598 |
||
599 |
def _finished_writing(self): |
|
600 |
"""See SmartClientMediumRequest._finished_writing.
|
|
601 |
||
602 |
This invokes self._medium._flush to ensure all bytes are transmitted.
|
|
603 |
"""
|
|
604 |
self._medium._flush() |
|
605 |
||
606 |
def _read_bytes(self, count): |
|
607 |
"""See SmartClientMediumRequest._read_bytes.
|
|
608 |
|
|
609 |
This forwards to self._medium._read_bytes because we are operating
|
|
610 |
on the mediums stream.
|
|
611 |
"""
|
|
612 |
return self._medium._read_bytes(count) |
|
613 |