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