All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gateway.notify.NotifyProxy Maven / Gradle / Ivy


package com.gateway.notify;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.CopyOnWriteArraySet;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.app.common.thread.AsyncSeqThreadGroup;
import com.gateway.connector.proto.Proto;
import com.gateway.connector.tcp.TcpConnector;
import com.gateway.invoke.TopicManager;
import com.gateway.invoke.filter.IFilterTopic;
import com.gateway.utils.ProtoUtils;
import com.gateway.utils.TopicUtils;

public class NotifyProxy {

	protected Logger logger = LoggerFactory.getLogger(NotifyProxy.class);
	
	private TopicManager topicManager;

	public TopicManager getTopicManager() {
		return topicManager;
	}

	public void setTopicManager(TopicManager topicManager) {
		this.topicManager = topicManager;
	}

	private TcpConnector tcpConnector;
	private AsynNotify asynNotify = new AsynNotify(Runtime.getRuntime().availableProcessors() * 2);

	public void setTcpConnector(TcpConnector tcpConnector) {
		this.tcpConnector = tcpConnector;
	}

	public class notifyMsg {
		public String sid;
		public String topic;
		public String content;
	}

	public class AsynNotify extends AsyncSeqThreadGroup {

		public AsynNotify(int asyncBaseNum) {
			super(asyncBaseNum);
		}

		@Override
		protected void process(notifyMsg msg) {
			xNotify(msg);
		}
	}

	public void notify(String topic, String content) {
		notifyMsg msg = new notifyMsg();
		msg.topic = topic;
		msg.content = content;
		
		asynNotify.add(topic, msg);

	}


	public void xNotify(notifyMsg msg) {
		String sid = msg.sid;
		if (StringUtils.isEmpty(sid)) {
			broadcastNotify(msg);
		} else {
			p2pNotify(msg);
		}
	}

	public void broadcastNotify(notifyMsg msg) {

		String topic = msg.topic;
		String content = msg.content;
		byte[] body = null;
		for (Entry> entry : topicManager.getTopics().entrySet()) {
			String sid = entry.getKey();
			CopyOnWriteArraySet cas = entry.getValue();
			for (String stopic : cas) {
				if (TopicUtils.matchTopic(stopic, topic)) {
					if (body == null) {
						body = getBody(topic, content);
					}
					Proto proto = ProtoUtils.generateNotify(sid, body);
					try {
						String userName=tcpConnector.send(sid, proto);
						logger.debug("{},{},{},{}", sid,userName, topic, content);
					} catch (Exception e) {
						logger.error("notify error", e);
					}
				}
			}
		}

	}

	private byte[] getBody(String topic, String content) {
		HashMap hm = new HashMap<>();
		hm.put("topic", topic);
		hm.put("content", content);
		byte[] body = JSON.toJSONBytes(hm);
		return body;
	}

	public void p2pNotify(notifyMsg msg) {
		String sid = msg.sid;
		String topic = msg.topic;
		String content = msg.content;
		byte[] body = getBody(topic, content);
		Proto proto = ProtoUtils.generateNotify(sid, body);
		try {
			if (tcpConnector.exist(sid)) {
				String userName=tcpConnector.send(sid, proto);
				logger.debug("{},{},{},{}", sid,userName, topic, content);
			}
		} catch (Exception e) {
			logger.error("notify error", e);
		}
	}

	public void notify(String sid, String topic, String content) {
		notifyMsg msg = new notifyMsg();
		msg.sid = sid;
		msg.topic = topic;
		msg.content = content;
		asynNotify.add(topic, msg);

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy