Search in sources :

Example 1 with ApiRequestService

use of com.akaxin.site.business.service.ApiRequestService in project openzaly by akaxincom.

the class ApiRequestHandler method doApiRequest.

private CommandResponse doApiRequest(final Channel channel, Command command) {
    CommandResponse comamndResponse = new ApiRequestService().process(command);
    // response
    CoreProto.TransportPackageData.Builder packageBuilder = CoreProto.TransportPackageData.newBuilder();
    // header
    Map<Integer, String> header = new HashMap<Integer, String>();
    // 站点业务版本(proto版本)
    header.put(CoreProto.HeaderKey.SITE_SERVER_VERSION_VALUE, CommandConst.SITE_VERSION);
    packageBuilder.putAllHeader(header);
    // errCode
    CoreProto.ErrorInfo errinfo = CoreProto.ErrorInfo.newBuilder().setCode(String.valueOf(comamndResponse.getErrCode())).setInfo(comamndResponse.getErrInfo()).build();
    packageBuilder.setErr(errinfo);
    // data
    if (comamndResponse.getParams() != null) {
        packageBuilder.setData(ByteString.copyFrom(comamndResponse.getParams())).build();
    }
    // 协议版本 CommandConst.PROTOCOL_VERSION=1.0
    String protocolVersion = CommandConst.PROTOCOL_VERSION;
    String action = comamndResponse.getAction() == null ? CommandConst.ACTION_RES : comamndResponse.getAction();
    ChannelFuture future = channel.writeAndFlush(new RedisCommand().add(protocolVersion).add(action).add(packageBuilder.build().toByteArray())).addListener(new GenericFutureListener<Future<? super Void>>() {

        @Override
        public void operationComplete(Future<? super Void> future) throws Exception {
            channel.close();
        }
    });
    // future.await(timeoutMillis);
    return comamndResponse;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) CoreProto(com.akaxin.proto.core.CoreProto) HashMap(java.util.HashMap) CommandResponse(com.akaxin.common.command.CommandResponse) ByteString(com.google.protobuf.ByteString) RedisCommand(com.akaxin.common.command.RedisCommand) ApiRequestService(com.akaxin.site.business.service.ApiRequestService) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future)

Aggregations

CommandResponse (com.akaxin.common.command.CommandResponse)1 RedisCommand (com.akaxin.common.command.RedisCommand)1 CoreProto (com.akaxin.proto.core.CoreProto)1 ApiRequestService (com.akaxin.site.business.service.ApiRequestService)1 ByteString (com.google.protobuf.ByteString)1 ChannelFuture (io.netty.channel.ChannelFuture)1 Future (io.netty.util.concurrent.Future)1 HashMap (java.util.HashMap)1