From fa5590e9211610c5fd8a144342a5a7b5de06b797 Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Wed, 26 Sep 2018 16:25:04 +0200 Subject: [PATCH] Rename Options to Config --- client.go | 26 +++++++++++++------------- client_test.go | 8 ++++---- cmd/xmpp_echo/xmpp_echo.go | 4 ++-- cmd/xmpp_jukebox/xmpp_jukebox.go | 4 ++-- session.go | 14 +++++++------- socket_proxy.go | 2 +- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/client.go b/client.go index dc2271a..ffb8274 100644 --- a/client.go +++ b/client.go @@ -14,7 +14,7 @@ import ( // server. type Client struct { // Store user defined options - options Options + config Config // Session gather data that can be accessed by users of this library Session *Session // TCP level connection / can be replaced by a TLS session after starttls @@ -25,31 +25,31 @@ type Client struct { Setting up the client / Checking the parameters */ -// NewClient generates a new XMPP client, based on Options passed as parameters. +// NewClient generates a new XMPP client, based on Config passed as parameters. // If host is not specified, the DNS SRV should be used to find the host from the domainpart of the JID. // Default the port to 5222. -// TODO: better options checks -func NewClient(options Options) (c *Client, err error) { +// TODO: better config checks +func NewClient(config Config) (c *Client, err error) { // TODO: If option address is nil, use the Jid domain to compose the address - if options.Address, err = checkAddress(options.Address); err != nil { + if config.Address, err = checkAddress(config.Address); err != nil { return } - if options.Password == "" { + if config.Password == "" { err = errors.New("missing password") return } c = new(Client) - c.options = options + c.config = config // Parse JID - if c.options.parsedJid, err = NewJid(c.options.Jid); err != nil { + if c.config.parsedJid, err = NewJid(c.config.Jid); err != nil { return } - if c.options.ConnectTimeout == 0 { - c.options.ConnectTimeout = 15 // 15 second as default + if c.config.ConnectTimeout == 0 { + c.config.ConnectTimeout = 15 // 15 second as default } return } @@ -80,8 +80,8 @@ func (c *Client) Connect() (*Session, error) { // TODO: Refactor = abstract retry loop in capped exponential back-off function var try = 0 var success bool - for try <= c.options.Retry || !success { - if tcpconn, err = net.DialTimeout("tcp", c.options.Address, time.Duration(c.options.ConnectTimeout)*time.Second); err == nil { + for try <= c.config.Retry || !success { + if tcpconn, err = net.DialTimeout("tcp", c.config.Address, time.Duration(c.config.ConnectTimeout)*time.Second); err == nil { success = true } try++ @@ -92,7 +92,7 @@ func (c *Client) Connect() (*Session, error) { // Connection is ok, we now open XMPP session c.conn = tcpconn - if c.conn, c.Session, err = NewSession(c.conn, c.options); err != nil { + if c.conn, c.Session, err = NewSession(c.conn, c.config); err != nil { return c.Session, err } diff --git a/client_test.go b/client_test.go index 5ed938b..a4ee4d0 100644 --- a/client_test.go +++ b/client_test.go @@ -23,11 +23,11 @@ func TestClient_Connect(t *testing.T) { mock.Start(t, testXMPPAddress, handlerConnectSuccess) // Test / Check result - options := Options{Address: testXMPPAddress, Jid: "test@localhost", Password: "test", Insecure: true} + config := Config{Address: testXMPPAddress, Jid: "test@localhost", Password: "test", Insecure: true} var client *Client var err error - if client, err = NewClient(options); err != nil { + if client, err = NewClient(config); err != nil { t.Errorf("connect create XMPP client: %s", err) } @@ -44,11 +44,11 @@ func TestClient_NoInsecure(t *testing.T) { mock.Start(t, testXMPPAddress, handlerAbortTLS) // Test / Check result - options := Options{Address: testXMPPAddress, Jid: "test@localhost", Password: "test"} + config := Config{Address: testXMPPAddress, Jid: "test@localhost", Password: "test"} var client *Client var err error - if client, err = NewClient(options); err != nil { + if client, err = NewClient(config); err != nil { t.Errorf("cannot create XMPP client: %s", err) } diff --git a/cmd/xmpp_echo/xmpp_echo.go b/cmd/xmpp_echo/xmpp_echo.go index c6e4486..5e1024f 100644 --- a/cmd/xmpp_echo/xmpp_echo.go +++ b/cmd/xmpp_echo/xmpp_echo.go @@ -13,7 +13,7 @@ import ( ) func main() { - options := xmpp.Options{ + config := xmpp.Config{ Address: "localhost:5222", Jid: "test@localhost", Password: "test", @@ -21,7 +21,7 @@ func main() { Insecure: true, } - client, err := xmpp.NewClient(options) + client, err := xmpp.NewClient(config) if err != nil { log.Fatal("Error: ", err) } diff --git a/cmd/xmpp_jukebox/xmpp_jukebox.go b/cmd/xmpp_jukebox/xmpp_jukebox.go index 76e0d66..dd51a51 100644 --- a/cmd/xmpp_jukebox/xmpp_jukebox.go +++ b/cmd/xmpp_jukebox/xmpp_jukebox.go @@ -99,11 +99,11 @@ func playSCURL(p *mpg123.Player, rawURL string) { } func connectXmpp(jid string, password string, address string) (client *xmpp.Client, err error) { - xmppOptions := xmpp.Options{Address: address, + xmppConfig := xmpp.Config{Address: address, Jid: jid, Password: password, PacketLogger: os.Stdout, Insecure: true, Retry: 10} - if client, err = xmpp.NewClient(xmppOptions); err != nil { + if client, err = xmpp.NewClient(xmppConfig); err != nil { return } diff --git a/session.go b/session.go index 1b33e14..bdeb75c 100644 --- a/session.go +++ b/session.go @@ -31,7 +31,7 @@ type Session struct { err error } -func NewSession(conn net.Conn, o Options) (net.Conn, *Session, error) { +func NewSession(conn net.Conn, o Config) (net.Conn, *Session, error) { s := new(Session) s.init(conn, o) @@ -62,12 +62,12 @@ func (s *Session) PacketId() string { return fmt.Sprintf("%x", s.lastPacketId) } -func (s *Session) init(conn net.Conn, o Options) { +func (s *Session) init(conn net.Conn, o Config) { s.setProxy(nil, conn, o) s.Features = s.open(o.parsedJid.domain) } -func (s *Session) reset(conn net.Conn, newConn net.Conn, o Options) { +func (s *Session) reset(conn net.Conn, newConn net.Conn, o Config) { if s.err != nil { return } @@ -77,7 +77,7 @@ func (s *Session) reset(conn net.Conn, newConn net.Conn, o Options) { } // TODO: setProxyLogger ? better name ? This is not a TCP / HTTP proxy -func (s *Session) setProxy(conn net.Conn, newConn net.Conn, o Options) { +func (s *Session) setProxy(conn net.Conn, newConn net.Conn, o Config) { if newConn != conn { s.socketProxy = newSocketProxy(newConn, o.PacketLogger) } @@ -135,7 +135,7 @@ func (s *Session) startTlsIfSupported(conn net.Conn, domain string) net.Conn { return conn } -func (s *Session) auth(o Options) { +func (s *Session) auth(o Config) { if s.err != nil { return } @@ -143,7 +143,7 @@ func (s *Session) auth(o Options) { s.err = authSASL(s.socketProxy, s.decoder, s.Features, o.parsedJid.username, o.Password) } -func (s *Session) bind(o Options) { +func (s *Session) bind(o Config) { if s.err != nil { return } @@ -176,7 +176,7 @@ func (s *Session) bind(o Options) { // TODO: remove when ejabberd is fixed: https://github.com/processone/ejabberd/issues/869 // After the bind, if the session is required (as per old RFC 3921), we send the session open iq -func (s *Session) rfc3921Session(o Options) { +func (s *Session) rfc3921Session(o Config) { if s.err != nil { return } diff --git a/socket_proxy.go b/socket_proxy.go index 6cabcee..5fe0367 100644 --- a/socket_proxy.go +++ b/socket_proxy.go @@ -6,7 +6,7 @@ import ( ) // Mediated Read / Write on socket -// Used if logFile from Options is not nil +// Used if logFile from Config is not nil type socketProxy struct { socket io.ReadWriter // Actual connection logFile *os.File