Search in sources :

Example 41 with CommandResponse

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

the class NettyServerHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, RedisCommand redisCmd) throws Exception {
    InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
    String clientIp = socketAddress.getAddress().getHostAddress();
    ChannelSession channelSession = ctx.channel().attr(ParserConst.CHANNELSESSION).get();
    // disconnect tcp connection as channel is unavailable
    if (channelSession.getChannel() == null || !channelSession.getChannel().isActive()) {
        // 断开连接事件(与对方的连接断开)
        ctx.disconnect();
        logger.warn("{} close client={} as its channel is not active ", AkxProject.PLN, clientIp);
    }
    String version = redisCmd.getParameterByIndex(0);
    String action = redisCmd.getParameterByIndex(1);
    byte[] params = redisCmd.getBytesParamByIndex(2);
    CoreProto.TransportPackageData packageData = CoreProto.TransportPackageData.parseFrom(params);
    Command command = new Command();
    command.setClientIp(clientIp);
    command.setSiteUserId(channelSession.getUserId());
    command.setDeviceId(channelSession.getDeviceId());
    command.setAction(action);
    command.setHeader(packageData.getHeaderMap());
    command.setParams(packageData.getData().toByteArray());
    command.setChannelSession(channelSession);
    command.setStartTime(System.currentTimeMillis());
    if (!RequestAction.IM_CTS_PING.getName().equalsIgnoreCase(command.getAction())) {
        logger.debug("{} client={} -> site version={} action={} params-length={}", AkxProject.PLN, clientIp, version, action, params.length);
    } else {
        logger.trace("{} client={} ping -> site", AkxProject.PLN, clientIp);
    }
    if (RequestAction.IM.getName().equals(command.getRety())) {
        // 如果是syncFinish,则这里需要修改channel中的syncFinTime
        channelSession.setActionForPsn(action);
        // 单独处理im.site.hello && im.site.auth
        if (RequestAction.SITE.getName().equalsIgnoreCase(command.getService())) {
            String anoRequest = command.getRety() + "." + command.getService();
            command.setRety(anoRequest);
        }
        CommandResponse response = this.executor.execute(command.getRety(), command);
        if (!RequestAction.IM_CTS_PING.getName().equals(command.getAction())) {
            // 输出IM请求结果
            LogUtils.requestResultLog(logger, command, response);
        }
    } else if (RequestAction.API.getName().equalsIgnoreCase(command.getRety())) {
        CommandResponse response = this.executor.execute(command.getRety(), command);
        // 输出API请求结果
        LogUtils.requestResultLog(logger, command, response);
    } else {
        /**
         * <pre>
         *
         * pipeline.addLast("A", new AHandler());
         * pipeline.addLast("B", new BHandler());
         * pipeline.addLast("C", new CHandler());
         *
         * ctx.close() && channel().close();
         * 		ctx.close(): close the channel in current handler,will start from the tail of the ChannelPipeline
         * 				do A.close() ,B.close(),C.close();
         * 		channel().close():close channel in all handler from pointer
         * 				do C.close() , B.close(), A.close();
         * </pre>
         */
        // 关闭channel事件(关闭自己的连接)
        ctx.channel().close();
        logger.error("{} client={} siteUserId={} action={} unknow request method", AkxProject.PLN, command.getClientIp(), command.getSiteUserId(), command.getAction());
        return;
    }
}
Also used : CoreProto(com.akaxin.proto.core.CoreProto) ChannelSession(com.akaxin.common.channel.ChannelSession) Command(com.akaxin.common.command.Command) RedisCommand(com.akaxin.common.command.RedisCommand) InetSocketAddress(java.net.InetSocketAddress) CommandResponse(com.akaxin.common.command.CommandResponse)

Example 42 with CommandResponse

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

the class AbstractCommonHandler method defaultErrorResponse.

// default error ressponse by ErrorCode2.ERROR
protected CommandResponse defaultErrorResponse() {
    CommandResponse commandResponse = new CommandResponse().setVersion(CommandConst.PROTOCOL_VERSION).setAction(CommandConst.ACTION_RES);
    commandResponse.setErrCode2(ErrorCode2.ERROR);
    return commandResponse;
}
Also used : CommandResponse(com.akaxin.common.command.CommandResponse)

Example 43 with CommandResponse

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

the class GroupPsnHandler method writePSN.

private void writePSN(String deviceId) {
    CommandResponse commandResponse = new CommandResponse().setVersion(CommandConst.PROTOCOL_VERSION).setAction(CommandConst.IM_STC_PSN);
    ImStcPsnProto.ImStcPsnRequest pshRequest = ImStcPsnProto.ImStcPsnRequest.newBuilder().build();
    commandResponse.setParams(pshRequest.toByteArray());
    commandResponse.setErrCode(ErrorCode.SUCCESS);
    ChannelWriter.writeByDeviceId(deviceId, commandResponse);
}
Also used : CommandResponse(com.akaxin.common.command.CommandResponse) ImStcPsnProto(com.akaxin.proto.client.ImStcPsnProto)

