mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-04-04 13:01:26 +00:00
Reorganize folders (stream and connection)
This commit is contained in:
0
network/connection/__init__.py
Normal file
0
network/connection/__init__.py
Normal file
22
network/connection/raw_connection.py
Normal file
22
network/connection/raw_connection.py
Normal file
@ -0,0 +1,22 @@
|
||||
import asyncio
|
||||
from .raw_connection_interface import IRawConnection
|
||||
|
||||
class RawConnection(IRawConnection):
|
||||
|
||||
def __init__(self, ip, port, reader, writer):
|
||||
self.conn_ip = ip
|
||||
self.conn_port = port
|
||||
self.reader = reader
|
||||
self.writer = writer
|
||||
|
||||
# def __init__(self, ip, port):
|
||||
# self.conn_ip = ip
|
||||
# self.conn_port = port
|
||||
# self.reader, self.writer = self.open_connection()
|
||||
|
||||
# async def open_connection(self):
|
||||
# """
|
||||
# opens a connection on self.ip and self.port
|
||||
# :return: a raw connection
|
||||
# """
|
||||
# return await asyncio.open_connection(self.conn_ip, self.conn_port)
|
||||
15
network/connection/raw_connection_interface.py
Normal file
15
network/connection/raw_connection_interface.py
Normal file
@ -0,0 +1,15 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class IRawConnection(ABC):
|
||||
"""
|
||||
A Raw Connection provides a Reader and a Writer
|
||||
open_connection should return such a connection
|
||||
"""
|
||||
|
||||
# @abstractmethod
|
||||
# async def open_connection(self):
|
||||
# """
|
||||
# opens a connection on ip and port
|
||||
# :return: a raw connection
|
||||
# """
|
||||
# pass
|
||||
@ -4,7 +4,7 @@ from transport.connection.raw_connection import RawConnection
|
||||
|
||||
class Swarm(INetwork):
|
||||
|
||||
def __init__(self, my_peer_id, peerstore):
|
||||
def __init__(self, my_peer_id, peerstore, upgrader):
|
||||
self.my_peer_id = my_peer_id
|
||||
self.peerstore = peerstore
|
||||
self.connections = {}
|
||||
@ -18,19 +18,17 @@ class Swarm(INetwork):
|
||||
|
||||
def new_stream(self, peer_id, protocol_id):
|
||||
"""
|
||||
Determine if a connection to peer_id already exists
|
||||
If a connection to peer_id exists, then
|
||||
c = existing connection,
|
||||
otherwise c = new muxed connection to peer_id
|
||||
s = c.open_stream(protocol_id)
|
||||
return s
|
||||
|
||||
:param peer_id: peer_id of destination
|
||||
:param protocol_id: protocol id
|
||||
:return: stream instance
|
||||
"""
|
||||
muxed_connection = None
|
||||
if peer_id in self.connections:
|
||||
"""
|
||||
If muxed connection already exists for peer_id,
|
||||
set muxed connection equal to
|
||||
existing muxed connection
|
||||
"""
|
||||
muxed_connection = self.connections[peer_id]
|
||||
else:
|
||||
addrs = self.peerstore.addrs(peer_id)
|
||||
|
||||
Reference in New Issue
Block a user