From 369e96226fc8d26759306f4133c9c16896c6f570 Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Mon, 14 Aug 2023 20:03:50 +0200 Subject: [PATCH] Ensure closing the window exits. --- gradle.properties | 2 +- src/main/java/biz/nellemann/mdexpl/App.java | 15 ++----- .../biz/nellemann/mdexpl/MainPresenter.java | 10 +---- .../mdexpl/service/DiscoveryService.java | 40 +++++-------------- 4 files changed, 15 insertions(+), 52 deletions(-) diff --git a/gradle.properties b/gradle.properties index 82348b2..49e5f0c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version = 0.0.1 +version = 0.0.2 diff --git a/src/main/java/biz/nellemann/mdexpl/App.java b/src/main/java/biz/nellemann/mdexpl/App.java index 83c2d9a..dfb8885 100644 --- a/src/main/java/biz/nellemann/mdexpl/App.java +++ b/src/main/java/biz/nellemann/mdexpl/App.java @@ -16,16 +16,14 @@ import java.io.IOException; public class App extends Application { - @Override - public void init() { - Platform.setImplicitExit(true); - } - @Override public void start(Stage primaryStage) throws IOException { // Make all stages close and the app exit when the primary stage is closed - primaryStage.setOnCloseRequest(e -> Platform.exit()); + Platform.setImplicitExit(true); + primaryStage.setOnCloseRequest(e -> { + System.exit(0); + }); // Set icon on the application bar var appIcon = new Image("/icon.png"); @@ -51,11 +49,6 @@ public class App extends Application { } - @Override - public void stop() { - } - - public static void main(String[] args) { launch(args); } diff --git a/src/main/java/biz/nellemann/mdexpl/MainPresenter.java b/src/main/java/biz/nellemann/mdexpl/MainPresenter.java index 9bc698a..4098a9b 100644 --- a/src/main/java/biz/nellemann/mdexpl/MainPresenter.java +++ b/src/main/java/biz/nellemann/mdexpl/MainPresenter.java @@ -38,17 +38,9 @@ public class MainPresenter { @FXML public void initialize() { log.info("initialize()"); - - view.addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, event -> { - log.info("Window closing"); - discoveryService.destroy(); - }); - - discoveryService = new DiscoveryService(); - discoveryService.initialize(); - discoveryService.setObservableList(devicesList); listView.setItems(devicesList); listView.setCellFactory(p -> new NetworkServiceCell()); + discoveryService = new DiscoveryService(devicesList); } diff --git a/src/main/java/biz/nellemann/mdexpl/service/DiscoveryService.java b/src/main/java/biz/nellemann/mdexpl/service/DiscoveryService.java index 071a619..313b28d 100644 --- a/src/main/java/biz/nellemann/mdexpl/service/DiscoveryService.java +++ b/src/main/java/biz/nellemann/mdexpl/service/DiscoveryService.java @@ -20,16 +20,10 @@ public class DiscoveryService { private final static Logger log = LoggerFactory.getLogger(DiscoveryService.class); - private ObservableList observableList; + private final ObservableList observableList; private JmDNS jmdns; - /*private final HashMap services = HashMap<>({ - "http", "https", "upnp", "ssh", "sip", "rdp", "ntp", "rdp", "rtsp", - "ntp", "smb", "nfs", "llrp", "ftp", "ep", "daap", "ipp", "ipps", - "googlecast", "sonos", "airplay", "smartenergy", "skype", "bittorrent" - );*/ - // From: http://www.dns-sd.org/serviceTypes.html + // https://jonathanmumm.com/tech-it/mdns-bonjour-bible-common-service-strings-for-various-vendors/ + some guesses private final Map services = new HashMap<>() {{ @@ -68,36 +62,20 @@ public class DiscoveryService { //put("rdlink", Color.KHAKI); }}; + public DiscoveryService(ObservableList list) { + log.info("DiscoveryService()"); + this.observableList = list; - public void initialize() { - log.info("initialize()"); try { jmdns = JmDNS.create(InetAddress.getByName("0.0.0.0"), "mdnsExplorer"); + services.forEach((item, color) -> { + String service = String.format("_%s._%s.local.", item, "tcp"); + NetworkServiceListener networkServiceListener = new NetworkServiceListener(service, observableList, color); + jmdns.addServiceListener(service, networkServiceListener); + }); } catch (IOException e) { log.error("initialize() - {}", e.getMessage()); } } - - public void destroy() { - if(jmdns != null) { - try { - jmdns.close(); - } catch (IOException e) { - log.error("destroy() - {}", e.getMessage()); - } - } - } - - - public void setObservableList(ObservableList list) { - this.observableList = list; - services.forEach((item, color) -> { - String service = String.format("_%s._%s.local.", item, "tcp"); - NetworkServiceListener networkServiceListener = new NetworkServiceListener(service, observableList, color); - jmdns.addServiceListener(service, networkServiceListener); - }); - } - - }