Exemplo n.º 1
0
 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
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
    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()
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
 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)
Exemplo n.º 6
0
 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