Search in sources :

Example 6 with RedisCommand

use of com.akaxin.common.command.RedisCommand in project openzaly by akaxincom.

the class ChannelWriter method writeAndClose.

public static void writeAndClose(final Channel channel, CommandResponse response) {
    CoreProto.TransportPackageData.Builder packageDataBuilder = CoreProto.TransportPackageData.newBuilder();
    CoreProto.ErrorInfo errorInfo = CoreProto.ErrorInfo.newBuilder().setCode(response.getErrCode()).setInfo(String.valueOf(response.getErrInfo())).build();
    packageDataBuilder.setErr(errorInfo);
    Map<Integer, String> header = new HashMap<Integer, String>();
    header.put(CoreProto.HeaderKey.SITE_SERVER_VERSION_VALUE, CommandConst.SITE_VERSION);
    packageDataBuilder.putAllHeader(header);
    if (response.getParams() != null) {
        packageDataBuilder.setData(ByteString.copyFrom(response.getParams()));
    }
    channel.writeAndFlush(new RedisCommand().add(response.getVersion()).add(response.getAction()).add(packageDataBuilder.build().toByteArray())).addListener(new GenericFutureListener<Future<? super Void>>() {

        @Override
        public void operationComplete(Future<? super Void> future) throws Exception {
            channel.close();
        }
    });
}
Also used : CoreProto(com.akaxin.proto.core.CoreProto) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) RedisCommand(com.akaxin.common.command.RedisCommand) Future(io.netty.util.concurrent.Future)

Example 7 with RedisCommand

use of com.akaxin.common.command.RedisCommand in project openzaly by akaxincom.

the class ImMessageHandler method handle.

public CommandResponse handle(Command command) {
    try {
        ChannelSession channelSession = command.getChannelSession();
        String deviceId = channelSession.getDeviceId();
        if (StringUtils.isEmpty(deviceId)) {
            channelSession.getChannel().close();
            logger.error("{} client={} im request error with deviceId={}.", AkxProject.PLN, command.getClientIp(), deviceId);
            return customResponse(ErrorCode2.ERROR);
        }
        ChannelSession acsession = ChannelManager.getChannelSession(deviceId);
        if (acsession == null) {
            channelSession.getChannel().close();
            logger.error("{} client={} im request error with channelSession={}", AkxProject.PLN, command.getClientIp(), acsession);
            return customResponse(ErrorCode2.ERROR);
        }
        if (!checkSiteUserId(command.getSiteUserId(), acsession.getUserId())) {
            channelSession.getChannel().close();
            logger.error("{} client={} im request fail siteUserId={},sessionUserId={}", AkxProject.PLN, command.getClientIp(), command.getSiteUserId(), acsession.getUserId());
            return customResponse(ErrorCode2.ERROR);
        }
        if (RequestAction.IM_CTS_PING.getName().equalsIgnoreCase(command.getAction())) {
            Map<Integer, String> header = new HashMap<Integer, String>();
            header.put(CoreProto.HeaderKey.SITE_SERVER_VERSION_VALUE, CommandConst.SITE_VERSION);
            CoreProto.TransportPackageData.Builder packBuilder = CoreProto.TransportPackageData.newBuilder();
            packBuilder.putAllHeader(header);
            channelSession.getChannel().writeAndFlush(new RedisCommand().add(CommandConst.PROTOCOL_VERSION).add(RequestAction.IM_STC_PONG.getName()).add(packBuilder.build().toByteArray()));
            // 检测是否需要给用户发送PSN
            if (channelSession.detectPsn()) {
                channelSession.getChannel().writeAndFlush(new RedisCommand().add(CommandConst.PROTOCOL_VERSION).add(RequestAction.IM_STC_PSN.getName()).add(packBuilder.build().toByteArray()));
                logger.debug("{} client={} siteUserId={} deviceId={} push psn {} {}", AkxProject.PLN, command.getClientIp(), command.getSiteUserId(), command.getDeviceId(), channelSession.getPsnTime(), channelSession.getSynFinTime());
            }
        } else {
            // 排除ping请求,其他请求走im服务
            new ImMessageService().execute(command);
        }
        return customResponse(ErrorCode2.SUCCESS);
    } catch (Exception e) {
        logger.error(StringHelper.format("{} client={} im request error.", AkxProject.PLN, command.getClientIp()), e);
    }
    return defaultErrorResponse();
}
Also used : ChannelSession(com.akaxin.common.channel.ChannelSession) HashMap(java.util.HashMap) ImMessageService(com.akaxin.site.message.service.ImMessageService) RedisCommand(com.akaxin.common.command.RedisCommand)

