// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package thrifttest.client;

import java.util.stream.IntStream;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import thrifttest.gen.Data;
import thrifttest.gen.Test;

public class Client {
    private static Test.Client getClient(String host, int port) throws Exception {
        TTransport transport = new TSocket(host, port);
        transport.open();
        return new Test.Client(new TBinaryProtocol(transport));
    }
    private static void testFunctional(String host, int port) throws Exception {
        Test.Client tst = getClient(host, port);
        int a = 123;
        int b = 456;
        int c = tst.add(a,  b);
        System.out.println(c);
        String s = "ABC";
        String s2 = tst.dup(s);
        System.out.println(s2);
        Data d = new Data(123, "ABC");
        Data d2 = tst.process(d);
        System.out.printf("%d %s\n", d2.getIv(), d2.getSv());
        tst.getOutputProtocol().getTransport().close();
    }
    private static void testInstantiation(String host, int port) throws Exception {
        Test.Client tst1 = getClient(host, port);
        for(int i = 0; i < 2; i++) {
            int n = tst1.getCounter();
            System.out.println(n);
        }
        tst1.getOutputProtocol().getTransport().close();
        Test.Client tst2 = getClient(host, port);
        for(int i = 0; i < 2; i++) {
            int n = tst2.getCounter();
            System.out.println(n);
        }
        tst2.getOutputProtocol().getTransport().close();
    }
    private static final int REP = 100000;
    private static void testPerformance(String host, int port) throws Exception {
        Test.Client tst = getClient(host, port);
        long t1 = System.currentTimeMillis();
        IntStream.range(0, REP).forEach(i -> { try { tst.noop(); } catch(TException ex) { } }); // not parallel as Thrift clients are not thread safe
        long t2 = System.currentTimeMillis();
        System.out.printf("%d requests per second\n", REP * 1000 / (t2 - t1));
        tst.getOutputProtocol().getTransport().close();
    }
    private static void test(String lbl, String host, int port) throws Exception {
        System.out.println(lbl + ":");
        testFunctional(host, port);
        testInstantiation(host, port);
        testPerformance(host, port);
    }
    private static final String JAVA_HOST = "localhost";
    private static final int JAVA_PORT = 12345;
    private static final String DOTNET_HOST = "localhost";
    private static final int DOTNET_PORT = 12346;
    private static final String PYTHON_HOST = "localhost";
    private static final int PYTHON_PORT = 12347;
    private static final String CPP_HOST = "localhost";
    private static final int CPP_PORT = 12348;
    public static void main(String[] args) throws Exception {
        test("Java", JAVA_HOST, JAVA_PORT);
        test(".NET", DOTNET_HOST, DOTNET_PORT);
        test("Python", PYTHON_HOST, PYTHON_PORT);
        test("C++", CPP_HOST, CPP_PORT);
    }
}
