def FindNode(self, request, context): print("Serving FindNode({}) request for {}".format( request.idkey, request.node.id)) k_closest_nodes = self.find_k_closest_nodes(request.idkey) # Update the k_buckets self.update_k_buckets(request.node, True) return csci4220_hw3_pb2.NodeList(responding_node=self.node, nodes=k_closest_nodes)
def FindNode(self, IDKey, context): id = IDKey.idkey print("Serving FindNode(%d) request for %d" % (id, IDKey.node.id)) closest = [] for i in range(4): closest += k_buckets[i] closest = sorted(closest, key=lambda x: id ^ x.id)[:k] AddorUpdateNode(IDKey.node) return csci4220_hw3_pb2.NodeList(responding_node=local_node, nodes=closest)
def FindNode(self, request, context): node = request.node id_key = request.idkey print("Serving FindNode({}) request for {}".format(id_key, node.id)) closest_nodes = find_k_closest(id_key) save_node(node) return csci4220_hw3_pb2.NodeList(responding_node=csci4220_hw3_pb2.Node( id=local_id, port=int(my_port), address=my_address), nodes=closest_nodes)
def FindNode(self, request, context): print("Serving FindNode(%d) request for %d" % (request.idkey, request.node.id)) #update key buckets: if self.id != request.node.id: i = self.compute_dist(self.id, request.node.id) - 1 self.k_bucket[i][request.node.id] = request.node k_nodes = self.find_nearest_k(request.idkey, self.k, [request.node.id, self.id]) cur_node = csci4220_hw3_pb2.Node(id=self.id, port=int(self.port), address=self.address) nl = csci4220_hw3_pb2.NodeList(responding_node=cur_node, nodes=k_nodes) return nl
def FindNode(self, request, context): """Takes an ID (use shared IDKey message type) and returns k nodes with distance closest to ID requested """ # request is IDKey # We can ignore context print(f'Serving FindNode({request.idkey}) request for {request.node.id}', flush=True) # closestNodes = self.DHT[round(log(distance))] # At most k nodes in this kClosest = self.DHT.kClosest(request.idkey) # update the k-bucket of the requestor i.e. request.node.id self.DHT.updateNodeLoc(request.node) # x = csci4220_hw3_pb2.NodeList(responding_node = self.DHT.ourNode) # x.nodes.extend(kClosest) # return x return csci4220_hw3_pb2.NodeList(responding_node = self.DHT.ourNode, nodes = kClosest)
def FindNode(self, request, context): global bucket # Findnode function for bootstrap if request.idkey==request.node.id: index = locateBucket(local_node,request.node.id) if index!=-1: if index not in bucket: tmp = [] tmp.append(nodeObj(request.node.id,request.node.port,request.node.address)) bucket[index] = tmp else: adding = True for k,v in bucket.items(): for i in range(0,len(v)): if v[i].id==request.node.id: adding = False if adding==True: n = len(bucket[index]) tmp = bucket[index] if n==bucket_max: tmp.pop() tmp.insert(0,nodeObj(request.node.id,request.node.port,request.node.address)) bucket[index] = tmp # add all nodes to a list and sort all_nodes = [] for k,v in bucket.items(): for j in range(len(v)): if v[j].id!=request.node.id: all_nodes.append(v[j]) ordered_nodes = sorted(all_nodes, key=lambda x : XOR(x.id,request.node.id)) node_tmp = csci4220_hw3_pb2.Node(id=local_node,port=local_port,address=local_address) ret = [] # update bucket i = 0 for node in ordered_nodes: i += 1 if i>bucket_max: break else: if node.id==0: tmp = csci4220_hw3_pb2.Node(id=1000,port=node.port,address=node.address) ret.append(tmp) else: tmp = csci4220_hw3_pb2.Node(id=node.id,port=node.port,address=node.address) ret.append(tmp) print("Serving FindNode(" + str(request.node.id) + ") request for " + str(request.idkey)) sys.stdout.flush() return csci4220_hw3_pb2.NodeList(responding_node=node_tmp,nodes=ret) # Findnode function for find node else: index = locateBucket(local_node,request.node.id) if index!=-1: if index not in bucket: tmp = [] tmp.append(nodeObj(request.node.id,request.node.port,request.node.address)) bucket[index] = tmp else: adding = True for k,v in bucket.items(): for i in range(0,len(v)): if v[i].id==request.node.id: adding = False if adding==True: n = len(bucket[index]) tmp = bucket[index] if n==bucket_max: tmp.pop() tmp.insert(0,nodeObj(request.node.id,request.node.port,request.node.address)) bucket[index] = tmp all_nodes = [] for k,v in bucket.items(): for j in range(len(v)): all_nodes.append(v[j]) ordered_nodes = sorted(all_nodes, key=lambda x : XOR(x.id,request.node.id)) node_tmp = csci4220_hw3_pb2.Node(id=local_node,port=local_port,address=local_address) ret = [] # update bucket for node in ordered_nodes: if node.id!=request.idkey: if node.id==0: tmp = csci4220_hw3_pb2.Node(id=1000,port=node.port,address=node.address) ret.append(tmp) else: tmp = csci4220_hw3_pb2.Node(id=node.id,port=node.port,address=node.address) ret.append(tmp) print("Serving FindNode(" + str(request.node.id) + ") request for " + str(request.idkey)) sys.stdout.flush() return csci4220_hw3_pb2.NodeList(responding_node=node_tmp,nodes=ret)