From d170626154a4853be386c7180050afe09be3ca0b Mon Sep 17 00:00:00 2001 From: xengineering Date: Wed, 28 Jun 2023 10:56:53 +0200 Subject: xmpp: Implement basic elementBuffer This is needed to buffer XML elements of a stream until they are complete and can be given to an element handler. --- xmpp/stream_pair.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'xmpp/stream_pair.go') diff --git a/xmpp/stream_pair.go b/xmpp/stream_pair.go index e02cca6..b13d8a3 100644 --- a/xmpp/stream_pair.go +++ b/xmpp/stream_pair.go @@ -9,6 +9,8 @@ func runStreamPair(s *session) { openStream(s) defer closeStream(s) + buf := newElementBuffer() + for { select { case data := <-s.in: @@ -18,8 +20,22 @@ func runStreamPair(s *session) { default: log.Printf("Unknown data '%d'!\n", data) } - case _ = <-s.rx: - // TODO route received XML token here + case t := <-s.rx: + err := buf.add(t) + if err != nil { + log.Printf("Could not add XML token to buffer: %v\n", err) + return + } + if buf.isComplete() { + element := buf.reset() + // TODO handle XML element here - this is just a dummy: + switch start := element[0].(type) { + case xml.StartElement: + log.Printf("Got XML element `%s`\n", start.Name.Local) + default: + log.Println("No xml.StartElement at start of element buffer!") + } + } } } } -- cgit v1.2.3-70-g09d2