Commit a0c82e9c authored by dieter's avatar dieter Committed by Kirill Smelkov

asyncio.base.Protocol: Rename _write -> write_message, _writeit -> write_message_iter

--------
kirr:

Those methods are not private to Protocol, because they are invoked e.g. from
other modules besides ZEO.asyncio.base - e.g. from ZEO.asyncio.server . Make
them public via dropping "_" prefix and naming them using more descriptive names.

Extracted from https://github.com/zopefoundation/ZEO/pull/195
parent f64a44ee
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
A ZEO protocol instance can be used as a connection. A ZEO protocol instance can be used as a connection.
It exchanges ``bytes`` messages. It exchanges ``bytes`` messages.
Messages are sent via the methods Messages are sent via the methods
``_write`` (send a single message) and ``write_message`` (send a single message) and
``_writeit`` (send the messages generated by an iterator) ``write_message_iter`` (send the messages generated by an iterator).
Received messages are reported via callbacks. Received messages are reported via callbacks.
Messages are received in the same order as they have been written; Messages are received in the same order as they have been written;
especially, the messages wrote with ``_writeit`` especially, the messages wrote with ``write_message_iter``
are received as contiguous messages. are received as contiguous messages.
The first message transmits the protocol version. The first message transmits the protocol version.
...@@ -94,19 +94,19 @@ class Protocol(asyncio.Protocol): ...@@ -94,19 +94,19 @@ class Protocol(asyncio.Protocol):
writelines = transport.writelines writelines = transport.writelines
from struct import pack from struct import pack
def write(message): def write_message(message):
if paused: if paused:
append(message) append(message)
else: else:
writelines((pack(">I", len(message)), message)) writelines((pack(">I", len(message)), message))
self._write = write self.write_message = write_message
def writeit(data): def write_message_iter(message_iter):
# Note, don't worry about combining messages. Iters # Note, don't worry about combining messages. Iters
# will be used with blobs, in which case, the individual # will be used with blobs, in which case, the individual
# messages will be big to begin with. # messages will be big to begin with.
data = iter(data) data = iter(message_iter)
if paused: if paused:
append(data) append(data)
return return
...@@ -116,7 +116,7 @@ class Protocol(asyncio.Protocol): ...@@ -116,7 +116,7 @@ class Protocol(asyncio.Protocol):
append(data) append(data)
break break
self._writeit = writeit self.write_message_iter = write_message_iter
got = 0 got = 0
want = 4 want = 4
...@@ -171,11 +171,11 @@ class Protocol(asyncio.Protocol): ...@@ -171,11 +171,11 @@ class Protocol(asyncio.Protocol):
def call_async(self, method, args): def call_async(self, method, args):
"""call method named *method* asynchronously with *args*.""" """call method named *method* asynchronously with *args*."""
self._write(self.encode(0, True, method, args)) self.write_message(self.encode(0, True, method, args))
def call_async_iter(self, it): def call_async_iter(self, it):
self._writeit(self.encode(0, True, method, args) self.write_message_iter(self.encode(0, True, method, args)
for method, args in it) for method, args in it)
def pause_writing(self): def pause_writing(self):
self.paused.append(1) self.paused.append(1)
......
...@@ -210,7 +210,7 @@ class Protocol(base.Protocol): ...@@ -210,7 +210,7 @@ class Protocol(base.Protocol):
self.decode = decoder(protocol_version) self.decode = decoder(protocol_version)
self.heartbeat_bytes = self.encode(-1, 0, '.reply', None) self.heartbeat_bytes = self.encode(-1, 0, '.reply', None)
self._write(self.protocol_version) self.write_message(self.protocol_version)
credentials = (self.credentials,) if self.credentials else () credentials = (self.credentials,) if self.credentials else ()
...@@ -280,7 +280,7 @@ class Protocol(base.Protocol): ...@@ -280,7 +280,7 @@ class Protocol(base.Protocol):
def call(self, future, method, args): def call(self, future, method, args):
self.message_id += 1 self.message_id += 1
self.futures[self.message_id] = future self.futures[self.message_id] = future
self._write(self.encode(self.message_id, False, method, args)) self.write_message(self.encode(self.message_id, False, method, args))
return future return future
def fut(self, method, *args): def fut(self, method, *args):
...@@ -293,7 +293,7 @@ class Protocol(base.Protocol): ...@@ -293,7 +293,7 @@ class Protocol(base.Protocol):
if future is None: if future is None:
future = Fut() future = Fut()
self.futures[message_id] = future self.futures[message_id] = future
self._write( self.write_message(
self.encode(message_id, False, 'loadBefore', (oid, tid))) self.encode(message_id, False, 'loadBefore', (oid, tid)))
@future.add_done_callback @future.add_done_callback
...@@ -321,7 +321,7 @@ class Protocol(base.Protocol): ...@@ -321,7 +321,7 @@ class Protocol(base.Protocol):
def heartbeat(self, write=True): def heartbeat(self, write=True):
if write: if write:
self._write(self.heartbeat_bytes) self.write_message(self.heartbeat_bytes)
self.heartbeat_handle = self.loop.call_later( self.heartbeat_handle = self.loop.call_later(
self.heartbeat_interval, self.heartbeat) self.heartbeat_interval, self.heartbeat)
......
...@@ -55,7 +55,7 @@ class ServerProtocol(base.Protocol): ...@@ -55,7 +55,7 @@ class ServerProtocol(base.Protocol):
def connection_made(self, transport): def connection_made(self, transport):
self.connected = True self.connected = True
super(ServerProtocol, self).connection_made(transport) super(ServerProtocol, self).connection_made(transport)
self._write(self.announce_protocol) self.write_message(self.announce_protocol)
def connection_lost(self, exc): def connection_lost(self, exc):
self.connected = False self.connected = False
...@@ -69,7 +69,8 @@ class ServerProtocol(base.Protocol): ...@@ -69,7 +69,8 @@ class ServerProtocol(base.Protocol):
def finish_connect(self, protocol_version): def finish_connect(self, protocol_version):
if protocol_version == b'ruok': if protocol_version == b'ruok':
self._write(json.dumps(self.zeo_storage.ruok()).encode("ascii")) self.write_message(
json.dumps(self.zeo_storage.ruok()).encode("ascii"))
self.close() self.close()
else: else:
version = protocol_version[1:] version = protocol_version[1:]
...@@ -136,7 +137,7 @@ class ServerProtocol(base.Protocol): ...@@ -136,7 +137,7 @@ class ServerProtocol(base.Protocol):
ValueError("Couldn't pickle response"), ValueError("Couldn't pickle response"),
True) True)
self._write(result) self.write_message(result)
def send_reply_threadsafe(self, message_id, result): def send_reply_threadsafe(self, message_id, result):
self.loop.call_soon_threadsafe(self.reply, message_id, result) self.loop.call_soon_threadsafe(self.reply, message_id, result)
......
...@@ -905,7 +905,7 @@ class ProtocolTests(setupstack.TestCase): ...@@ -905,7 +905,7 @@ class ProtocolTests(setupstack.TestCase):
self.loop = loop = Loop() self.loop = loop = Loop()
loop.create_connection(lambda: Protocol(loop, None), sock=True) loop.create_connection(lambda: Protocol(loop, None), sock=True)
def test_writeit(self): def test_write_message_iter(self):
"""test https://github.com/zopefoundation/ZEO/issues/150.""" """test https://github.com/zopefoundation/ZEO/issues/150."""
loop = self.loop loop = self.loop
protocol, transport = loop.protocol, loop.transport protocol, transport = loop.protocol, loop.transport
...@@ -915,8 +915,8 @@ class ProtocolTests(setupstack.TestCase): ...@@ -915,8 +915,8 @@ class ProtocolTests(setupstack.TestCase):
yield tag yield tag
yield tag yield tag
protocol._writeit(it(b"0")) protocol.write_message_iter(it(b"0"))
protocol._writeit(it(b"1")) protocol.write_message_iter(it(b"1"))
for b in b"0011": for b in b"0011":
l, t = transport.pop(2) l, t = transport.pop(2)
self.assertEqual(l, b"\x00\x00\x00\x01") self.assertEqual(l, b"\x00\x00\x00\x01")
......
...@@ -438,7 +438,7 @@ class ConnectionTests(CommonSetupTearDown): ...@@ -438,7 +438,7 @@ class ConnectionTests(CommonSetupTearDown):
def write(): def write():
try: try:
self._storage._server.client.protocol._write(msg) self._storage._server.client.protocol.write_message(msg)
except Exception as exc: except Exception as exc:
future.set_exception(exc) future.set_exception(exc)
else: else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment