summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-04-16 11:11:24 +0200
committerxengineering <me@xengineering.eu>2023-04-16 11:39:19 +0200
commit70456216ebbcc7e5006b6a005c571457e60be6e7 (patch)
tree6ec746b40792239efa2ec137c0d45589d3c30b68
parent22d06e868237dcec1ad01d7512679c14d97b275c (diff)
downloadlimox-70456216ebbcc7e5006b6a005c571457e60be6e7.tar
limox-70456216ebbcc7e5006b6a005c571457e60be6e7.tar.zst
limox-70456216ebbcc7e5006b6a005c571457e60be6e7.zip
Implement TCP connect / disconnect
-rw-r--r--go/main.go67
1 files changed, 61 insertions, 6 deletions
diff --git a/go/main.go b/go/main.go
index 4688055..2803e68 100644
--- a/go/main.go
+++ b/go/main.go
@@ -5,6 +5,7 @@ import (
"log"
"net"
"os"
+ "reflect"
"gioui.org/app"
"gioui.org/font/gofont"
@@ -17,6 +18,19 @@ import (
"gioui.org/widget/material"
)
+type XmppEvent uint8
+
+const (
+ XmppDisconnect XmppEvent = iota
+ XmppConnect
+)
+
+type GuiEvent uint8
+
+const (
+ Disconnect GuiEvent = iota
+)
+
type LimoxState uint8
const (
@@ -29,7 +43,8 @@ type Limox struct {
JidEditor widget.Editor
PwdEditor widget.Editor
MainButton widget.Clickable
- XmppError chan error
+ XmppEvents chan any
+ GuiEvents chan GuiEvent
State LimoxState
Window *app.Window
Operations op.Ops
@@ -58,7 +73,8 @@ func NewLimox() Limox {
),
Operations: op.Ops{},
Theme: material.NewTheme(gofont.Collection()),
- XmppError: make(chan error),
+ XmppEvents: make(chan any),
+ GuiEvents: make(chan GuiEvent),
State: Disconnected,
}
}
@@ -76,8 +92,23 @@ func (l *Limox) run() error {
}
l.draw(e)
}
- case <-l.XmppError:
- l.State = Disconnected
+ case ev := <-l.XmppEvents:
+ switch ev.(type) {
+ case error:
+ log.Print(ev)
+ l.State = Disconnected
+ case XmppEvent:
+ switch ev {
+ case XmppDisconnect:
+ l.State = Disconnected
+ case XmppConnect:
+ l.State = Connected
+ default:
+ log.Fatalf("Unknown XmppEvent '%d'\n", ev)
+ }
+ default:
+ log.Fatalf("Unknown event type '%s'.\n", reflect.TypeOf(ev))
+ }
}
}
}
@@ -93,6 +124,7 @@ func (l *Limox) buttonCallback() {
l.State = Disconnected
case Connected:
log.Println("Disconnected")
+ l.GuiEvents <- Disconnect
l.State = Disconnected
}
}
@@ -105,11 +137,34 @@ func (l *Limox) xmpp(jid string, pwd string) {
tcpServer, err := net.ResolveTCPAddr("tcp", domain+":"+"5222")
if err != nil {
- log.Print(err)
- l.XmppError <- err
+ l.XmppEvents <- err
return
}
log.Printf("Server: %s\n", tcpServer)
+
+ conn, err := net.DialTCP("tcp", nil, tcpServer)
+ if err != nil {
+ l.XmppEvents <- err
+ return
+ }
+ l.XmppEvents <- XmppConnect
+
+ var closing bool = false
+ for {
+ ev := <-l.GuiEvents
+ switch ev {
+ case Disconnect:
+ closing = true
+ default:
+ log.Fatalf("Unknown GuiEvent '%d'!\n", ev)
+ }
+ if closing {
+ break
+ }
+ }
+
+ conn.Close()
+ l.XmppEvents <- XmppDisconnect
}
// domainpart extracts the domain name from a JID / XMPP address. See