summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-05-15 20:23:16 +0200
committerxengineering <me@xengineering.eu>2023-05-15 20:23:16 +0200
commit757a8b7856bb7f72db6d95c208ba8cf66c0d54e7 (patch)
tree93a48c67205adaca09dc55837bd35dc19e87e21c
parentc6090c0d375f28002166a463cfc7b7cda4425626 (diff)
downloadlimox-757a8b7856bb7f72db6d95c208ba8cf66c0d54e7.tar
limox-757a8b7856bb7f72db6d95c208ba8cf66c0d54e7.tar.zst
limox-757a8b7856bb7f72db6d95c208ba8cf66c0d54e7.zip
Introduce type xmppReceiver
This simplifies the setup of the XML decoder.
-rw-r--r--xmpp.go51
1 files changed, 33 insertions, 18 deletions
diff --git a/xmpp.go b/xmpp.go
index c432a62..015b9e1 100644
--- a/xmpp.go
+++ b/xmpp.go
@@ -23,10 +23,9 @@ func xmpp(rxChan chan GuiEvent, txChan chan any, jid string, pwd string) {
}
defer conn.Close()
- receiver := make(chan xml.Token)
- termination := make(chan bool)
- go rxRoutine(conn, receiver, termination)
- defer func() { termination <- true }()
+ receiver := newXmppReceiver(conn)
+ go receiver.run()
+ defer receiver.stop()
enc := xml.NewEncoder(conn)
defer enc.Close()
@@ -48,7 +47,7 @@ func xmpp(rxChan chan GuiEvent, txChan chan any, jid string, pwd string) {
default:
log.Printf("Unknown GuiEvent '%d'!\n", ev)
}
- case rx := <-receiver:
+ case rx := <-receiver.data:
dbg.Indent("S: ", " ")
dbg.EncodeToken(rx)
dbg.Flush()
@@ -56,36 +55,52 @@ func xmpp(rxChan chan GuiEvent, txChan chan any, jid string, pwd string) {
}
}
-func setupConn(jid string) (*tls.Conn, error) {
- domain := domainpart(jid)
+type xmppReceiver struct {
+ terminator chan bool
+ data chan xml.Token
+ decoder *xml.Decoder
+}
- roots, err := x509.SystemCertPool()
- if err != nil {
- return nil, err
+func newXmppReceiver(conn *tls.Conn) xmppReceiver {
+ return xmppReceiver{
+ make(chan bool),
+ make(chan xml.Token),
+ xml.NewDecoder(conn),
}
-
- return tls.Dial("tcp", domain+":"+"5223", &tls.Config{RootCAs: roots})
}
-func rxRoutine(conn *tls.Conn, tokens chan xml.Token, terminator chan bool) {
- dec := xml.NewDecoder(conn)
-
+func (r *xmppReceiver) run() {
for {
select {
- case <-terminator:
+ case <-r.terminator:
return
default:
- t, err := dec.Token()
+ t, err := r.decoder.Token()
if err != nil {
log.Print(err)
}
if t != nil {
- tokens <- t
+ r.data <- t
}
}
}
}
+func (r *xmppReceiver) stop() {
+ r.terminator <- true
+}
+
+func setupConn(jid string) (*tls.Conn, error) {
+ domain := domainpart(jid)
+
+ roots, err := x509.SystemCertPool()
+ if err != nil {
+ return nil, err
+ }
+
+ return tls.Dial("tcp", domain+":"+"5223", &tls.Config{RootCAs: roots})
+}
+
func sendStreamStart(enc *xml.Encoder, dbg *xml.Encoder, jid string) xml.EndElement {
start := xml.StartElement{
xml.Name{"jabber:client", "stream:stream"},