Skip to content

Commit 7b48ddf

Browse files
committed
start isn't user facing, update bools to reflect
1 parent fa770e3 commit 7b48ddf

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/chordnet/node.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,25 @@ class Node:
3636
_net: _Net
3737
_timer: threading.Timer | None
3838
is_running: bool
39-
40-
def __init__(self, ip: str, port: int, daemon:bool=True) -> None:
39+
_use_daemon: bool
40+
_interval: float
41+
_debug: bool
42+
43+
def __init__(self,
44+
ip: str,
45+
port: int,
46+
daemon:bool=True,
47+
interval:float=1.0,
48+
debug:bool=False
49+
) -> None:
4150
"""Initializes a new Chord node.
4251
4352
Args:
4453
ip: IP address for the node.
4554
port: Port number to listen on.
4655
daemon: whether to run the daemon.
56+
interval: daemon interval.
57+
debug: whether to print node state after every daemon run.
4758
"""
4859
self.address = Address(ip, port)
4960

@@ -56,6 +67,10 @@ def __init__(self, ip: str, port: int, daemon:bool=True) -> None:
5667
self._net = _Net(ip, port, self._process_request)
5768
self.is_running = False
5869

70+
self._use_daemon = daemon
71+
self._interval = interval
72+
self._debug = debug
73+
5974
def successor(self) -> Address | None:
6075
"""Alias for self.finger_table[0]."""
6176
return self.finger_table[0]
@@ -135,21 +150,21 @@ def fix_fingers(self) -> None:
135150
# Move to the next finger table entry, wrapping around if necessary
136151
self._next = (self._next + 1) % Address._M
137152

138-
def _daemon(self, interval:float = 1.0, debug:bool = True) -> None:
153+
def _daemon(self) -> None:
139154
"""Runs fix_fingers and stabilize periodically.
140155
141156
Args:
142157
interval: Time interval between periodic calls.
143158
debug: Whether to print node state
144159
"""
145-
if self.is_running:
160+
if self._use_daemon and self.is_running:
146161
self.stabilize()
147162
self.fix_fingers()
148-
if debug:
163+
if self._debug:
149164
print(f"pred: {self.predecessor}, succ: {self.successor()}")
150165
print(self.finger_table)
151166

152-
self._timer = threading.Timer(interval, self._daemon)
167+
self._timer = threading.Timer(self._interval, self._daemon)
153168
self._timer.daemon = True
154169
self._timer.start()
155170

@@ -324,9 +339,7 @@ def notify(self, potential_successor: Address | None)-> bool:
324339
return False
325340

326341

327-
def start(
328-
self, daemon: bool = True, interval: float = 1.0, debug:bool = False
329-
) -> None:
342+
def start(self) -> None:
330343
"""Starts the Chord node's daemon and network listener.
331344
332345
Begins accepting incoming network connections in a separate thread.
@@ -340,8 +353,8 @@ def start(
340353
"""
341354
self._net.start()
342355
self.is_running = True
343-
if daemon:
344-
self._daemon(interval, debug)
356+
if self._use_daemon:
357+
self._daemon()
345358

346359

347360

0 commit comments

Comments
 (0)