/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testclient;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import trueguidelibrary.TrueGuideLibrary;

/**
 *
 * @author MY
 */
public class TestClient extends Thread {

    static int count = 0;
    static int i = 100;
    static String tlvString = "";
    static String ipport = "";

    /**
     * @param args the command line arguments
     */
    public void run() {

        thread_fn();
    }

    static void thread_fn() {
        try {
            // TODO code application logic here
            TrueGuideLibrary tg = new TrueGuideLibrary();
            tg.tgVer = 2;
            Date dt1 = new Date();
            tg.Hostnames[0] = ipport;
            System.out.println("Connect started");

            tg.connect();
            System.out.println("Connect Ended");
            Date dt = new Date();
            int count_itr = i, iosize = 0;
            while (count_itr > 0) {
                //String tlvString="select usrname,mobno from trueguide.tusertbl where mobno='9945950646'";
                int type = 709;
          //  tlvString="2#Niyaz#Niyaz , Best Briyani of Belgaum#-1";
                // type=1010;
                System.out.println("wirePush Started");
                tg.wirePush(type, tlvString.length(), tlvString);

                System.out.println("wirePush Ended -->" + count_itr);
                List GetValuesFromTbl = tg.GetValuesFromTbl("X^1_1");

                //if(GetValuesFromTbl !=null)
                //            System.out.println("GetValuesFromTbl="+GetValuesFromTbl.size()+" Len="+GetValuesFromTbl);
                //System.out.println("RCVBUFF="+tg.rcv_buff);
                count_itr--;
            }
            tg.disconnect_connection();
            Date eddt = new Date();
//            synchronized(this){
            System.out.println("thread is running..." + (count));
            System.out.println("dt1=" + dt1 + " dt=" + dt + " eddt=" + eddt + " IOSIZE=" + tg.rcv_buff.length());
            count++;
  //          }

        } catch (IOException ex) {
            Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    boolean save_JArray(String key,JSONArray jsonArray)
    {
        if(jsonArray == null) return false;
         JSONObject obj = new JSONObject();
         try {
             obj.put("result", jsonArray);
             String jstr=obj.toString();
         } catch (JSONException e) {
           e.printStackTrace();
           return false;
        }
        return true;
    }
    JSONArray getJArray(String key)
    {
        String js="";
        JSONObject obj=new JSONObject(js);
        JSONArray jr = (JSONArray) obj.get("result");
        return jr;
    }
    
public static String format_time() {
    long epoch = System.currentTimeMillis();

    SimpleDateFormat sdf = new SimpleDateFormat("HH_mm_ss_SSS");
    return "Time-"+sdf.format(new Date(epoch));
}
    
    public static void main(String[] args) throws JSONException {
        System.out.println("TIME:"+format_time());

        
        NewJPanel x=new NewJPanel();
        x.setVisible(true);
/*
        JSONObject jsonObj = new JSONObject();
         JSONArray jsonArray = new JSONArray();
         for(int i=0;i<10;i++){
             jsonObj = new JSONObject();
         jsonObj.put("Num", "123"+i);
         jsonObj.put("STEPOCH", "123"+i);
         jsonObj.put("ENDEPOCH", "123"+i);
         jsonObj.put("RINGS", "123"+i);
         jsonObj.put("ROUND", i+"");
         jsonArray.put(jsonObj);
         }
         String json=jsonArray.toString();
         JSONObject obj = new JSONObject();

try {
    obj.put("result", jsonArray);
    System.out.println("RES="+obj.toString());
             jsonArray = (JSONArray) obj.get("result");
             System.out.println("RES="+jsonArray.toString());
             int ln=jsonArray.length();
             String js=obj.toString();
             JSONObject Jobj=new JSONObject(js);
             
             
             JSONObject jsonObject = jsonArray.getJSONObject(0);
             String ENDEPOCH=jsonObject.getString("ENDEPOCH");
             System.out.println("ENDEPOCH="+ENDEPOCH+"  ln="+ln);
} catch (JSONException e) {
    e.printStackTrace();
}
         
  */       
         
         
/*
        int countx = 0;
        int argsLen = args.length;
        if (argsLen != 4) {
            System.out.println("./testClient.jar <Threads> <RunsPerThread> <CommandToExecute> <IP:PORT>");
            System.out.println("java -jar testClient.jar 1 2 \"select usrname,mobno from trueguide.tusertbl where mobno='9945950646'\", anthropic.in:3336");
            countx = 1;
            i = 1;
            tlvString = "select usrname,mobno from trueguide.tusertbl where mobno='9945950646'";
            ipport = "anthropic.in:4445";
            //   System.exit(1);
        } else {
            System.out.println("args=" + args[0] + " " + args[1] + " " + args[2] + " ");

            countx = Integer.parseInt(args[0]);
            i = Integer.parseInt(args[1]);
            tlvString = args[2];
            ipport = args[3];
        }
        System.out.println("count=" + countx + " i=" + i + " tlvString=" + tlvString);
        int j = 0;
        while (j < countx) {
        //TestClient t1=new TestClient();  
            //t1.start(); 
            thread_fn();
            j++;
            //System.out.println("count="+countx+" j="+j);
        }
        */
        


    }

}
