def __init__(self, portNumber): self.portNumber = portNumber self.keyValueMap = dict() self.size = 0 self.identity = socket.gethostname().rstrip(".local") self.identifier = int("0x"+hashlib.sha1(self.identity).hexdigest(),0) % pow(2,mEntries) # initalize fingertable self.fingerTable = FingerTable(mEntries)
class Node(NodeCommunication, NodeOperations): def __init__(self, portNumber): self.portNumber = portNumber self.keyValueMap = dict() self.size = 0 self.identity = socket.gethostname().rstrip(".local") self.identifier = int("0x"+hashlib.sha1(self.identity).hexdigest(),0) % pow(2,mEntries) # initalize fingertable self.fingerTable = FingerTable(mEntries) def join(self, predecessor): # Join the system. Find my place # corner case for first node if predecessor == socket.gethostname().rstrip(".local"): for i in range(0,mEntries): self.fingerTable.new_entry(i, self.identifier, self.identity) return preIdentifier = int("0x"+hashlib.sha1(predecessor).hexdigest(),0) % pow(2,mEntries) identifier = self.identifier if preIdentifier > identifier: identifier += pow(2, mEntries)+1 # find my place in the system successor = self.request_successor(predecessor, self.portNumber, str(identifier)) # Calculate first finger and join the system self.fingerTable.new_entry(0,self.identifier, successor) # Update the predecessor self.update_predecessor(self.fingerTable.get_succ_identity(), self.portNumber, self.fingerTable.get_succ_identity(), self.identity) # Calculate the rest of the fingers self.find_fingers(self.identifier, self.portNumber) # send message to all others nodes, that they should update their finger table self.update_others_finger_table(self.fingerTable.get_succ_identity(), self.portNumber, self.fingerTable.get_succ_identity(), self.identity) # Calculate and find the fingers for the finger table def find_fingers(self, identifier, portNumber): for i in range(2, mEntries + 1): value = int((identifier + pow(2,i-1)) % pow(2,mEntries)) if value < identifier: value += pow(2,mEntries)+1 successor = self.check_corner_cases(value) if successor is None: successor = self.request_successor(self.fingerTable.get_succ_identity(), portNumber, str(value)) self.fingerTable.new_entry(i-1, identifier, successor) # Function to check corner cases, very important when we pass the zero identifier # might be easier with threaded connection def check_corner_cases(self, identifier): # only one node if self.fingerTable.get_succ_identifier() == self.identifier: identity = self.fingerTable.get_succ_identity() return identity if self.identifier > self.fingerTable.get_succ_identifier(): if identifier > pow(2,mEntries): # well.... identifier -= (pow(2,mEntries)+1) else: if self.identifier >= identifier: return self.identity if self.identifier > identifier: if self.fingerTable.get_succ_identifier() > identifier: return self.fingerTable.get_succ_identity() else: return self.request_successor(self.fingerTable.get_succ_identity(), self.portNumber, str(identifier)) else: return self.fingerTable.get_succ_identity() return None