From 93668403433cbfc22fe9c38ebbd6ef67af625ad2 Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 4 Sep 2023 22:18:53 +0200 Subject: xmpp: Add type SessionFrontend Some parts of the `xmpp` module functionality should be written for the using application software. To reference a running session more easily it is an advantage to have an opaque struct for this instead of decoupled channels. --- limox.go | 13 +++++-------- xmpp/session.go | 13 +++++++++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/limox.go b/limox.go index 0b5c2ac..6c83e45 100644 --- a/limox.go +++ b/limox.go @@ -25,8 +25,7 @@ type Limox struct { JidEditor widget.Editor PwdEditor widget.Editor MainButton widget.Clickable - sessionIn chan<- any - sessionOut chan any + frontend xmpp.SessionFrontend State LimoxState Window *app.Window Operations op.Ops @@ -41,7 +40,6 @@ func NewLimox() Limox { ), Operations: op.Ops{}, Theme: material.NewTheme(), - sessionOut: make(chan any), State: Disconnected, } @@ -64,7 +62,7 @@ func (l *Limox) run() error { } l.draw(e) } - case data := <-l.sessionOut: + case data := <-l.frontend.In: switch data.(type) { case xmpp.SessionDisconnect: l.State = Disconnected @@ -79,20 +77,19 @@ func (l *Limox) run() error { } func (l *Limox) buttonCallback() { - c := l.sessionIn switch l.State { case Disconnected: jid := l.JidEditor.Text() pwd := l.PwdEditor.Text() setLastJid(jid) setLastPwd(pwd) - l.sessionIn = xmpp.StartSession(l.sessionOut, jid, pwd) + l.frontend = xmpp.StartSession(jid, pwd) l.State = Connecting case Connecting: - go func() { c <- xmpp.SessionShouldDisconnect{} }() + go func() { l.frontend.Out <- xmpp.SessionShouldDisconnect{} }() l.State = Disconnected case Connected: - go func() { c <- xmpp.SessionShouldDisconnect{} }() + go func() { l.frontend.Out <- xmpp.SessionShouldDisconnect{} }() l.State = Disconnected } } diff --git a/xmpp/session.go b/xmpp/session.go index 4dfd76f..ad90c5e 100644 --- a/xmpp/session.go +++ b/xmpp/session.go @@ -13,29 +13,34 @@ type SessionConnect struct{} type SessionDisconnect struct{} type SessionShouldDisconnect struct{} +type SessionFrontend struct { + In chan any + Out chan any +} + type session struct { jid string pwd string in chan any - out chan<- any + out chan any transport *tls.Conn tx *xml.Encoder rx chan any resourceReq string } -func StartSession(out chan<- any, jid string, pwd string) (in chan<- any) { +func StartSession(jid string, pwd string) (SessionFrontend) { s := session{} s.jid = jid s.pwd = pwd s.in = make(chan any) - s.out = out + s.out = make(chan any) s.rx = make(chan any, 0) go s.run() - return s.in + return SessionFrontend{s.out, s.in} } func (s *session) run() { -- cgit v1.2.3-70-g09d2