mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
restructured
This commit is contained in:
@ -1,27 +0,0 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class IConnection(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def get_observed_addrs(self):
|
||||
"""
|
||||
retrieve observed addresses from underlying transport
|
||||
:return: list of multiaddrs
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_peer_info(self):
|
||||
"""
|
||||
retrieve peer info object that the connection connects to
|
||||
:return: a peer info object
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def set_peer_info(self, peer_info):
|
||||
"""
|
||||
:param peer_info: a peer info object that contains info of peer
|
||||
:return: True if successful
|
||||
"""
|
||||
pass
|
||||
@ -28,3 +28,4 @@ class INetwork(ABC):
|
||||
:param *args: one or many multiaddrs to start listening on
|
||||
:return: True if at least one success
|
||||
"""
|
||||
pass
|
||||
@ -1,55 +0,0 @@
|
||||
from .stream_interface import IStream
|
||||
import asyncio
|
||||
|
||||
class Stream(IStream):
|
||||
|
||||
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
|
||||
"""
|
||||
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,46 +0,0 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class IStream(ABC):
|
||||
|
||||
def __init__(self, peer_id, multi_addr):
|
||||
self.peer_id = peer_id
|
||||
self.multi_addr = multi_addr
|
||||
|
||||
@abstractmethod
|
||||
def protocol(self):
|
||||
"""
|
||||
:return: protocol id that stream runs on
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def set_protocol(self, protocol_id):
|
||||
"""
|
||||
:param protocol_id: protocol id that stream runs on
|
||||
:return: true if successful
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def read(self):
|
||||
"""
|
||||
read from stream
|
||||
:return: bytes of input
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def write(self, _bytes):
|
||||
"""
|
||||
write to stream
|
||||
:return: number of bytes written
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def close(self):
|
||||
"""
|
||||
close stream
|
||||
:return: true if successful
|
||||
"""
|
||||
pass
|
||||
@ -1,54 +0,0 @@
|
||||
import asyncio
|
||||
from .transport_interface import ITransport
|
||||
from .listener_interface import IListener
|
||||
|
||||
class TCP(ITransport):
|
||||
|
||||
def __init__(self):
|
||||
self.multiaddr = None
|
||||
|
||||
class Listener(IListener):
|
||||
|
||||
def listen(self, multiaddr):
|
||||
"""
|
||||
put listener in listening mode and wait for incoming connections
|
||||
:param multiaddr: multiaddr of peer
|
||||
:return: return True if successful
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_addrs(self):
|
||||
"""
|
||||
retrieve list of addresses the listener is listening on
|
||||
:return: return list of addrs
|
||||
"""
|
||||
pass
|
||||
|
||||
def close(self, options=None):
|
||||
"""
|
||||
close the listener such that no more connections
|
||||
can be open on this transport instance
|
||||
:param options: optional object potential with timeout
|
||||
a timeout value in ms that fires and destroy all connections
|
||||
:return: return True if successful
|
||||
"""
|
||||
pass
|
||||
|
||||
def dial(self, multiaddr, options=None):
|
||||
"""
|
||||
dial a transport to peer listening on multiaddr
|
||||
:param multiaddr: multiaddr of peer
|
||||
:param options: optional object
|
||||
:return: list of multiaddrs
|
||||
"""
|
||||
pass
|
||||
|
||||
def create_listener(self, handler_function, options=None):
|
||||
"""
|
||||
create listener on transport
|
||||
:param options: optional object with properties the listener must have
|
||||
:param handler_function: a function called when a new conntion is received
|
||||
that takes a connection as argument which implements interface-connection
|
||||
:return: a listener object that implements listener_interface.py
|
||||
"""
|
||||
pass
|
||||
@ -1,24 +0,0 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class ITransport(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def dial(self, multiaddr, options=None):
|
||||
"""
|
||||
dial a transport to peer listening on multiaddr
|
||||
:param multiaddr: multiaddr of peer
|
||||
:param options: optional object
|
||||
:return: list of multiaddrs
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def create_listener(self, handler_function, options=None):
|
||||
"""
|
||||
create listener on transport
|
||||
:param options: optional object with properties the listener must have
|
||||
:param handler_function: a function called when a new conntion is received
|
||||
that takes a connection as argument which implements interface-connection
|
||||
:return: a listener object that implements listener_interface.py
|
||||
"""
|
||||
pass
|
||||
Reference in New Issue
Block a user