From 9e64202e5c41ce4648c2576a9ccc1cdbe979f7f0 Mon Sep 17 00:00:00 2001 From: zixuanzh Date: Sun, 21 Oct 2018 12:55:45 -0400 Subject: [PATCH] basic-host and remove context --- host/basic_host.py | 22 ++++++++++++---------- host/host_interface.py | 7 +------ network/network_interface.py | 6 ++---- network/stream.py | 3 +-- network/stream_interface.py | 3 +-- network/swarm.py | 6 ++---- requirements.txt | 1 - 7 files changed, 19 insertions(+), 29 deletions(-) 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/network_interface.py b/network/network_interface.py index 46c2d57..f04c3a3 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,9 +15,8 @@ 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 """ diff --git a/network/stream.py b/network/stream.py index 42fc1f3..634e25f 100644 --- a/network/stream.py +++ b/network/stream.py @@ -2,8 +2,7 @@ from .stream_interface import IStream class Stream(IStream): - def __init__(self, context, peer_id): - self.context = context + def __init__(self, peer_id): self.peer_id = peer_id def protocol(self): 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..8da3cb5 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,9 +13,8 @@ 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 """ diff --git a/requirements.txt b/requirements.txt index 54b18e2..e69de29 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +0,0 @@ -pylint \ No newline at end of file