From 71f50b832ee178cadc24e1ffb40c7a4d210c550b Mon Sep 17 00:00:00 2001 From: localhorst Date: Fri, 7 Jun 2019 09:52:09 +0200 Subject: [PATCH] added autoconnect at startup --- .../org/hso/texturesyncclient/app/Main.kt | 23 +----- .../controller/Controller.kt | 70 +++++++++++++------ .../view/startupView/StartupViewController.kt | 50 ++++++++++--- 3 files changed, 88 insertions(+), 55 deletions(-) diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/app/Main.kt b/client/src/main/kotlin/org/hso/texturesyncclient/app/Main.kt index c6292e8..c2f1c7a 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/app/Main.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/app/Main.kt @@ -1,31 +1,12 @@ package org.hso.texturesyncclient.app -import org.hso.texturesyncclient.controller.RootController -import org.hso.texturesyncclient.controller.SettingsController -import org.hso.texturesyncclient.view.importView.ImportView -import org.hso.texturesyncclient.view.mainView.MainView -import org.hso.texturesyncclient.view.mainView.MainViewController import org.hso.texturesyncclient.view.startupView.StartupView import org.hso.texturesyncclient.view.startupView.StartupViewController import tornadofx.App class Main: App(StartupView::class){ - val controller = RootController() - - private val svc: StartupViewController by inject() - - init { - SettingsController.init() - - if (SettingsController.serverAddressIsSet()) { - //load settings in ui and try to connect - println("serverAddress is set") - svc.setServerAddress(SettingsController.getServerAddress()) - svc.btnConnectAction(SettingsController.getServerAddress()) - } else { - println("serverAddress is not set") - } - } + //start first controller + private val svc = StartupViewController() } \ No newline at end of file diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/controller/Controller.kt b/client/src/main/kotlin/org/hso/texturesyncclient/controller/Controller.kt index ec02dca..4b2fac5 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/controller/Controller.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/controller/Controller.kt @@ -5,6 +5,7 @@ import javafx.collections.ObservableList import javafx.event.EventHandler import javafx.stage.DirectoryChooser import org.hso.texturesyncclient.alerts.JFXOkayCancelAlert +import org.hso.texturesyncclient.controller.net.AutoConnect import org.hso.texturesyncclient.controller.net.Connection import org.hso.texturesyncclient.model.GUIModel import org.hso.texturesyncclient.model.Sha256 @@ -37,17 +38,6 @@ class RootController : Controller() { private var lastExportDir: String = System.getProperty("user.home") - init { - /*var data = Texture() - var img = con.getTexturePreview(data.textureHash) - var test = GUIModel(data, img) - test.exportItem.setOnAction { - - } - mvc.addElement(test)*/ - } - - /** * calculate the resolution, get today's date -> upload to server * @param path the absolute path of the file on the client's system @@ -74,7 +64,6 @@ class RootController : Controller() { } catch (e: Exception) { println(e) } - } /** @@ -82,18 +71,53 @@ class RootController : Controller() { * @param name server name as IP or domain */ fun initConnection(name: String) { - try { - con = Connection(InetAddress.getByName(name)) - con.ping() - println("Connection to Server successful") - SettingsController.setServerAddress(name) - switchStartupToMain() - println("swithing to MainView @ initCon") - } catch (e: Exception) { - println(e) - println("Connection to Server NOT successful") + if (name == " ") { + //no user input, try automatic connect or restore address from settings file + println("try auto connect") + try { + val foundServer = AutoConnect.searchServer() + if (foundServer != null) { + println("[auto] server found") + con = foundServer + con.ping() + println("auto Connection to Server successful") + switchStartupToMain() + } else { + println("[auto] no server found") + } + } catch (e: Exception) { + println(e) + println("auto Connection to Server NOT successful") + } + if (SettingsController.serverAddressIsSet()) { + println("[file] try connect with settings file") + try { + con = Connection(InetAddress.getByName(SettingsController.getServerAddress())) + con.ping() + println("[file] Connection to Server successful") + switchStartupToMain() + } catch (e: Exception) { + println(e) + println("[file] Connection to Server NOT successful") + } + }else{ + println("[file] no address in settings file") + } + } else { + //try to connect with user input + try { + println("try connect with user input") + con = Connection(InetAddress.getByName(name)) + con.ping() + println("Connection to Server successful") + SettingsController.setServerAddress(name) //store address in settings file + switchStartupToMain() + println("swithing to MainView @ initCon") + } catch (e: Exception) { + println(e) + println("Connection to Server NOT successful") + } } - } fun search(tags: ObservableList): ArrayList { diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/view/startupView/StartupViewController.kt b/client/src/main/kotlin/org/hso/texturesyncclient/view/startupView/StartupViewController.kt index 71e0234..0015f89 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/view/startupView/StartupViewController.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/view/startupView/StartupViewController.kt @@ -1,7 +1,9 @@ package org.hso.texturesyncclient.view.startupView import org.hso.texturesyncclient.controller.RootController +import org.hso.texturesyncclient.controller.SettingsController import org.hso.texturesyncclient.view.mainView.MainView +import tornadofx.ConfigProperties import tornadofx.Controller @@ -10,29 +12,55 @@ class StartupViewController : Controller() { private val sv = find(StartupView::class) private val rootc = find(RootController::class) - fun setServerAddress(address: String){ + init { + println("init StartupViewController") + SettingsController.init() + startConnectionUI() + runAsync { + rootc.initConnection(" ") + } ui { + // reset for later use + endConnectionUI() + } + } + + fun setServerAddress(address: String) { sv.tfServerIP.text = address sv.tfServerIP.isFocusTraversable = false } fun btnConnectAction(name: String) { - sv.labelStatus.text = "Verbinden ..." - sv.tfServerIP.isEditable = false - sv.btnConnect.isDisable = true - sv.spinnerStatus.isVisible = true - + startConnectionUI() runAsync { rootc.initConnection(name) } ui { // reset for later use - sv.spinnerStatus.isVisible = false - sv.labelStatus.text = "Verbindung zum Server einrichten" - sv.tfServerIP.isEditable = true - sv.btnConnect.isDisable = false - sv.tfServerIP.clear() + endConnectionUI() } } + /** + * show spinner and block textfied + button and set label + */ + fun startConnectionUI() { + sv.labelStatus.text = "Verbinden ..." + sv.tfServerIP.isEditable = false + sv.btnConnect.isDisable = true + sv.spinnerStatus.isVisible = true + } + + /** + * remove spinner and unblock textfied + button and set label + */ + fun endConnectionUI() { + sv.spinnerStatus.isVisible = false + sv.labelStatus.text = "Verbindung zum Server einrichten" + sv.tfServerIP.isEditable = true + sv.btnConnect.isDisable = false + sv.tfServerIP.clear() + } + + }