attempt to handle more errors
authorBernhard Tittelbach <xro@realraum.at>
Fri, 8 Nov 2013 03:55:18 +0000 (03:55 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Fri, 8 Nov 2013 03:55:18 +0000 (03:55 +0000)
go/r3-netstatus/main.go
go/r3-netstatus/r3xmppbot/r3xmppbot.go

index bb6ffb5..c565df2 100644 (file)
@@ -121,7 +121,7 @@ func EventToXMPP(bot *r3xmppbot.XmppBot, events <- chan interface{}, xmpp_presen
                     last_buttonpress = 0
                 }
                 // Try to XMPP Ping the server and if that fails, quit XMPPBot
-                if bot.PingServer(2000) == false { return }
+                if bot.PingServer(3500) == false { return }
             case r3events.DoorProblemEvent:
                 xmpp_presence_events_chan <- r3xmppbot.XMPPMsgEvent{Msg: fmt.Sprintf("Door Problem: %s. SeverityLevel: %d (%s)",event.Problem, event.Severity, time.Unix(event.Ts,0).String()), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: false}
         }
@@ -146,7 +146,7 @@ func RunXMPPBot(ps *pubsub.PubSub, zmqctx *zmq.Context) {
         } else {
             Syslog_.Printf("Error starting XMPP Bot: %s", xmpperr.Error())
         }
-        time.Sleep(5 * time.Second)
+        time.Sleep(10 * time.Second)
     }
 }
 
@@ -194,7 +194,7 @@ func main() {
     go RunXMPPBot(ps, zmqctx)
 
     // --- receive and distribute events ---
-    ticker := time.NewTicker(time.Duration(5) * time.Minute)
+    ticker := time.NewTicker(time.Duration(6) * time.Minute)
     for {
     select {
         case e := <-zmqsub.In():
index 6740a84..795d7a8 100644 (file)
@@ -254,9 +254,6 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout
     if inmsg.Body == nil || inmsg.GetHeader() == nil {
         return
     }
-    if inmsg.Type == "error" || inmsg.Error != nil {
-        Syslog_.Printf("XMPP Message Error: %s", inmsg.Error.Error())
-    }
     bodytext :=inmsg.Body.Chardata
     if botdata.isAuthenticated(inmsg.GetHeader().From) {
         switch bodytext {
@@ -315,16 +312,36 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm
         close(jabber_events)
     }()
 
+    var error_count int = 0
     var incoming_stanza interface{}
+
+    handleStanzaError := func() bool {
+        error_count++
+        if error_count > 15 {
+            Syslog_.Println("handleIncomingXMPPStanzas: too many errors in series.. bailing out")
+            botdata.StopBot()
+            return true
+        }
+        return false
+    }
+
     for incoming_stanza = range xmppin {
         switch stanza := incoming_stanza.(type) {
             case *xmpp.Message:
+                if stanza.GetHeader() == nil { continue }
                 botdata.handleIncomingMessageDialog(*stanza, xmppout, jabber_events)
+                if stanza.Type == "error" || stanza.Error != nil {
+                    Syslog_.Printf("XMPP %T Error: %s", stanza, stanza)
+                    if handleStanzaError() { return }
+                    continue
+                } else { error_count = 0 }
             case *xmpp.Presence:
                 if stanza.GetHeader() == nil { continue }
                 if stanza.Type == "error" || stanza.Error != nil {
-                    Syslog_.Printf("XMPP Presence Error: %s", stanza.Error.Error())
-                }
+                    Syslog_.Printf("XMPP %T Error: %s", stanza, stanza)
+                    if handleStanzaError() { return }
+                    continue
+                } else { error_count = 0 }
                 switch stanza.GetHeader().Type {
                     case "subscribe":
                         xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribed", "", "")
@@ -340,11 +357,15 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm
                     default:
                         jabber_events <- JabberEvent{stanza.GetHeader().From, true, R3NoChange, false}
                 }
+
             case *xmpp.Iq:
                 if stanza.GetHeader() == nil { continue }
                 if stanza.Type == "error" || stanza.Error != nil {
-                    Syslog_.Printf("XMPP Iq Error: %s", stanza.Error.Error())
-                }
+                    Syslog_.Printf("XMPP %T Error: %s", stanza, stanza)
+                    if handleStanzaError() { return }
+                    continue
+                } else { error_count = 0 }
+
                 if HandleServerToClientPing(stanza, xmppout) {continue} //if true then routine handled it and we can continue
                 Debug_.Printf("Unhandled Iq: %s", stanza)
         }