use of com.bonree.brfs.common.proto.NettyMessageProto.NettyMessageReqRes in project BRFS by zhangnianli.
the class NettyMessageClient method createMessage.
private static NettyMessageReqRes createMessage() {
NettyMessageReqRes.Builder builder = NettyMessageReqRes.newBuilder();
builder.setSessionId(UUID.randomUUID().toString());
builder.setOptType(NettyMessageReqRes.OptType.STORATE_NAME);
StorageNameReqRes snRequest = StorageNameReqRes.newBuilder().setRequest(StorageNameRequest.newBuilder().setName("sdk").setUser("weizheng").setStorageNameOptType(StorageNameRequest.StorageNameOptType.CREATE).build()).build();
NettyMessageReqRes request = builder.setSnReqRes(snRequest).build();
return request;
}
use of com.bonree.brfs.common.proto.NettyMessageProto.NettyMessageReqRes in project BRFS by zhangnianli.
the class NettyMessageClient method main.
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
// HttpHost proxy = new HttpHost("127.0.0.1", 8080);
// httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpPost httppost = new HttpPost("http://127.0.0.1:8080");
ByteArrayEntity entity = new ByteArrayEntity(createMessage().toByteArray());
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity receiveEntity = response.getEntity();
byte[] content = EntityUtils.toByteArray(receiveEntity);
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
NettyMessageReqRes responseMsg = NettyMessageReqRes.parseFrom(content);
System.out.println(responseMsg);
System.out.println("success");
}
use of com.bonree.brfs.common.proto.NettyMessageProto.NettyMessageReqRes 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