restart xmpp server after error or ping-timeout
[svn42.git] / go / r3-netstatus / r3xmppbot / ping.go
1 package r3xmppbot
2
3 import (
4         xmpp "code.google.com/p/goexmpp"
5     "time"
6     "encoding/xml"    
7 )
8
9 // XMPP Ping
10 type XMPPPing struct {
11         XMLName xml.Name     `xml:"urn:xmpp:ping ping"`
12 }
13
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" }}
20             return true
21         }
22     }
23     return false
24 }
25
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'/>
29 ///</iq>
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_,
34                                                             Id: <-xmpp.Id,
35                                                             Type: "get",
36                                                             Nested: []interface{}{XMPPPing{}}   }   }
37     pong := make(chan bool, 1)
38     defer close(pong)
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)
43         if !ok {
44             Syslog_.Printf("response to iq ping wasn't iq: %s", v)
45             pong <- false
46             return true //let other handlers process reply
47         }
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
54         }
55         pong <- true
56         return let_others_handle_stanza // return false so that Stanza v will not be appear in xmppclient_.Out()
57     }
58     botdata.xmppclient_.HandleStanza(iqping.Id, f)
59     botdata.xmppclient_.Out <- iqping    
60     go func() {
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
64     }()
65     return <- pong
66 }