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 @@
A ZEO protocol instance can be used as a connection.
It exchanges ``bytes`` messages.
Messages are sent via the methods
``_write`` (send a single message) and
``_writeit`` (send the messages generated by an iterator)
``write_message`` (send a single message) and
``write_message_iter`` (send the messages generated by an iterator).
Received messages are reported via callbacks.
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.
The first message transmits the protocol version.
......@@ -94,19 +94,19 @@ class Protocol(asyncio.Protocol):
writelines = transport.writelines
from struct import pack
def write(message):
def write_message(message):
if paused:
append(message)
else:
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
# will be used with blobs, in which case, the individual
# messages will be big to begin with.
data = iter(data)
data = iter(message_iter)
if paused:
append(data)
return
......@@ -116,7 +116,7 @@ class Protocol(asyncio.Protocol):
append(data)
break
self._writeit = writeit
self.write_message_iter = write_message_iter
got = 0
want = 4
......@@ -171,10 +171,10 @@ class Protocol(asyncio.Protocol):
def call_async(self, method, 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):
self._writeit(self.encode(0, True, method, args)
self.write_message_iter(self.encode(0, True, method, args)
for method, args in it)
def pause_writing(self):
......
......@@ -210,7 +210,7 @@ class Protocol(base.Protocol):
self.decode = decoder(protocol_version)
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 ()
......@@ -280,7 +280,7 @@ class Protocol(base.Protocol):
def call(self, future, method, args):
self.message_id += 1
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
def fut(self, method, *args):
......@@ -293,7 +293,7 @@ class Protocol(base.Protocol):
if future is None:
future = Fut()
self.futures[message_id] = future
self._write(
self.write_message(
self.encode(message_id, False, 'loadBefore', (oid, tid)))
@future.add_done_callback
......@@ -321,7 +321,7 @@ class Protocol(base.Protocol):
def heartbeat(self, write=True):
if write:
self._write(self.heartbeat_bytes)
self.write_message(self.heartbeat_bytes)
self.heartbeat_handle = self.loop.call_later(
self.heartbeat_interval, self.heartbeat)
......
......@@ -55,7 +55,7 @@ class ServerProtocol(base.Protocol):
def connection_made(self, transport):
self.connected = True
super(ServerProtocol, self).connection_made(transport)
self._write(self.announce_protocol)
self.write_message(self.announce_protocol)
def connection_lost(self, exc):
self.connected = False
......@@ -69,7 +69,8 @@ class ServerProtocol(base.Protocol):
def finish_connect(self, protocol_version):
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()
else:
version = protocol_version[1:]
......@@ -136,7 +137,7 @@ class ServerProtocol(base.Protocol):
ValueError("Couldn't pickle response"),
True)
self._write(result)
self.write_message(result)
def send_reply_threadsafe(self, message_id, result):
self.loop.call_soon_threadsafe(self.reply, message_id, result)
......
......@@ -905,7 +905,7 @@ class ProtocolTests(setupstack.TestCase):
self.loop = loop = Loop()
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."""
loop = self.loop
protocol, transport = loop.protocol, loop.transport
......@@ -915,8 +915,8 @@ class ProtocolTests(setupstack.TestCase):
yield tag
yield tag
protocol._writeit(it(b"0"))
protocol._writeit(it(b"1"))
protocol.write_message_iter(it(b"0"))
protocol.write_message_iter(it(b"1"))
for b in b"0011":
l, t = transport.pop(2)
self.assertEqual(l, b"\x00\x00\x00\x01")
......
......@@ -438,7 +438,7 @@ class ConnectionTests(CommonSetupTearDown):
def write():
try:
self._storage._server.client.protocol._write(msg)
self._storage._server.client.protocol.write_message(msg)
except Exception as exc:
future.set_exception(exc)
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