⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timtransport.java

📁 java实现的一个简单组播通信聊天工具
💻 JAVA
字号:
package jgroup;

import java.util.Map;
import java.util.Properties;
import org.jgroups.Address;
import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.jgroups.tests.perf.Receiver;
import org.jgroups.tests.perf.Transport;

public class TimTransport extends ReceiverAdapter implements Transport {
	private JChannel channel = null;
	private String groupName = "TimDemo";
	private Receiver receiver = null;
	String PROTOCOL_STACK_UDP1 = "UDP(bind_addr=192.168.0.132";
	String PROTOCOL_STACK_UDP2 = ";mcast_port=8888";
	String PROTOCOL_STACK_UDP3 = ";mcast_addr=225.1.1.1";
	String PROTOCOL_STACK_UDP4 = ";tos=8;loopback=false;max_bundle_size=6200;"
			+ "use_incoming_packet_handler=true;use_outgoing_packet_handler=false;ip_ttl=2;enable_bundling=true):"
			+ "PING:MERGE2:FD_SOCK:FD:VERIFY_SUSPECT:"
			+ "pbcast.NAKACK(gc_lag=50;max_xmit_size=50000;use_mcast_xmit=false;"
			+ "retransmit_timeout=300,600,1200,2400,4800;discard_delivered_msgs=true):"
			+ "UNICAST:pbcast.STABLE:VIEW_SYNC:"
			+ "pbcast.GMS(print_local_addr=false;join_timeout=3000;"
			+ "join_retry_timeout=2000;"
			+ "shun=true;view_bundling=true):"
			+ "FC(max_credits=2000000;min_threshold=0.10):FRAG2(frag_size=50000)";

	
	public Object getLocalAddress() {
		return channel != null ? channel.getLocalAddress() : null;
	}

	public void start() throws Exception {
		channel.connect(groupName);
	}

	public void stop() {
		if (channel != null) {
			channel.shutdown();
		}
	}

	public void destroy() {
		if (channel != null) {
			channel.close();
			channel = null;
		}
	}

	public void setReceiver(Receiver r) {
		this.receiver = r;
	}

	public Map dumpStats() {
		return channel != null ? channel.dumpStats() : null;
	}

	public void send(Object destination, byte[] payload) throws Exception {
		byte[] tmp = new byte[payload.length];
		//System.arraycopy(payload, 0, tmp, 0, payload.length);
		Message msg = null;
		msg = new Message((Address) destination, null, payload);
		if (channel != null) {
			channel.send(msg);
		}
	}

	public void receive(Message msg) {
		Address sender = msg.getSrc();
		byte[] payload = msg.getBuffer();
		if (receiver != null) {
			try {
				receiver.receive(sender, payload);
			} catch (Throwable tt) {
				tt.printStackTrace();
			}
		}
	}

	public void create(Properties config) throws Exception {
		String PROTOCOL_STACK = PROTOCOL_STACK_UDP1 + PROTOCOL_STACK_UDP2
				+ PROTOCOL_STACK_UDP3 + PROTOCOL_STACK_UDP4;
		channel = new JChannel(PROTOCOL_STACK);
		channel.setReceiver(this);
	}

	public void send(Object destination, byte[] payload, boolean oob)
			throws Exception {
		send(destination, payload);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -