diff options
author | xengineering <me@xengineering.eu> | 2023-06-03 20:52:45 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-06-03 20:52:45 +0200 |
commit | e313bce92afea09b2da657b5a4f7de2c958c0100 (patch) | |
tree | 8975cb4432d2ed5195b3902c80cd5e702ea40cc0 | |
parent | 30d99c706e3a75948c49289fc6b0317258957819 (diff) | |
download | limox-e313bce92afea09b2da657b5a4f7de2c958c0100.tar limox-e313bce92afea09b2da657b5a4f7de2c958c0100.tar.zst limox-e313bce92afea09b2da657b5a4f7de2c958c0100.zip |
Introduce new communication pattern
-rw-r--r-- | limox.go | 24 | ||||
-rw-r--r-- | xmpp/session.go | 42 |
2 files changed, 31 insertions, 35 deletions
@@ -26,8 +26,8 @@ type Limox struct { JidEditor widget.Editor PwdEditor widget.Editor MainButton widget.Clickable - session *xmpp.Session - XmppConn chan xmpp.Event + sessionOut chan any + sessionIn chan any State LimoxState Window *app.Window Operations op.Ops @@ -42,7 +42,7 @@ func NewLimox() Limox { ), Operations: op.Ops{}, Theme: material.NewTheme(gofont.Collection()), - XmppConn: make(chan xmpp.Event), + sessionIn: make(chan any), State: Disconnected, } @@ -65,14 +65,14 @@ func (l *Limox) run() error { } l.draw(e) } - case ev := <-l.XmppConn: - switch ev { - case xmpp.DisconnectEvent: + case data := <-l.sessionIn: + switch data.(type) { + case xmpp.SessionDisconnect: l.State = Disconnected - case xmpp.ConnectEvent: + case xmpp.SessionConnect: l.State = Connected default: - log.Printf("Unknown xmpp.Event '%d'\n", ev) + log.Printf("Unknown XMPP data '%d'\n", data) } l.Window.Invalidate() } @@ -86,15 +86,13 @@ func (l *Limox) buttonCallback() { pwd := l.PwdEditor.Text() setLastJid(jid) setLastPwd(pwd) - l.session = xmpp.NewSession(jid, pwd) - l.XmppConn = l.session.Out - go l.session.Run() + l.sessionOut = xmpp.StartSession(l.sessionIn, jid, pwd) l.State = Connecting case Connecting: - l.session.Close() + go func() { l.sessionOut <- xmpp.SessionShouldDisconnect{} }() l.State = Disconnected case Connected: - l.session.Close() + go func() { l.sessionOut <- xmpp.SessionShouldDisconnect{} }() l.State = Disconnected } } diff --git a/xmpp/session.go b/xmpp/session.go index 80c07a1..2e7bade 100644 --- a/xmpp/session.go +++ b/xmpp/session.go @@ -1,46 +1,44 @@ package xmpp import ( - "time" "log" + "time" ) +type SessionConnect struct{} +type SessionDisconnect struct{} +type SessionShouldDisconnect struct{} + type Session struct { - in, Out chan Event + in, out chan any } -func NewSession(jid string, pwd string) *Session { +func StartSession(out chan any, jid string, pwd string) chan any { s := Session{} - s.in = make(chan Event) - s.Out = make(chan Event) + s.in = make(chan any) + s.out = out + + go s.run() - return &s + return s.in } -func (s *Session) Run() { - defer func() { s.Out <- DisconnectEvent }() +func (s *Session) run() { + defer func() { s.out <- SessionDisconnect{} }() - s.init() + time.Sleep(time.Second) // faked connect time + s.out <- SessionConnect{} for { select { - case ev := <-s.in: - switch ev { - case ShouldDisconnectEvent: + case data := <-s.in: + switch data.(type) { + case SessionShouldDisconnect: return default: - log.Printf("Unknown Event '%d'!\n", ev) + log.Printf("Unknown data '%d'!\n", data) } } } } - -func (s *Session) init() { - time.Sleep(time.Second) - s.Out <- ConnectEvent -} - -func (s *Session) Close() { - go func(){ s.in <- ShouldDisconnectEvent }() -} |