mirror of
https://github.com/yzernik/squeaknode.git
synced 2026-08-16 13:01:04 +02:00
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:
parent
c6d70ad862
commit
97ac3c49cc
2 changed files with 37 additions and 42 deletions
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue