Beispiel #1
0
    def testBucketFull(self):
        kbucket = KBucket(2)

        kbucket.add(contact, 1)
        kbucket.add(contact2, 1)

        self.assertRaises(Exception, kbucket.add, contact3, 1)
Beispiel #2
0
    def testUpdateAContact(self):
        kbucket = KBucket(2)
        kbucket.add(contact, 1)
        kbucket.add(contact2, 1)

        kbucket.add(contact, 1)
        self.assertEqual(2, len(kbucket.getBucket(1)))
        self.assertEqual(contact, kbucket.getBucket(1)[1])
Beispiel #3
0
    def testUpdateAContact(self):
        kbucket = KBucket(2)
        kbucket.add(contact, 1)
        kbucket.add(contact2, 1)

        kbucket.add(contact, 1)
        self.assertEqual(2, len(kbucket.getBucket(1)))
        self.assertEqual(contact, kbucket.getBucket(1)[1])
Beispiel #4
0
    def testAppendNewContact(self):
        kbucket = KBucket(2)
        kbucket.add(contact, 1)
        kbucket.add(contact2, 1)

        self.assertEqual(contact, kbucket.getBucket(1)[0])
        self.assertEqual(contact2, kbucket.getBucket(1)[1])
Beispiel #5
0
    def testAppendNewContact(self):
        kbucket = KBucket(2)
        kbucket.add(contact, 1)
        kbucket.add(contact2, 1)

        self.assertEqual(contact, kbucket.getBucket(1)[0])
        self.assertEqual(contact2, kbucket.getBucket(1)[1])
Beispiel #6
0
    def testBucketFull(self):
        kbucket = KBucket(2)

        kbucket.add(contact, 1)
        kbucket.add(contact2, 1)

        self.assertRaises(Exception, kbucket.add, contact3, 1)
Beispiel #7
0
Datei: node.py Projekt: 0xNaN/dak
class Node:
    def __init__(self, k):
       self._id =  hashlib.sha256(str(random.randint(0, 2**32 - 1)).encode('utf-8')).hexdigest()[:20]
       self._kbucket = KBucket(k)

    def getid(self):
        return self._id

    def find_node(self, nodeid):
        distance = xor(self._id.encode('utf-8'), nodeid.encode('utf-8'))
        return self._kbucket.getkclose(distance)

    def addcontact(self, contact):
        contactId = contact[0]

        distance = xor(self._id.encode('utf-8'), contactId.encode('utf-8'))
        if(log2(distance).__round__() - 1  <= log2(distance)):
            index = log2(distance).__round__() - 1
        else:
            index = log2(distance).__round__()
        log("addding {} to bucket {}".format(contactId, index))
        self._kbucket.add(contact, index)
Beispiel #8
0
class Node:
    def __init__(self, k):
        self._id = hashlib.sha256(
            str(random.randint(0, 2**32 - 1)).encode('utf-8')).hexdigest()[:20]
        self._kbucket = KBucket(k)

    def getid(self):
        return self._id

    def find_node(self, nodeid):
        distance = xor(self._id.encode('utf-8'), nodeid.encode('utf-8'))
        return self._kbucket.getkclose(distance)

    def addcontact(self, contact):
        contactId = contact[0]

        distance = xor(self._id.encode('utf-8'), contactId.encode('utf-8'))
        if (log2(distance).__round__() - 1 <= log2(distance)):
            index = log2(distance).__round__() - 1
        else:
            index = log2(distance).__round__()
        log("addding {} to bucket {}".format(contactId, index))
        self._kbucket.add(contact, index)
Beispiel #9
0
 def __init__(self, k):
     self._id = hashlib.sha256(
         str(random.randint(0, 2**32 - 1)).encode('utf-8')).hexdigest()[:20]
     self._kbucket = KBucket(k)
Beispiel #10
0
    def testAddAContact(self):
        kbucket = KBucket(2)

        kbucket.add(contact, 1)

        self.assertEqual(contact, kbucket.getBucket(1)[0])
Beispiel #11
0
    def testGetKCloseContactsWhenBucketIsNotFull(self):
        kbucket = KBucket(2)
        kbucket.add(contact, 1)
        kbucket.add(contact2, 2)
        kbucket.add(contact3, 5)

        self.assertEqual([contact, contact2], kbucket.getkclose(1))
        self.assertEqual([contact3, contact2], kbucket.getkclose(5))

        kbucket = KBucket(4)
        kbucket.add("a", 1)
        kbucket.add("b", 1)

        kbucket.add("c", 2)
        kbucket.add("d", 6)
        kbucket.add("e", 6)
        kbucket.add("f", 6)
        kbucket.add("g", 6)

        self.assertEqual(["a", "b", "c", "d"], kbucket.getkclose(1))
        self.assertEqual(["d", "e", "f", "g"], kbucket.getkclose(6))
        self.assertEqual(["c", "a", "b", "d"], kbucket.getkclose(2))
Beispiel #12
0
    def testGetKCloseSimple(self):
        kbucket = KBucket(2)
        kbucket.add(contact, 1)
        kbucket.add(contact2, 1)

        self.assertEqual([contact, contact2], kbucket.getkclose(1))
Beispiel #13
0
Datei: node.py Projekt: 0xNaN/dak
 def __init__(self, k):
    self._id =  hashlib.sha256(str(random.randint(0, 2**32 - 1)).encode('utf-8')).hexdigest()[:20]
    self._kbucket = KBucket(k)
Beispiel #14
0
    def testAddAContact(self):
        kbucket = KBucket(2)

        kbucket.add(contact, 1);

        self.assertEqual(contact, kbucket.getBucket(1)[0])
Beispiel #15
0
    def testGetKCloseContactsWhenBucketIsNotFull(self):
        kbucket = KBucket(2)
        kbucket.add(contact, 1)
        kbucket.add(contact2, 2)
        kbucket.add(contact3, 5)

        self.assertEqual([contact, contact2], kbucket.getkclose(1))
        self.assertEqual([contact3, contact2], kbucket.getkclose(5))

        kbucket = KBucket(4)
        kbucket.add("a", 1)
        kbucket.add("b", 1)

        kbucket.add("c", 2)
        kbucket.add("d", 6)
        kbucket.add("e", 6)
        kbucket.add("f", 6)
        kbucket.add("g", 6)

        self.assertEqual(["a", "b", "c", "d"], kbucket.getkclose(1))
        self.assertEqual(["d", "e", "f", "g"], kbucket.getkclose(6))
        self.assertEqual(["c", "a", "b", "d"], kbucket.getkclose(2))
Beispiel #16
0
    def testGetKCloseSimple(self):
        kbucket = KBucket(2)
        kbucket.add(contact, 1)
        kbucket.add(contact2, 1)

        self.assertEqual([contact, contact2], kbucket.getkclose(1))