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

/src/main/java/com/linbox/im/server/connector/rpc/IMService.java

https://gitlab.com/Mr.Tomato/linbox_server
Java | 137 lines | 107 code | 26 blank | 4 comment | 6 complexity | a9d9a13964a67934b55fd77cd88aaf4b MD5 | raw file
  1. package com.linbox.im.server.connector.rpc;
  2. import com.alibaba.fastjson.JSON;
  3. import com.google.common.base.Strings;
  4. import com.linbox.im.interfaces.IIMService;
  5. import com.linbox.im.message.Message;
  6. import com.linbox.im.message.MessageType;
  7. import com.linbox.im.message.MessageWrapper;
  8. import com.linbox.im.message.SendMsgRequest;
  9. import com.linbox.im.message.system.SystemMessage;
  10. import com.linbox.im.server.constant.MessageTopic;
  11. import com.linbox.im.server.service.IOutboxService;
  12. import com.linbox.im.server.storage.dao.IGroupDAO;
  13. import com.linbox.im.server.storage.dao.IServerDAO;
  14. import org.apache.kafka.clients.producer.KafkaProducer;
  15. import org.apache.kafka.clients.producer.ProducerRecord;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import java.util.Set;
  20. /**
  21. * Created by lrsec on 7/27/15.
  22. */
  23. public class IMService implements IIMService {
  24. private static Logger logger = LoggerFactory.getLogger(IMService.class);
  25. @Autowired
  26. private KafkaProducer<String,String> kafkaProducer;
  27. @Autowired
  28. private IOutboxService outboxService;
  29. @Autowired
  30. private IServerDAO serverDAO;
  31. @Autowired
  32. private IGroupDAO groupDAO;
  33. @Override
  34. public Set<String> getIMServerList() {
  35. return serverDAO.getServers();
  36. }
  37. @Override
  38. public String getPassword(long userId) {
  39. return serverDAO.generatePassword(userId);
  40. }
  41. @Override
  42. public void sendMessage(String fromUserId, String toId, String mineType, String content, int messageType, String creatorId) {
  43. logger.debug("Received message from rpc call. From: {}. To: {}. MimeType: {}. Content: {}. MessageType: {}. CreatorID: {}", fromUserId, toId, mineType, content, messageType, creatorId);
  44. if (Strings.isNullOrEmpty(fromUserId)) {
  45. logger.error("Sender user id could not be blank");
  46. return;
  47. }
  48. if (Strings.isNullOrEmpty(fromUserId)) {
  49. logger.error("Target user id could not be blank");
  50. return;
  51. }
  52. if (!isLegalSender(fromUserId)) {
  53. logger.warn("User {} is not allowed to send message through im rpc service", fromUserId);
  54. return;
  55. }
  56. long current = System.currentTimeMillis();
  57. String toUserId = null;
  58. String toGroupId = null;
  59. MessageType type = MessageType.parse(messageType);
  60. if (type == null) {
  61. logger.warn("Unknown message type {} for user {}. Stop sending messages", messageType, fromUserId);
  62. return;
  63. }
  64. switch (type) {
  65. case Session:
  66. toUserId = toId;
  67. break;
  68. case Group:
  69. toGroupId = toId;
  70. break;
  71. default:
  72. logger.warn("Unknown message type {} in sending message through rpc service.", type.getName());
  73. return;
  74. }
  75. Message msg = new Message();
  76. msg.rId = current;
  77. msg.fromUserId = fromUserId;
  78. msg.toUserId = toUserId;
  79. msg.groupId = toGroupId;
  80. msg.msgId = -1;
  81. msg.mimeType = mineType;
  82. msg.content = content;
  83. msg.sendTime = -1;
  84. msg.type = messageType;
  85. SendMsgRequest request = new SendMsgRequest();
  86. request.rId = current;
  87. request.userId = fromUserId;
  88. request.remoteId = toUserId;
  89. request.groupId = toGroupId;
  90. request.msg = msg;
  91. request.type = messageType;
  92. MessageWrapper wrapper = request.toWrapper();
  93. String json = JSON.toJSONString(wrapper);
  94. kafkaProducer.send(new ProducerRecord(MessageTopic.TOPIC_SEND_MSG, json));
  95. }
  96. // only allow users in white list to send message
  97. private boolean isLegalSender(String userId) {
  98. return true;
  99. }
  100. @Override
  101. public long generateGroupId() {
  102. return groupDAO.generateGroupId();
  103. }
  104. @Override
  105. public void sendSystemMessage(String targetUserId, int systemType, String content) {
  106. SystemMessage systemMessage = new SystemMessage();
  107. systemMessage.systemType = systemType;
  108. systemMessage.content = content;
  109. outboxService.put(targetUserId, systemMessage.toWrapperJson());
  110. }
  111. }