def setFingertable(self, node_list): for i in node_list: n = NodeID() n.id = i.id n.ip = i.ip n.port = i.port self.finger_table.append(n)
def findSucc(self, key): qnode = NodeID() qnode.id = self.sha256ID qnode.ip = self.ip qnode.port = self.port nNode = NodeID() nNode = self.findPred(key) #print('predNode: ', nNode.id) if qnode.id == key: return qnode else: if nNode.id == qnode.id: return self.getNodeSucc() else: ttransport = TSocket.TSocket(nNode.ip, nNode.port) ttransport.open() pprotocol = TBinaryProtocol.TBinaryProtocol(ttransport) client = FileStore.Client(pprotocol) cNode = NodeID() cNode = client.getNodeSucc() #ttransport.close() return cNode
def findPred(self, key): if not len(self.finger_table): raise SystemException("Find Pred Error - Finger table is missing") if self.NodeID >= self.finger_table[0].id: if not (key <= self.NodeID and key > self.finger_table[0].id): n = NodeID() n.id = self.NodeID n.ip = self.ip n.port = int(self.port_num) return n else: if key > self.NodeID and key <= self.finger_table[0].id: n = NodeID() n.id = self.NodeID n.ip = self.ip n.port = int(self.port_num) return n if self.NodeID >= key: for i in reversed(self.finger_table): if not (i.id <= self.NodeID and i.id > key): t = TSocket.TSocket(i.ip, i.port) t = TTransport.TBufferedTransport(t) p = TBinaryProtocol.TBinaryProtocol(t) c = FileStore.Client(p) t.open() k = c.findPred(key) t.close() return k raise SystemException("Find Pred Error - Cannot find pred 1") else: for i in reversed(self.finger_table): if i.id > self.NodeID and i.id <= key: t = TSocket.TSocket(i.ip, i.port) t = TTransport.TBufferedTransport(t) p = TBinaryProtocol.TBinaryProtocol(t) c = FileStore.Client(p) t.open() k = c.findPred(key) t.close() return k raise SystemException("Find Pred Error - Cannot find pred 2")
def _createNodeId(self, server_port): # Create new object for NodeId new_node_id = NodeID() new_node_id.ip = str(socket.gethostbyname( socket.gethostname())) # Set Node IP Address as string new_node_id.port = server_port # Set Node Port Number as integer # Set Node's ID as SHA256 hash value using "<Node_IP>:<Node_Port>" as string encoded_address = (new_node_id.ip + COLON_STRING + str(new_node_id.port)).encode(ENCODE_TYPE_UTF_8) new_node_id.id = hashlib.sha256(encoded_address).hexdigest() return new_node_id
def findPred(self, key): cNode = NodeID() sha256id = hashlib.sha256(self.id.encode()) cNode.id = sha256id.hexdigest() cNode.ip = self.ip cNode.port = self.port tempNode = NodeID() if not self.fTable: z = SystemException() z.message = "no fTable" raise z tempNode = self.fTable[0] x = self.checkInRange(key, cNode.id, tempNode.id) #print('x:', x) while not x: for i in range(len(self.fTable) - 1, 0, -1): temp = NodeID() temp = self.fTable[i] y = self.checkInRange(temp.id, cNode.id, key) #print('temp.id: ', temp.id, '\n', 'cNode.id:', cNode.id, '\n', 'key:', key ) #print('y',y) if y: ttransport = TSocket.TSocket(temp.ip, temp.port) #ttransport = TTransport.TBufferedTransport(ttransport) ttransport.open() pprotocol = TBinaryProtocol.TBinaryProtocol(ttransport) client = FileStore.Client(pprotocol) #ttransport.open() sNode = NodeID() sNode = client.findPred(key) ttransport.close() return sNode break return cNode
def createNode(id1, ip, port): node = NodeID() node.id = id1 node.ip = ip node.port = port return node
def addNode(self, host, port): new_node_key = hashlib.sha256(host + ":" + str(port)).hexdigest() new_node = NodeID() new_node.ip = host new_node.port = port new_node.id = new_node_key ## Make its fingertable (current node to find all answers) new_fingertable = [] for i in range(256): finger_key = \ hex((int(new_node_key, 16) + (2**i)) % (2**256)).strip("0x").strip('L') succ = self.findSucc(finger_key) if(self.contains(new_node_key, finger_key, succ.id)): new_fingertable.append(new_node) else: new_fingertable.append(self.findSucc(finger_key)) ## Launch new server Popen(["python2", "server.py"] + [str(port)]) sleep(3) ## Update other nodes fingertables transport = TSocket.TSocket(host, port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) transport.open() client.setFingertable(new_fingertable) transport.close() new_node_succ = self.findSucc(new_node_key) new_node_pred = self.findPred(new_node_key) succ_files = [] if(new_node_succ.id == self.myNode.id): succ_files = self.getFiles() else: ## Update other nodes fingertables transport = TSocket.TSocket(new_node_succ.ip, new_node_succ.port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) transport.open() succ_files = client.getFiles() transport.close() # New node must take ownership of some files from successor, # while successor must relinquish ownership of some for file_obj in succ_files: if(self.contains(file_obj.meta.contentHash,\ new_node_pred.id,\ new_node_key)): ## File belongs to new node if(new_node_succ.id == self.myNode.id): self.removeFile(file_obj.meta.contentHash) else: ## Update other nodes fingertables transport = TSocket.TSocket(new_node_succ.ip, new_node_succ.port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) transport.open() client.removeFile(file_obj.meta.contentHash) ## Update other nodes fingertables transport = TSocket.TSocket(host, port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) transport.open() client.addFile(file_obj)
def __del__(self): # Remove directory try: shutil.rmtree(self.serverDir) except: print('Failed to remove %s' % self.serverDir) print('%i: Node at %s:%i leaving' % (nodeID.port, nodeID.ip, nodeID.port)) if __name__ == '__main__': port = int(sys.argv[1]) ip = socket.gethostbyname(socket.gethostname()) nodeID = NodeID() nodeID.id = getID(ip, port) nodeID.ip = ip nodeID.port = port handler = FileStoreHandler() processor = FileStore.Processor(handler) transport = TSocket.TServerSocket(port=port) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) # You could do one of these for a multithreaded server # server = TServer.TThreadedServer( # processor, transport, tfactory, pfactory) # server = TServer.TThreadPoolServer( # processor, transport, tfactory, pfactory)
def node_instance(ip, port, id): n = NodeID() n.ip = ip n.port = int(port) n.id = id return n