4 xmpp "code.google.com/p/goexmpp"
10 type XMPPPing struct {
11 XMLName xml.Name `xml:"urn:xmpp:ping ping"`
14 func HandleServerToClientPing(iq *xmpp.Iq, xmppout chan<- xmpp.Stanza) bool {
15 ///<iq from='juliet@capulet.lit/balcony' to='capulet.lit' id='s2c1' type='result'/>
16 if iq.Type != "get" { return false}
17 for _, ele := range iq.Nested {
18 if _, ok := ele.(*XMPPPing); ok {
19 xmppout <- &xmpp.Iq{Header: xmpp.Header{To: iq.From, From: iq.To, Id: iq.Id, Type: "result" }}
26 func (botdata *XmppBot) PingServer(timeout_ms time.Duration) (is_up bool) {
27 ///<iq from='juliet@capulet.lit/balcony' to='capulet.lit' id='c2s1' type='get'>
28 /// <ping xmlns='urn:xmpp:ping'/>
30 server_jid := new(xmpp.JID)
31 server_jid.Set(botdata.my_jid_)
32 iqping := &xmpp.Iq{Header: xmpp.Header{To: server_jid.Domain,
33 From: botdata.my_jid_,
36 Nested: []interface{}{XMPPPing{}} } }
37 pong := make(chan bool, 1)
39 f := func(v xmpp.Stanza) bool {
40 defer recover() //recover from writing to possibly already closed chan
41 let_others_handle_stanza := false
42 iq, ok := v.(*xmpp.Iq)
44 Syslog_.Printf("response to iq ping wasn't iq: %s", v)
46 return true //let other handlers process reply
48 if iq.Type == "error" && iq.Error != nil && iq.Error.Type == "cancel"{
49 Debug_.Printf("response to iq ping was cancel, server does not support ping")
50 //server does not support ping, but at least we know server is still there
51 } else if iq.Type != "result" {
52 Syslog_.Printf("response to iq ping was not pong: %s", v)
53 let_others_handle_stanza = true //let other handlers process reply
56 return let_others_handle_stanza // return false so that Stanza v will not be appear in xmppclient_.Out()
58 botdata.xmppclient_.HandleStanza(iqping.Id, f)
59 botdata.xmppclient_.Out <- iqping
61 defer func() {if x:= recover(); x == nil { Syslog_.Printf("response to iq ping timed out !!") }}() //recover from writing to possibly already closed chan. If we did not need to recover, then Handler did not receive reply
62 time.Sleep(timeout_ms * time.Millisecond)
63 pong <- false //xmpp ping timed out