From c9c8526476bb027cce562abe16c7e1254047044f Mon Sep 17 00:00:00 2001 From: Matt Goodall Date: Sun, 8 Jul 2012 23:29:51 +0100 Subject: [PATCH] Separate stream creation from XMPP use (client, component, etc). The address of the server is not necessarily related to the JID, e.g. DNS SRV lookups, manual configuration, etc. Splitting them up means there's more control and flexibility over how things are put together. We can always add convenience funcs later, e.g. something like DialClient(jid, password), to handle the common case. --- client.go | 10 +++++++++- component.go | 11 ++++++++++- src/xmpp/client.go | 9 ++------- src/xmpp/component.go | 9 ++------- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/client.go b/client.go index 366ce88..0c82c39 100644 --- a/client.go +++ b/client.go @@ -12,12 +12,20 @@ var ( ) func main() { + // Parse args. flag.Parse() jid, _ := xmpp.ParseJID(*jid) password := *password - x, err := xmpp.NewClientXMPP(jid, password, &xmpp.ClientConfig{InsecureSkipVerify: true}) + // Create stream. + stream, err := xmpp.NewStream(jid.Domain + ":5222") + if err != nil { + log.Fatal(err) + } + + // Configure stream as a client connection. + x, err := xmpp.NewClientXMPP(stream, jid, password, &xmpp.ClientConfig{InsecureSkipVerify: true}) if err != nil { log.Fatal(err) } diff --git a/component.go b/component.go index b56391c..cd49f45 100644 --- a/component.go +++ b/component.go @@ -7,16 +7,25 @@ import ( ) var ( + addr = flag.String("a", "", "Server component address") jid = flag.String("j", "", "JID") secret = flag.String("s", "", "Component secret") ) func main() { flag.Parse() + addr := *addr jid, _ := xmpp.ParseJID(*jid) secret := *secret - x, err := xmpp.NewComponentXMPP("localhost:5347", jid, secret) + // Create stream. + stream, err := xmpp.NewStream(addr) + if err != nil { + log.Fatal(err) + } + + // Configure stream as a component connection. + x, err := xmpp.NewComponentXMPP(stream, jid, secret) if err != nil { log.Fatal(err) } diff --git a/src/xmpp/client.go b/src/xmpp/client.go index 29c3fb6..cc28781 100644 --- a/src/xmpp/client.go +++ b/src/xmpp/client.go @@ -19,13 +19,8 @@ type ClientConfig struct { InsecureSkipVerify bool } -// Create a client XMPP stream. -func NewClientXMPP(jid JID, password string, config *ClientConfig) (*XMPP, error) { - - stream, err := NewStream(jid.Domain + ":5222") - if err != nil { - return nil, err - } +// Create a client XMPP over the stream. +func NewClientXMPP(stream *Stream, jid JID, password string, config *ClientConfig) (*XMPP, error) { for { diff --git a/src/xmpp/component.go b/src/xmpp/component.go index 8470aa3..246313a 100644 --- a/src/xmpp/component.go +++ b/src/xmpp/component.go @@ -7,13 +7,8 @@ import ( "fmt" ) -// Create a component XMPP connection. -func NewComponentXMPP(addr string, jid JID, secret string) (*XMPP, error) { - - stream, err := NewStream(addr) - if err != nil { - return nil, err - } +// Create a component XMPP connection over the stream. +func NewComponentXMPP(stream *Stream, jid JID, secret string) (*XMPP, error) { streamId, err := startComponent(stream, jid) if err != nil {