From 48dfd9a77ad3b5534c9d4a4beed0dd1fd36300c8 Mon Sep 17 00:00:00 2001 From: Alex Haynes Date: Sat, 20 Oct 2018 13:42:16 -0400 Subject: [PATCH 1/5] started on stream --- network/stream.py | 17 +++++++++++++++-- requirements.txt | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/network/stream.py b/network/stream.py index 42fc1f3..9765891 100644 --- a/network/stream.py +++ b/network/stream.py @@ -1,4 +1,5 @@ from .stream_interface import IStream +import asyncio class Stream(IStream): @@ -6,18 +7,30 @@ class Stream(IStream): self.context = context self.peer_id = peer_id + peer_store = context.peer_store + peer_addr = peer_store.get(peer_id) + + # look up peer_id -> multiaddr in peer store + # parse multiaddr and set_protocol based on it + # open connection to multiaddr + # save connection to stream's state + self.open_connection(ip, port) + + async def open_connection(self, ip, port): + self.reader, self.writer = await asyncio.open_connection(ip, port) + def protocol(self): """ :return: protocol id that stream runs on """ - pass + return self.protocol_id def set_protocol(self, protocol_id): """ :param protocol_id: protocol id that stream runs on :return: true if successful """ - pass + self.protocol_id = protocol_id def read(self): """ diff --git a/requirements.txt b/requirements.txt index e69de29..404572b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1 @@ +asyncio \ No newline at end of file From 007d9915e238212f7650544e988dd0d5d48fa456 Mon Sep 17 00:00:00 2001 From: Alex Haynes Date: Sun, 21 Oct 2018 13:35:56 -0400 Subject: [PATCH 2/5] implemented stream io interactions --- network/stream.py | 13 ++++++++++--- requirements.txt | 3 ++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/network/stream.py b/network/stream.py index 9765891..eeb8589 100644 --- a/network/stream.py +++ b/network/stream.py @@ -9,6 +9,8 @@ class Stream(IStream): peer_store = context.peer_store peer_addr = peer_store.get(peer_id) + ip = peer_addr.get_protocol("ip4") + port = peer_addr.get_protocol("tcp") # look up peer_id -> multiaddr in peer store # parse multiaddr and set_protocol based on it @@ -37,18 +39,23 @@ class Stream(IStream): read from stream :return: bytes of input """ - pass + return self.reader.read(-1) def write(self, _bytes): """ write to stream :return: number of bytes written """ - pass + return self.write_to_stream(_bytes) + + async def write_to_stream(self, _bytes): + to_return = self.writer.write(_bytes) + await self.writer.drain() + return to_return def close(self): """ close stream :return: true if successful """ - pass + self.writer.close() diff --git a/requirements.txt b/requirements.txt index 404572b..fa498a0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -asyncio \ No newline at end of file +asyncio +multiaddr \ No newline at end of file From 08b37ee7cd606b20df4774cc1b14e0d42332619a Mon Sep 17 00:00:00 2001 From: Alex Haynes Date: Sun, 21 Oct 2018 14:48:53 -0400 Subject: [PATCH 3/5] updated to not require peerstore --- network/stream.py | 8 +++----- network/stream_interface.py | 3 ++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/network/stream.py b/network/stream.py index 024740c..04b8bda 100644 --- a/network/stream.py +++ b/network/stream.py @@ -3,13 +3,11 @@ import asyncio class Stream(IStream): - def __init__(self, peer_id): + def __init__(self, peer_id, maddr): self.peer_id = peer_id - peer_store = context.peer_store - peer_addr = peer_store.get(peer_id) - ip = peer_addr.get_protocol("ip4") - port = peer_addr.get_protocol("tcp") + ip = maddr.get_protocol_value("ip4") + port = maddr.get_protocol_value("tcp") # look up peer_id -> multiaddr in peer store # parse multiaddr and set_protocol based on it diff --git a/network/stream_interface.py b/network/stream_interface.py index 4649e73..b6ab342 100644 --- a/network/stream_interface.py +++ b/network/stream_interface.py @@ -2,8 +2,9 @@ from abc import ABC, abstractmethod class IStream(ABC): - def __init__(self, peer_id): + def __init__(self, peer_id, maddr): self.peer_id = peer_id + self.maddr = maddr @abstractmethod def protocol(self): From f32b0cc3ef1f3f47f03973911a6c4bd141d74190 Mon Sep 17 00:00:00 2001 From: Alex Haynes Date: Sun, 21 Oct 2018 14:49:20 -0400 Subject: [PATCH 4/5] removed comments --- network/stream.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/network/stream.py b/network/stream.py index 04b8bda..bc33f03 100644 --- a/network/stream.py +++ b/network/stream.py @@ -8,11 +8,7 @@ class Stream(IStream): ip = maddr.get_protocol_value("ip4") port = maddr.get_protocol_value("tcp") - - # look up peer_id -> multiaddr in peer store - # parse multiaddr and set_protocol based on it - # open connection to multiaddr - # save connection to stream's state + self.open_connection(ip, port) async def open_connection(self, ip, port): From 51921fe728f13fbab18c4a1c63b520f513bd0a06 Mon Sep 17 00:00:00 2001 From: Alex Haynes Date: Sun, 21 Oct 2018 15:11:27 -0400 Subject: [PATCH 5/5] fixed super and multi_addr variable name --- network/stream.py | 9 +++++---- network/stream_interface.py | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/network/stream.py b/network/stream.py index bc33f03..161a5a7 100644 --- a/network/stream.py +++ b/network/stream.py @@ -3,12 +3,13 @@ import asyncio class Stream(IStream): - def __init__(self, peer_id, maddr): + def __init__(self, peer_id, multi_addr): + IStream.__init__(self, peer_id, multi_addr) self.peer_id = peer_id - ip = maddr.get_protocol_value("ip4") - port = maddr.get_protocol_value("tcp") - + ip = multi_addr.get_protocol_value("ip4") + port = multi_addr.get_protocol_value("tcp") + self.open_connection(ip, port) async def open_connection(self, ip, port): diff --git a/network/stream_interface.py b/network/stream_interface.py index b6ab342..124bb45 100644 --- a/network/stream_interface.py +++ b/network/stream_interface.py @@ -2,9 +2,9 @@ from abc import ABC, abstractmethod class IStream(ABC): - def __init__(self, peer_id, maddr): + def __init__(self, peer_id, multi_addr): self.peer_id = peer_id - self.maddr = maddr + self.multi_addr = multi_addr @abstractmethod def protocol(self):