Skip to content

Commit 6d8e756

Browse files
committed
hide internals behind a public api
1 parent a8e2566 commit 6d8e756

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

src/chordnet/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
"""init.py: defines importable classes."""
2-
from .address import Address
3-
from .net import _Net
4-
from .node import Node
2+
from .chordnet import ChordNet
53

6-
__all__=['Node', 'Address', '_Net']
4+
__all__=['ChordNet']
File renamed without changes.

src/chordnet/node.py renamed to src/chordnet/_node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
from loguru import logger as log
66

7+
from ._net import _Net
78
from .address import Address
8-
from .net import _Net
99

1010
callback_t = Callable[[str, list[str]], str | Address | None]
11-
class Node:
11+
class _Node:
1212
"""Implements a Chord distributed hash table node.
1313
1414
This is meant to run on a host and handle any chord-related

src/chordnet/chordnet.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""chordnet.py: chordnet api."""
2+
from ._node import _Node
3+
4+
5+
class ChordNet:
6+
"""Interface for interacting with Chord networks."""
7+
8+
_node: _Node
9+
10+
def __init__(self,
11+
ip: str,
12+
port: int,
13+
interval:float=1.0,
14+
) -> None:
15+
"""Initializes a new Chord node.
16+
17+
Args:
18+
ip: IP address for the node.
19+
port: Port number to listen on.
20+
daemon: whether to run the daemon.
21+
interval: daemon interval.
22+
debug: whether to print node state after every daemon run.
23+
"""
24+
self._node = _Node(ip, port, interval=interval)
25+
26+
27+
def create(self) -> None:
28+
"""Create a new ChordNet network (a new "ring").
29+
30+
This creates a new network with one node (this one).
31+
"""
32+
self._node.create()
33+
34+
def join(self, known_ip: str, known_port: int) -> None:
35+
"""Joins an existing ChordNet network (an existing ring).
36+
37+
An existing chordnet can be joined through any node already on the ring.
38+
39+
Args:
40+
known_ip (str): IP address of an existing node in the Chord ring.
41+
known_port (int): Port number of the existing node.
42+
"""
43+
self._node.join(known_ip, known_port)
44+
45+
def leave(self) -> None:
46+
"""Leave the current network.
47+
48+
Allows for a graceful shutdown of this node. Should be called before
49+
program exit, but the network can recover if this does not happen.
50+
"""
51+
pass

tests/test_net.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
import pytest
1212

13-
from chordnet import (
14-
_Net,
15-
)
13+
from chordnet._net import _Net
1614

1715

1816
def test_net_initialization() -> None:

tests/test_node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import pytest
1111

12-
from chordnet import Address
13-
from chordnet import Node as ChordNode
12+
from chordnet._node import _Node as ChordNode
13+
from chordnet.address import Address
1414

1515
# Global test variables
1616
ip: str = "1.2.3.4"

0 commit comments

Comments
 (0)