Spec out basics on send/send_batch once again
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Luke Murphy 2020-08-01 18:53:28 +02:00
parent fe32a87a63
commit d05ac50741
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 26 additions and 37 deletions

View File

@ -1,46 +1,40 @@
"""Sans I/O wire protocol for Hypercore"""
from typing import List, Optional
from typing import List, Tuple
import attr
import pyvarint
from pyvarint import encode, encoding_length
__all__ = ["SimpleMessageChannel"]
@attr.s(auto_attribs=True)
class SimpleMessageChannel:
"""A simple message channel."""
message: Optional[bytes] = None
ptr: int = 0
varint: int = 0
factor: int = 1
length: int = 0
header: int = 0
state: int = 0
consumed: int = 0
max_size: int = 8 * 1024 * 1024
types: List = attr.Factory(list)
receiving: bool = False
destroyed: bool = False
error: Optional[Exception] = None
# TODO(decentral1se): context, onmessage, onmissing
async def send(self, channel: int, type: int, message: bytes) -> bytes:
"""Produce data that can be sent over the channel."""
"""Encode a channel, type and message data to be sent.
:param channel: the message channel identifier
:param type: the type of message
:param message: the message data
"""
header = channel << 4 or type
length = self.encoding_length(type, message) + pyvarint.encoding_length(header)
# TODO(decentral1se): implement offset in pyvarint encode/decode
length = len(message) + encoding_length(header)
return encode(length) + encode(header) + message
async def recv(self, data: bytes) -> bool:
"""Receive data sent over a channel."""
pass
def destroy(self) -> None:
"""Mark message channel as destroyed."""
self.destroyed = True
def encoding_length(self, type:int, message: bytes) -> int:
"""TODO"""
async def send_batch(self, messages: List[Tuple[int, int, bytes]]) -> bytes:
"""Encodes a series of messages into a single payload of bytes.
:param messages: Several data messages
"""
payload = b""
for (channel, type, message) in messages:
payload += await self.send(channel, type, message)
return payload
async def recv(self, data: bytes) -> Tuple[int, int, bytes]:
"""Encode a channel, type, message to be sent.
:param data: the message data
"""
pass

View File

@ -1,5 +0,0 @@
async def test_send_produces_bytes(smc):
channel, type, message = 0, 0, b"abc"
payload = await smc.send(channel, type, message)
assert isinstance(payload, bytes)