def _unlock_file(self, filename): # 解锁一个文件 channel = grpc.insecure_channel('%s:%s' % (self.LOCK_HOST, self.LOCK_PORT)) stub = service_pb2_grpc.lockServiceStub(channel) reply = stub.Unlock(service_pb2.SimpleRequest(text=filename)) return reply
def _get_directory(self, filename): # 请求文件目录 channel = grpc.insecure_channel('%s:%s' % (self.DIR_HOST, self.DIR_PORT)) stub = service_pb2_grpc.directoryServiceStub(channel) reply = stub.GetFileServer(service_pb2.SimpleRequest(text=filename)) return reply
def _get_slaves(self): # 获取其余所有文件服务器节点位置 channel = grpc.insecure_channel('{}:{}'.format(self.DIR_HOST, self.DIR_PORT)) stub = service_pb2_grpc.directoryServiceStub(channel) response = stub.GetSlaves( service_pb2.SimpleRequest( text='{}:{}'.format(self.HOST, self.PORT))) return response.text.split()
def _download_file(self, server, filename): # 发送下载请求 path = os.path.join(self.BUCKET_LOCATION, filename) channel = grpc.insecure_channel(server) stub = service_pb2_grpc.fileServiceStub(channel) reply = stub.Download(service_pb2.SimpleRequest(text=filename)) if reply.flag: with open(path, "w") as f: f.write(reply.data) return reply.flag
def _lock_file(self, filename): # 锁定一个文件 channel = grpc.insecure_channel('%s:%s' % (self.LOCK_HOST, self.LOCK_PORT)) stub = service_pb2_grpc.lockServiceStub(channel) while True: reply = stub.Lock(service_pb2.SimpleRequest(text=filename)) if reply.flag: # 上锁成功 return reply.text else: # 上锁失败,延迟一段时间 request_data = reply.text.splitlines() wait_time = float(request_data[1].split()[1]) time.sleep(wait_time)
def _remove_file(self, server, filename): # 移除某个文件副本 channel = grpc.insecure_channel(server) stub = service_pb2_grpc.fileServiceStub(channel) reply = stub.Delete(service_pb2.SimpleRequest(text=filename)) return reply