use of com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage in project BRFS by zhangnianli.
the class TcpDiskNodeClient method listFiles.
@Override
public List<FileInfo> listFiles(String path, int level) {
try {
ListFileMessage listFileMessage = new ListFileMessage();
listFileMessage.setPath(path);
listFileMessage.setLevel(level);
BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_LIST_FILE);
message.setBody(ProtoStuffUtils.serialize(listFileMessage));
CompletableFuture<BaseResponse> future = new CompletableFuture<BaseResponse>();
client.sendMessage(message, new ResponseHandler<BaseResponse>() {
@Override
public void handle(BaseResponse response) {
future.complete(response);
}
@Override
public void error(Throwable e) {
future.completeExceptionally(e);
}
});
BaseResponse response = future.get();
if (response != null && response.getCode() == ResponseCode.OK) {
return JsonUtils.toObject(response.getBody(), new TypeReference<List<FileInfo>>() {
});
}
} catch (Exception e) {
LOG.error("list files of dir[{}] with level[{}] error", path, level, e);
}
return null;
}
use of com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage in project BRFS by zhangnianli.
the class ListFileMessageHandler method handleMessage.
@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
ListFileMessage message = ProtoStuffUtils.deserialize(baseMessage.getBody(), ListFileMessage.class);
if (message == null) {
writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
return;
}
String dirPath = null;
try {
dirPath = context.getConcreteFilePath(message.getPath());
File dir = new File(dirPath);
if (!dir.exists()) {
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
if (!dir.isDirectory()) {
writer.write(new BaseResponse(ResponseCode.ERROR));
return;
}
FileInfo dirInfo = new FileInfo();
dirInfo.setLevel(0);
dirInfo.setType(FileInfo.TYPE_DIR);
dirInfo.setPath(dirPath);
fileList.addLast(dirInfo);
ArrayList<FileInfo> fileInfoList = new ArrayList<FileInfo>();
traverse(message.getLevel(), fileInfoList);
BaseResponse response = new BaseResponse(ResponseCode.OK);
response.setBody(JsonUtils.toJsonBytes(fileInfoList));
writer.write(response);
} catch (Exception e) {
LOG.error("list dir[{}] error", dirPath, e);
writer.write(new BaseResponse(ResponseCode.ERROR));
}
}
Aggregations