Package logic

Source Code of logic.CaptureThread

package logic;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;

import messages.PlaneDataObject;
import messages.planesim.SimDataObject;

/**
* Diese Klasse enthaelt 2 Threads, die auf eingehende Daten simulierter Flugzeuge sowie Daten von X-Plane lauschen und diese zur Verarbeitung weiterreichen.
*
* @author Rainer Keller
*/

public class CaptureThread {
    private UdpParser parser;
    private ATMServer server;
    /**
     * Konstruktor
     * @param server Hauptklasse des Servers
     */
    public CaptureThread(ATMServer server){
        this.server=server;
        parser = new UdpParser();
        new UdpListener();
        new SimPlaneListener();
    }
    /**
     * Thread der auf simulierte Flugzeuge hoert
     */
    public class SimPlaneListener extends Thread {
        ServerSocket serverSocket;
        Socket clientSocket = null;
        ObjectInputStream in;
        boolean run=true;
       
        public SimPlaneListener() {
           
            try {
                serverSocket = new ServerSocket(48000);
                clientSocket = serverSocket.accept();
                in = new ObjectInputStream(clientSocket.getInputStream());
            } catch (IOException e) {
                System.out.println(e);
            }
            System.out.println("Receiving Planes: ");
            start();
        }
       
        @Override
        public void run() {

            while (run) {
                SimDataObject so;
                try {
                    so  = (SimDataObject) in.readObject();
                    PlaneDataObject pdo = new PlaneDataObject(so.getName(),"simulator",so.getLat(),so.getLon(),so.getAlt(), 0,0);
//                    System.out.println("Lat: "+pdo.getLatitude()+" Lon: "+pdo.getLongitude());
                    server.broadcast(pdo);
                   
                } catch (IOException ex) {
                    System.out.println("lost stream, restarting...");
                    run=false;
                   
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
            }
           
            try {
                in.close();
                clientSocket.close();
                serverSocket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            new SimPlaneListener();
        }
    }
   
    /**
     * Thread which listens to X-Plane 8 UDP and puts the date into the parser.
     */
    public class UdpListener extends Thread{
        public UdpListener(){
            start();
        }
        @Override
        public void run(){
            try {
                int port = 49000;

                DatagramSocket dsocket = new DatagramSocket(port);
               
                byte[] buffer = new byte[2048];
               
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
               
                System.out.println("UDP capturing started: ");
               
                while (true) {
                    dsocket.receive(packet);
                    PlaneDataObject pdo = parser.getPlaneData(buffer, packet.getAddress().toString());
                    if (pdo != null){
                        server.broadcast(pdo);
                       
//                        System.out.println("planedata broadcasted");
                    }
                    else System.out.println("package error");
                    packet.setLength(buffer.length);
                }
            } catch (Exception e) {
                System.err.println(e);
            }
        }
    }
}
TOP

Related Classes of logic.CaptureThread

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.