sleep before reconnecting
[auerswald-callnotifier.git] / src / callnotifier / Source / Remote.php
index 39b042c..be7604f 100644 (file)
@@ -13,9 +13,19 @@ class Source_Remote
 
     public function run()
     {
-        $this->connect($this->config->host, $this->config->port);
-        $this->init();
-        $this->loop();
+        do {
+            try {
+                $tryAgain = false;
+                $this->connect($this->config->host, $this->config->port);
+                $this->init();
+                $this->loop();
+            } catch (Exception_ConnectionReset $e) {
+                $tryAgain = true;
+                //connection is refused directly after a connection reset
+                //hopefully waiting a bit will help
+                sleep(10);
+            }
+        } while ($tryAgain);
         $this->disconnect();
     }
 
@@ -64,6 +74,11 @@ class Source_Remote
     function read_response()
     {
         $res = socket_read($this->socket, 2048, PHP_NORMAL_READ);
+        if ($res === false) {
+            //handle "Connection reset by peer" that appears nightly since
+            // version 4.0N
+            throw new Exception_ConnectionReset();
+        }
         return substr($res, 2, -1);
     }