package com.yihu.hos.client;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Airhead
 * @since 2016/11/24.
 */
public class BrokerServerClientTest {
    @Test
    public void invokeSync() throws Exception {
        BrokerServerClient brokerServerClient = new BrokerServerClient("127.0.0.1");
        ClientRequest clientRequest = new ClientRequest();
        clientRequest.setModule("serviceName");
        clientRequest.setMethod("methodName");
        Map<String, String> args = new HashMap<>();
        args.put("arg1", "value1");
        args.put("arg2", "value2");
        clientRequest.setArgs(args);
        ClientResponse clientResponse = brokerServerClient.invokeSync(clientRequest);
        if (clientResponse.getStackTrace() != null) {
            throw new Exception(clientResponse.getError());
        }

        System.out.println(clientResponse.getResult());
    }

    @Test
    public void invokeAsync() throws Exception {
        BrokerServerClient brokerServerClient = new BrokerServerClient("127.0.0.1");
        ClientRequest clientRequest = new ClientRequest();
        clientRequest.setModule("serviceName");
        clientRequest.setMethod("methodName");
        Map<String, String> args = new HashMap<>();
        args.put("arg1", "value1");
        args.put("arg2", "value2");
        clientRequest.setArgs(args);
        brokerServerClient.invokeAsync(clientRequest, null);
    }

    class AsyncCallback implements ResultCallback<ClientResponse>{

        @Override
        public void onReturn(ClientResponse result) {
            if (result.getStackTrace() != null) {
                System.out.println(result.getError().getMessage());
            }

            System.out.println(result.getResult());
        }
    }

}