Refactor handle_connection method out of network manager (#1324)

* Refactor handle_connection method out of network manager

* Delete old commented methods
This commit is contained in:
Jonathan Zernik 2021-09-17 18:50:14 -07:00 committed by GitHub
parent c6d70ad862
commit 97ac3c49cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 42 deletions

View file

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

View file

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