Example 44 with CommandResponse

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

the class UserPsnHandler method writePsn.

private void writePsn(String deviceId) {
    CommandResponse commandResponse = new CommandResponse().setVersion(CommandConst.PROTOCOL_VERSION).setAction(CommandConst.IM_STC_PSN);
    ImStcPsnProto.ImStcPsnRequest pshRequest = ImStcPsnProto.ImStcPsnRequest.newBuilder().build();
    commandResponse.setParams(pshRequest.toByteArray());
    commandResponse.setErrCode(ErrorCode.SUCCESS);
    ChannelWriter.writeByDeviceId(deviceId, commandResponse);
}
Also used : CommandResponse(com.akaxin.common.command.CommandResponse) ImStcPsnProto(com.akaxin.proto.client.ImStcPsnProto)

Example 45 with CommandResponse

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

the class ApiDeviceService method list.

public CommandResponse list(Command command) {
    CommandResponse commandResponse = new CommandResponse();
    ErrorCode2 errCode = ErrorCode2.ERROR;
    try {
        ApiDeviceListProto.DeviceListInfoRequest request = ApiDeviceListProto.DeviceListInfoRequest.parseFrom(command.getParams());
        String siteFriendId = request.getSiteUserId();
        LogUtils.requestDebugLog(logger, command, request.toString());
        if (StringUtils.isNotBlank(siteFriendId)) {
            ApiDeviceListProto.DeviceListInfoResponse.Builder responseBuilder = ApiDeviceListProto.DeviceListInfoResponse.newBuilder();
            List<UserDeviceBean> deviceList = UserDeviceDao.getInstance().getActiveDeviceList(siteFriendId);
            for (UserDeviceBean device : deviceList) {
                DeviceProto.SimpleDeviceProfile deviceProfile = DeviceProto.SimpleDeviceProfile.newBuilder().setDeviceId(String.valueOf(device.getDeviceId())).setDeviceName(String.valueOf(device.getDeviceName())).setUserDevicePubk(String.valueOf(device.getUserDevicePubk())).setLastLoginTime(device.getActiveTime()).build();
                responseBuilder.addList(deviceProfile);
            }
            commandResponse.setParams(responseBuilder.build().toByteArray());
            errCode = ErrorCode2.SUCCESS;
        } else {
            errCode = ErrorCode2.ERROR_PARAMETER;
        }
    } catch (Exception e) {
        errCode = ErrorCode2.ERROR_SYSTEMERROR;
        LogUtils.requestErrorLog(logger, command, e);
    }
    return commandResponse.setErrCode2(errCode);
}
Also used : ErrorCode2(com.akaxin.common.constant.ErrorCode2) ApiDeviceListProto(com.akaxin.proto.site.ApiDeviceListProto) UserDeviceBean(com.akaxin.site.storage.bean.UserDeviceBean) CommandResponse(com.akaxin.common.command.CommandResponse) DeviceProto(com.akaxin.proto.core.DeviceProto)

Aggregations

CommandResponse (com.akaxin.common.command.CommandResponse)82 ErrorCode2 (com.akaxin.common.constant.ErrorCode2)68 UserProto (com.akaxin.proto.core.UserProto)12 PluginBean (com.akaxin.site.storage.bean.PluginBean)7 ByteString (com.google.protobuf.ByteString)7 GroupProto (com.akaxin.proto.core.GroupProto)6 GroupProfileBean (com.akaxin.site.storage.bean.GroupProfileBean)6 SimpleUserBean (com.akaxin.site.storage.bean.SimpleUserBean)6 UserProfileBean (com.akaxin.site.storage.bean.UserProfileBean)6 UserDeviceBean (com.akaxin.site.storage.bean.UserDeviceBean)5 Command (com.akaxin.common.command.Command)4 DeviceProto (com.akaxin.proto.core.DeviceProto)4 ProtocolStringList (com.google.protobuf.ProtocolStringList)4 ConfigProto (com.akaxin.proto.core.ConfigProto)3 GroupMemberBean (com.akaxin.site.storage.bean.GroupMemberBean)3 UserGroupBean (com.akaxin.site.storage.bean.UserGroupBean)3 RedisCommand (com.akaxin.common.command.RedisCommand)2 ImStcPsnProto (com.akaxin.proto.client.ImStcPsnProto)2 CoreProto (com.akaxin.proto.core.CoreProto)2 FileProto (com.akaxin.proto.core.FileProto)2