示例#1
0
文件: testKbucket.py 项目: 0xNaN/dak
    def testBucketFull(self):
        kbucket = KBucket(2)

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

        self.assertRaises(Exception, kbucket.add, contact3, 1)
示例#2
0
文件: testKbucket.py 项目: zhanji/dak
    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])
示例#3
0
文件: testKbucket.py 项目: 0xNaN/dak
    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])
示例#4
0
文件: testKbucket.py 项目: zhanji/dak
    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])
示例#5
0
文件: testKbucket.py 项目: 0xNaN/dak
    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])
示例#6
0
文件: testKbucket.py 项目: zhanji/dak
    def testBucketFull(self):
        kbucket = KBucket(2)

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

        self.assertRaises(Exception, kbucket.add, contact3, 1)
示例#7
0
文件: node.py 项目: 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)
示例#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)
示例#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)
示例#10
0
文件: testKbucket.py 项目: zhanji/dak
    def testAddAContact(self):
        kbucket = KBucket(2)

        kbucket.add(contact, 1)

        self.assertEqual(contact, kbucket.getBucket(1)[0])
示例#11
0
文件: testKbucket.py 项目: zhanji/dak
    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))
示例#12
0
文件: testKbucket.py 项目: zhanji/dak
    def testGetKCloseSimple(self):
        kbucket = KBucket(2)
        kbucket.add(contact, 1)
        kbucket.add(contact2, 1)

        self.assertEqual([contact, contact2], kbucket.getkclose(1))
示例#13
0
文件: node.py 项目: 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)
示例#14
0
文件: testKbucket.py 项目: 0xNaN/dak
    def testAddAContact(self):
        kbucket = KBucket(2)

        kbucket.add(contact, 1);

        self.assertEqual(contact, kbucket.getBucket(1)[0])
示例#15
0
文件: testKbucket.py 项目: 0xNaN/dak
    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))
示例#16
0
文件: testKbucket.py 项目: 0xNaN/dak
    def testGetKCloseSimple(self):
        kbucket = KBucket(2)
        kbucket.add(contact, 1)
        kbucket.add(contact2, 1)

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