mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
fixed merge conflicts
This commit is contained in:
39
network/multiaddr.py
Normal file
39
network/multiaddr.py
Normal file
@ -0,0 +1,39 @@
|
||||
class Multiaddr:
|
||||
|
||||
def __init__(self, addr):
|
||||
if not addr[0] == "/":
|
||||
raise MultiaddrValueError("Invalid input multiaddr.")
|
||||
|
||||
addr = addr[1:]
|
||||
protocol_map = dict()
|
||||
split_addr = addr.split("/")
|
||||
|
||||
if not split_addr or len(split_addr) % 2 != 0:
|
||||
raise MultiaddrValueError("Invalid input multiaddr.")
|
||||
|
||||
is_protocol = True
|
||||
curr_protocol = ""
|
||||
|
||||
for addr_part in split_addr:
|
||||
if is_protocol:
|
||||
curr_protocol = addr_part
|
||||
else:
|
||||
protocol_map[curr_protocol] = addr_part
|
||||
is_protocol = not is_protocol
|
||||
|
||||
self.protocol_map = protocol_map
|
||||
self.addr = addr
|
||||
|
||||
def get_protocols(self):
|
||||
return list(self.protocol_map.keys())
|
||||
|
||||
def get_protocol_value(self, protocol):
|
||||
if protocol not in self.protocol_map:
|
||||
return None
|
||||
|
||||
return self.protocol_map[protocol]
|
||||
|
||||
|
||||
class MultiaddrValueError(ValueError):
|
||||
"""Raised when the input string to the Multiaddr constructor was invalid."""
|
||||
pass
|
||||
@ -2,8 +2,7 @@ from abc import ABC, abstractmethod
|
||||
|
||||
class INetwork(ABC):
|
||||
|
||||
def __init__(self, context, my_peer_id, peer_store):
|
||||
self.context = context
|
||||
def __init__(self, my_peer_id, peer_store):
|
||||
self.my_peer_id = my_peer_id
|
||||
self.peer_store = peer_store
|
||||
|
||||
@ -16,10 +15,16 @@ class INetwork(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def new_stream(self, context, peer_id):
|
||||
def new_stream(self, peer_id):
|
||||
"""
|
||||
:param context: context instance
|
||||
:param peer_id: peer_id of destination
|
||||
:return: stream instance
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def listen(self, *args):
|
||||
"""
|
||||
:param *args: one or many multiaddrs to start listening on
|
||||
:return: true if at least one success
|
||||
"""
|
||||
|
||||
@ -3,8 +3,7 @@ import asyncio
|
||||
|
||||
class Stream(IStream):
|
||||
|
||||
def __init__(self, context, peer_id):
|
||||
self.context = context
|
||||
def __init__(self, peer_id):
|
||||
self.peer_id = peer_id
|
||||
|
||||
peer_store = context.peer_store
|
||||
|
||||
@ -2,8 +2,7 @@ from abc import ABC, abstractmethod
|
||||
|
||||
class IStream(ABC):
|
||||
|
||||
def __init__(self, context, peer_id):
|
||||
self.context = context
|
||||
def __init__(self, peer_id):
|
||||
self.peer_id = peer_id
|
||||
|
||||
@abstractmethod
|
||||
|
||||
@ -2,8 +2,7 @@ from .network_interface import INetwork
|
||||
|
||||
class Swarm(INetwork):
|
||||
|
||||
def __init__(self, context, my_peer_id, peer_store):
|
||||
self.context = context
|
||||
def __init__(self, my_peer_id, peer_store):
|
||||
self.my_peer_id = my_peer_id
|
||||
self.peer_store = peer_store
|
||||
|
||||
@ -14,10 +13,16 @@ class Swarm(INetwork):
|
||||
"""
|
||||
pass
|
||||
|
||||
def new_stream(self, context, peer_id):
|
||||
def new_stream(self, peer_id):
|
||||
"""
|
||||
:param context: context instance
|
||||
:param peer_id: peer_id of destination
|
||||
:return: stream instance
|
||||
"""
|
||||
pass
|
||||
|
||||
def listen(self, *args):
|
||||
"""
|
||||
:param *args: one or many multiaddrs to start listening on
|
||||
:return: true if at least one success
|
||||
"""
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user