added autoconnect at startup

This commit is contained in:
Hendrik Schutter 2019-06-07 09:52:09 +02:00
parent 0f8d2ba256
commit 71f50b832e
3 changed files with 88 additions and 55 deletions

View File

@ -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()
}

View File

@ -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<String>): ArrayList<GUIModel> {

View File

@ -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()
}
}