package com.xxl.rpc.sample.client;

import com.xxl.rpc.remoting.invoker.XxlRpcInvokerFactory;
import com.xxl.rpc.remoting.invoker.call.CallType;
import com.xxl.rpc.remoting.invoker.call.XxlRpcInvokeCallback;
import com.xxl.rpc.remoting.invoker.call.XxlRpcInvokeFuture;
import com.xxl.rpc.remoting.invoker.reference.XxlRpcReferenceBean;
import com.xxl.rpc.remoting.invoker.route.LoadBalance;
import com.xxl.rpc.remoting.net.impl.netty.client.NettyClient;
import com.xxl.rpc.sample.api.DemoService;
import com.xxl.rpc.sample.api.dto.UserDTO;
import com.xxl.rpc.serialize.impl.HessianSerializer;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author xuxueli 2018-10-21 20:48:40
 */
public class XxlRpcClientAplication {

	public static void main(String[] args) throws Exception {

		// test
		testSYNC();
		testFUTURE();
		testCALLBACK();
		testONEWAY();

		TimeUnit.SECONDS.sleep(2);

		// stop client invoker factory (default by getInstance, exist inner thread, need destory)
		XxlRpcInvokerFactory.getInstance().stop();



		// init invoker factory
		/*XxlRpcInvokerFactory invokerFactory = new XxlRpcInvokerFactory();
		invokerFactory.start();*/

		/*String serviceKey = XxlRpcProviderFactory.makeServiceKey(DemoService.class.getName(), null);		// registry local
		invokerFactory.getServiceRegistry().registry(new HashSet<String>(Arrays.asList(serviceKey)), "127.0.0.1:7080");*/

		// test

		// stop invoker factory
		/*invokerFactory.stop();*/

	}



	/**
	 * CallType.SYNC
	 */
	public static void testSYNC() throws Exception {
		// init client
		XxlRpcReferenceBean referenceBean = new XxlRpcReferenceBean();
		referenceBean.setClient(NettyClient.class);
		referenceBean.setSerializer(HessianSerializer.class);
		referenceBean.setCallType(CallType.SYNC);
		referenceBean.setLoadBalance(LoadBalance.ROUND);
		referenceBean.setIface(DemoService.class);
		referenceBean.setVersion(null);
		referenceBean.setTimeout(500);
		referenceBean.setAddress("127.0.0.1:7080");
		referenceBean.setAccessToken(null);
		referenceBean.setInvokeCallback(null);
		referenceBean.setInvokerFactory(null);

		DemoService demoService = (DemoService) referenceBean.getObject();

		// test
        UserDTO userDTO = demoService.sayHi("[SYNC]jack");
		System.out.println(userDTO);


		// test mult
		/*int count = 100;
		long start = System.currentTimeMillis();
		for (int i = 0; i < count; i++) {
			UserDTO userDTO2 = demoService.sayHi("[SYNC]jack"+i );
			System.out.println(i + "##" + userDTO2.toString());
		}
		long end = System.currentTimeMillis();
    	System.out.println("run count:"+ count +", cost:" + (end - start));*/

	}


	/**
	 * CallType.FUTURE
	 */
	public static void testFUTURE() throws Exception {
		// client test
		XxlRpcReferenceBean referenceBean = new XxlRpcReferenceBean();
		referenceBean.setClient(NettyClient.class);
		referenceBean.setSerializer(HessianSerializer.class);
		referenceBean.setCallType(CallType.FUTURE);
		referenceBean.setLoadBalance(LoadBalance.ROUND);
		referenceBean.setIface(DemoService.class);
		referenceBean.setVersion(null);
		referenceBean.setTimeout(500);
		referenceBean.setAddress("127.0.0.1:7080");
		referenceBean.setAccessToken(null);
		referenceBean.setInvokeCallback(null);
		referenceBean.setInvokerFactory(null);

		DemoService demoService = (DemoService) referenceBean.getObject();

		// test
		demoService.sayHi("[FUTURE]jack" );
        Future<UserDTO> userDTOFuture = XxlRpcInvokeFuture.getFuture(UserDTO.class);
		UserDTO userDTO = userDTOFuture.get();

		System.out.println(userDTO.toString());
	}


	/**
	 * CallType.CALLBACK
	 */
	public static void testCALLBACK() throws Exception {
		// client test
		XxlRpcReferenceBean referenceBean = new XxlRpcReferenceBean();
		referenceBean.setClient(NettyClient.class);
		referenceBean.setSerializer(HessianSerializer.class);
		referenceBean.setCallType(CallType.CALLBACK);
		referenceBean.setLoadBalance(LoadBalance.ROUND);
		referenceBean.setIface(DemoService.class);
		referenceBean.setVersion(null);
		referenceBean.setTimeout(500);
		referenceBean.setAddress("127.0.0.1:7080");
		referenceBean.setAccessToken(null);
		referenceBean.setInvokeCallback(null);
		referenceBean.setInvokerFactory(null);

		DemoService demoService = (DemoService) referenceBean.getObject();


        // test
        XxlRpcInvokeCallback.setCallback(new XxlRpcInvokeCallback<UserDTO>() {
            @Override
            public void onSuccess(UserDTO result) {
                System.out.println(result);
            }

            @Override
            public void onFailure(Throwable exception) {
                exception.printStackTrace();
            }
        });

        demoService.sayHi("[CALLBACK]jack");
	}


	/**
	 * CallType.ONEWAY
	 */
	public static void testONEWAY() throws Exception {
		// client test
		XxlRpcReferenceBean referenceBean = new XxlRpcReferenceBean();
		referenceBean.setClient(NettyClient.class);
		referenceBean.setSerializer(HessianSerializer.class);
		referenceBean.setCallType(CallType.ONEWAY);
		referenceBean.setLoadBalance(LoadBalance.ROUND);
		referenceBean.setIface(DemoService.class);
		referenceBean.setVersion(null);
		referenceBean.setTimeout(500);
		referenceBean.setAddress("127.0.0.1:7080");
		referenceBean.setAccessToken(null);
		referenceBean.setInvokeCallback(null);
		referenceBean.setInvokerFactory(null);

		DemoService demoService = (DemoService) referenceBean.getObject();

		// test
        demoService.sayHi("[ONEWAY]jack");
	}

}

@@ -808,16 +808,250 @@
 tion %7B%0A%0A
+%09%09/*String serviceKey = XxlRpcProviderFactory.makeServiceKey(DemoService.class.getName(), null);%0A%09%09XxlRpcInvokerFactory.getInstance().getServiceRegistry().registry(new HashSet%3CString%3E(Arrays.asList(serviceKey)), %22127.0.0.1:7080%22);*/%0A%0A
 %09%09// tes
@@ -1289,435 +1289,8 @@
 );%0A%0A
-%0A%0A%09%09// init invoker factory%0A%09%09/*XxlRpcInvokerFactory invokerFactory = new XxlRpcInvokerFactory();%0A%09%09invokerFactory.start();*/%0A%0A%09%09/*String serviceKey = XxlRpcProviderFactory.makeServiceKey(DemoService.class.getName(), null);%09%09// registry local%0A%09%09invokerFactory.getServiceRegistry().registry(new HashSet%3CString%3E(Arrays.asList(serviceKey)), %22127.0.0.1:7080%22);*/%0A%0A%09%09// test%0A%0A%09%09// stop invoker factory%0A%09%09/*invokerFactory.stop();*/%0A%0A
 %09%7D%0A%0A

update document