1+ """net.py: Class for handling networking operations for nodes."""
12import socket
23import sys
34import threading
5+ from typing import Callable , Tuple
6+
7+ from .address import Address
8+
9+ callback = Callable [[str , list [str ]], str | Address | None ]
410
511class _Net :
612
7- def __init__ (self , ip , port , request_handler ):
13+ _ip : str
14+ _port : int
15+ _request_handler : callback
16+ _running : bool
17+ _network_thread : threading .Thread | None
18+ server_socket : socket .socket | None
19+ network_thread : threading .Thread | None
20+
21+ def __init__ (self , ip : str , port : int , request_handler : callback ) -> None :
822 self ._ip = ip
923 self ._port = port
1024 self ._request_handler = request_handler
@@ -13,9 +27,8 @@ def __init__(self, ip, port, request_handler):
1327 self .server_socket = None
1428 self .network_thread = None
1529
16- def start (self ):
17- """
18- Starts the Chord node's network listener.
30+ def start (self ) -> None :
31+ """Starts the Chord node's network listener.
1932
2033 Begins accepting incoming network connections in a separate thread.
2134 """
@@ -33,9 +46,8 @@ def start(self):
3346
3447
3548
36- def stop (self ):
37- """
38- Gracefully stops the Chord node's network listener.
49+ def stop (self ) -> None :
50+ """Gracefully stops the Chord node's network listener.
3951
4052 Closes the server socket and waits for the network thread to terminate.
4153 """
@@ -47,9 +59,10 @@ def stop(self):
4759
4860
4961
50- def send_request (self , dest_node , method , * args ):
51- """
52- Sends a network request to a specific node.
62+ def send_request (
63+ self , dest_node : Address , method : str , * args : object
64+ ) -> str | None :
65+ """Sends a network request to a specific node.
5366
5467 Args:
5568 dest_node (Address): The network address to send the request to
@@ -78,7 +91,7 @@ def send_request(self, dest_node, method, *args):
7891 sock .send (request .encode ())
7992
8093 # Receive the response
81- response = sock .recv (1024 ).decode ()
94+ response : str = sock .recv (1024 ).decode ()
8295
8396 return response
8497
@@ -95,15 +108,19 @@ def send_request(self, dest_node, method, *args):
95108
96109
97110
98- def _listen_for_connections (self ):
99- """
100- Continuously listens for incoming network connections.
111+ def _listen_for_connections (self ) -> None :
112+ """Continuously listens for incoming network connections.
101113
102- Accepts client connections and spawns a thread to handle each connection.
114+ Accepts client connections and spawns a thread to handle
115+ each connection.
103116 """
104117 while self ._running :
105118 try :
106- client_socket , address = self .server_socket .accept ()
119+ client_socket : socket .socket | None = None
120+ address : Tuple [str , int ] | None = None
121+
122+ if self .server_socket :
123+ client_socket , address = self .server_socket .accept ()
107124 # Handle each connection in a separate thread
108125 threading .Thread (
109126 target = self ._handle_connection ,
@@ -117,16 +134,15 @@ def _listen_for_connections(self):
117134
118135
119136
120- def _handle_connection (self , client_socket ):
121- """
122- Processes an individual network connection.
137+ def _handle_connection (self , client_socket : socket .socket ) -> None :
138+ """Processes an individual network connection.
123139
124140 Args:
125141 client_socket (socket): The socket connection to handle.
126142 """
127143 try :
128144 # Receive request
129- request = client_socket .recv (1024 ).decode ()
145+ request : str = client_socket .recv (1024 ).decode ()
130146
131147 # Parse request
132148 method , * args = request .split (':' )
0 commit comments