From 97ac3c49cc90ea27dae9541be8601a71677c2eb6 Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Fri, 17 Sep 2021 18:50:14 -0700 Subject: [PATCH] Refactor handle_connection method out of network manager (#1324) * Refactor handle_connection method out of network manager * Delete old commented methods --- squeaknode/network/network_manager.py | 35 ++------------------- squeaknode/network/peer_handler.py | 44 +++++++++++++++++++++------ 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/squeaknode/network/network_manager.py b/squeaknode/network/network_manager.py index e604ce6d..0154f156 100644 --- a/squeaknode/network/network_manager.py +++ b/squeaknode/network/network_manager.py @@ -28,7 +28,6 @@ import squeak.params from squeak.messages import MsgSerializable from squeaknode.core.peer_address import PeerAddress -from squeaknode.network.connection import Connection from squeaknode.network.connection_manager import ConnectionManager from squeaknode.network.peer import Peer from squeaknode.network.peer_client import PeerClient @@ -61,8 +60,9 @@ class NetworkManager(object): def start(self, squeak_controller): peer_handler = PeerHandler( + self.local_address, + self.connection_manager, squeak_controller, - self.handle_connection, ) self.peer_server = PeerServer( peer_handler, @@ -107,37 +107,6 @@ class NetworkManager(object): peer, )) - def handle_connection( - self, - squeak_controller, - peer_socket: socket.socket, - address: PeerAddress, - outgoing: bool, - ): - """Handles all sending and receiving of messages for the given peer. - - This method blocks until the peer connection has stopped. - """ - peer = Peer( - peer_socket, - self.local_address, - address, - outgoing, - self.connection_manager.single_peer_changed_listener, - ) - - logger.debug( - 'Setting up connection for peer address {} ...'.format(address)) - try: - with Connection(peer, squeak_controller).connect( - self.connection_manager - ) as connection: - connection.handle_connection() - finally: - peer.stop() - logger.debug( - 'Stopped connection for peer address {}.'.format(address)) - @property def local_address(self) -> PeerAddress: return PeerAddress( diff --git a/squeaknode/network/peer_handler.py b/squeaknode/network/peer_handler.py index e976b4c9..cead2be0 100644 --- a/squeaknode/network/peer_handler.py +++ b/squeaknode/network/peer_handler.py @@ -21,9 +21,10 @@ # SOFTWARE. import logging import socket -import threading from squeaknode.core.peer_address import PeerAddress +from squeaknode.network.connection import Connection +from squeaknode.network.peer import Peer logger = logging.getLogger(__name__) @@ -35,15 +36,40 @@ class PeerHandler(): def __init__( self, + local_address, + connection_manager, squeak_controller, - handle_connection_fn, ): - super().__init__() + self.local_address = local_address + self.connection_manager = connection_manager self.squeak_controller = squeak_controller - self.handle_connection_fn = handle_connection_fn - def handle_connection(self, peer_socket: socket.socket, address: PeerAddress, outgoing: bool): - threading.Thread( - target=self.handle_connection_fn, - args=(self.squeak_controller, peer_socket, address, outgoing,), - ).start() + def handle_connection( + self, + peer_socket: socket.socket, + address: PeerAddress, + outgoing: bool, + ): + """Handle a new socket connection. + + This method blocks until the socket connection has stopped. + """ + peer = Peer( + peer_socket, + self.local_address, + address, + outgoing, + self.connection_manager.single_peer_changed_listener, + ) + + logger.debug( + 'Setting up connection for peer address {} ...'.format(address)) + try: + with Connection(peer, self.squeak_controller).connect( + self.connection_manager + ) as connection: + connection.handle_connection() + finally: + peer.stop() + logger.debug( + 'Stopped connection for peer address {}.'.format(address))