Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
authorChris Jones <christian.jones@sri.com>
Fri, 30 Aug 2013 17:24:39 -0600
changeset 118 fb9bb98a8d70
parent 116 5c6d6d51d3ba
child 119 712aa5780660
Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
id.go
roster.go
stream.go
xmpp.go
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/id.go	Fri Aug 30 17:24:39 2013 -0600
@@ -0,0 +1,29 @@
+package xmpp
+
+// Code to generate unique IDs for outgoing messages.
+
+import (
+	"fmt"
+)
+
+var id <-chan string
+
+func init() {
+	// Start the unique id generator.
+	idCh := make(chan string)
+	id = idCh
+	go func(ch chan<- string) {
+		id := int64(1)
+		for {
+			str := fmt.Sprintf("id_%d", id)
+			ch <- str
+			id++
+		}
+	}(idCh)
+}
+
+// This function may be used as a convenient way to generate a unique
+// id for an outgoing iq, message, or presence stanza.
+func NextId() string {
+	return <-id
+}
--- a/roster.go	Tue Mar 26 10:54:44 2013 -0600
+++ b/roster.go	Fri Aug 30 17:24:39 2013 -0600
@@ -49,7 +49,7 @@
 	rosterUpdate := rosterClients[client.Uid].rosterUpdate
 
 	iq := &Iq{Header: Header{From: client.Jid.String(), Type: "get",
-		Id: <-Id, Nested: []interface{}{RosterQuery{}}}}
+		Id: NextId(), Nested: []interface{}{RosterQuery{}}}}
 	ch := make(chan error)
 	f := func(v Stanza) bool {
 		defer close(ch)
--- a/stream.go	Tue Mar 26 10:54:44 2013 -0600
+++ b/stream.go	Fri Aug 30 17:24:39 2013 -0600
@@ -390,7 +390,7 @@
 
 	// Now re-send the initial handshake message to start the new
 	// session.
-	hsOut := &stream{To: cl.Jid.Domain, Version: Version}
+	hsOut := &stream{To: cl.Jid.Domain, Version: XMPPVersion}
 	cl.xmlOut <- hsOut
 }
 
@@ -446,7 +446,7 @@
 	case "success":
 		Info.Log("Sasl authentication succeeded")
 		cl.Features = nil
-		ss := &stream{To: cl.Jid.Domain, Version: Version}
+		ss := &stream{To: cl.Jid.Domain, Version: XMPPVersion}
 		cl.xmlOut <- ss
 	}
 }
@@ -592,7 +592,7 @@
 	if res != "" {
 		bindReq.Resource = &res
 	}
-	msg := &Iq{Header: Header{Type: "set", Id: <-Id,
+	msg := &Iq{Header: Header{Type: "set", Id: NextId(),
 		Nested: []interface{}{bindReq}}}
 	f := func(st Stanza) bool {
 		iq, ok := st.(*Iq)
--- a/xmpp.go	Tue Mar 26 10:54:44 2013 -0600
+++ b/xmpp.go	Fri Aug 30 17:24:39 2013 -0600
@@ -19,7 +19,7 @@
 
 const (
 	// Version of RFC 3920 that we implement.
-	Version = "1.0"
+	XMPPVersion = "1.0"
 
 	// Various XML namespaces.
 	NsClient  = "jabber:client"
@@ -36,24 +36,6 @@
 	clientSrv = "xmpp-client"
 )
 
-// This channel may be used as a convenient way to generate a unique
-// id for an iq, message, or presence stanza.
-var Id <-chan string
-
-func init() {
-	// Start the unique id generator.
-	idCh := make(chan string)
-	Id = idCh
-	go func(ch chan<- string) {
-		id := int64(1)
-		for {
-			str := fmt.Sprintf("id_%d", id)
-			ch <- str
-			id++
-		}
-	}(idCh)
-}
-
 // Extensions can add stanza filters and/or new XML element types.
 type Extension struct {
 	StanzaHandlers map[string]func(*xml.Name) interface{}
@@ -135,7 +117,7 @@
 	}
 
 	cl := new(Client)
-	cl.Uid = <-Id
+	cl.Uid = NextId()
 	cl.password = password
 	cl.Jid = *jid
 	cl.socket = tcp
@@ -173,7 +155,7 @@
 	}
 
 	// Initial handshake.
-	hsOut := &stream{To: jid.Domain, Version: Version}
+	hsOut := &stream{To: jid.Domain, Version: XMPPVersion}
 	cl.xmlOut <- hsOut
 
 	return cl, nil
@@ -267,7 +249,7 @@
 // presence. The presence can be as simple as a newly-initialized
 // Presence struct.  See RFC 3921, Section 3.
 func (cl *Client) StartSession(getRoster bool, pr *Presence) error {
-	id := <-Id
+	id := NextId()
 	iq := &Iq{Header: Header{To: cl.Jid.Domain, Id: id, Type: "set",
 		Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsSession, Local: "session"}}}}}
 	ch := make(chan error)