From 6aa1e668eea5b988f5b355a206fc05e32ff56a85 Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Fri, 8 Nov 2019 12:07:55 +0100 Subject: [PATCH] Fix issues after refactor Relates to #126 --- _examples/go.sum | 2 ++ _examples/muc_bot/README.md | 3 +++ client.go | 1 + component.go | 13 +++++++------ stream_manager.go | 3 +++ xmpp_transport.go | 15 +++++++++++++-- 6 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 _examples/muc_bot/README.md diff --git a/_examples/go.sum b/_examples/go.sum index 6e3b0bc..19c4ace 100644 --- a/_examples/go.sum +++ b/_examples/go.sum @@ -23,6 +23,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190908185732-236ed259b199/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= @@ -102,4 +103,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gotest.tools v2.1.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= gotest.tools/gotestsum v0.3.5/go.mod h1:Mnf3e5FUzXbkCfynWBGOwLssY7gTQgCHObK9tMpAriY= mvdan.cc/sh v2.6.4+incompatible/go.mod h1:IeeQbZq+x2SUGBensq/jge5lLQbS3XT2ktyp3wrt4x8= +nhooyr.io/websocket v1.6.5 h1:8TzpkldRfefda5JST+CnOH135bzVPz5uzfn/AF+gVKg= nhooyr.io/websocket v1.6.5/go.mod h1:F259lAzPRAH0htX2y3ehpJe09ih1aSHN7udWki1defY= diff --git a/_examples/muc_bot/README.md b/_examples/muc_bot/README.md new file mode 100644 index 0000000..24414fa --- /dev/null +++ b/_examples/muc_bot/README.md @@ -0,0 +1,3 @@ +# XMPP Multi-User (MUC) chat bot example + +This code shows how to build a simple basic XMPP Multi-User chat bot using Fluux Go XMPP library. diff --git a/client.go b/client.go index a7e6c7d..b7111f2 100644 --- a/client.go +++ b/client.go @@ -25,6 +25,7 @@ const ( StateConnected StateSessionEstablished StateStreamError + StatePermanentError ) // Event is a structure use to convey event changes related to client state. This diff --git a/component.go b/component.go index d459c00..d3c890e 100644 --- a/component.go +++ b/component.go @@ -64,6 +64,7 @@ func (c *Component) Connect() error { var state SMState return c.Resume(state) } + func (c *Component) Resume(sm SMState) error { var err error var streamId string @@ -72,13 +73,13 @@ func (c *Component) Resume(sm SMState) error { } c.transport, err = NewComponentTransport(c.ComponentOptions.TransportConfiguration) if err != nil { - c.updateState(StateStreamError) - return err + c.updateState(StatePermanentError) + return NewConnError(err, true) } if streamId, err = c.transport.Connect(); err != nil { - c.updateState(StateStreamError) - return err + c.updateState(StatePermanentError) + return NewConnError(err, true) } c.updateState(StateConnected) @@ -91,7 +92,7 @@ func (c *Component) Resume(sm SMState) error { // Check server response for authentication val, err := stanza.NextPacket(c.decoder) if err != nil { - c.updateState(StateDisconnected) + c.updateState(StatePermanentError) return NewConnError(err, true) } @@ -105,7 +106,7 @@ func (c *Component) Resume(sm SMState) error { go c.recv() return nil default: - c.updateState(StateStreamError) + c.updateState(StatePermanentError) return NewConnError(errors.New("expecting handshake result, got "+v.Name()), true) } } diff --git a/stream_manager.go b/stream_manager.go index 1011f6e..bf7fba8 100644 --- a/stream_manager.go +++ b/stream_manager.go @@ -86,9 +86,12 @@ func (sm *StreamManager) Run() error { case StateStreamError: sm.client.Disconnect() // Only try reconnecting if we have not been kicked by another session to avoid connection loop. + // TODO: Make this conflict exception a permanent error if e.StreamError != "conflict" { sm.connect() } + case StatePermanentError: + // Do not attempt to reconnect } } sm.client.SetHandler(handler) diff --git a/xmpp_transport.go b/xmpp_transport.go index a67d5bc..99e0f56 100644 --- a/xmpp_transport.go +++ b/xmpp_transport.go @@ -116,16 +116,27 @@ func (t XMPPTransport) Ping() error { } func (t XMPPTransport) Read(p []byte) (n int, err error) { + if t.readWriter == nil { + return 0, errors.New("cannot read: not connected, no readwriter") + } return t.readWriter.Read(p) } func (t XMPPTransport) Write(p []byte) (n int, err error) { + if t.readWriter == nil { + return 0, errors.New("cannot write: not connected, no readwriter") + } return t.readWriter.Write(p) } func (t XMPPTransport) Close() error { - _, _ = t.readWriter.Write([]byte("")) - return t.conn.Close() + if t.readWriter != nil { + _, _ = t.readWriter.Write([]byte("")) + } + if t.conn != nil { + return t.conn.Close() + } + return nil } func (t *XMPPTransport) LogTraffic(logFile io.Writer) {