diff --git a/cmd/rs2322tcp-client/main.go b/cmd/rs2322tcp-client/main.go index b4d0ef3..941eafe 100644 --- a/cmd/rs2322tcp-client/main.go +++ b/cmd/rs2322tcp-client/main.go @@ -1,23 +1,63 @@ /* -Package main implements the rs2322tcp client application. - -The client will provide a local virtual serial interface and -transparently transport serial data over TCP to the rs2322tcp server. - -At this stage the application only provides the basic program -structure and version information. - -Project: rs2322tcp -Module: git.lang-dieter.de/rs2322tcp -*/ + * ============================================================================ + * Projekt.....: rs2322tcp + * Datei.......: cmd/rs2322tcp-client/main.go + * Copyright (C) 2026 Dieter Lang + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Beschreibung: + * Einstiegspunkt für den grafischen rs2322tcp-Client. + * + * Der Client wird als eigenständige Desktop-Anwendung ausgeführt. Die + * grafische Oberfläche basiert auf Fyne und bildet sowohl die + * Statusanzeige als auch später die Konfiguration der lokalen virtuellen + * seriellen Ports ab. + * + * Die eigentliche Client- und Runtime-Logik bleibt in den internen + * Packages gekapselt. Diese Datei ist bewusst auf den Programmstart + * und die Übergabe an die GUI beschränkt. + * ============================================================================ + */ package main import ( "fmt" + "fyne.io/fyne/v2/app" + + "git.lang-dieter.de/rs2322tcp/internal/gui" "git.lang-dieter.de/rs2322tcp/internal/version" ) func main() { fmt.Printf("rs2322tcp-client %s\n", version.Version) + + application := app.NewWithID("git.lang-dieter.de.rs2322tcp.client") + + guiApplication, err := gui.NewApp( + application, + "./configs/client.json", + ) + if err != nil { + fmt.Printf("GUI konnte nicht gestartet werden: %v\n", err) + return + } + + // Das technische Client-System wird nach dem Erzeugen des Fensters + // asynchron gestartet. Dadurch bleibt die GUI auch während des + // Verbindungsaufbaus sofort sichtbar und bedienbar. + guiApplication.Start() + + // SetOnClosed gehört zum Fyne-Fenster, nicht zur Fyne-Anwendung. + // + // Beim Schließen des Hauptfensters wird die technische Client- + // Anwendung beendet. Dadurch werden Runtime, Bridges, virtuelle + // Ports und die Serververbindung sauber geschlossen. + guiApplication.Window().SetOnClosed(func() { + guiApplication.Close() + }) + + // ShowAndRun zeigt das Fenster und startet den Fyne-Eventloop. + guiApplication.ShowAndRun() } diff --git a/configs/client.json b/configs/client.json index 1bf3094..6541eaf 100644 --- a/configs/client.json +++ b/configs/client.json @@ -1,6 +1,6 @@ { "server": { - "address": "100.64.0.10", + "address": "127.0.0.1", "port": 5000 }, "virtual_port_range": { diff --git a/go.mod b/go.mod index d033741..34e931b 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,45 @@ module git.lang-dieter.de/rs2322tcp go 1.25.0 -require go.bug.st/serial v1.7.1 +require ( + go.bug.st/serial v1.7.1 + golang.org/x/sys v0.43.0 +) -require golang.org/x/sys v0.43.0 // indirect +require ( + fyne.io/fyne/v2 v2.8.0 + fyne.io/systray v1.12.2 // indirect + github.com/BurntSushi/toml v1.6.0 // indirect + github.com/anthonynsimon/bild v0.14.0 // indirect + github.com/clipperhouse/uax29/v2 v2.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fredbi/uri v1.1.1 // indirect + github.com/fsnotify/fsnotify v1.9.0 // indirect + github.com/fyne-io/gl-js v0.2.1-0.20260315212741-029c47fd27e8 // indirect + github.com/fyne-io/glfw-js v0.4.0 // indirect + github.com/fyne-io/image v0.1.1 // indirect + github.com/fyne-io/oksvg v0.2.0 // indirect + github.com/go-gl/gl v0.0.0-20260331235117-4566fea9a276 // indirect + github.com/go-gl/glfw/v3.4/glfw v0.1.0-pre.1.0.20260707082822-2a407d02d01a // indirect + github.com/go-text/render v0.2.1 // indirect + github.com/go-text/typesetting v0.3.4 // indirect + github.com/godbus/dbus/v5 v5.2.2 // indirect + github.com/hack-pad/go-indexeddb v0.3.2 // indirect + github.com/hack-pad/safejs v0.1.0 // indirect + github.com/jeandeaual/go-locale v0.0.0-20250612000132-0ef82f21eade // indirect + github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/mattn/go-runewidth v0.0.24 // indirect + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect + github.com/nicksnyder/go-i18n/v2 v2.5.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rymdport/portal v0.4.2 // indirect + github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c // indirect + github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef // indirect + github.com/stretchr/testify v1.11.1 // indirect + github.com/yuin/goldmark v1.8.2 // indirect + golang.org/x/image v0.24.0 // indirect + golang.org/x/net v0.35.0 // indirect + golang.org/x/text v0.22.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index dc4a229..098e522 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,88 @@ +fyne.io/fyne/v2 v2.8.0 h1:KNUdIk1eKsXSPy/wU6MdiR1hppAPvyzbjPbtJ8h6EUQ= +fyne.io/fyne/v2 v2.8.0/go.mod h1:tLJK7CVtUBOnMiSDR+J88t/quiGuEhwGs09tIVM1RXg= +fyne.io/systray v1.12.2 h1:Y8DZxgLHsVQt6rY9Zrkkg+j67S7vv/1F2viOWKPpVeA= +fyne.io/systray v1.12.2/go.mod h1:RVwqP9nYMo7h5zViCBHri2FgjXF7H2cub7MAq4NSoLs= +github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= +github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/anthonynsimon/bild v0.14.0 h1:IFRkmKdNdqmexXHfEU7rPlAmdUZ8BDZEGtGHDnGWync= +github.com/anthonynsimon/bild v0.14.0/go.mod h1:hcvEAyBjTW69qkKJTfpcDQ83sSZHxwOunsseDfeQhUs= +github.com/clipperhouse/uax29/v2 v2.2.0 h1:ChwIKnQN3kcZteTXMgb1wztSgaU+ZemkgWdohwgs8tY= +github.com/clipperhouse/uax29/v2 v2.2.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/felixge/fgprof v0.9.3 h1:VvyZxILNuCiUCSXtPtYmmtGvb65nqXh2QFWc0Wpf2/g= +github.com/felixge/fgprof v0.9.3/go.mod h1:RdbpDgzqYVh/T9fPELJyV7EYJuHB55UTEULNun8eiPw= +github.com/fredbi/uri v1.1.1 h1:xZHJC08GZNIUhbP5ImTHnt5Ya0T8FI2VAwI/37kh2Ko= +github.com/fredbi/uri v1.1.1/go.mod h1:4+DZQ5zBjEwQCDmXW5JdIjz0PUA+yJbvtBv+u+adr5o= +github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= +github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +github.com/fyne-io/gl-js v0.2.1-0.20260315212741-029c47fd27e8 h1:0kdPD/GEntpWmZEK5Zu/xE6Tr37jYCVDf9QP8lA/QK8= +github.com/fyne-io/gl-js v0.2.1-0.20260315212741-029c47fd27e8/go.mod h1:ZcepK8vmOYLu96JoxbCKJy2ybr+g1pTnaBDdl7c3ajI= +github.com/fyne-io/glfw-js v0.4.0 h1:I9hREBeFyI10cNIqbMKYb1PRidyPDgwob8o2la9SfQo= +github.com/fyne-io/glfw-js v0.4.0/go.mod h1:SDchsFZh4n7nVuBoiowOhOgIBdz+qUQVeC1w9fe2yVU= +github.com/fyne-io/image v0.1.1 h1:WH0z4H7qfvNUw5l4p3bC1q70sa5+YWVt6HCj7y4VNyA= +github.com/fyne-io/image v0.1.1/go.mod h1:xrfYBh6yspc+KjkgdZU/ifUC9sPA5Iv7WYUBzQKK7JM= +github.com/fyne-io/oksvg v0.2.0 h1:mxcGU2dx6nwjJsSA9PCYZDuoAcsZ/OuJlvg/Q9Njfo8= +github.com/fyne-io/oksvg v0.2.0/go.mod h1:dJ9oEkPiWhnTFNCmRgEze+YNprJF7YRbpjgpWS4kzoI= +github.com/go-gl/gl v0.0.0-20260331235117-4566fea9a276 h1:IO5P06Pcj9K04d+l4nrf3c2U56+dAotIFG6u4P1wAHI= +github.com/go-gl/gl v0.0.0-20260331235117-4566fea9a276/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw= +github.com/go-gl/glfw/v3.4/glfw v0.1.0-pre.1.0.20260707082822-2a407d02d01a h1:HWK0MBggT/T6YH7VffE10xBIhqeTq8JzIUPJXrRy87g= +github.com/go-gl/glfw/v3.4/glfw v0.1.0-pre.1.0.20260707082822-2a407d02d01a/go.mod h1:T5Dn0JwIJOX1euPZ/iT4tq6nFYtmukjcYa7937HuYK8= +github.com/go-text/render v0.2.1 h1:qwHhxqGUjjg4L0XyJWj7M7bpY75NZM+kBpv2Yfw5mcg= +github.com/go-text/render v0.2.1/go.mod h1:HCCAq8MUlm/WRcXshBb4K/n+IkjeXQ1c2Ba+yICSm0A= +github.com/go-text/typesetting v0.3.4 h1:YYurUOtEb9kGSOz4uE3k4OpBGsp1dDL8+fjCeaFamAU= +github.com/go-text/typesetting v0.3.4/go.mod h1:4qZCQphq4KSgGTAeI0uMEkVbROgfah8BuyF5LRYr7XY= +github.com/go-text/typesetting-utils v0.0.0-20260223113751-2d88ac90dae3 h1:drBZzMgdYPbmyXqOto4YhhJGrFIQCX94FpR4MzTCsos= +github.com/go-text/typesetting-utils v0.0.0-20260223113751-2d88ac90dae3/go.mod h1:3/62I4La/HBRX9TcTpBj4eipLiwzf+vhI+7whTc9V7o= +github.com/godbus/dbus/v5 v5.2.2 h1:TUR3TgtSVDmjiXOgAAyaZbYmIeP3DPkld3jgKGV8mXQ= +github.com/godbus/dbus/v5 v5.2.2/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c= +github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y= +github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg= +github.com/hack-pad/go-indexeddb v0.3.2 h1:DTqeJJYc1usa45Q5r52t01KhvlSN02+Oq+tQbSBI91A= +github.com/hack-pad/go-indexeddb v0.3.2/go.mod h1:QvfTevpDVlkfomY498LhstjwbPW6QC4VC/lxYb0Kom0= +github.com/hack-pad/safejs v0.1.0 h1:qPS6vjreAqh2amUqj4WNG1zIw7qlRQJ9K10eDKMCnE8= +github.com/hack-pad/safejs v0.1.0/go.mod h1:HdS+bKF1NrE72VoXZeWzxFOVQVUSqZJAG0xNCnb+Tio= +github.com/jeandeaual/go-locale v0.0.0-20250612000132-0ef82f21eade h1:FmusiCI1wHw+XQbvL9M+1r/C3SPqKrmBaIOYwVfQoDE= +github.com/jeandeaual/go-locale v0.0.0-20250612000132-0ef82f21eade/go.mod h1:ZDXo8KHryOWSIqnsb/CiDq7hQUYryCgdVnxbj8tDG7o= +github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25 h1:YLvr1eE6cdCqjOe972w/cYF+FjW34v27+9Vo5106B4M= +github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25/go.mod h1:kLgvv7o6UM+0QSf0QjAse3wReFDsb9qbZJdfexWlrQw= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mattn/go-runewidth v0.0.24 h1:cpokDiIn0MGnhdHwuWnJBITySJ20QyNGnY2kR/ay2DU= +github.com/mattn/go-runewidth v0.0.24/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= +github.com/nicksnyder/go-i18n/v2 v2.5.1 h1:IxtPxYsR9Gp60cGXjfuR/llTqV8aYMsC472zD0D1vHk= +github.com/nicksnyder/go-i18n/v2 v2.5.1/go.mod h1:DrhgsSDZxoAfvVrBVLXoxZn/pN5TXqaDbq7ju94viiQ= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/pkg/profile v1.7.0 h1:hnbDkaNWPCLMO9wGLdBFTIZvzDrDfBM2072E1S9gJkA= +github.com/pkg/profile v1.7.0/go.mod h1:8Uer0jas47ZQMJ7VD+OHknK4YDY07LPUC6dEvqDjvNo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rymdport/portal v0.4.2 h1:7jKRSemwlTyVHHrTGgQg7gmNPJs88xkbKcIL3NlcmSU= +github.com/rymdport/portal v0.4.2/go.mod h1:kFF4jslnJ8pD5uCi17brj/ODlfIidOxlgUDTO5ncnC4= +github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c h1:km8GpoQut05eY3GiYWEedbTT0qnSxrCjsVbb7yKY1KE= +github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c/go.mod h1:cNQ3dwVJtS5Hmnjxy6AgTPd0Inb3pW05ftPSX7NZO7Q= +github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef h1:Ch6Q+AZUxDBCVqdkI8FSpFyZDtCVBc2VmejdNrm5rRQ= +github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef/go.mod h1:nXTWP6+gD5+LUJ8krVhhoeHjvHTutPxMYl5SvkcnJNE= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/yuin/goldmark v1.8.2 h1:kEGpgqJXdgbkhcOgBxkC0X0PmoPG1ZyoZ117rDVp4zE= +github.com/yuin/goldmark v1.8.2/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg= go.bug.st/serial v1.7.1 h1:5aP8wYL0UjEYOVs3oPAGscjaSfRQLHtCvBFXNN/rwtc= go.bug.st/serial v1.7.1/go.mod h1:d0MmS16Qt9b1m06yoYRNUXhRRTJV5Qg2S5EKqQtnayQ= +golang.org/x/image v0.24.0 h1:AN7zRgVsbvmTfNyqIbbOraYL8mSwcKncEj8ofjgzcMQ= +golang.org/x/image v0.24.0/go.mod h1:4b/ITuLfqYq1hqZcjofwctIhi7sZh2WaCjvsBNjjya8= +golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8= +golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk= golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI= golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= +golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/client/application.go b/internal/client/application.go new file mode 100644 index 0000000..ace78a0 --- /dev/null +++ b/internal/client/application.go @@ -0,0 +1,293 @@ +/* + * ============================================================================ + * Projekt.....: rs2322tcp + * Datei.......: internal/client/application.go + * Copyright (C) 2026 Dieter Lang + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Beschreibung: + * Gemeinsame Anwendungsschicht für den rs2322tcp-Client. + * + * Die Anwendungsschicht verbindet die Konfiguration, die Control-Verbindung + * zum Server und die clientseitige Runtime. Sie stellt damit einen einfachen + * Lebenszyklus für die spätere grafische Benutzeroberfläche bereit. + * + * Der technische Client wird bei Start und Reconnect vollständig neu + * initialisiert: + * + * Konfiguration laden + * | + * v + * Server verbinden + * | + * v + * Geräteliste abfragen + * | + * v + * Runtime starten + * + * Ein Reconnect beendet zunächst die bestehende Runtime und Control- + * Verbindung und führt anschließend denselben Startablauf erneut aus. + * + * Die Anwendungsschicht enthält bewusst keine GUI-Logik und keine + * Fyne-Abhängigkeit. + * ============================================================================ + */ +package client + +import ( + "fmt" + "sync" + + "git.lang-dieter.de/rs2322tcp/internal/config" + "git.lang-dieter.de/rs2322tcp/internal/transport" +) + +/////////////////////////////////////////////////////////////////////////////// +// Application +/////////////////////////////////////////////////////////////////////////////// + +// Application represents one running rs2322tcp client application. +// +// Application owns the current client connection and Runtime. The +// configuration file is the persistent source of the client configuration. +// +// The Application is intended to be used by the command-line entry point +// as well as by the later graphical user interface. +type Application struct { + mu sync.Mutex + + configFile string + + config *config.ClientConfig + client *Client + runtime *Runtime + devices []transport.RemoteDeviceInfo + + runtimeDone chan error +} + +// NewApplication creates a new client Application. +// +// No configuration is loaded and no network connection is established. +// Start must be called before the application can be used. +func NewApplication(configFile string) (*Application, error) { + if configFile == "" { + return nil, fmt.Errorf("configuration file is empty") + } + + return &Application{ + configFile: configFile, + }, nil +} + +/////////////////////////////////////////////////////////////////////////////// +// Start +/////////////////////////////////////////////////////////////////////////////// + +// Start loads the client configuration, connects to the server and starts +// the client Runtime. +// +// Start is intended for the initial application startup. An already running +// application must be stopped first by calling Close or Reconnect. +// +// The Runtime runs asynchronously because it waits for the configured +// bridges while the GUI must remain responsive. +func (a *Application) Start() error { + if a == nil { + return fmt.Errorf("application is nil") + } + + a.mu.Lock() + + if a.runtime != nil || a.client != nil { + a.mu.Unlock() + + return fmt.Errorf("application is already running") + } + + a.mu.Unlock() + + cfg, err := config.LoadClient(a.configFile) + if err != nil { + return fmt.Errorf("load client configuration: %w", err) + } + + address := fmt.Sprintf( + "%s:%d", + cfg.Server.Address, + cfg.Server.Port, + ) + + client, err := New(address) + if err != nil { + return err + } + + devices, err := client.GetDevices() + if err != nil { + _ = client.Close() + + return fmt.Errorf("get remote devices: %w", err) + } + + manager := NewVirtualPortManager(cfg.VirtualPortRange) + + runtime, err := NewRuntime(client, manager) + if err != nil { + _ = client.Close() + + return fmt.Errorf("create client runtime: %w", err) + } + + runtimeDone := make(chan error, 1) + + a.mu.Lock() + a.config = cfg + a.client = client + a.runtime = runtime + a.devices = append( + []transport.RemoteDeviceInfo(nil), + devices..., + ) + a.runtimeDone = runtimeDone + a.mu.Unlock() + + go func() { + runtimeDone <- runtime.Run(*cfg) + }() + + return nil +} + +/////////////////////////////////////////////////////////////////////////////// +// Reconnect +/////////////////////////////////////////////////////////////////////////////// + +// Reconnect stops the current client connection and starts it again. +// +// The configuration file is loaded again during Start. This is intentional: +// changes made by the GUI are therefore picked up automatically. +// +// Reconnect is also useful when the server connection has been lost or the +// user explicitly requests a new connection. +func (a *Application) Reconnect() error { + if a == nil { + return fmt.Errorf("application is nil") + } + + if err := a.Close(); err != nil { + return fmt.Errorf("close current connection: %w", err) + } + + return a.Start() +} + +/////////////////////////////////////////////////////////////////////////////// +// Close +/////////////////////////////////////////////////////////////////////////////// + +// Close stops the Runtime and closes the client control connection. +// +// Calling Close more than once is safe. +func (a *Application) Close() error { + if a == nil { + return nil + } + + a.mu.Lock() + + runtime := a.runtime + client := a.client + + a.runtime = nil + a.client = nil + a.config = nil + a.devices = nil + a.runtimeDone = nil + + a.mu.Unlock() + + var firstErr error + + if runtime != nil { + if err := runtime.Close(); err != nil { + firstErr = err + } + } else if client != nil { + if err := client.Close(); err != nil { + firstErr = err + } + } + + return firstErr +} + +/////////////////////////////////////////////////////////////////////////////// +// Configuration +/////////////////////////////////////////////////////////////////////////////// + +// Config returns a copy of the currently loaded client configuration. +// +// It returns nil when the application has not been started. +func (a *Application) Config() *config.ClientConfig { + if a == nil { + return nil + } + + a.mu.Lock() + defer a.mu.Unlock() + + if a.config == nil { + return nil + } + + cfg := *a.config + + cfg.VirtualPorts = append( + []config.VirtualPortConfig(nil), + a.config.VirtualPorts..., + ) + + return &cfg +} + +/////////////////////////////////////////////////////////////////////////////// +// Devices +/////////////////////////////////////////////////////////////////////////////// + +// Devices returns a copy of the remote devices received from the server. +// +// The returned slice belongs to the caller and can therefore be modified +// without changing the Application state. +func (a *Application) Devices() []transport.RemoteDeviceInfo { + if a == nil { + return nil + } + + a.mu.Lock() + defer a.mu.Unlock() + + return append( + []transport.RemoteDeviceInfo(nil), + a.devices..., + ) +} + +/////////////////////////////////////////////////////////////////////////////// +// Runtime state +/////////////////////////////////////////////////////////////////////////////// + +// Running reports whether the application currently owns a Runtime and a +// client connection. +func (a *Application) Running() bool { + if a == nil { + return false + } + + a.mu.Lock() + defer a.mu.Unlock() + + return a.runtime != nil && a.client != nil +} diff --git a/internal/client/application_test.go b/internal/client/application_test.go new file mode 100644 index 0000000..c58dfb1 --- /dev/null +++ b/internal/client/application_test.go @@ -0,0 +1,157 @@ +/* + * ============================================================================ + * Projekt.....: rs2322tcp + * Datei.......: internal/client/application_test.go + * Copyright (C) 2026 Dieter Lang + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Beschreibung: + * Tests für die Anwendungsschicht des rs2322tcp-Clients. + * + * Die Tests prüfen zunächst den Lebenszyklus der Application, ohne dafür + * eine echte Serververbindung aufbauen zu müssen. + * + * Insbesondere wird sichergestellt, dass eine neu erzeugte Application + * zunächst keinen laufenden Client und keine laufende Runtime besitzt und + * dass Close auch vor einem Start sicher verwendet werden kann. + * ============================================================================ + */ + +package client + +import "testing" + +/////////////////////////////////////////////////////////////////////////////// +// NewApplication +/////////////////////////////////////////////////////////////////////////////// + +// TestNewApplicationRejectsEmptyConfigFile verifies that an empty +// configuration-file path is rejected. +func TestNewApplicationRejectsEmptyConfigFile(t *testing.T) { + t.Helper() + + application, err := NewApplication("") + if err == nil { + t.Fatal("NewApplication with empty config file returned no error") + } + + if application != nil { + t.Fatal("NewApplication with empty config file returned an application") + } +} + +// TestNewApplication verifies that a valid configuration-file path creates +// an application without starting it. +func TestNewApplication(t *testing.T) { + t.Helper() + + application, err := NewApplication("client.json") + if err != nil { + t.Fatalf("NewApplication: %v", err) + } + + if application == nil { + t.Fatal("NewApplication returned nil application") + } + + if application.Running() { + t.Fatal("new application reports running") + } + + if application.Config() != nil { + t.Fatal("new application returned a configuration") + } + + if devices := application.Devices(); len(devices) != 0 { + t.Fatalf( + "new application returned %d devices", + len(devices), + ) + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Close +/////////////////////////////////////////////////////////////////////////////// + +// TestApplicationCloseBeforeStart verifies that Close can safely be called +// before the application has been started. +func TestApplicationCloseBeforeStart(t *testing.T) { + t.Helper() + + application, err := NewApplication("client.json") + if err != nil { + t.Fatalf("NewApplication: %v", err) + } + + if err := application.Close(); err != nil { + t.Fatalf("Close before Start: %v", err) + } + + if application.Running() { + t.Fatal("application reports running after Close") + } +} + +// TestApplicationCloseIsIdempotent verifies that Close can be called more +// than once without producing an error. +func TestApplicationCloseIsIdempotent(t *testing.T) { + t.Helper() + + application, err := NewApplication("client.json") + if err != nil { + t.Fatalf("NewApplication: %v", err) + } + + if err := application.Close(); err != nil { + t.Fatalf("first Close: %v", err) + } + + if err := application.Close(); err != nil { + t.Fatalf("second Close: %v", err) + } + + if application.Running() { + t.Fatal("application reports running after repeated Close") + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Nil receiver +/////////////////////////////////////////////////////////////////////////////// + +// TestNilApplication verifies that the public lifecycle methods behave +// safely when called on a nil Application receiver. +// +// This is deliberately a small defensive test. The GUI should normally +// never operate on a nil Application. +func TestNilApplication(t *testing.T) { + t.Helper() + + var application *Application + + if err := application.Start(); err == nil { + t.Fatal("nil Application Start returned no error") + } + + if err := application.Reconnect(); err == nil { + t.Fatal("nil Application Reconnect returned no error") + } + + if err := application.Close(); err != nil { + t.Fatalf("nil Application Close: %v", err) + } + + if application.Running() { + t.Fatal("nil Application reports running") + } + + if application.Config() != nil { + t.Fatal("nil Application returned a configuration") + } + + if devices := application.Devices(); devices != nil { + t.Fatal("nil Application returned devices") + } +} diff --git a/internal/gui/app.go b/internal/gui/app.go new file mode 100644 index 0000000..fe559cf --- /dev/null +++ b/internal/gui/app.go @@ -0,0 +1,245 @@ +/* + * ============================================================================ + * Projekt.....: rs2322tcp + * Datei.......: internal/gui/app.go + * Copyright (C) 2026 Dieter Lang + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Beschreibung: + * Grundgerüst der grafischen Benutzeroberfläche des rs2322tcp-Clients. + * + * Die GUI verwendet Fyne und stellt die Benutzeroberfläche für den + * technischen rs2322tcp-Client bereit. + * + * Die eigentliche Kommunikation mit dem Server sowie die Verwaltung der + * virtuellen seriellen Ports bleiben vollständig in internal/client. + * + * Der technische Client wird im Hintergrund gestartet, damit das Fyne- + * Fenster unmittelbar angezeigt werden kann. Dadurch bleibt die GUI auch + * dann bedienbar, wenn der Server nicht erreichbar ist oder der Aufbau + * der Verbindung längere Zeit benötigt. + * + * Aktualisierungen von Fyne-Widgets aus einer Hintergrund-Goroutine werden + * über fyne.Do() auf den Fyne-GUI-Thread übertragen. + * + * Die GUI stellt außerdem die Funktion "Server neu verbinden" bereit. + * Dabei wird die bestehende technische Client-Anwendung beendet und + * anschließend vollständig neu gestartet. Dadurch wird die aktuelle + * client.json erneut geladen. + * ============================================================================ + */ +package gui + +import ( + "fmt" + + "fyne.io/fyne/v2" + "fyne.io/fyne/v2/container" + "fyne.io/fyne/v2/widget" + + "git.lang-dieter.de/rs2322tcp/internal/client" +) + +// App represents the graphical rs2322tcp client application. +// +// App owns the Fyne window and the technical client application. The GUI +// does not access the Runtime or the Client directly. All technical client +// operations are performed through client.Application. +type App struct { + fyneApp fyne.App + window fyne.Window + + clientApplication *client.Application + + statusLabel *widget.Label + errorLabel *widget.Label + reconnectButton *widget.Button +} + +// NewApp creates the graphical rs2322tcp client application. +// +// No server connection is established by NewApp. The GUI can therefore be +// displayed immediately after this function returns. +func NewApp( + fyneApp fyne.App, + configFile string, +) (*App, error) { + if fyneApp == nil { + return nil, fmt.Errorf("fyne application is nil") + } + + application, err := client.NewApplication(configFile) + if err != nil { + return nil, err + } + + window := fyneApp.NewWindow("rs2322tcp Client") + window.Resize(fyne.NewSize(600, 400)) + + app := &App{ + fyneApp: fyneApp, + window: window, + clientApplication: application, + statusLabel: widget.NewLabel("Server: nicht verbunden"), + errorLabel: widget.NewLabel(""), + } + + app.reconnectButton = widget.NewButton( + "Server neu verbinden", + app.reconnect, + ) + + app.buildContent() + + return app, nil +} + +// buildContent creates the initial main-window content. +// +// The layout is deliberately simple. More detailed status information and +// the device-assignment dialog will be added in later development steps. +func (a *App) buildContent() { + title := widget.NewLabel("rs2322tcp Client") + + content := container.NewVBox( + title, + a.statusLabel, + a.errorLabel, + a.reconnectButton, + ) + + a.window.SetContent(content) +} + +// Start starts the technical client application in the background. +// +// The Fyne window is deliberately not blocked by the network connection. +// The status shown by the GUI is updated through fyne.Do(), because the +// technical client runs outside the Fyne GUI thread. +// +// While the initial connection is being established, the reconnect button +// is disabled. This prevents multiple concurrent connection attempts. +func (a *App) Start() { + if a == nil { + return + } + + a.statusLabel.SetText("Server: Verbindung wird aufgebaut ...") + a.errorLabel.SetText("") + a.reconnectButton.Disable() + + go func() { + err := a.clientApplication.Start() + + if err != nil { + fyne.Do(func() { + a.statusLabel.SetText("Server: nicht verbunden") + a.errorLabel.SetText( + fmt.Sprintf("Fehler: %v", err), + ) + a.reconnectButton.Enable() + }) + + return + } + + devices := a.clientApplication.Devices() + + fyne.Do(func() { + a.statusLabel.SetText( + fmt.Sprintf( + "Server: verbunden – %d Geräte verfügbar", + len(devices), + ), + ) + + a.errorLabel.SetText("") + a.reconnectButton.Enable() + }) + }() +} + +// reconnect starts a complete reconnect of the technical client. +// +// The reconnect operation is deliberately handled by +// client.Application.Reconnect(). The GUI therefore does not need to know +// how the technical client closes connections, reloads the configuration, +// connects to the server or starts the Runtime. +// +// While reconnecting, the button is disabled so that only one reconnect +// operation can be active at a time. +func (a *App) reconnect() { + if a == nil { + return + } + + a.statusLabel.SetText("Server: Verbindung wird neu aufgebaut ...") + a.errorLabel.SetText("") + a.reconnectButton.Disable() + + go func() { + err := a.clientApplication.Reconnect() + + if err != nil { + fyne.Do(func() { + a.statusLabel.SetText("Server: nicht verbunden") + a.errorLabel.SetText( + fmt.Sprintf("Fehler: %v", err), + ) + a.reconnectButton.Enable() + }) + + return + } + + devices := a.clientApplication.Devices() + + fyne.Do(func() { + a.statusLabel.SetText( + fmt.Sprintf( + "Server: verbunden – %d Geräte verfügbar", + len(devices), + ), + ) + + a.errorLabel.SetText("") + a.reconnectButton.Enable() + }) + }() +} + +// Close closes the technical client application. +// +// The Runtime and the server connection are closed before the GUI window +// itself is closed. +func (a *App) Close() { + if a == nil { + return + } + + _ = a.clientApplication.Close() +} + +// ShowAndRun displays the main window and starts the Fyne event loop. +// +// This method blocks until the GUI application terminates. +func (a *App) ShowAndRun() { + if a == nil { + return + } + + a.window.ShowAndRun() +} + +// Window returns the main application window. +// +// The method is provided for the program entry point and for later GUI +// initialization that needs access to the window. +func (a *App) Window() fyne.Window { + if a == nil { + return nil + } + + return a.window +}