import java.net.DatagramPacket;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class evo_udp{
    Thread thread;
    DatagramSocket socket;
    String test = new String();
    
    
    public static void main(String[] args) {
        evo_udp u = new evo_udp();
    }
   

    public evo_udp(){
        new StartThread();
    }
    public class StartThread implements Runnable{
        StartThread(){
            thread = new Thread(this);
            thread.start();
        }
        public void StopThread() {
            thread.interrupted();
            socket.close();
        }
        
        public void run(){
            try{
                // receive buffer
                byte[] buffer = new byte[20];
                // send buffer
                byte[] sbuffer = new byte[20];
                // listen to this port
                int port = 1235;
                String c = null;
                int ret = 0;
                
                //try executing command and return value
                try{
                    //run the command echo 66 see if 66 comes back on windows
                    //Process p = Runtime.getRuntime().exec(
                      //      "cmd /c e:\\runme 66");
                     
                    
                    Process p = Runtime.getRuntime().exec(
                            "./check_evo.bat");
                    
                    //System.out.println(p);                    
                    BufferedReader stdInput = new BufferedReader(new
                            InputStreamReader(p.getInputStream()));
                    
                    BufferedReader stdError = new BufferedReader(new
                            InputStreamReader(p.getErrorStream()));
                    
                    //read the output from the command
                    System.out.println("Here is the standard output of the command:\n");
                    while ((c = stdInput.readLine()) != null) {
                        //System.out.println("Here 3:\n");
                        System.out.println(c);
                        ret = Integer.valueOf(c).intValue();
                        //System.out.println("Here 4:\n");
                    }
                    System.out.println("Here 1:\n");
                    
                    //read any errors from the attempted command
                    while ((c = stdError.readLine()) != null) {
                        System.out.println("Here is the standard error of the command (if any):\n");
                        System.out.println(c);
                    }
                    System.out.println("Here 2:\n");
                    //do I need this?
                    //System.exit(0);
                }
                catch (IOException e) {
                    System.out.println("Exception happened - here's what I know: ");
                    e.printStackTrace();
                    //do I need this?
                    //System.exit(-1);
                }

            try{
                    socket = new DatagramSocket(port);
                    
                    while(true){
                        try{
                            for (int i=0; i<11; i++ ) { 
                                buffer[i]=0; 
                            }
                            DatagramPacket packet = new DatagramPacket(buffer, buffer.length );
                            socket.receive(packet);
                            InetAddress client = packet.getAddress();
                            byte[] iByte = null;
                            iByte=client.getAddress();
                            int client_port = packet.getPort();
                            
                            // uncoment next few System.out lines to see what is received and from who
                            String r = String.format("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", buffer[0], buffer[1],buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[9], buffer[10], buffer[11]);
                            String from = new String();
                            
                            String test = new String(buffer);
                            String ip = new String();
                            from = "From "+client +" " +client_port;
                            test = test.substring(0,4);

                            // set ip to first 7 characters of ip
                            ip = client.getHostAddress().substring(0,7);
                            
                            //display to screen From IP and Port
                            //System.out.println(from);
                            
                            // display to screen data received in hex
                            //System.out.println(r);
                            
                            // check for END sent from client to exit application
                            if (test.equals("STOP")) {
                                System.out.println("UDP Listener Stopped");
                                StopThread();
                            }
                            
                            //set up send back what was received except first 4 bytes
                            int temp = 0;
                            temp=0x000000FF & ((int)buffer[4]);
                            sbuffer[0] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[5]);
                            sbuffer[1] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[6]);
                            sbuffer[2] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[7]);
                            sbuffer[3] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[8]);
                            sbuffer[4] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[9]);
                            sbuffer[5] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[10]);
                            sbuffer[6] = (byte)temp;
                            temp=0x000000FF & ((int)buffer[11]);
                            sbuffer[7] = (byte)temp;
                            
                            //sent data has from ip
                            sbuffer[8] = iByte[0];
                            sbuffer[9] = iByte[1];
                            sbuffer[10] = iByte[2];
                            sbuffer[11] = iByte[3];
                                                        
                            //sent data has from port here
                            sbuffer[12]=(byte)((client_port & 0x0000ff00)>>>8);  //buf[2] = 51;
                            sbuffer[13]=(byte)((client_port & 0x000000ff));      //buf[3] = -97;  bytes are signed.

                            //pad some junk
                            temp=0x000000FF & ((int)0);
                            sbuffer[14] = (byte)temp;
                            temp=0x000000FF & ((int)66);
                            sbuffer[15] = (byte)temp;
                            
                            //# of users 
                            //all with 255 will send ? to users 
                            //for number of users on server
                            //need to find a way to count # of users
                            //already are connected to server
                            //and pass that here
                            temp=0x000000FF & ((int)0);
                            sbuffer[16] = (byte)temp;
                            temp=0x000000FF & ((int)0);
                            sbuffer[17] = (byte)temp;
                            temp=0x000000FF & ((int)0);
                            sbuffer[18] = (byte)temp;
                            temp=0x000000FF & ((int)0);
                            //sbuffer[19] = (byte)temp;
                            sbuffer[19]=(byte)ret;
                            
                            DatagramPacket sndPkg = new DatagramPacket(sbuffer, 20, client, client_port);
                            
                            // send the data back
                            // easy way to stop certain ip's to enter
                            if (ip.substring(0,3).equals("x.xx")) {
                                    continue;
                            }
                            else {
                                    // ok now send if not above ip's'
                                    socket.send(sndPkg);
                            }
                            // uncomment next system.out few lines to see what is sent back
                            String to = new String();
                            to = "To "+client+" "+client_port;
                            String s = String.format("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", sbuffer[0], sbuffer[1],sbuffer[2], sbuffer[3], sbuffer[4], sbuffer[5], sbuffer[6], sbuffer[7], sbuffer[8], sbuffer[9], sbuffer[10], sbuffer[11], sbuffer[12], sbuffer[13], sbuffer[14], sbuffer[15], sbuffer[16], sbuffer[17], sbuffer[18], sbuffer[19]);

                            //display the to info
                            //System.out.println(to);
                            
                            //display the string being sent
                            //System.out.println(s);
                        } 
                        catch(UnknownHostException ue){}
                    }
                } 
                catch(java.net.BindException b){}
            } 
            catch (IOException e){
                System.err.println(e);
            }
        }
    }
}

