/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package web;



import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;



public class udpListener {



    public static void main(String[] args) throws IOException {

		DatagramSocket gpssocket = new DatagramSocket(5400);
		DatagramSocket accsocket = new DatagramSocket(5401);

		byte[] buf1 = new byte[256];
                byte[] buf2 = new byte[256];

while(true){
                DatagramPacket packet1 = new DatagramPacket(buf1, buf1.length);
                DatagramPacket packet2 = new DatagramPacket(buf2, buf2.length);
                gpssocket.receive(packet1);
                accsocket.receive(packet2);
                String receivedgps = new String(packet1.getData(), 0, packet1.getLength());
                String receivedacc = new String(packet2.getData(), 0, packet2.getLength());
                System.out.println(receivedgps+" | "+receivedacc);
                String[] gps = receivedgps.split("\\|");
                String[] acc = receivedacc.split("\\|");
                if(Double.parseDouble(acc[0])>40 || Double.parseDouble(acc[1])>40 ||
                        Double.parseDouble(acc[0])<-40 || Double.parseDouble(acc[1])<-40){
                    System.out.println("Devrilen tren: "+gps[0]);

                    Emergency E=new Emergency(gps[0].trim());
                try {
                    E.processRequest(null, null);

                } catch (ServletException ex) {
                    Logger.getLogger(udpListener.class.getName()).log(Level.SEVERE, null, ex);
                }

/*
                    URL servlet = new URL("http://localhost:8080/NewsApp-war/Emergency");
                    URLConnection conn=servlet.openConnection();
                    conn.setDoOutput(true);
                    OutputStream stream = conn.getOutputStream();
                    stream.write(Integer.parseInt(gps[0].trim()));
                    stream.close();
                    
*/


                    break;


                    }
            }
        }
}