
package samplehttp.client;

//Import statements
import java.io.*;

import java.net.*;





class SenderThread extends Thread {

    private String m_host;
    private int m_port;
    private String m_treattime;
    private int m_loops;
    private long m_wait;
    private long m_time;
    private int m_fejb;

    public SenderThread(String host, int port, String treattime, String  wait, int loops, int fejb){
        m_host = host;
        m_port = port;
        m_treattime = treattime;
        Long w = new Long(wait.trim());
        m_wait = w.longValue();
        m_loops = loops;
        m_fejb = fejb;
    }

    public void run() {
        System.out.println("Running a SenderThread...");
        long d_begin = System.currentTimeMillis();
        Sender s = new Sender(m_host,"samplehttp/SampleServlet", m_port, m_treattime, m_fejb );
        try{
            for (int l = 0; l < m_loops; l++) {
                s.execute();
                Thread.currentThread().sleep(m_wait);
            }
        }catch(Exception e) {
            System.out.println(">>>  SenderThread. Exception caught = "+e);
        }
        long d_end = System.currentTimeMillis();
        m_time = d_end - d_begin;
    }

    public long getTime(){
        return m_time;
    }
}

public class SampleClient {


    private static final int m_MAXTHREADS = 500;
    private static int poolsize = 1;
    private static int port;
    private static int loops = 1;
    private static String  wait = "1000";        // Wait between 2 requests default = 1second
    private static String  treattime = "10";     // Treatment time in servlet default = 10 ms
    private static String host;
    private static int   flgejb = 0;

    public static void main(String args[]) {

        // Get command args
        for (int argn = 0; argn < args.length; argn++) {
            String s_arg = args[argn];
            String sarg;
            Integer i_arg;

            if ((argn+1<args.length) && (s_arg.equals("-h") == true)) {
                host = args[++argn];

            } else if ((argn+1<args.length) && (s_arg.equals("-p") == true)) {
                s_arg = args[++argn];
                i_arg = java.lang.Integer.valueOf(s_arg);
                port = i_arg.intValue();
            } else if ((argn+1<args.length) && (s_arg.equals("-n") == true)) {
                s_arg = args[++argn];
                i_arg = java.lang.Integer.valueOf(s_arg);
                poolsize = i_arg.intValue();
            } else if ((argn+1<args.length) && (s_arg.equals("-l") == true)) {
                s_arg = args[++argn];
                i_arg = java.lang.Integer.valueOf(s_arg);
                loops = i_arg.intValue();
            } else if ((argn+1<args.length) && (s_arg.equals("-t") == true)) {
                s_arg = args[++argn];
                treattime = s_arg;
            } else if ((argn+1<args.length) && (s_arg.equals("-w") == true)) {
                s_arg = args[++argn];
                wait = s_arg;

            } else if (s_arg.equals("-ejb") == true) {
                flgejb = 1;            
            } else if (s_arg.equals("-ejbl") == true) {
                flgejb = 2;
            } else if (s_arg.equals("-ejbc") == true) {
                flgejb = 3;
            } else if (s_arg.equals("-?") == true) {
                usage();
                System.exit(0);
            } else {
                usage();
                System.exit(2);
            }
        }

        long tbegin = System.currentTimeMillis();
        // Start pool of client threads
        SenderThread t_thr[] = new SenderThread [m_MAXTHREADS];
        long ttime[]  = new long[m_MAXTHREADS];
        for (int p = 0; p < poolsize; p++) {
            t_thr[p] = new SenderThread(host, port, treattime, wait, loops, flgejb);
            t_thr[p].start();
        }
        for (int p = 0; p < poolsize; p++) {
            try {
                t_thr[p].join();
                ttime[p] = t_thr[p].getTime();

            } catch (InterruptedException e) {
                System.out.println("ERROR: Problem in SenderThread.join():\n"+e);
                System.exit(2);
            }
        }
        long tend = System.currentTimeMillis();
        long time = tend - tbegin;
        System.out.println("Fin de l'essai avec nb clients= "+ poolsize+ " timetowait betwwen requests = "+wait);
        System.out.println("nb loops in client = "+loops+ " treatment time in servlet = "+treattime);
        System.out.print("using ejb = ");
        if (flgejb == 0)
            System.out.println("no");
        if (flgejb == 1)
            System.out.println("yes via remote interface");
        if (flgejb == 2)
            System.out.println("yes via local interface");
        if (flgejb == 3)
            System.out.println("yes via remote interface + getConnection");

        System.out.println("Time : "+time);
    }


    private static void usage() {
        System.out.println("Usage: jclient -cp ...  samplehttp.client.SampleClient <args>");
        System.out.println("  -h  <host>            hostname");
        System.out.println("  -p <port>       port number ");
        System.out.println("  -n <thread>     number of client threads");
        System.out.println("  -t <treatmenttime>     treatment time in servlet");
        System.out.println("  -w <timetowait>     time to wait in thread client");
        System.out.println("  -l <nbloops>     nb loops in each thread client");
        System.out.println("  -ejb       call ejb in servlet");
        System.out.println("  -ejbl      call ejb via local interface in servlet");
        System.out.println("  -ejbc      call ejb + getconnectionin servlet");

    }
 
}

