Rename Options to Config

This commit is contained in:
Mickael Remond 2018-09-26 16:25:04 +02:00
parent 1c3aaad174
commit fa5590e921
No known key found for this signature in database
GPG Key ID: E6F6045D79965AA3
6 changed files with 29 additions and 29 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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