Skip to content

Commit

Permalink
Add ability to stop modeS thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglasdc3 committed Dec 14, 2020
1 parent d27b12d commit 4c9e2c0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/main/java/aero/t2s/modes/ModeS.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ public void onMessage(Consumer<DownlinkFormat> 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<String, Track> getTracks() {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/aero/t2s/modes/ModeSHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ protected short[] toData(final String input) throws EmptyMessageException {

return BinaryHelper.stringToByteArray(hex);
}

public void start() {

}

public void stop() {

}
}
15 changes: 11 additions & 4 deletions src/main/java/aero/t2s/modes/ModeSListener.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
Expand All @@ -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;
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/aero/t2s/modes/ModeSTrackHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ public class ModeSTrackHandler extends ModeSHandler {

private final Decoder decoder;
private final Executor executor = Executors.newSingleThreadExecutor();
private final Map<String, Track> tracks;

private boolean cleanupEnabled = true;
private final Timer timer;

public ModeSTrackHandler(Map<String, Track> 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() {
Expand All @@ -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) {
Expand Down

0 comments on commit 4c9e2c0

Please sign in to comment.