Example 8 with RedisCommand

use of com.akaxin.common.command.RedisCommand in project openzaly by akaxincom.

the class SyncGroupMessageHandler method groupMessageToClient.

private long groupMessageToClient(Channel channel, String userId, List<GroupMessageBean> groupMessageList) {
    long nextPointer = 0;
    ImStcMessageProto.ImStcMessageRequest.Builder requestBuilder = ImStcMessageProto.ImStcMessageRequest.newBuilder();
    for (GroupMessageBean bean : groupMessageList) {
        try {
            nextPointer = NumUtils.getMax(nextPointer, bean.getId());
            switch(bean.getMsgType()) {
                case CoreProto.MsgType.GROUP_TEXT_VALUE:
                    CoreProto.GroupText groupText = CoreProto.GroupText.newBuilder().setMsgId(bean.getMsgId()).setSiteUserId(bean.getSendUserId()).setSiteGroupId(bean.getSiteGroupId()).setText(ByteString.copyFromUtf8(bean.getContent())).setTime(bean.getMsgTime()).build();
                    ImStcMessageProto.MsgWithPointer gmsg = ImStcMessageProto.MsgWithPointer.newBuilder().setType(MsgType.GROUP_TEXT).setPointer(bean.getId()).setGroupText(groupText).build();
                    requestBuilder.addList(gmsg);
                    break;
                case CoreProto.MsgType.GROUP_SECRET_TEXT_VALUE:
                    // do nothing
                    break;
                case CoreProto.MsgType.GROUP_IMAGE_VALUE:
                    CoreProto.GroupImage groupImage = CoreProto.GroupImage.newBuilder().setMsgId(bean.getMsgId()).setSiteUserId(bean.getSendUserId()).setSiteGroupId(bean.getSiteGroupId()).setImageId(bean.getContent()).setTime(bean.getMsgTime()).build();
                    ImStcMessageProto.MsgWithPointer groupImageMsg = ImStcMessageProto.MsgWithPointer.newBuilder().setType(MsgType.GROUP_IMAGE).setPointer(bean.getId()).setGroupImage(groupImage).build();
                    requestBuilder.addList(groupImageMsg);
                    break;
                case CoreProto.MsgType.GROUP_SECRET_IMAGE_VALUE:
                    // do nothing
                    break;
                case CoreProto.MsgType.GROUP_VOICE_VALUE:
                    CoreProto.GroupVoice groupVoice = CoreProto.GroupVoice.newBuilder().setMsgId(bean.getMsgId()).setSiteUserId(bean.getSendUserId()).setSiteGroupId(bean.getSiteGroupId()).setVoiceId(bean.getContent()).setTime(bean.getMsgTime()).build();
                    ImStcMessageProto.MsgWithPointer groupVoiceMsg = ImStcMessageProto.MsgWithPointer.newBuilder().setType(MsgType.GROUP_VOICE).setPointer(bean.getId()).setGroupVoice(groupVoice).build();
                    requestBuilder.addList(groupVoiceMsg);
                    break;
                case CoreProto.MsgType.GROUP_SECRET_VOICE_VALUE:
                    // do nothing
                    break;
                case CoreProto.MsgType.GROUP_NOTICE_VALUE:
                    CoreProto.GroupMsgNotice groupNotice = CoreProto.GroupMsgNotice.newBuilder().setMsgId(bean.getMsgId()).setSiteUserId(bean.getSendUserId()).setSiteGroupId(bean.getSiteGroupId()).setText(ByteString.copyFromUtf8((bean.getContent()))).setTime(bean.getMsgTime()).build();
                    ImStcMessageProto.MsgWithPointer groupMsgNotice = ImStcMessageProto.MsgWithPointer.newBuilder().setPointer(bean.getId()).setType(MsgType.GROUP_NOTICE).setGroupMsgNotice(groupNotice).build();
                    requestBuilder.addList(groupMsgNotice);
                    break;
            }
        } catch (Exception e) {
            logger.error(StringHelper.format("sync group message error bean={}", bean), e);
        }
    }
    Map<Integer, String> header = new HashMap<Integer, String>();
    header.put(CoreProto.HeaderKey.SITE_SERVER_VERSION_VALUE, CommandConst.SITE_VERSION);
    ImStcMessageProto.ImStcMessageRequest request = requestBuilder.build();
    CoreProto.TransportPackageData datas = CoreProto.TransportPackageData.newBuilder().putAllHeader(header).setData(ByteString.copyFrom(request.toByteArray())).build();
    channel.writeAndFlush(new RedisCommand().add(CommandConst.PROTOCOL_VERSION).add(CommandConst.IM_MSG_TOCLIENT).add(datas.toByteArray()));
    return nextPointer;
}
Also used : CoreProto(com.akaxin.proto.core.CoreProto) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) ImStcMessageProto(com.akaxin.proto.client.ImStcMessageProto) RedisCommand(com.akaxin.common.command.RedisCommand) GroupMessageBean(com.akaxin.site.storage.bean.GroupMessageBean)

