[icinga-checkins] icinga.org: icingaweb2-module-director/master: CoreApi: clean up the streaming prototype

git at icinga.org git at icinga.org
Tue Feb 2 17:43:24 CET 2016


Module: icingaweb2-module-director
Branch: master
Commit: e4f0f97d4f2d4ba8cee5ae722a77d1bb0e5c5f57
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=e4f0f97d4f2d4ba8cee5ae722a77d1bb0e5c5f57

Author: Thomas Gelf <thomas at gelf.net>
Date:   Mon Feb  1 15:23:30 2016 +0100

CoreApi: clean up the streaming prototype

---

 library/Director/Core/CoreApi.php       |    6 ++++++
 library/Director/Core/RestApiClient.php |   32 +++++++++++++++++++++----------
 2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/library/Director/Core/CoreApi.php b/library/Director/Core/CoreApi.php
index 4771c0f..1f43299 100644
--- a/library/Director/Core/CoreApi.php
+++ b/library/Director/Core/CoreApi.php
@@ -45,6 +45,12 @@ class CoreApi
         )->getResult('name');
     }
 
+    public function onEvent($callback)
+    {
+        $this->client->onEvent($callback);
+        return $this;
+    }
+
     public function getObject($name, $pluraltype, $attrs = array())
     {
         $params = (object) array(
diff --git a/library/Director/Core/RestApiClient.php b/library/Director/Core/RestApiClient.php
index 6730b9e..ae20cca 100644
--- a/library/Director/Core/RestApiClient.php
+++ b/library/Director/Core/RestApiClient.php
@@ -21,6 +21,8 @@ class RestApiClient
 
     protected $readBuffer = '';
 
+    protected $onEvent;
+
     public function __construct($peer, $port = 5665, $cn = null)
     {
         $this->peer = $peer;
@@ -37,6 +39,12 @@ class RestApiClient
         return $this;
     }
 
+    public function onEvent($callback)
+    {
+        $this->onEvent = $callback;
+        return $this;
+    }
+
     public function getPeerIdentity()
     {
         return $this->peer;
@@ -156,7 +164,6 @@ class RestApiClient
         if ($res === false) {
             throw new Exception('CURL ERROR: ' . curl_error($curl));
         }
-
         Benchmark::measure('Rest Api, got response');
 
         if ($stream) {
@@ -210,12 +217,12 @@ class RestApiClient
     {
         $length = strlen($data);
         $this->readBuffer .= $data;
-        echo "Got $length bytes\n";
-        $this->dumpEvents();
+        // echo "Got $length bytes\n";
+        $this->processEvents();
         return $length;
     }
 
-    protected function dumpEvents()
+    protected function processEvents()
     {
         $offset = 0;
         while (false !== ($pos = strpos($this->readBuffer, "\n", $offset))) {
@@ -227,11 +234,16 @@ class RestApiClient
 
             $str = substr($this->readBuffer, $offset, $pos);
             $decoded = json_decode($str);
-if ($decoded === false) {
-  die('No json: ' . $str);
-}
-printf("Processing %s bytes\n", strlen($str));
-print_r($decoded);
+            if ($decoded === false) {
+              throw new Exception('Got invalid JSON: ' . $str);
+            }
+
+            // printf("Processing %s bytes\n", strlen($str));
+            // print_r($decoded);
+            if ($this->onEvent !== null) {
+                $func = $this->onEvent;
+                $func($decoded);
+            }
 
             $offset = $pos + 1;
         }
@@ -240,7 +252,7 @@ print_r($decoded);
             $this->readBuffer = substr($this->readBuffer, $offset + 1);
         }
 
-echo "REMAINING: " . strlen($this->readBuffer) . "\n";
+        // echo "REMAINING: " . strlen($this->readBuffer) . "\n";
     }
 
     public function __destruct()



More information about the icinga-checkins mailing list