/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.machinist.server.hardware;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import org.machinist.client.hardware.GPSservice;
import java.net.*;

/**
 *
 * @author BURKAY
 */
public class GPSserviceImpl extends RemoteServiceServlet implements GPSservice {

    private static String stream = "";
    boolean firstRun = true;

    public String myMethod() {
        if (firstRun) {
            try {
                connect();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            firstRun = false;
        }
        return (stream);
    }

    void connect() throws Exception {
        //PS:Port numbers can change

        int port = 5000;

        DatagramSocket socket = new DatagramSocket(port); //Train1
        DatagramSocket socket2 = new DatagramSocket(port + 1); //Acc1

        DatagramSocket socket3 = new DatagramSocket(port + 2); //Train2
        //DatagramSocket socket4 = new DatagramSocket(port + 3); //Acc2

        DatagramSocket socket5 = new DatagramSocket(port + 4); //Train3
        //DatagramSocket socket6 = new DatagramSocket(port + 5); //Acc3

        byte[] buf = new byte[256];
        byte[] buf2 = new byte[256];
        byte[] buf3 = new byte[256];
        //byte[] buf4 = new byte[256];
        byte[] buf5 = new byte[256];
        //byte[] buf6 = new byte[256];

        while (true) {
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            DatagramPacket packet2 = new DatagramPacket(buf2, buf2.length);
            DatagramPacket packet3 = new DatagramPacket(buf3, buf3.length);
          //DatagramPacket packet4 = new DatagramPacket(buf4, buf4.length);
            DatagramPacket packet5 = new DatagramPacket(buf5, buf5.length);
          //DatagramPacket packet6 = new DatagramPacket(buf6, buf6.length);
            socket.receive(packet);
            socket2.receive(packet2);
            socket3.receive(packet3);
          //socket4.receive(packet4);
            socket5.receive(packet5);
          //socket6.receive(packet6);
            String received = new String(packet.getData(), 0, packet.getLength());
            String received2 = new String(packet2.getData(), 0, packet2.getLength());
            String received3 = new String(packet3.getData(), 0, packet3.getLength());
          //String received4 = new String(packet4.getData(), 0, packet4.getLength());
            String received5 = new String(packet5.getData(), 0, packet5.getLength());
          //String received6 = new String(packet6.getData(), 0, packet6.getLength());

            stream = received + " | " + received2 + " | " + received3 + " | " + received5;
        }
    }
}


