Make client use the new Router

This commit is contained in:
Mickael Remond 2019-06-18 12:34:25 +02:00 committed by Mickaël Rémond
parent 7aef8357ed
commit 45c7ca74b1

View file

@ -70,8 +70,8 @@ type Client struct {
Session *Session Session *Session
// TCP level connection / can be replaced by a TLS session after starttls // TCP level connection / can be replaced by a TLS session after starttls
conn net.Conn conn net.Conn
// Packet channel // Router is used to dispatch packets
RecvChannel chan Packet router *Router
// Track and broadcast connection state // Track and broadcast connection state
EventManager EventManager
} }
@ -84,7 +84,7 @@ Setting up the client / Checking the parameters
// If host is not specified, the DNS SRV should be used to find the host from the domainpart of the JID. // 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. // Default the port to 5222.
// TODO: better config checks // TODO: better config checks
func NewClient(config Config) (c *Client, err error) { func NewClient(config Config, r *Router) (c *Client, err error) {
// TODO: If option address is nil, use the Jid domain to compose the address // TODO: If option address is nil, use the Jid domain to compose the address
if config.Address, err = checkAddress(config.Address); err != nil { if config.Address, err = checkAddress(config.Address); err != nil {
return nil, NewConnError(err, true) return nil, NewConnError(err, true)
@ -103,14 +103,12 @@ func NewClient(config Config) (c *Client, err error) {
c = new(Client) c = new(Client)
c.config = config c.config = config
c.router = r
if c.config.ConnectTimeout == 0 { if c.config.ConnectTimeout == 0 {
c.config.ConnectTimeout = 15 // 15 second as default c.config.ConnectTimeout = 15 // 15 second as default
} }
// Create a default channel that developers can override
c.RecvChannel = make(chan Packet)
return return
} }
@ -173,13 +171,6 @@ func (c *Client) SetHandler(handler EventHandler) {
c.Handler = handler c.Handler = handler
} }
// Recv abstracts receiving preparsed XMPP packets from a channel.
// Channel allow client to receive / dispatch packets in for range loop.
// TODO: Deprecate this function in favor of reading directly from the RecvChannel ?
func (c *Client) Recv() <-chan Packet {
return c.RecvChannel
}
// Send marshals XMPP stanza and sends it to the server. // Send marshals XMPP stanza and sends it to the server.
func (c *Client) Send(packet Packet) error { func (c *Client) Send(packet Packet) error {
data, err := xml.Marshal(packet) data, err := xml.Marshal(packet)
@ -219,13 +210,13 @@ func (c *Client) recv(keepaliveQuit chan<- struct{}) (err error) {
// Handle stream errors // Handle stream errors
switch packet := val.(type) { switch packet := val.(type) {
case StreamError: case StreamError:
c.RecvChannel <- val c.router.Route(c, val)
close(c.RecvChannel) close(keepaliveQuit)
c.streamError(packet.Error.Local, packet.Text) c.streamError(packet.Error.Local, packet.Text)
return errors.New("stream error: " + packet.Error.Local) return errors.New("stream error: " + packet.Error.Local)
} }
c.RecvChannel <- val c.router.Route(c, val)
} }
} }