package org.craft.atom.rpc;

import java.io.IOException;
import java.net.InetSocketAddress;

import lombok.Getter;
import lombok.Setter;

import org.craft.atom.rpc.api.RpcClient;
import org.craft.atom.rpc.spi.RpcConnector;
import org.craft.atom.rpc.spi.RpcProtocol;

/**
 * @author mindwind
 * @version 1.0, Aug 14, 2014
 */
public class DefaultRpcClient implements RpcClient {

	
	@Getter @Setter private String       host                  ;
	@Getter @Setter private int          port                  ;
	@Getter @Setter private int          heartbeatInMillis     ;
	@Getter @Setter private int          connectTimeoutInMillis;
	@Getter @Setter private RpcConnector connector             ; 
	@Getter @Setter private RpcProtocol  protocol              ;
	
	
	// ~ -------------------------------------------------------------------------------------------------------------
	
	
	public DefaultRpcClient() {
		connectTimeoutInMillis = Integer.MAX_VALUE;
		connector = new DefaultRpcConnector();
		protocol  = new DefaultRpcProtocol();
		init();
	}
	
	public void init() {
		connector.setProtocol(protocol);
		connector.setAddress(new InetSocketAddress(host, port));
		connector.setHeartbeatInMillis(heartbeatInMillis);
		connector.setConnectTimeoutInMillis(connectTimeoutInMillis);
	}
	
	
	// ~ -------------------------------------------------------------------------------------------------------------
	
	
	@Override
	public <T> T refer(Class<T> interfaceClass) {
		// TODO
		return null;
	}


	@Override
	public long connect() throws IOException {
		return connector.connect();
	}


	@Override
	public void disconnect(long connectionId) throws IOException {
		disconnect(connectionId);
	}

}

@@ -258,16 +258,63 @@
 rotocol;
+%0Aimport org.craft.atom.rpc.spi.RpcProxyFactory;
 %0A%0A/**%0A *
@@ -459,16 +459,19 @@
 g       
+   
 host    
@@ -524,16 +524,19 @@
         
+   
 port    
@@ -589,16 +589,19 @@
         
+   
 heartbea
@@ -654,16 +654,19 @@
         
+   
 connectT
@@ -719,16 +719,19 @@
 nnector 
+   
 connecto
@@ -785,16 +785,19 @@
 otocol  
+   
 protocol
@@ -813,16 +813,79 @@
      ;%0A%09
+@Getter @Setter private RpcProxyFactory proxyFactory          ;
 %0A%09%0A%09// ~
@@ -1041,124 +1041,225 @@
 nect
-TimeoutInMillis = Integer.MAX_VALUE;%0A%09%09connector = new DefaultRpcConnector();%0A%09%09protocol  = new DefaultRpcProtocol()
+or              = new DefaultRpcConnector()   ;%0A%09%09protocol               = new DefaultRpcProtocol()    ;%0A%09%09proxyFactory           = new DefaultRpcProxyFactory();%0A%09%09connectTimeoutInMillis = Integer.MAX_VALUE           
 ;%0A%09%09
@@ -1694,29 +1694,52 @@
 %7B%0A%09%09
-// TODO%0A%09%09return null
+return proxyFactory.getProxy(interfaceClass)
 ;%0A%09%7D

improve - implement refer()