refactor for sprint

This commit is contained in:
zixuanzh
2018-11-11 09:56:44 -05:00
parent fd0958aa4f
commit 2bde260f5f
20 changed files with 102 additions and 112 deletions

0
muxer/mplex/__init__.py Normal file
View File

View File

@ -0,0 +1,41 @@
from .muxed_connection_interface import IMuxedConn
class MuxedConn(IMuxedConn):
"""
reference: https://github.com/libp2p/go-mplex/blob/master/multiplex.go
"""
def __init__(self, conn, initiator):
"""
create a new muxed connection
:param conn: an instance of raw connection
:param initiator: boolean to prevent multiplex with self
"""
self.raw_conn = conn
self.initiator = initiator
def close(self):
"""
close the stream muxer and underlying raw connection
"""
pass
def is_closed(self):
"""
check connection is fully closed
:return: true if successful
"""
pass
def open_stream(self, protocol_id, stream_name):
"""
creates a new muxed_stream
:return: a new stream
"""
pass
def accept_stream(self):
"""
accepts a muxed stream opened by the other end
:return: the accepted stream
"""
pass

View File

@ -0,0 +1,34 @@
from abc import ABC, abstractmethod
class IMuxedConn(ABC):
"""
reference: https://github.com/libp2p/go-stream-muxer/blob/master/muxer.go
"""
# TODO closer
@abstractmethod
def is_closed(self):
"""
check connection is fully closed
:return: true if successful
"""
pass
@abstractmethod
def open_stream(self, protocol_id, stream_name):
"""
creates a new muxed_stream
:param protocol_id: id to be associated with stream
:param stream_name: name as part of identifier
:return: a new stream
"""
pass
@abstractmethod
def accept_stream(self):
"""
accepts a muxed stream opened by the other end
:return: the accepted stream
"""
pass

View File

@ -0,0 +1,34 @@
from .muxed_stream_interface import IMuxedStream
class MuxedStream(IMuxedStream):
"""
reference: https://github.com/libp2p/go-mplex/blob/master/stream.go
"""
def __init__(self, protocol_id, stream_name):
self.protocol_id = protocol_id
self.name = stream_name
def read(self):
pass
def write(self):
pass
def close(self):
pass
def reset(self):
"""
closes both ends of the stream
tells this remote side to hang up
:return: error/exception
"""
pass
def set_deadline(self, ttl):
"""
set deadline for muxed stream
:return: a new stream
"""
pass

View File

@ -0,0 +1,25 @@
from abc import ABC, abstractmethod
# from datetime import time
class IMuxedStream(ABC):
# TODO Reader
# TODO Writer
# TODO Closer
@abstractmethod
def reset(self):
"""
closes both ends of the stream
tells this remote side to hang up
:return: error/exception
"""
pass
@abstractmethod
def set_deadline(self, ttl):
"""
set deadline for muxed stream
:return: a new stream
"""
pass

View File

@ -0,0 +1,44 @@
from .muxed_stream import MuxedStream
from .muxed_connection import MuxedConn
class Multiplex(object):
"""
reference: https://github.com/whyrusleeping/go-smux-multiplex/blob/master/multiplex.go
"""
def __init__(self, conn, initiator):
"""
:param conn: an instance of raw connection
: param initiator: boolean to prevent multiplex with self
"""
self.muxed_conn = MuxedConn(conn, initiator)
def close(self):
"""
close the stream muxer and underlying raw connection
"""
return self.muxed_conn.close()
def is_closed(self):
"""
check connection is fully closed
:return: true if successful
"""
return self.muxed_conn.is_closed()
def open_stream(self, protocol_id, stream_name):
"""
creates a new muxed_stream
:return: a new stream
"""
return self.muxed_conn.open_stream(protocol_id, stream_name)
def accept_stream(self, _muxed_stream):
"""
accepts a muxed stream opened by the other end
:param _muxed_stream: stream to be accepted
:return: the accepted stream
"""
pass
# def new_conn(raw_conn, is_server):
# pass