diff options
author | xengineering <me@xengineering.eu> | 2023-05-15 21:19:30 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-15 21:19:30 +0200 |
commit | 01011c81c6fafb7435a98c9bf2309082cf5b13f8 (patch) | |
tree | eeb6f7c79147ce689dc9d803b4202f9c13561523 | |
parent | 8023777ac1b0171783e1c55f67a6179d63708272 (diff) | |
download | limox-01011c81c6fafb7435a98c9bf2309082cf5b13f8.tar limox-01011c81c6fafb7435a98c9bf2309082cf5b13f8.tar.zst limox-01011c81c6fafb7435a98c9bf2309082cf5b13f8.zip |
Split XML token decoder logic into xmpp/decoder.go
-rw-r--r-- | xmpp/decoder.go | 43 | ||||
-rw-r--r-- | xmpp/xmpp.go | 44 |
2 files changed, 47 insertions, 40 deletions
diff --git a/xmpp/decoder.go b/xmpp/decoder.go new file mode 100644 index 0000000..97329c7 --- /dev/null +++ b/xmpp/decoder.go @@ -0,0 +1,43 @@ +package xmpp + +import ( + "crypto/tls" + "encoding/xml" + "log" +) + +type decoder struct { + terminator chan bool + data chan xml.Token + decoder *xml.Decoder +} + +func newDecoder(conn *tls.Conn) decoder { + return decoder{ + make(chan bool), + make(chan xml.Token), + xml.NewDecoder(conn), + } +} + +func (r *decoder) run() { + for { + select { + case <-r.terminator: + return + default: + t, err := r.decoder.Token() + if err != nil { + log.Print(err) + } + if t != nil { + c := xml.CopyToken(t) + r.data <- c + } + } + } +} + +func (r *decoder) stop() { + r.terminator <- true +} diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 64ca494..fa61e0b 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -24,9 +24,9 @@ func Run(rxChan chan Event, txChan chan any, jid string, pwd string) { } defer conn.Close() - receiver := newXmppReceiver(conn) - go receiver.run() - defer receiver.stop() + decoder := newDecoder(conn) + go decoder.run() + defer decoder.stop() enc := xml.NewEncoder(conn) defer enc.Close() @@ -48,7 +48,7 @@ func Run(rxChan chan Event, txChan chan any, jid string, pwd string) { default: log.Printf("Unknown Event '%d'!\n", ev) } - case rx := <-receiver.data: + case rx := <-decoder.data: dbg.Indent("S: ", " ") dbg.EncodeToken(rx) dbg.Flush() @@ -56,42 +56,6 @@ func Run(rxChan chan Event, txChan chan any, jid string, pwd string) { } } -type xmppReceiver struct { - terminator chan bool - data chan xml.Token - decoder *xml.Decoder -} - -func newXmppReceiver(conn *tls.Conn) xmppReceiver { - return xmppReceiver{ - make(chan bool), - make(chan xml.Token), - xml.NewDecoder(conn), - } -} - -func (r *xmppReceiver) run() { - for { - select { - case <-r.terminator: - return - default: - t, err := r.decoder.Token() - if err != nil { - log.Print(err) - } - if t != nil { - c := xml.CopyToken(t) - r.data <- c - } - } - } -} - -func (r *xmppReceiver) stop() { - r.terminator <- true -} - func setupConn(jid string) (*tls.Conn, error) { domain := domainpart(jid) |