Skip to content

Commit 4c9e2c0

Browse files
committed
Add ability to stop modeS thread
1 parent d27b12d commit 4c9e2c0

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

src/main/java/aero/t2s/modes/ModeS.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ public void onMessage(Consumer<DownlinkFormat> consumer) {
5555
}
5656

5757
public void start() {
58-
listener.start();
58+
this.handler.start();
59+
this.listener.start();
5960
}
6061

6162
public void stop() {
62-
listener.interrupt();
63+
this.listener.interrupt();
64+
this.handler.stop();
6365
}
6466

6567
public Map<String, Track> getTracks() {

src/main/java/aero/t2s/modes/ModeSHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ protected short[] toData(final String input) throws EmptyMessageException {
3939

4040
return BinaryHelper.stringToByteArray(hex);
4141
}
42+
43+
public void start() {
44+
45+
}
46+
47+
public void stop() {
48+
49+
}
4250
}

src/main/java/aero/t2s/modes/ModeSListener.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package aero.t2s.modes;
22

3+
import org.slf4j.LoggerFactory;
4+
35
import java.io.BufferedReader;
46
import java.io.IOException;
57
import java.io.InputStreamReader;
68
import java.net.InetSocketAddress;
9+
import java.nio.channels.ClosedChannelException;
710
import java.nio.channels.SocketChannel;
811

912
class ModeSListener extends Thread {
@@ -12,6 +15,8 @@ class ModeSListener extends Thread {
1215

1316
private int attempts;
1417

18+
private boolean running = true;
19+
1520
public ModeSListener(InetSocketAddress address, ModeSHandler handler) {
1621
super("Mode S Listener");
1722
this.address = address;
@@ -20,23 +25,25 @@ public ModeSListener(InetSocketAddress address, ModeSHandler handler) {
2025

2126
@Override
2227
public void run() {
23-
while (! isInterrupted()) {
28+
while (!isInterrupted() && running) {
2429
try (SocketChannel socketChannel = SocketChannel.open()) {
2530
attempts++;
31+
LoggerFactory.getLogger(getClass()).info("[ModeSListener] Connecting to {}", address.toString());
2632
socketChannel.connect(address);
2733
BufferedReader is = new BufferedReader(new InputStreamReader(socketChannel.socket().getInputStream()));
2834

2935
String hex;
3036
attempts = 0;
31-
while (! isInterrupted()) {
37+
while (!isInterrupted()) {
3238
hex = is.readLine();
3339
handler.handle(hex);
3440
}
3541
} catch (IOException e) {
3642
e.printStackTrace();
3743

38-
if (attempts == 5) {
39-
interrupt();
44+
if (attempts == 5 || isInterrupted()) {
45+
LoggerFactory.getLogger(getClass()).info("[ModeSListener] Disconnecting");
46+
running = false;
4047
}
4148
}
4249
}

src/main/java/aero/t2s/modes/ModeSTrackHandler.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@ public class ModeSTrackHandler extends ModeSHandler {
1717

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

2122
private boolean cleanupEnabled = true;
23+
private final Timer timer;
2224

2325
public ModeSTrackHandler(Map<String, Track> tracks, double originLat, double originLon, ModeSDatabase database) {
26+
this.tracks = tracks;
2427
this.decoder = new Decoder(tracks, originLat, originLon, database);
2528

26-
Timer timer = new Timer();
29+
timer = new Timer();
30+
}
31+
32+
@Override
33+
public void start() {
2734
timer.scheduleAtFixedRate(new TimerTask() {
2835
@Override
2936
public void run() {
@@ -38,7 +45,11 @@ public void run() {
3845
expired.forEach((icao) -> onDeleted.accept(tracks.remove(icao)));
3946
}
4047
}, 1000, 5000);
48+
}
4149

50+
@Override
51+
public void stop() {
52+
timer.cancel();
4253
}
4354

4455
public void handle(final String input) {

0 commit comments

Comments
 (0)