From 4c9e2c0e677a1d91933b0590a01277d77afc4e28 Mon Sep 17 00:00:00 2001 From: Ken Andries Date: Mon, 14 Dec 2020 23:56:42 +0100 Subject: [PATCH] Add ability to stop modeS thread --- src/main/java/aero/t2s/modes/ModeS.java | 6 ++++-- src/main/java/aero/t2s/modes/ModeSHandler.java | 8 ++++++++ src/main/java/aero/t2s/modes/ModeSListener.java | 15 +++++++++++---- .../java/aero/t2s/modes/ModeSTrackHandler.java | 13 ++++++++++++- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/java/aero/t2s/modes/ModeS.java b/src/main/java/aero/t2s/modes/ModeS.java index 4e25716..ad9b448 100644 --- a/src/main/java/aero/t2s/modes/ModeS.java +++ b/src/main/java/aero/t2s/modes/ModeS.java @@ -55,11 +55,13 @@ public void onMessage(Consumer consumer) { } public void start() { - listener.start(); + this.handler.start(); + this.listener.start(); } public void stop() { - listener.interrupt(); + this.listener.interrupt(); + this.handler.stop(); } public Map getTracks() { diff --git a/src/main/java/aero/t2s/modes/ModeSHandler.java b/src/main/java/aero/t2s/modes/ModeSHandler.java index 2347e9e..a4ea818 100644 --- a/src/main/java/aero/t2s/modes/ModeSHandler.java +++ b/src/main/java/aero/t2s/modes/ModeSHandler.java @@ -39,4 +39,12 @@ protected short[] toData(final String input) throws EmptyMessageException { return BinaryHelper.stringToByteArray(hex); } + + public void start() { + + } + + public void stop() { + + } } diff --git a/src/main/java/aero/t2s/modes/ModeSListener.java b/src/main/java/aero/t2s/modes/ModeSListener.java index 6742e32..348910a 100644 --- a/src/main/java/aero/t2s/modes/ModeSListener.java +++ b/src/main/java/aero/t2s/modes/ModeSListener.java @@ -1,9 +1,12 @@ package aero.t2s.modes; +import org.slf4j.LoggerFactory; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetSocketAddress; +import java.nio.channels.ClosedChannelException; import java.nio.channels.SocketChannel; class ModeSListener extends Thread { @@ -12,6 +15,8 @@ class ModeSListener extends Thread { private int attempts; + private boolean running = true; + public ModeSListener(InetSocketAddress address, ModeSHandler handler) { super("Mode S Listener"); this.address = address; @@ -20,23 +25,25 @@ public ModeSListener(InetSocketAddress address, ModeSHandler handler) { @Override public void run() { - while (! isInterrupted()) { + while (!isInterrupted() && running) { try (SocketChannel socketChannel = SocketChannel.open()) { attempts++; + LoggerFactory.getLogger(getClass()).info("[ModeSListener] Connecting to {}", address.toString()); socketChannel.connect(address); BufferedReader is = new BufferedReader(new InputStreamReader(socketChannel.socket().getInputStream())); String hex; attempts = 0; - while (! isInterrupted()) { + while (!isInterrupted()) { hex = is.readLine(); handler.handle(hex); } } catch (IOException e) { e.printStackTrace(); - if (attempts == 5) { - interrupt(); + if (attempts == 5 || isInterrupted()) { + LoggerFactory.getLogger(getClass()).info("[ModeSListener] Disconnecting"); + running = false; } } } diff --git a/src/main/java/aero/t2s/modes/ModeSTrackHandler.java b/src/main/java/aero/t2s/modes/ModeSTrackHandler.java index 9bf51ee..f78c84b 100644 --- a/src/main/java/aero/t2s/modes/ModeSTrackHandler.java +++ b/src/main/java/aero/t2s/modes/ModeSTrackHandler.java @@ -17,13 +17,20 @@ public class ModeSTrackHandler extends ModeSHandler { private final Decoder decoder; private final Executor executor = Executors.newSingleThreadExecutor(); + private final Map tracks; private boolean cleanupEnabled = true; + private final Timer timer; public ModeSTrackHandler(Map tracks, double originLat, double originLon, ModeSDatabase database) { + this.tracks = tracks; this.decoder = new Decoder(tracks, originLat, originLon, database); - Timer timer = new Timer(); + timer = new Timer(); + } + + @Override + public void start() { timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { @@ -38,7 +45,11 @@ public void run() { expired.forEach((icao) -> onDeleted.accept(tracks.remove(icao))); } }, 1000, 5000); + } + @Override + public void stop() { + timer.cancel(); } public void handle(final String input) {