import random
import socket
import struct
import csv
import os, fnmatch

def createIp():
    return socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff)))

def createPort():
    port = random.randint(0, 49151)
    return str(port)

def createId(ip, port, bitsNumber):
    id = extractKBits(int(hash(ip + port)), bitsNumber)
    if id == 0:
        id = id + 1
    if id < 0 : 
        id *= -1
    return id

def extractKBits(num, k, p = 0):
    binary = bin(num) 
    # remove first two characters 
    binary = binary[2:] 
    end = len(binary) - p 
    start = end - k + 1
    # extract k  bit sub-string 
    kBitSubStr = binary[start : end+1] 
    # convert extracted sub-string into decimal again
    return int(kBitSubStr,2)

def writeEdgeCsv(nodeList):
    with open('edge.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        for i in nodeList:
            if i is not None:
                for n in i['node']._routingTable:
                    listNodeEdge = []
                    for k in n:
                        if k is not None:
                            listNodeEdge.append(i['id'])
                            listNodeEdge.append(k['id'])
                            node_writer.writerow(listNodeEdge)

def writeSnapshotCsv(nodeList, counter):
    with open('snapshot'+str(counter)+'.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        for i in nodeList:
            if i is not None:
                for n in i['node']._routingTable:
                    listNodeEdge = []
                    for k in n:
                        if k is not None:
                            listNodeEdge.append(i['id'])
                            listNodeEdge.append(k['id'])
                            node_writer.writerow(listNodeEdge)


def writeNodeCsv(nodeList):
    with open('node.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        new_list = [i["id"] for i in nodeList]
        node_writer.writerow(new_list)

def openCsv(name):
    with open(name, 'r') as f:
        reader = csv.reader(f)
        node = list(reader)
        return node

def findFile(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                result.append(os.path.join(root, name).replace('../kademlia/', ''))
    return result

def removeFile(list):
    for i in list:
        os.remove(i)
        print("File Removed!")
d08b62349aeda21542f63f4b6a92026dd98f1533
<mask token>


def createId(ip, port, bitsNumber):
    id = extractKBits(int(hash(ip + port)), bitsNumber)
    if id == 0:
        id = id + 1
    if id < 0:
        id *= -1
    return id


<mask token>


def writeEdgeCsv(nodeList):
    with open('edge.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"',
            quoting=csv.QUOTE_MINIMAL)
        for i in nodeList:
            if i is not None:
                for n in i['node']._routingTable:
                    listNodeEdge = []
                    for k in n:
                        if k is not None:
                            listNodeEdge.append(i['id'])
                            listNodeEdge.append(k['id'])
                            node_writer.writerow(listNodeEdge)


<mask token>


def findFile(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                result.append(os.path.join(root, name).replace(
                    '../kademlia/', ''))
    return result


def removeFile(list):
    for i in list:
        os.remove(i)
        print('File Removed!')

<mask token>


def createPort():
    port = random.randint(0, 49151)
    return str(port)


def createId(ip, port, bitsNumber):
    id = extractKBits(int(hash(ip + port)), bitsNumber)
    if id == 0:
        id = id + 1
    if id < 0:
        id *= -1
    return id


<mask token>


def writeEdgeCsv(nodeList):
    with open('edge.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"',
            quoting=csv.QUOTE_MINIMAL)
        for i in nodeList:
            if i is not None:
                for n in i['node']._routingTable:
                    listNodeEdge = []
                    for k in n:
                        if k is not None:
                            listNodeEdge.append(i['id'])
                            listNodeEdge.append(k['id'])
                            node_writer.writerow(listNodeEdge)


def writeSnapshotCsv(nodeList, counter):
    with open('snapshot' + str(counter) + '.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"',
            quoting=csv.QUOTE_MINIMAL)
        for i in nodeList:
            if i is not None:
                for n in i['node']._routingTable:
                    listNodeEdge = []
                    for k in n:
                        if k is not None:
                            listNodeEdge.append(i['id'])
                            listNodeEdge.append(k['id'])
                            node_writer.writerow(listNodeEdge)


<mask token>


def openCsv(name):
    with open(name, 'r') as f:
        reader = csv.reader(f)
        node = list(reader)
        return node


def findFile(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                result.append(os.path.join(root, name).replace(
                    '../kademlia/', ''))
    return result


def removeFile(list):
    for i in list:
        os.remove(i)
        print('File Removed!')

<mask token>


def createIp():
    return socket.inet_ntoa(struct.pack('>I', random.randint(1, 4294967295)))


def createPort():
    port = random.randint(0, 49151)
    return str(port)


def createId(ip, port, bitsNumber):
    id = extractKBits(int(hash(ip + port)), bitsNumber)
    if id == 0:
        id = id + 1
    if id < 0:
        id *= -1
    return id


<mask token>


def writeEdgeCsv(nodeList):
    with open('edge.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"',
            quoting=csv.QUOTE_MINIMAL)
        for i in nodeList:
            if i is not None:
                for n in i['node']._routingTable:
                    listNodeEdge = []
                    for k in n:
                        if k is not None:
                            listNodeEdge.append(i['id'])
                            listNodeEdge.append(k['id'])
                            node_writer.writerow(listNodeEdge)


def writeSnapshotCsv(nodeList, counter):
    with open('snapshot' + str(counter) + '.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"',
            quoting=csv.QUOTE_MINIMAL)
        for i in nodeList:
            if i is not None:
                for n in i['node']._routingTable:
                    listNodeEdge = []
                    for k in n:
                        if k is not None:
                            listNodeEdge.append(i['id'])
                            listNodeEdge.append(k['id'])
                            node_writer.writerow(listNodeEdge)


<mask token>


def openCsv(name):
    with open(name, 'r') as f:
        reader = csv.reader(f)
        node = list(reader)
        return node


def findFile(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                result.append(os.path.join(root, name).replace(
                    '../kademlia/', ''))
    return result


def removeFile(list):
    for i in list:
        os.remove(i)
        print('File Removed!')

<mask token>


def createIp():
    return socket.inet_ntoa(struct.pack('>I', random.randint(1, 4294967295)))


def createPort():
    port = random.randint(0, 49151)
    return str(port)


def createId(ip, port, bitsNumber):
    id = extractKBits(int(hash(ip + port)), bitsNumber)
    if id == 0:
        id = id + 1
    if id < 0:
        id *= -1
    return id


<mask token>


def writeEdgeCsv(nodeList):
    with open('edge.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"',
            quoting=csv.QUOTE_MINIMAL)
        for i in nodeList:
            if i is not None:
                for n in i['node']._routingTable:
                    listNodeEdge = []
                    for k in n:
                        if k is not None:
                            listNodeEdge.append(i['id'])
                            listNodeEdge.append(k['id'])
                            node_writer.writerow(listNodeEdge)


def writeSnapshotCsv(nodeList, counter):
    with open('snapshot' + str(counter) + '.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"',
            quoting=csv.QUOTE_MINIMAL)
        for i in nodeList:
            if i is not None:
                for n in i['node']._routingTable:
                    listNodeEdge = []
                    for k in n:
                        if k is not None:
                            listNodeEdge.append(i['id'])
                            listNodeEdge.append(k['id'])
                            node_writer.writerow(listNodeEdge)


def writeNodeCsv(nodeList):
    with open('node.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"',
            quoting=csv.QUOTE_MINIMAL)
        new_list = [i['id'] for i in nodeList]
        node_writer.writerow(new_list)


def openCsv(name):
    with open(name, 'r') as f:
        reader = csv.reader(f)
        node = list(reader)
        return node


def findFile(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                result.append(os.path.join(root, name).replace(
                    '../kademlia/', ''))
    return result


def removeFile(list):
    for i in list:
        os.remove(i)
        print('File Removed!')

import random
import socket
import struct
import csv
import os, fnmatch

def createIp():
    return socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff)))

def createPort():
    port = random.randint(0, 49151)
    return str(port)

def createId(ip, port, bitsNumber):
    id = extractKBits(int(hash(ip + port)), bitsNumber)
    if id == 0:
        id = id + 1
    if id < 0 : 
        id *= -1
    return id

def extractKBits(num, k, p = 0):
    binary = bin(num) 
    # remove first two characters 
    binary = binary[2:] 
    end = len(binary) - p 
    start = end - k + 1
    # extract k  bit sub-string 
    kBitSubStr = binary[start : end+1] 
    # convert extracted sub-string into decimal again
    return int(kBitSubStr,2)

def writeEdgeCsv(nodeList):
    with open('edge.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        for i in nodeList:
            if i is not None:
                for n in i['node']._routingTable:
                    listNodeEdge = []
                    for k in n:
                        if k is not None:
                            listNodeEdge.append(i['id'])
                            listNodeEdge.append(k['id'])
                            node_writer.writerow(listNodeEdge)

def writeSnapshotCsv(nodeList, counter):
    with open('snapshot'+str(counter)+'.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        for i in nodeList:
            if i is not None:
                for n in i['node']._routingTable:
                    listNodeEdge = []
                    for k in n:
                        if k is not None:
                            listNodeEdge.append(i['id'])
                            listNodeEdge.append(k['id'])
                            node_writer.writerow(listNodeEdge)


def writeNodeCsv(nodeList):
    with open('node.csv', mode='w') as node_file:
        node_writer = csv.writer(node_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        new_list = [i["id"] for i in nodeList]
        node_writer.writerow(new_list)

def openCsv(name):
    with open(name, 'r') as f:
        reader = csv.reader(f)
        node = list(reader)
        return node

def findFile(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                result.append(os.path.join(root, name).replace('../kademlia/', ''))
    return result

def removeFile(list):
    for i in list:
        os.remove(i)
        print("File Removed!")