commit
3cd533b2a5
|
@ -1,40 +1,55 @@
|
|||
from .stream_interface import IStream
|
||||
import asyncio
|
||||
|
||||
class Stream(IStream):
|
||||
|
||||
def __init__(self, peer_id):
|
||||
def __init__(self, peer_id, multi_addr):
|
||||
IStream.__init__(self, peer_id, multi_addr)
|
||||
self.peer_id = peer_id
|
||||
|
||||
ip = multi_addr.get_protocol_value("ip4")
|
||||
port = multi_addr.get_protocol_value("tcp")
|
||||
|
||||
self.open_connection(ip, port)
|
||||
|
||||
async def open_connection(self, ip, port):
|
||||
self.reader, self.writer = await asyncio.open_connection(ip, port)
|
||||
|
||||
def protocol(self):
|
||||
"""
|
||||
:return: protocol id that stream runs on
|
||||
"""
|
||||
pass
|
||||
return self.protocol_id
|
||||
|
||||
def set_protocol(self, protocol_id):
|
||||
"""
|
||||
:param protocol_id: protocol id that stream runs on
|
||||
:return: true if successful
|
||||
"""
|
||||
pass
|
||||
self.protocol_id = protocol_id
|
||||
|
||||
def read(self):
|
||||
"""
|
||||
read from stream
|
||||
:return: bytes of input
|
||||
"""
|
||||
pass
|
||||
return self.reader.read(-1)
|
||||
|
||||
def write(self, _bytes):
|
||||
"""
|
||||
write to stream
|
||||
:return: number of bytes written
|
||||
"""
|
||||
pass
|
||||
return self.write_to_stream(_bytes)
|
||||
|
||||
async def write_to_stream(self, _bytes):
|
||||
to_return = self.writer.write(_bytes)
|
||||
await self.writer.drain()
|
||||
return to_return
|
||||
|
||||
def close(self):
|
||||
"""
|
||||
close stream
|
||||
:return: true if successful
|
||||
"""
|
||||
pass
|
||||
self.writer.close()
|
||||
|
|
|
@ -2,8 +2,9 @@ from abc import ABC, abstractmethod
|
|||
|
||||
class IStream(ABC):
|
||||
|
||||
def __init__(self, peer_id):
|
||||
def __init__(self, peer_id, multi_addr):
|
||||
self.peer_id = peer_id
|
||||
self.multi_addr = multi_addr
|
||||
|
||||
@abstractmethod
|
||||
def protocol(self):
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
asyncio
|
||||
pylint
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user