use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.
the class TcpDiskNodeClient method ping.
@Override
public boolean ping() {
try {
BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_PING_PONG);
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 true;
}
} catch (Exception e) {
LOG.error("ping to server error", e);
}
return false;
}
use of com.bonree.brfs.common.net.tcp.BaseResponse 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.common.net.tcp.BaseResponse in project BRFS by zhangnianli.
the class TcpDiskNodeClient method deleteDir.
@Override
public boolean deleteDir(String path, boolean force, boolean recursive) {
try {
DeleteFileMessage deleteFileMessage = new DeleteFileMessage();
deleteFileMessage.setFilePath(path);
deleteFileMessage.setForce(force);
deleteFileMessage.setRecursive(recursive);
BaseMessage message = new BaseMessage(DataNodeBootStrap.TYPE_DELETE_FILE);
message.setBody(ProtoStuffUtils.serialize(deleteFileMessage));
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 true;
}
} catch (Exception e) {
LOG.error("delete file [{}] error", path, e);
}
return false;
}
use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.
the class AsyncTcpClientGroup method createClient.
@Override
public TcpClient<BaseMessage, BaseResponse> createClient(TcpClientConfig config, Executor executor) throws InterruptedException {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator());
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.connectTimeoutMillis());
if (executor == null) {
executor = new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
};
}
AsyncTcpClient client = new AsyncTcpClient(executor);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new IdleStateHandler(0, DEFAULT_WRITE_IDLE_TIMEOUT_SECONDS, 0)).addLast(new BaseMessageEncoder()).addLast(new BaseResponseDecoder()).addLast(new SimpleChannelInboundHandler<TokenMessage<BaseResponse>>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, TokenMessage<BaseResponse> msg) throws Exception {
client.handle(msg.messageToken(), msg.message());
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent e = (IdleStateEvent) evt;
if (e.state() == IdleState.WRITER_IDLE) {
ctx.writeAndFlush(new BaseMessage(-1));
}
}
}
});
}
});
ChannelFuture future = bootstrap.connect(config.remoteAddress()).sync();
if (!future.isSuccess()) {
return null;
}
Channel channel = future.channel();
channelList.add(channel);
channel.closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
channelList.remove(channel);
}
});
LOG.info("create tcp client for {}", config.remoteAddress());
client.attach(channel);
return client;
}
use of com.bonree.brfs.common.net.tcp.BaseResponse in project BRFS by zhangnianli.
the class BaseResponseDecoder method decode.
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (response == null) {
if (in.readableBytes() < Integer.BYTES * 3) {
return;
}
final int token = in.readInt();
final int code = in.readInt();
final int length = in.readInt();
response = new TokenMessage<BaseResponse>() {
BaseResponse baseResponse = new BaseResponse(code);
@Override
public int messageToken() {
return token;
}
@Override
public BaseResponse message() {
return baseResponse;
}
};
if (length == 0) {
out.add(response);
response = null;
return;
}
response.message().setBody(new byte[length]);
}
if (in.readableBytes() < response.message().getBody().length) {
return;
}
in.readBytes(response.message().getBody());
out.add(response);
response = null;
}
Aggregations