Search in sources :

Example 1 with NettyMessageReqRes

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;
}
Also used : NettyMessageReqRes(com.bonree.brfs.common.proto.NettyMessageProto.NettyMessageReqRes) StorageNameReqRes(com.bonree.brfs.common.proto.StorageNameProtos.StorageNameReqRes)

Example 2 with NettyMessageReqRes

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");
}
Also used : NettyMessageReqRes(com.bonree.brfs.common.proto.NettyMessageProto.NettyMessageReqRes) HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 3 with NettyMessageReqRes

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);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) StorageNameResponse(com.bonree.brfs.common.proto.StorageNameProtos.StorageNameResponse) StorageNameReqRes(com.bonree.brfs.common.proto.StorageNameProtos.StorageNameReqRes) ByteBuf(io.netty.buffer.ByteBuf) NettyMessageReqRes(com.bonree.brfs.common.proto.NettyMessageProto.NettyMessageReqRes) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) StorageNameRequest(com.bonree.brfs.common.proto.StorageNameProtos.StorageNameRequest) FileDataReqRes(com.bonree.brfs.common.proto.FileDataProtos.FileDataReqRes) HttpContent(io.netty.handler.codec.http.HttpContent)

Aggregations

NettyMessageReqRes (com.bonree.brfs.common.proto.NettyMessageProto.NettyMessageReqRes)3 StorageNameReqRes (com.bonree.brfs.common.proto.StorageNameProtos.StorageNameReqRes)2 FileDataReqRes (com.bonree.brfs.common.proto.FileDataProtos.FileDataReqRes)1 StorageNameRequest (com.bonree.brfs.common.proto.StorageNameProtos.StorageNameRequest)1 StorageNameResponse (com.bonree.brfs.common.proto.StorageNameProtos.StorageNameResponse)1 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpContent (io.netty.handler.codec.http.HttpContent)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)1