//package demo;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;

class DevChangeThread extends Thread {

    DatagramSocket socket;
    boolean ready=false;

    public void close()
    {

        //you have to wait till the window is set up 
        while( ready==false){
           try{
                Thread.sleep(1);
            }
            catch (InterruptedException ex)
            {
            
            }
        }
        socket.close();

    }

    @Override
    public void run() {
        super.run();

        byte[] buffer = new byte[128];

            try{            
                socket = new DatagramSocket(null);
                socket.setReuseAddress(true);
                socket.bind(new InetSocketAddress("127.0.0.1",10000));
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

        try{
            ELGenji.RegisterGenjiCallbackPort(10000);

            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
            ready=true;

            while(true){
                socket.receive(packet);

                String receivedMessage = new String(packet.getData(), 0, packet.getLength());
                System.out.println("Genji DevChange:" + receivedMessage);
            }

        }
        catch (SocketException e)
        {
            if (e.getMessage().contains("Socket closed"))
            {
               
            }
             else 
             {
                e.printStackTrace();
            
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }
}

public class DemoMain2{

    public static void main(String[] args) {
        DevChangeThread aThread = new DevChangeThread();
        aThread.start();

        new Demo(1).start();

        aThread.close();
    }
}


