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

org.caiguoqing.uyuni.rpc.client.socketclient.SocketClient Maven / Gradle / Ivy

The newest version!
/**
 * 
 */
package org.caiguoqing.uyuni.rpc.client.socketclient;

import java.lang.reflect.Proxy;

import org.caiguoqing.uyuni.rpc.client.BaseClient;
import org.caiguoqing.uyuni.rpc.client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author org.caiguoqing
 * 
 */
public class SocketClient extends BaseClient implements Client {
	private Logger logger = LoggerFactory.getLogger(SocketClient.class);
	private static SocketClient client = new SocketClient();
	
	private SocketClient(){
		
	}
	
	public static SocketClient getInstance(){
		return client;
	}
	
	@SuppressWarnings("unchecked")
	public  T refer(final Class referInterface,String host,int port) throws Exception{
		if(referInterface == null) {
			throw new IllegalArgumentException("Interface is null"); 
		}
		if(!referInterface.isInterface()){
			throw new IllegalArgumentException("The " + referInterface.getName() + " is not interface"); 
		}
		if (host == null || host.length() == 0)
           throw new IllegalArgumentException("Host is null");  
       if (port <= 0 || port > 65535)  
           throw new IllegalArgumentException("Invalid port " + port);  
       logger.info("refer " + referInterface.getName() + " from server " 
           + host + ":" + port);
       
       String key = host+":"+port;
       SocketProxyHandler handler = hosts.get(key);
       if(handler == null){
    	   handler = new SocketProxyHandler(host, port, interCount);
    	   hosts.put(key,handler);
       }
       
       return (T) Proxy.newProxyInstance(referInterface.getClassLoader(), 
       		new Class[] {referInterface},handler);
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy