def __init__(self, username, server_address, server_port): self.username = username self.server_address = server_address self.server_port = server_port # create a gRPC channel + stub channel = grpc.insecure_channel(server_address + ':' + str(server_port)) self.raft_stub = raft_proto_rpc.RaftServiceStub(channel) self.file_transfer_stub = file_transfer_proto_rpc.DataTransferServiceStub(channel)
def send_request_to_other_raft_nodes(other_node, return_set): try: with grpc.insecure_channel(other_node["ip"] + ':' + other_node["port"]) as channel: stub = file_transfer_proto_rpc.DataTransferServiceStub(channel) files = stub.ListFiles(request, timeout=1) for file_name in files.lstFileNames: return_set.add(file_name) except: log_info("Fail to connect to: ", other_node["ip"], other_node["port"])
def run(raft_ip, raft_port): with grpc.insecure_channel(raft_ip + ':' + raft_port) as channel: stub = file_transfer_rpc.DataTransferServiceStub(channel) request = file_transfer.RequestFileList() request.isClient = True response = stub.ListFiles(request) log_info("Got list of files: ") pprint.pprint(response)
def request_file_info_from_other_raft_nodes(request): for node in other_raft_nodes: try: with grpc.insecure_channel(node["ip"] + ':' + node["port"]) as channel: stub = file_transfer_proto_rpc.DataTransferServiceStub(channel) file_location_info = stub.GetFileLocation(request) log_info("Response received From other Raft: ") # pprint.pprint(file_location_info) log_info(file_location_info.maxChunks) log_info("is file found in other Raft:", file_location_info.isFileFound) if file_location_info.isFileFound: return file_location_info except: log_info("Fail to connect to: ", node["ip"], node["port"]) file_location_info = file_transfer_proto.FileLocationInfo() file_location_info.isFileFound = False return file_location_info
def UploadFile(self, FileUploadData_stream, context): dc = findDataCenter() dc_stub = file_transfer_pb2_grpc.DataTransferServiceStub( grpc.insecure_channel(dc)) return dc_stub.UploadFile(FileUploadData_stream)
def DownloadChunk(self, chunkInfo, context): dc_list = file_log[chunkInfo.fileName + "_" + str(chunkInfo.chunkId)] dc_stub = file_transfer_pb2_grpc.DataTransferServiceStub( grpc.insecure_channel(dc_list[0])) return dc_stub.DownloadChunk(chunkInfo)
def RequestFileInfo(self, fileInfo, context): global file_info_timer, cached_file_info fileName = fileInfo.fileName if my_state != States.Leader: try: leader_stub = file_transfer_pb2_grpc.DataTransferServiceStub( grpc.insecure_channel(leader_id)) return leader_stub.RequestFileInfo(fileInfo, timeout=1) except: return FileLocationInfo(fileName=fileName, maxChunks=0, lstProxy=[], isFileFound=False) else: fileFound = False for f_c in file_log.keys(): if fileName in f_c and len(file_log[f_c]) != 0: fileFound = True break if fileFound: proxies = findProxies() proxies_ser = [] for p in proxies: ip_port = p.split(':') proxies_ser.append( ProxyInfo(ip=ip_port[0], port=ip_port[1])) try: return FileLocationInfo( fileName=fileName, maxChunks=file_max_chunks[fileName], lstProxy=proxies_ser, isFileFound=True) except: return FileLocationInfo(fileName=fileName, maxChunks=0, lstProxy=[], isFileFound=False) else: if not file_info_cache_event.isSet(): external_stub = "" for n in live_external_nodes: try: external_stub = file_transfer_pb2_grpc.DataTransferServiceStub( grpc.insecure_channel(n)) file_loc_info = external_stub.GetFileLocation( fileInfo, timeout=0.1) if file_loc_info.isFileFound: cached_file_info[fileName] = file_loc_info file_info_timer[fileName] = 10 file_info_cache_event.set() return file_loc_info except: pass cached_file_info[fileName] = FileLocationInfo( fileName=fileName, maxChunks=0, lstProxy=[], isFileFound=False) file_info_timer[fileName] = 10 file_info_cache_event.set() return FileLocationInfo(fileName=fileName, maxChunks=0, lstProxy=[], isFileFound=False) else: if fileName not in cached_file_info.keys(): external_stub = "" for n in live_external_nodes: try: external_stub = file_transfer_pb2_grpc.DataTransferServiceStub( grpc.insecure_channel(n)) file_loc_info = external_stub.GetFileLocation( fileInfo, timeout=0.1) if file_loc_info.isFileFound: cached_file_info[fileName] = file_loc_info file_info_timer[fileName] = 10 file_info_cache_event.set() return file_loc_info except: pass cached_file_info[fileName] = FileLocationInfo( fileName=fileName, maxChunks=0, lstProxy=[], isFileFound=False) file_info_timer[fileName] = 10 file_info_cache_event.set() return FileLocationInfo(fileName=fileName, maxChunks=0, lstProxy=[], isFileFound=False) else: file_info_timer[fileName] = 10 return cached_file_info[fileName]