Example 9 with RedisCommand

use of com.akaxin.common.command.RedisCommand in project openzaly by akaxincom.

the class ApiRequestHandler method tellClientSessionError.

private void tellClientSessionError(final Channel channel) {
    // 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(ErrorCode2.ERROR_SESSION.getCode()).setInfo(ErrorCode2.ERROR_SESSION.getInfo()).build();
    packageBuilder.setErr(errInfo);
    // 协议版本 CommandConst.PROTOCOL_VERSION=1.0
    String protocolVersion = CommandConst.PROTOCOL_VERSION;
    String action = CommandConst.ACTION_RES;
    channel.writeAndFlush(new RedisCommand().add(protocolVersion).add(action).add(packageBuilder.build().toByteArray())).addListener(new GenericFutureListener<Future<? super Void>>() {

        public void operationComplete(Future<? super Void> future) throws Exception {
            channel.close();
        }
    });
}
Also used : CoreProto(com.akaxin.proto.core.CoreProto) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) RedisCommand(com.akaxin.common.command.RedisCommand) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future)

Example 10 with RedisCommand

use of com.akaxin.common.command.RedisCommand 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

RedisCommand (com.akaxin.common.command.RedisCommand)10 HashMap (java.util.HashMap)8 CoreProto (com.akaxin.proto.core.CoreProto)7 ByteString (com.google.protobuf.ByteString)7 Future (io.netty.util.concurrent.Future)3 ChannelSession (com.akaxin.common.channel.ChannelSession)2 Command (com.akaxin.common.command.Command)2 CommandResponse (com.akaxin.common.command.CommandResponse)2 IRedisCommandResponse (com.akaxin.common.netty.IRedisCommandResponse)2 NettyClient2 (com.akaxin.common.netty.NettyClient2)2 ImStcMessageProto (com.akaxin.proto.client.ImStcMessageProto)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ApiRequestService (com.akaxin.site.business.service.ApiRequestService)1 ImMessageService (com.akaxin.site.message.service.ImMessageService)1 GroupMessageBean (com.akaxin.site.storage.bean.GroupMessageBean)1 U2MessageBean (com.akaxin.site.storage.bean.U2MessageBean)1 InetSocketAddress (java.net.InetSocketAddress)1