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

import java.util.Properties;
import java.util.stream.IntStream;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;

import corbatest.common.Data;
import corbatest.common.Test;
import corbatest.common.TestHelper;

public class Client {
    private static Test getStub(String orbhost, int orbport, String name) throws Exception {
        Properties p = new Properties();
        p.put("org.omg.CORBA.ORBClass", "com.sun.corba.se.impl.orb.ORBImpl");
        p.put("org.omg.CORBA.ORBInitialHost", orbhost);
        p.put("org.omg.CORBA.ORBInitialPort", Integer.toString(orbport));
        ORB orb = ORB.init(new String[0], p);
        NamingContext nc = NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
        return TestHelper.narrow(nc.resolve(new NameComponent[] { new NameComponent(name, "") }));
    }
    private static void testFunctional(String orbhost, int orbport, String name) throws Exception {
        Test tst = getStub(orbhost, orbport, name);
        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.iv, d2.sv);
        tst._release();
    }
    private static void testInstantiation(String orbhost, int orbport, String name) throws Exception {
        Test tst1 = getStub(orbhost, orbport, name);
        for(int i = 0; i < 2; i++) {
            int n = tst1.getCounter();
            System.out.println(n);
        }
        tst1._release();
        Test tst2 = getStub(orbhost, orbport, name);
        for(int i = 0; i < 2; i++) {
            int n = tst2.getCounter();
            System.out.println(n);
        }
        tst2._release();
    }
    private static final int REP = 100000;
    private static void testPerformance(String orbhost, int orbport, String name) throws Exception {
        Test tst = getStub(orbhost, orbport, name);
        long t1 = System.currentTimeMillis();
        IntStream.range(0, REP).parallel().forEach(i -> { tst.noop(); });
        long t2 = System.currentTimeMillis();
        System.out.printf("%d requests per second\n", REP * 1000 / (t2 - t1));
        tst._release();
    }
    private static void test(String lbl, String orbhost, int orbport, String name) throws Exception {
        System.out.println(lbl + ":");
        testFunctional(orbhost, orbport, name);
        testInstantiation(orbhost, orbport, name);
        testPerformance(orbhost, orbport, name);
    }
    private static final String JAVASE_HOST = "localhost";
    private static final int JAVASE_PORT = 1050;
    private static final String JAVASE_NAME1 = "TestJ1";
    private static final String JAVASE_NAME2 = "TestJ2";
    private static final String IIOPNET_HOST = "localhost";
    private static final int IIOPNET_PORT = 12345;
    private static final String IIOPNET_NAME = "TestN";
    private static final String OMNIORB_HOST = "localhost";
    private static final int OMNIORB_PORT = 2809;
    private static final String OMNIORB_NAME1 = "TestC1";
    private static final String OMNIORB_NAME2 = "TestC2";
    private static final String OMNIORB_NAME3 = "TestPy";
    public static void main(String[] args) throws Exception {
        test("Java SE ORB and Java Server (old style)", JAVASE_HOST, JAVASE_PORT, JAVASE_NAME1);
        test("Java SE ORB and Java Server (new style)", JAVASE_HOST, JAVASE_PORT, JAVASE_NAME2);
        test("IIOP.NET ORB and .NET Server", IIOPNET_HOST, IIOPNET_PORT, IIOPNET_NAME);
        test("OmniORB ORB and C++ Server (old style)", OMNIORB_HOST, OMNIORB_PORT, OMNIORB_NAME1);
        test("OmniORB ORB and C++ Server (new style)", OMNIORB_HOST, OMNIORB_PORT, OMNIORB_NAME2);
        test("OmniORB ORB and Python Server", OMNIORB_HOST, OMNIORB_PORT, OMNIORB_NAME3);
    }
}
