diff --git a/.gitignore b/.gitignore index 894a44c..ee759a4 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ venv.bak/ # mypy .mypy_cache/ + +# pycharm +.idea/ diff --git a/host/basic_host.py b/host/basic_host.py index 5ea942a..133571c 100644 --- a/host/basic_host.py +++ b/host/basic_host.py @@ -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 diff --git a/host/host_interface.py b/host/host_interface.py index 13ff83b..eaca0f8 100644 --- a/host/host_interface.py +++ b/host/host_interface.py @@ -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 """ diff --git a/network/multiaddr.py b/network/multiaddr.py new file mode 100644 index 0000000..e6d4a5c --- /dev/null +++ b/network/multiaddr.py @@ -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 diff --git a/network/network_interface.py b/network/network_interface.py index 46c2d57..ae805d3 100644 --- a/network/network_interface.py +++ b/network/network_interface.py @@ -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 + """ diff --git a/network/stream.py b/network/stream.py index eeb8589..024740c 100644 --- a/network/stream.py +++ b/network/stream.py @@ -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 diff --git a/network/stream_interface.py b/network/stream_interface.py index f49deb5..4649e73 100644 --- a/network/stream_interface.py +++ b/network/stream_interface.py @@ -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 diff --git a/network/swarm.py b/network/swarm.py index b6db89a..50f7b31 100644 --- a/network/swarm.py +++ b/network/swarm.py @@ -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 diff --git a/requirements.txt b/requirements.txt index fa498a0..831d89d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ asyncio -multiaddr \ No newline at end of file +pylint