xmpp/id.go
author Chris Jones <christian.jones@sri.com>
Sun, 09 Feb 2014 09:50:38 -0700
changeset 182 626c390682fc
parent 126 367e76b3028e
permissions -rw-r--r--
Added a license (and my email address).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
118
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     1
package xmpp
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     2
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     3
// Code to generate unique IDs for outgoing messages.
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     4
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     5
import (
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     6
	"fmt"
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     7
)
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     8
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     9
var id <-chan string
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    10
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    11
func init() {
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    12
	// Start the unique id generator.
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    13
	idCh := make(chan string)
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    14
	id = idCh
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    15
	go func(ch chan<- string) {
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    16
		id := int64(1)
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    17
		for {
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    18
			str := fmt.Sprintf("id_%d", id)
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    19
			ch <- str
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    20
			id++
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    21
		}
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    22
	}(idCh)
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    23
}
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    24
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    25
// This function may be used as a convenient way to generate a unique
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    26
// id for an outgoing iq, message, or presence stanza.
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    27
func NextId() string {
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    28
	return <-id
fb9bb98a8d70 Code cleanup: Renamed the Version constant. Moved the unique-ID generator into its own file.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    29
}