mirror of https://github.com/libp2p/py-libp2p.git
ZX
6 years ago
committed by
GitHub
9 changed files with 124 additions and 95 deletions
@ -0,0 +1,42 @@ |
|||
import asyncio |
|||
from .net_stream_interface import INetStream |
|||
|
|||
class NetStream(INetStream): |
|||
|
|||
def __init__(self, muxed_stream): |
|||
self.muxed_stream = muxed_stream |
|||
|
|||
def get_protocol(self): |
|||
""" |
|||
:return: protocol id that stream runs on |
|||
""" |
|||
return self.protocol_id |
|||
|
|||
def set_protocol(self, protocol_id): |
|||
""" |
|||
:param protocol_id: protocol id that stream runs on |
|||
:return: true if successful |
|||
""" |
|||
self.protocol_id = protocol_id |
|||
|
|||
def read(self): |
|||
""" |
|||
read from stream |
|||
:return: bytes of input until EOF |
|||
""" |
|||
return self.muxed_stream.read() |
|||
|
|||
def write(self, bytes): |
|||
""" |
|||
write to stream |
|||
:return: number of bytes written |
|||
""" |
|||
return self.muxed_stream.write(bytes) |
|||
|
|||
def close(self): |
|||
""" |
|||
close stream |
|||
:return: true if successful |
|||
""" |
|||
self.muxed_stream.close() |
|||
return True |
@ -1,6 +1,6 @@ |
|||
from abc import ABC, abstractmethod |
|||
|
|||
class IStream(ABC): |
|||
class INetStream(ABC): |
|||
|
|||
def __init__(self, peer_id, multi_addr, connection): |
|||
self.peer_id = peer_id |
@ -1,58 +0,0 @@ |
|||
import asyncio |
|||
from .stream_interface import IStream |
|||
|
|||
class Stream(IStream): |
|||
|
|||
def __init__(self, peer_id, multi_addr, connection): |
|||
IStream.__init__(self, peer_id, multi_addr, connection) |
|||
self.peer_id = peer_id |
|||
|
|||
self.multi_addr = multi_addr |
|||
|
|||
self.stream_ip = multi_addr.get_protocol_value("ip4") |
|||
self.stream_port = multi_addr.get_protocol_value("tcp") |
|||
|
|||
self.reader = connection.reader |
|||
self.writer = connection.writer |
|||
|
|||
# TODO should construct protocol id from constructor |
|||
self.protocol_id = None |
|||
|
|||
def get_protocol(self): |
|||
""" |
|||
:return: protocol id that stream runs on |
|||
""" |
|||
return self.protocol_id |
|||
|
|||
def set_protocol(self, protocol_id): |
|||
""" |
|||
:param protocol_id: protocol id that stream runs on |
|||
:return: true if successful |
|||
""" |
|||
self.protocol_id = protocol_id |
|||
|
|||
def read(self): |
|||
""" |
|||
read from stream |
|||
:return: bytes of input |
|||
""" |
|||
return self.reader.read(-1) |
|||
|
|||
def write(self, _bytes): |
|||
""" |
|||
write to stream |
|||
:return: number of bytes written |
|||
""" |
|||
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 |
|||
""" |
|||
self.writer.close() |
@ -1,18 +1,24 @@ |
|||
class TransportUpgrader(object): |
|||
|
|||
def __init__(self, secOpt, muxerOpt): |
|||
self.sec = secOpt |
|||
self.muxer = muxerOpt |
|||
def __init__(self, secOpt, muxerOpt): |
|||
self.sec = secOpt |
|||
self.muxer = muxerOpt |
|||
|
|||
def upgrade_listener(self, transport, listeners): |
|||
""" |
|||
upgrade multiaddr listeners to libp2p-transport listeners |
|||
def upgrade_listener(self, transport, listeners): |
|||
""" |
|||
upgrade multiaddr listeners to libp2p-transport listeners |
|||
|
|||
""" |
|||
pass |
|||
|
|||
def upgrade_security(self): |
|||
pass |
|||
""" |
|||
pass |
|||
|
|||
def upgrade_security(self): |
|||
pass |
|||
|
|||
def upgrade_muxer(self): |
|||
pass |
|||
def upgrade_connection(self, conn): |
|||
""" |
|||
upgrade raw connection to muxed connection |
|||
""" |
|||
# For PoC, no security |
|||
# Default to mplex |
|||
pass |
|||
|
Loading…
Reference in new issue