use of com.akaxin.common.command.RedisCommand 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;
}
}
use of com.akaxin.common.command.RedisCommand in project openzaly by akaxincom.
the class SyncU2MessageHandler method u2MessageToClient.
private long u2MessageToClient(Channel channel, List<U2MessageBean> u2MessageList) {
long nextPointer = 0;
ImStcMessageProto.ImStcMessageRequest.Builder requestBuilder = ImStcMessageProto.ImStcMessageRequest.newBuilder();
for (U2MessageBean bean : u2MessageList) {
try {
nextPointer = NumUtils.getMax(nextPointer, bean.getId());
switch(bean.getMsgType()) {
case CoreProto.MsgType.TEXT_VALUE:
CoreProto.MsgText MsgText = CoreProto.MsgText.newBuilder().setMsgId(bean.getMsgId()).setSiteUserId(bean.getSendUserId()).setSiteFriendId(bean.getSiteUserId()).setText(ByteString.copyFromUtf8(bean.getContent())).setTime(bean.getMsgTime()).build();
ImStcMessageProto.MsgWithPointer textMsg = ImStcMessageProto.MsgWithPointer.newBuilder().setType(MsgType.TEXT).setPointer(bean.getId()).setText(MsgText).build();
// logger.info("[Syncing U2] text message OK. bean={}", u2Bean);
requestBuilder.addList(textMsg);
break;
case CoreProto.MsgType.SECRET_TEXT_VALUE:
byte[] secretTexgt = Base64.getDecoder().decode(bean.getContent());
CoreProto.MsgSecretText secretText = CoreProto.MsgSecretText.newBuilder().setMsgId(bean.getMsgId()).setSiteUserId(bean.getSendUserId()).setSiteFriendId(bean.getSiteUserId()).setText(ByteString.copyFrom(secretTexgt)).setSiteDeviceId(String.valueOf(bean.getDeviceId())).setTsKey(bean.getTsKey()).setTime(bean.getMsgTime()).build();
ImStcMessageProto.MsgWithPointer secretTextMsg = ImStcMessageProto.MsgWithPointer.newBuilder().setType(MsgType.SECRET_TEXT).setPointer(bean.getId()).setSecretText(secretText).build();
// logger.info("[Syncing U2] secret text message OK. bean={}",
// u2Bean);
requestBuilder.addList(secretTextMsg);
break;
case CoreProto.MsgType.IMAGE_VALUE:
CoreProto.MsgImage msgImage = CoreProto.MsgImage.newBuilder().setImageId(bean.getContent()).setMsgId(bean.getMsgId()).setSiteUserId(bean.getSendUserId()).setSiteFriendId(bean.getSiteUserId()).setTime(bean.getMsgTime()).setImageId(bean.getContent()).build();
ImStcMessageProto.MsgWithPointer imageMsgWithPointer = ImStcMessageProto.MsgWithPointer.newBuilder().setType(MsgType.IMAGE).setPointer(bean.getId()).setImage(msgImage).build();
// logger.info("[Syncing U2] image message OK. bean={}", u2Bean);
requestBuilder.addList(imageMsgWithPointer);
break;
case CoreProto.MsgType.SECRET_IMAGE_VALUE:
CoreProto.MsgSecretImage secretImage = CoreProto.MsgSecretImage.newBuilder().setMsgId(bean.getMsgId()).setSiteUserId(bean.getSendUserId()).setSiteFriendId(bean.getSiteUserId()).setImageId(bean.getContent()).setSiteDeviceId(String.valueOf(bean.getDeviceId())).setTsKey(bean.getTsKey()).setTime(bean.getMsgTime()).build();
ImStcMessageProto.MsgWithPointer secretImageMsg = ImStcMessageProto.MsgWithPointer.newBuilder().setType(MsgType.SECRET_IMAGE).setPointer(bean.getId()).setSecretImage(secretImage).build();
// logger.info("[Syncing U2] secret image message OK. bean={}",
// u2Bean);
requestBuilder.addList(secretImageMsg);
break;
case CoreProto.MsgType.VOICE_VALUE:
CoreProto.MsgVoice voice = CoreProto.MsgVoice.newBuilder().setMsgId(bean.getMsgId()).setSiteUserId(bean.getSendUserId()).setSiteFriendId(bean.getSiteUserId()).setVoiceId(bean.getContent()).setTime(bean.getMsgTime()).build();
ImStcMessageProto.MsgWithPointer voiceMsg = ImStcMessageProto.MsgWithPointer.newBuilder().setType(MsgType.VOICE).setPointer(bean.getId()).setVoice(voice).build();
// logger.info("[Syncing U2] voice message OK. bean={0}", u2Bean);
requestBuilder.addList(voiceMsg);
break;
case CoreProto.MsgType.SECRET_VOICE_VALUE:
CoreProto.MsgSecretVoice secretVoice = CoreProto.MsgSecretVoice.newBuilder().setMsgId(bean.getMsgId()).setSiteUserId(bean.getSendUserId()).setSiteFriendId(bean.getSiteUserId()).setVoicdId(bean.getContent()).setSiteDeviceId(String.valueOf(bean.getDeviceId())).setTsKey(bean.getTsKey()).setTime(bean.getMsgTime()).build();
ImStcMessageProto.MsgWithPointer secretVoiceMsg = ImStcMessageProto.MsgWithPointer.newBuilder().setType(MsgType.SECRET_VOICE).setPointer(bean.getId()).setSecretVoice(secretVoice).build();
// logger.info("[Syncing U2] secret voice message OK. bean={}",
// u2Bean);
requestBuilder.addList(secretVoiceMsg);
break;
case CoreProto.MsgType.U2_NOTICE_VALUE:
CoreProto.U2MsgNotice u2Notice = CoreProto.U2MsgNotice.newBuilder().setMsgId(bean.getMsgId()).setSiteUserId(bean.getSendUserId()).setSiteFriendId(bean.getSiteUserId()).setText(ByteString.copyFromUtf8(bean.getContent())).setTime(bean.getMsgTime()).build();
ImStcMessageProto.MsgWithPointer u2NoticeMsg = ImStcMessageProto.MsgWithPointer.newBuilder().setPointer(bean.getId()).setType(MsgType.U2_NOTICE).setU2MsgNotice(u2Notice).build();
requestBuilder.addList(u2NoticeMsg);
break;
default:
logger.error("Message type error! when sync to client bean={}", bean);
break;
}
} catch (Exception e) {
logger.error(StringHelper.format("sync u2 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 data = 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(data.toByteArray()));
return nextPointer;
}
use of com.akaxin.common.command.RedisCommand in project openzaly by akaxincom.
the class WritePackage method syncWrite.
public byte[] syncWrite(String action, byte[] byteData) {
try {
CoreProto.TransportPackageData.Builder packageDataBuilder = CoreProto.TransportPackageData.newBuilder();
Map<Integer, String> header = new HashMap<Integer, String>();
header.put(CoreProto.HeaderKey.SITE_SERVER_VERSION_VALUE, CommandConst.SITE_VERSION);
packageDataBuilder.putAllHeader(header);
packageDataBuilder.setData(ByteString.copyFrom(byteData));
NettyClient2 nettyClient = new NettyClient2();
nettyClient.connect(AKAXIN_PUSH_ADDRESS, AKAXIN_PUSH_PORT);
Future<IRedisCommandResponse> future = nettyClient.sendRedisCommand(new RedisCommand().add(CommandConst.PROTOCOL_VERSION).add(action).add(packageDataBuilder.build().toByteArray()));
IRedisCommandResponse response = future.get(5, TimeUnit.SECONDS);
nettyClient.disconnect();
if (response != null && response.isSuccess()) {
return getPackageBytes(response.getRedisCommand());
}
logger.debug("write push to platform finish response={}", response);
} catch (Exception e) {
logger.error("sync send package error ", e);
}
return null;
}
use of com.akaxin.common.command.RedisCommand in project openzaly by akaxincom.
the class WritePackage method asyncWrite.
public void asyncWrite(String action, byte[] byteData) {
try {
CoreProto.TransportPackageData.Builder packageDataBuilder = CoreProto.TransportPackageData.newBuilder();
Map<Integer, String> header = new HashMap<Integer, String>();
header.put(CoreProto.HeaderKey.SITE_SERVER_VERSION_VALUE, CommandConst.SITE_VERSION);
packageDataBuilder.putAllHeader(header);
packageDataBuilder.setData(ByteString.copyFrom(byteData));
NettyClient2 nettyClient = new NettyClient2();
nettyClient.connect(AKAXIN_PUSH_ADDRESS, AKAXIN_PUSH_PORT);
Future<IRedisCommandResponse> future = nettyClient.sendRedisCommand(new RedisCommand().add(CommandConst.PROTOCOL_VERSION).add(action).add(packageDataBuilder.build().toByteArray()));
IRedisCommandResponse response = future.get(5, TimeUnit.SECONDS);
logger.debug("write push to platform finish response={}", response);
nettyClient.disconnect();
} catch (Exception e) {
logger.error("async send package to platform error", e);
}
}
use of com.akaxin.common.command.RedisCommand in project openzaly by akaxincom.
the class NettyClientHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, RedisCommand redisCmd) throws Exception {
String version = redisCmd.getParameterByIndex(0);
String action = redisCmd.getParameterByIndex(1);
byte[] params = redisCmd.getBytesParamByIndex(2);
CoreProto.TransportPackageData packageData = CoreProto.TransportPackageData.parseFrom(params);
CoreProto.ErrorInfo errInfo = packageData.getErr();
Command command = new Command();
command.setHeader(packageData.getHeaderMap());
command.setParams(packageData.getData().toByteArray());
// logger.info("netty client channel handler command={}", command.toString());
NettyClientHandler.this.nettyClient.handleResponse(new RedisCommandResponse(redisCmd, errInfo.getCode(), errInfo.getInfo()));
}
Aggregations