Merge pull request #6 from zixuanzh/basic-host

Basic host implemented
This commit is contained in:
Robert Zajac 2018-10-21 12:59:11 -04:00 committed by GitHub
commit 0e7d8dd927
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 28 deletions

View File

@ -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

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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):

View File

@ -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

View File

@ -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
"""