use of com.bonree.brfs.common.proto.FileDataProtos.FileDataReqRes in project BRFS by zhangnianli.
the class NettyMessageServerHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
HttpContent httpRequest = (HttpContent) msg;
ByteBuf bBuf = httpRequest.content();
byte[] bytes = new byte[bBuf.readableBytes()];
bBuf.readBytes(bytes);
NettyMessageReqRes req = NettyMessageReqRes.parseFrom(bytes);
NettyMessageReqRes.Builder builder = NettyMessageReqRes.newBuilder();
builder.setSessionId(req.getSessionId()).setOptType(req.getOptType());
if (NettyMessageReqRes.OptType.STORATE_NAME == req.getOptType()) {
StorageNameRequest snRequest = req.getSnReqRes().getRequest();
StorageNameResponse.Builder snBuilder = StorageNameResponse.newBuilder();
if (StorageNameRequest.StorageNameOptType.CREATE == snRequest.getStorageNameOptType()) {
System.out.println("create sn:" + snRequest);
snBuilder.setCode(StorageNameResponse.ResultCode.SUCCESS);
snBuilder.setDesc("create sn successfully!");
} else if (StorageNameRequest.StorageNameOptType.UPDATE == snRequest.getStorageNameOptType()) {
System.out.println("update sn:" + snRequest);
snBuilder.setCode(StorageNameResponse.ResultCode.SUCCESS);
snBuilder.setDesc("update sn successfully!");
} else if (StorageNameRequest.StorageNameOptType.DELETE == snRequest.getStorageNameOptType()) {
System.out.println("delete sn:" + snRequest);
snBuilder.setCode(StorageNameResponse.ResultCode.SUCCESS);
snBuilder.setDesc("delete sn successfully!");
}
StorageNameReqRes snResponse = StorageNameReqRes.newBuilder().setResponse(snBuilder.build()).build();
builder.setSnReqRes(snResponse);
} else if (NettyMessageReqRes.OptType.DATA_FILE == req.getOptType()) {
FileDataReqRes dataRequest = req.getDataReqRes();
if (FileDataReqRes.DataOptType.WRITE == dataRequest.getDataOptType()) {
System.out.println("write data:");
for (int i = 0; i < dataRequest.getDataCount(); i++) {
System.out.println(dataRequest.getData(i));
}
} else if (FileDataReqRes.DataOptType.READ == dataRequest.getDataOptType()) {
System.out.println("read data:");
for (int i = 0; i < dataRequest.getFidCount(); i++) {
System.out.println(dataRequest.getFid(i));
}
} else if (FileDataReqRes.DataOptType.DELETE == dataRequest.getDataOptType()) {
System.out.println("删除方式:" + dataRequest.getDeleteDataType().name());
System.out.println("删除时间段:" + dataRequest.getBeginTime() + "--" + dataRequest.getEndTime());
}
}
NettyMessageReqRes responseContent = builder.build();
ByteBuf byteBuf = Unpooled.wrappedBuffer(responseContent.toByteArray());
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, byteBuf);
System.out.println("return result!");
System.out.println(new String(responseContent.toByteArray()));
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
Aggregations