PageRenderTime 68ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/linbox/im/server/connector/tcp/handler/IMMessageHandler.java

https://gitlab.com/Mr.Tomato/linbox_server
Java | 138 lines | 102 code | 33 blank | 3 comment | 10 complexity | 3fa5de45ef07178fd853e5e1f1e81376 MD5 | raw file
  1. package com.linbox.im.server.connector.tcp.handler;
  2. import com.alibaba.fastjson.JSON;
  3. import com.linbox.im.message.*;
  4. import com.linbox.im.message.system.SyncSystemUnreadRequest;
  5. import com.linbox.im.server.constant.MessageTopic;
  6. import com.linbox.im.server.monitor.IConnectorMonitor;
  7. import io.netty.channel.ChannelHandlerContext;
  8. import io.netty.channel.ChannelInboundHandlerAdapter;
  9. import org.apache.commons.lang.StringUtils;
  10. import org.apache.kafka.clients.producer.KafkaProducer;
  11. import org.apache.kafka.clients.producer.ProducerRecord;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.context.support.ClassPathXmlApplicationContext;
  15. /**
  16. * Created by lrsec on 6/25/15.
  17. */
  18. public class IMMessageHandler extends ChannelInboundHandlerAdapter {
  19. private static Logger logger = LoggerFactory.getLogger(IMMessageHandler.class);
  20. private String userId = null;
  21. private IConnectorMonitor connectorMonitorService;
  22. private KafkaProducer<String,String> kafkaProducer;
  23. public IMMessageHandler(ClassPathXmlApplicationContext appContext) {
  24. connectorMonitorService = (IConnectorMonitor)appContext.getBean("connectorMonitor");
  25. kafkaProducer = (KafkaProducer)appContext.getBean("kafkaProducer");
  26. }
  27. @Override
  28. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  29. logger.debug("Active IMMessageHandler");
  30. connectorMonitorService.incrConnCount();
  31. }
  32. @Override
  33. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  34. logger.debug("Inactive IMMessageHandler");
  35. connectorMonitorService.decConnCount();
  36. }
  37. @Override
  38. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  39. MessageWrapper wrapper = (MessageWrapper) msg;
  40. if (wrapper == null) {
  41. logger.error("Get an null message wrapper in message handler");
  42. return;
  43. }
  44. RequestResponseType type = wrapper.type;
  45. if (type == null) {
  46. logger.error("Illegal message type for user: {}. Message type: {}", userId, msg.getClass().getName());
  47. return;
  48. }
  49. String json = JSON.toJSONString(wrapper);
  50. logger.debug("Handle {}. Message content: {}", type.getName(), json);
  51. switch(type) {
  52. case SyncUnreadRequestMsg:
  53. if (!StringUtils.equalsIgnoreCase(userId, ((SyncUnreadRequest)wrapper.content).userId)) {
  54. logger.error("The chat id is not match for {}. Authenticated user id: {}. Message content: {}", type.getName(), userId, json);
  55. return;
  56. }
  57. kafkaProducer.send(new ProducerRecord<String, String>(MessageTopic.TOPIC_SYNC_UNREAD, json));
  58. break;
  59. case ReadAckRequestMsg:
  60. if (!StringUtils.equalsIgnoreCase(userId, ((ReadAckRequest)wrapper.content).userId)) {
  61. logger.error("The chat id is not match for {}. Authenticated user id: {}. Message content: {}", type.getName(), userId, json);
  62. return;
  63. }
  64. kafkaProducer.send(new ProducerRecord<String, String>(MessageTopic.TOPIC_READ_ACK, json));
  65. break;
  66. case PullOldMsgRequestMsg:
  67. if (!StringUtils.equalsIgnoreCase(userId, ((PullOldMsgRequest)wrapper.content).userId)) {
  68. logger.error("The chat id is not match for {}. Authenticated user id: {}. Message content: {}", type.getName(), userId, json);
  69. return;
  70. }
  71. kafkaProducer.send(new ProducerRecord<String, String>(MessageTopic.TOPIC_PULL_OLD_MEG, json));
  72. break;
  73. case SendMsgRequestMsg:
  74. if (!StringUtils.equalsIgnoreCase(userId, ((SendMsgRequest)wrapper.content).userId)) {
  75. logger.error("The chat id is not match for {}. Authenticated user id: {}. Message content: {}", type.getName(), userId, json);
  76. return;
  77. }
  78. kafkaProducer.send(new ProducerRecord<String, String>(MessageTopic.TOPIC_SEND_MSG,json));
  79. break;
  80. case Ping:
  81. if (!StringUtils.equalsIgnoreCase(userId, ((Ping)wrapper.content).userId)) {
  82. logger.error("The chat id is not match for {}. Authenticated user id: {}. Message content: {}", type.getName(), userId, json);
  83. return;
  84. }
  85. kafkaProducer.send(new ProducerRecord<String, String>(MessageTopic.TOPIC_PING, json));
  86. break;
  87. case SyncSystemUnreadRequestMsg:
  88. if (!StringUtils.equalsIgnoreCase(userId, ((SyncSystemUnreadRequest)wrapper.content).userId)) {
  89. logger.error("The chat id is not match for {}. Authenticated user id: {}. Message content: {}", type.getName(), userId, json);
  90. return;
  91. }
  92. kafkaProducer.send(new ProducerRecord<String, String>(MessageTopic.TOPIC_SYNC_SYSTEM_UNREAD, json));
  93. break;
  94. default:
  95. logger.error("{} is not handled right now. Message content: {}", type.getName(), JSON.toJSONString(msg));
  96. }
  97. }
  98. @Override
  99. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  100. logger.error("", cause);
  101. }
  102. void setUserId(String u) {
  103. userId = u;
  104. }
  105. }