xmpp/roster.go
author Chris Jones <christian.jones@sri.com>
Sat, 07 Sep 2013 10:30:22 -0700
changeset 127 a8f9a0c07fc8
parent 126 367e76b3028e
child 128 8342afcffc92
permissions -rw-r--r--
Renamed client.In and client.Out to Recv and Send, respectively.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     1
// Copyright 2011 The Go Authors.  All rights reserved.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     2
// Use of this source code is governed by a BSD-style
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     3
// license that can be found in the LICENSE file.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     4
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     5
package xmpp
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     6
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
     7
// This file contains support for roster management, RFC 3921, Section 7.
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
     8
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     9
import (
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    10
	"encoding/xml"
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    11
)
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    12
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    13
// Roster query/result
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    14
type RosterQuery struct {
116
Chris Jones <christian.jones@sri.com>
parents: 113
diff changeset
    15
	XMLName xml.Name     `xml:"jabber:iq:roster query"`
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    16
	Item    []RosterItem `xml:"item"`
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    17
}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    18
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    19
// See RFC 3921, Section 7.1.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    20
type RosterItem struct {
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    21
	XMLName      xml.Name `xml:"jabber:iq:roster item"`
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    22
	Jid          string   `xml:"jid,attr"`
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    23
	Subscription string   `xml:"subscription,attr"`
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    24
	Name         string   `xml:"name,attr"`
72
Chris Jones <christian.jones@sri.com>
parents: 61
diff changeset
    25
	Group        []string
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    26
}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    27
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    28
type rosterCb struct {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    29
	id string
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    30
	cb func()
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    31
}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    32
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    33
type Roster struct {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    34
	Extension
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    35
	get chan []RosterItem
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    36
	callbacks chan rosterCb
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    37
	toServer chan Stanza
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    38
}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    39
57
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
    40
type rosterClient struct {
72
Chris Jones <christian.jones@sri.com>
parents: 61
diff changeset
    41
	rosterChan   <-chan []RosterItem
57
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
    42
	rosterUpdate chan<- RosterItem
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
    43
}
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
    44
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    45
// Implicitly becomes part of NewClient's extStanza arg.
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    46
func newRosterQuery(name *xml.Name) interface{} {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 37
diff changeset
    47
	return &RosterQuery{}
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    48
}
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    49
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    50
func (r *Roster) rosterMgr(upd <-chan Stanza) {
72
Chris Jones <christian.jones@sri.com>
parents: 61
diff changeset
    51
	roster := make(map[string]RosterItem)
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    52
	waits := make(map[string]func())
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    53
	var snapshot []RosterItem
57
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
    54
	for {
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
    55
		select {
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    56
		case stan, ok := <- upd:
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    57
			if !ok {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    58
				return
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    59
			}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    60
			hdr := stan.GetHeader()
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    61
			if f := waits[hdr.Id] ; f != nil {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    62
				delete(waits, hdr.Id)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    63
				f()
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    64
			}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    65
			iq, ok := stan.(*Iq)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    66
			if iq.Type != "set" {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    67
				continue
57
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
    68
			}
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    69
			var rq *RosterQuery
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    70
			for _, ele := range iq.Nested {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    71
				if q, ok := ele.(*RosterQuery); ok {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    72
					rq = q
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    73
					break
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    74
				}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    75
			}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    76
			if rq == nil {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    77
				continue
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    78
			}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    79
			for _, item := range rq.Item {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    80
				roster[item.Jid] = item
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    81
			}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    82
			snapshot = []RosterItem{}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    83
			for _, ri := range roster {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    84
				snapshot = append(snapshot, ri)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    85
			}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    86
		case r.get <- snapshot:
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    87
		case cb := <- r.callbacks:
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    88
			waits[cb.id] = cb.cb
57
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
    89
		}
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
    90
	}
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
    91
}
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
    92
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    93
func (r *Roster) makeFilters() (Filter, Filter) {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    94
	rosterUpdate := make(chan Stanza)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    95
	go r.rosterMgr(rosterUpdate)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    96
	recv := func(in <-chan Stanza, out chan<- Stanza) {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    97
		defer close(out)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    98
		for stan := range in {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
    99
			rosterUpdate <- stan
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   100
			out <- stan
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   101
		}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   102
	}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   103
	send := func(in <-chan Stanza, out chan<- Stanza) {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   104
		defer close(out)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   105
		for {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   106
			select {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   107
			case stan, ok := <- in:
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   108
				if !ok {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   109
					return
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   110
				}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   111
				out <- stan
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   112
			case stan := <- r.toServer:
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   113
				out <- stan
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   114
			}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   115
		}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   116
	}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   117
	return recv, send
57
e6cb3f049137 Revamped how the roster works. We're now using a channel to transmit snapshots
Chris Jones <chris@cjones.org>
parents: 55
diff changeset
   118
}
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   119
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   120
func newRosterExt() *Roster {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   121
	r := Roster{}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   122
	r.StanzaHandlers = make(map[string]func(*xml.Name) interface{})
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   123
	r.StanzaHandlers[NsRoster] = newRosterQuery
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   124
	r.RecvFilter, r.SendFilter = r.makeFilters()
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   125
	r.get = make(chan []RosterItem)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   126
	r.callbacks = make(chan rosterCb)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   127
	r.toServer = make(chan Stanza)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   128
	return &r
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   129
}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   130
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   131
// Return the most recent snapshot of the roster status. This is
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   132
// updated automatically as roster updates are received from the
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   133
// server, but especially in response to calls to Update().
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   134
func (r *Roster) Get() []RosterItem {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   135
	return <-r.get
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   136
}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   137
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   138
// Synchronously fetch this entity's roster from the server and cache
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   139
// that information. The client can access the roster by watching for
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   140
// RosterQuery objects or by calling Get().
124
34e917ca6a11 Updated the example to match the new API, and fixed the roster Update to not require a reference to the client.
Chris Jones <christian.jones@sri.com>
parents: 121
diff changeset
   141
func (r *Roster) Update() {
34e917ca6a11 Updated the example to match the new API, and fixed the roster Update to not require a reference to the client.
Chris Jones <christian.jones@sri.com>
parents: 121
diff changeset
   142
	iq := &Iq{Header: Header{Type: "get", Id: NextId(),
34e917ca6a11 Updated the example to match the new API, and fixed the roster Update to not require a reference to the client.
Chris Jones <christian.jones@sri.com>
parents: 121
diff changeset
   143
		Nested: []interface{}{RosterQuery{}}}}
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   144
	waitchan := make(chan int)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   145
	done := func() {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   146
		close(waitchan)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   147
	}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   148
	r.waitFor(iq.Id, done)
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   149
	r.toServer <- iq
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   150
	<-waitchan
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   151
}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   152
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   153
func (r *Roster) waitFor(id string, cb func()) {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   154
	r.callbacks <- rosterCb{id: id, cb: cb}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents: 118
diff changeset
   155
}