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

cn.tom.transport.udp.UdpClient Maven / Gradle / Ivy

The newest version!
package cn.tom.transport.udp;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.tom.transport.Client;
import cn.tom.transport.Id;
import cn.tom.transport.IoAdaptor;
import cn.tom.transport.Messager.MessageCallback;
import cn.tom.transport.Ticket;

public class UdpClient implements Client  {
	private static final Logger log = LoggerFactory.getLogger(UdpClientTest.class);
	private UdpSelector selector;
	private String address;
	private UdpSession session;
	private int port;
	private IoAdaptor ioAdaptor;
	private int readTimeout = 20000;
	
	public UdpClient(String address, int port) {
		this.address = address;
		this.port = port;
		start();
	}
	
	
	private void start(){
		 try {
			selector = new UdpSelector("udp-client");
			selector.start();
		} catch (IOException e) {
			log.error("", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void doConnect() throws IOException {
		if (this.session != null) {
			if (this.session.isActive()) {
				return;
			}
		}
		this.session = (UdpSession) selector.registerClient(address, port, ioAdaptor);
	}


	@Override
	public boolean hasConnected() {
		return session!=null && session.isActive();
	}

	@Override
	public void invokeAsync(T req, MessageCallback callback) throws IOException {
		connectIfNeed();
		ioAdaptor.createTicket(req, readTimeout*10, callback);
		session.write(req);
	}
	
	public T invokeSync(T msg) throws IOException, InterruptedException {
		return invokeSync(msg, readTimeout);
	}

	public T invokeSync(T req, int timeout) throws IOException, InterruptedException {
    	Ticket ticket = null; 
		try {
			connectIfNeed();
			ticket = ioAdaptor.createTicket(req, timeout, null); 
			session.write(req);
			if(!ticket.await(timeout, TimeUnit.MILLISECONDS)){
				if(!session.isActive()){
					throw new IOException("Connection reset by peer");
				} else {
					return null;
				}
			}
		    return (T) ticket.response(); 
		} finally{
			if(ticket != null){
				ioAdaptor.removeTicket(ticket.getId());
			}
		}   
    } 
	@Override
	public void send(T msg) throws IOException {
		connectIfNeed();
		session.writeAndFlush(msg);
	}
	
	public void connectIfNeed() throws IOException {
		if (!this.hasConnected()) {
			synchronized (address) {
				if(!this.hasConnected()){
					doConnect();
					heartbeat();
				}
			}
		}
	}

	@Override
	public void setIoAdaptor(IoAdaptor ioAdaptor) {
		this.ioAdaptor = ioAdaptor;
	}
	
	public IoAdaptor getIoAdaptor() {
		return ioAdaptor;
	}
	
	@Override
	public void heartbeat() {
		if(hasConnected()){
			ioAdaptor.heartbeat(session);
		}
	}

	@Override
	public void close() throws IOException {
		if (this.session != null) {
			this.session.close();
		}
		if(selector!=null){
			selector.close();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy