Exemple #1
0
    def test_nth_bit(self):
        # ‭10011000011101100101010000110010‬...
        node_id = NodeId(0x9876543200000000000000000000000000000000)

        self.assertEqual(node_id.nth_bit(0), 1)
        self.assertEqual(node_id.nth_bit(1), 0)
        self.assertEqual(node_id.nth_bit(2), 0)
        self.assertEqual(node_id.nth_bit(3), 1)
        self.assertEqual(node_id.nth_bit(4), 1)
        self.assertEqual(node_id.nth_bit(5), 0)
Exemple #2
0
    def test_first_insert(self):
        """
        Should split root node when only first bucket is present.
        """
        owner_id = NodeId(0xffffffffffffffffffffffffffffffffffffffff)
        table = RoutingTable(owner_id)

        table.insert('127.0.0.1', '9001', NodeId(0b0010))
        table.insert('127.0.0.1', '9002', NodeId(0b0100))
        print("Found: %s" % table.find(owner_id))
        self.assertIsNotNone(
            table.find(owner_id),
            "routing table find operation did not return anything")
        self.assertEqual('9001', table.find(NodeId(0b0010)).port)
        self.assertEqual('9002', table.find(NodeId(0b0100)).port)
Exemple #3
0
    def __init__(self, address, port, node_id=None, bootstrap=[]):
        """
        Creates a new peer, with the given settings.

        Does not bind a socket yet. Call `serve_forever` to bind and listen.

        >>> PeerServer('', '9000').serve_forever()

        :param address: IP address to bind to
        :param port: Port to bind to
        :param node_id: Optional Node identifier, if this
                   peer already has a persisted identity.
        :param bootstrap: A list of (ip, port) tuples used
                          as an entry point into the network.
        """
        super().__init__('%s:%s' % (address, port))

        self._id = NodeId.generate() if node_id is None else node_id
        self._bootstrap = list(bootstrap)
        print('Starting Peer %s' % repr(self._id))
Exemple #4
0
    def test_sort(self):
        """
        Should sort contacts by last seen datetime.
        """
        # assume
        bucket = KBucket()
        bucket.add(Contact(None, None, NodeId(7)))
        bucket.add(Contact(None, None, NodeId(3)))
        bucket.add(Contact(None, None, NodeId(1)))

        bucket.get(NodeId(1)).last_seen = datetime(2019, 9, 2)
        bucket.get(NodeId(7)).last_seen = datetime(2019, 9, 3)
        bucket.get(NodeId(3)).last_seen = datetime(2019, 10, 1)

        # act
        bucket.sort()

        # assert
        self.assertEqual(
            NodeId(3),
            bucket._contacts[-1].node_id,
        )
Exemple #5
0
import gevent

from dht.peer import PeerServer
from dht.route import NodeId


if __name__ == '__main__':
    print('Starting up')

    id = NodeId.generate()

    # Stop gevent from outputting exception
    # details on keyboard interrupt.
    gevent.get_hub().NOT_ERROR += (KeyboardInterrupt,)

    peer = PeerServer('', '9000')
    cancel = gevent.event.Event()

    try:
        peer.start()
        print('Bootstrap here')
        cancel.wait()
    except KeyboardInterrupt:
        print('User requested stop')
    finally:
        print('Stopping')
        peer.stop(timeout=1)

        # Currently this doesn't do anything, as the
        # cancel event is just used to stop main() execution.
Exemple #6
0
    def test_has_prefix(self):
        node_id = NodeId(0xf550000000000000000000000000000000000000)

        self.assertTrue(node_id.has_prefix(0xf5))
        self.assertFalse(node_id.has_prefix(0xaa))
Exemple #7
0
    def test_xor(self):
        id_1 = NodeId(0x01)
        id_2 = NodeId(0x02)

        self.assertEqual(NodeId(0x03), id_1 ^ id_2)
Exemple #8
0
    def test_contains(self):
        bucket = KBucket()
        bucket.add(Contact('127.0.0.1', 9090, NodeId(0x1000)))

        self.assertTrue(bucket.contains(NodeId(0x1000)))