package edu.cmu.cs.gabriel.util;

import android.util.Log;
import edu.cmu.cs.gabriel.Const;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;

/**
 * Created by junjuew on 5/14/16.
 */
public class PingThread extends Thread {
    private static final String LOG_TAG = "Ping Thread";
    private volatile boolean running = true;

    private String serverIP = null;
    private float ping_interval = -1;
    Process p;

    public PingThread(String serverIP, int interval){
    	this.serverIP = serverIP;
        ping_interval = (float) interval / 1000;
        Log.i(LOG_TAG, "ping interval: "+ping_interval);
    }


    public void kill(){
        if (p != null){
            Log.i(LOG_TAG, " kill process");
            p.destroy();
        }
    }

    public void run() {
        if (ping_interval == 0){

        } else {
            Log.i(LOG_TAG, "ping thread running");
            System.out.println("executeCommand");
            Runtime runtime = Runtime.getRuntime();
//            String cmd="su -c ";
//            cmd+="'" + ping_cmd + "'";
//            String[] deviceCommands = {"su", ping_cmd};

            String ping_cmd="/system/bin/ping " +"-i "
                    + String.valueOf(ping_interval) +" " + serverIP;
            Log.i(LOG_TAG, "ping cmd: " + ping_cmd);
            try
            {
                p = runtime.exec("echo");
                DataOutputStream os = new DataOutputStream(p.getOutputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                Thread.sleep(10);
                os.writeBytes(ping_cmd + "\n");
                os.flush();

                // wait until the command is finished
//            cmd = "echo -n 0\n";
//            os.write(cmd.getBytes("ASCII"));
//            os.flush();
//            reader.read();

                while (true) {
                	Log.v(LOG_TAG, "aaa");
                    Thread.sleep(5);
                    Log.v(LOG_TAG, "bbb");
                    String s = null;
                    int idx=0;
                    while ((s = reader.readLine()) != null) {
                        if (idx % 500 != 0){
                            Log.v(LOG_TAG, s);
                        }
                        idx++;
                    }
                }
            }
            catch (InterruptedException ignore)
            {
                ignore.printStackTrace();
                System.out.println(" Exception:"+ignore);
            }
            catch (IOException e)
            {
                e.printStackTrace();
                System.out.println(" Exception:"+e);
            }
        }
        Log.i(LOG_TAG, "ping thread finished");
    }


}

@@ -1084,83 +1084,8 @@
 ();%0A
-//            String cmd=%22su -c %22;%0A//            cmd+=%22'%22 + ping_cmd + %22'%22;
 %0A// 
@@ -1256,32 +1256,90 @@
 %22 %22 + serverIP;%0A
+            //ping_cmd = %22su -c %22 + %22'%22 + ping_cmd + %22'%22;%0A
             Log.
@@ -1439,20 +1439,18 @@
 e.exec(%22
-echo
+su
 %22);%0A    
@@ -2008,109 +2008,26 @@
     
-%09Log.v(LOG_TAG, %22aaa%22);%0A                    Thread.sleep(5);%0A                    Log.v(LOG_TAG, %22bbb%22
+    Thread.sleep(5
 );%0A 
@@ -2192,18 +2192,19 @@
 dx %25 500
- !
+0 =
 = 0)%7B%0A  

Fix background pinging