fixed merge conflicts
This commit is contained in:
commit
f51f975533
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -102,3 +102,6 @@ venv.bak/
|
|||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
|
||||
# pycharm
|
||||
.idea/
|
||||
|
|
|
@ -8,23 +8,23 @@ from .host_interface import IHost
|
|||
class BasicHost(IHost):
|
||||
|
||||
# default options constructor
|
||||
def __init__(self, context, network):
|
||||
self.context = context
|
||||
self.network = network
|
||||
def __init__(self, _network):
|
||||
self.network = _network
|
||||
# self.stream_handlers = {}
|
||||
|
||||
def get_id(self):
|
||||
"""
|
||||
:return: peer_id of host
|
||||
"""
|
||||
pass
|
||||
return self.network.get_peer_id()
|
||||
|
||||
def get_network(self):
|
||||
"""
|
||||
:return: network instance of host
|
||||
"""
|
||||
pass
|
||||
return self.network
|
||||
|
||||
def mux(self):
|
||||
def get_mux(self):
|
||||
"""
|
||||
:return: mux instance of host
|
||||
"""
|
||||
|
@ -37,15 +37,17 @@ class BasicHost(IHost):
|
|||
:param stream_handler: a stream handler function
|
||||
:return: true if successful
|
||||
"""
|
||||
pass
|
||||
return self.network.set_stream_handler(protocol_id, stream_handler)
|
||||
|
||||
|
||||
# protocol_id can be a list of protocol_ids
|
||||
# stream will decide which protocol_id to run on
|
||||
def new_stream(self, context, peer_id, protocol_id):
|
||||
def new_stream(self, peer_id, protocol_id):
|
||||
"""
|
||||
:param context: a context instance
|
||||
:param peer_id: peer_id that host is connecting
|
||||
:param proto_id: protocol id that stream runs on
|
||||
:return: true if successful
|
||||
"""
|
||||
pass
|
||||
stream = self.network.new_stream(peer_id)
|
||||
stream.set_protocol(protocol_id)
|
||||
return stream
|
||||
|
|
|
@ -2,11 +2,6 @@ from abc import ABC, abstractmethod
|
|||
|
||||
class IHost(ABC):
|
||||
|
||||
# default options constructor
|
||||
def __init__(self, context, network):
|
||||
self.context = context
|
||||
self.network = network
|
||||
|
||||
@abstractmethod
|
||||
def get_id(self):
|
||||
"""
|
||||
|
@ -22,7 +17,7 @@ class IHost(ABC):
|
|||
pass
|
||||
|
||||
@abstractmethod
|
||||
def mux(self):
|
||||
def get_mux(self):
|
||||
"""
|
||||
:return: mux instance of host
|
||||
"""
|
||||
|
|
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
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
asyncio
|
||||
multiaddr
|
||||
pylint
|
||||
|
|
Loading…
Reference in New Issue
Block a user