xmpp.go
changeset 110 7696e6a01709
parent 106 ffb9d27fea79
child 111 36287f2cf06e
equal deleted inserted replaced
109:3887d7ad19c1 110:7696e6a01709
    80 	handlers     chan *stanzaHandler
    80 	handlers     chan *stanzaHandler
    81 	inputControl chan int
    81 	inputControl chan int
    82 	// Incoming XMPP stanzas from the server will be published on
    82 	// Incoming XMPP stanzas from the server will be published on
    83 	// this channel. Information which is only used by this
    83 	// this channel. Information which is only used by this
    84 	// library to set up the XMPP stream will not appear here.
    84 	// library to set up the XMPP stream will not appear here.
    85 	In <-chan Stanza
    85 	In <-chan interface{}
    86 	// Outgoing XMPP stanzas to the server should be sent to this
    86 	// Outgoing XMPP stanzas to the server should be sent to this
    87 	// channel.
    87 	// channel.
    88 	Out    chan<- Stanza
    88 	Out    chan<- interface{}
    89 	xmlOut chan<- interface{}
    89 	xmlOut chan<- interface{}
    90 	// Features advertised by the remote. This will be updated
    90 	// Features advertised by the remote. This will be updated
    91 	// asynchronously as new features are received throughout the
    91 	// asynchronously as new features are received throughout the
    92 	// connection process. It should not be updated once
    92 	// connection process. It should not be updated once
    93 	// StartSession() returns.
    93 	// StartSession() returns.
    94 	Features  *Features
    94 	Features  *Features
    95 	filterOut chan<- <-chan Stanza
    95 	filterOut chan<- <-chan interface{}
    96 	filterIn  <-chan <-chan Stanza
    96 	filterIn  <-chan <-chan interface{}
    97 }
    97 }
    98 
    98 
    99 // Connect to the appropriate server and authenticate as the given JID
    99 // Connect to the appropriate server and authenticate as the given JID
   100 // with the given password. This function will return as soon as a TCP
   100 // with the given password. This function will return as soon as a TCP
   101 // connection has been established, but before XMPP stream negotiation
   101 // connection has been established, but before XMPP stream negotiation
   198 	ch := make(chan interface{})
   198 	ch := make(chan interface{})
   199 	go writeXml(w, ch)
   199 	go writeXml(w, ch)
   200 	return ch
   200 	return ch
   201 }
   201 }
   202 
   202 
   203 func (cl *Client) startStreamReader(xmlIn <-chan interface{}, srvOut chan<- interface{}) <-chan Stanza {
   203 func (cl *Client) startStreamReader(xmlIn <-chan interface{}, srvOut chan<- interface{}) <-chan interface{} {
   204 	ch := make(chan Stanza)
   204 	ch := make(chan interface{})
   205 	go cl.readStream(xmlIn, ch)
   205 	go cl.readStream(xmlIn, ch)
   206 	return ch
   206 	return ch
   207 }
   207 }
   208 
   208 
   209 func (cl *Client) startStreamWriter(xmlOut chan<- interface{}) chan<- Stanza {
   209 func (cl *Client) startStreamWriter(xmlOut chan<- interface{}) chan<- interface{} {
   210 	ch := make(chan Stanza)
   210 	ch := make(chan interface{})
   211 	go writeStream(xmlOut, ch, cl.inputControl)
   211 	go writeStream(xmlOut, ch, cl.inputControl)
   212 	return ch
   212 	return ch
   213 }
   213 }
   214 
   214 
   215 func (cl *Client) startFilter(srvIn <-chan Stanza) <-chan Stanza {
   215 func (cl *Client) startFilter(srvIn <-chan interface{}) <-chan interface{} {
   216 	cliIn := make(chan Stanza)
   216 	cliIn := make(chan interface{})
   217 	filterOut := make(chan (<-chan Stanza))
   217 	filterOut := make(chan (<-chan interface{}))
   218 	filterIn := make(chan (<-chan Stanza))
   218 	filterIn := make(chan (<-chan interface{}))
   219 	nullFilter := make(chan Stanza)
   219 	nullFilter := make(chan interface{})
   220 	go filterBottom(srvIn, nullFilter)
   220 	go filterBottom(srvIn, nullFilter)
   221 	go filterTop(filterOut, filterIn, nullFilter, cliIn)
   221 	go filterTop(filterOut, filterIn, nullFilter, cliIn)
   222 	cl.filterOut = filterOut
   222 	cl.filterOut = filterOut
   223 	cl.filterIn = filterIn
   223 	cl.filterIn = filterIn
   224 	return cliIn
   224 	return cliIn
   266 // session, retrieve the roster, and broadcast an initial
   266 // session, retrieve the roster, and broadcast an initial
   267 // presence. The presence can be as simple as a newly-initialized
   267 // presence. The presence can be as simple as a newly-initialized
   268 // Presence struct.  See RFC 3921, Section 3.
   268 // Presence struct.  See RFC 3921, Section 3.
   269 func (cl *Client) StartSession(getRoster bool, pr *Presence) error {
   269 func (cl *Client) StartSession(getRoster bool, pr *Presence) error {
   270 	id := <-Id
   270 	id := <-Id
   271 	iq := &Iq{To: cl.Jid.Domain, Id: id, Type: "set", Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsSession, Local: "session"}}}}
   271 	iq := &Iq{Stanza: Stanza{To: cl.Jid.Domain, Id: id, Type: "set",
       
   272 		Nested: []interface{}{Generic{XMLName:
       
   273 			xml.Name{Space: NsSession, Local: "session"}}}}}
   272 	ch := make(chan error)
   274 	ch := make(chan error)
   273 	f := func(st Stanza) bool {
   275 	f := func(st interface{}) bool {
   274 		iq, ok := st.(*Iq)
   276 		iq, ok := st.(*Iq)
   275 		if !ok {
   277 		if !ok {
   276 			Warn.Log("iq reply not iq; can't start session")
   278 			Warn.Log("iq reply not iq; can't start session")
   277 			ch <- errors.New("bad session start reply")
   279 			ch <- errors.New("bad session start reply")
   278 			return false
   280 			return false
   307 // AddFilter adds a new filter to the top of the stack through which
   309 // AddFilter adds a new filter to the top of the stack through which
   308 // incoming stanzas travel on their way up to the client. The new
   310 // incoming stanzas travel on their way up to the client. The new
   309 // filter's output channel is given to this function, and it returns a
   311 // filter's output channel is given to this function, and it returns a
   310 // new input channel which the filter should read from. When its input
   312 // new input channel which the filter should read from. When its input
   311 // channel closes, the filter should close its output channel.
   313 // channel closes, the filter should close its output channel.
   312 func (cl *Client) AddFilter(out <-chan Stanza) <-chan Stanza {
   314 func (cl *Client) AddFilter(out <-chan interface{}) <-chan interface{} {
   313 	cl.filterOut <- out
   315 	cl.filterOut <- out
   314 	return <-cl.filterIn
   316 	return <-cl.filterIn
   315 }
   317 }