Code

Updated socket & Daemon class
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 26 Mar 2008 14:47:12 +0000 (14:47 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 26 Mar 2008 14:47:12 +0000 (14:47 +0000)
-Updated error message. Added timeout message

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@9999 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-core/include/class_gosaSupportDaemon.inc
gosa-core/include/class_socketClient.inc

index 219fdbb36ca2ed0d03dcce6ff3d5524e5527e3c8..8270f9ffc2fb7abf841ae56f0b5d6a8e04139834 100644 (file)
@@ -134,28 +134,47 @@ class gosaSupportDaemon
   }
 
 
-  public function FAI_get_packages($release,$package = "")
+  public function FAI_get_packages($release,$attrs, $package="")
   {
     $this->reset_error();
+    $ret = array();
 
+    if(!is_array($attrs) || !count($attrs)){
+      trigger_error("Second parameter must be an array. With at least one attribute name.");
+      return($ret);
+    }
+    $attr = "<select>package</select>";
+    foreach($attrs as $at){
+#      $attr.= "<select>".trim($at)."</select>";
+    }
     if(empty($package)){
       $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
-        "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
+        $attr.
+        "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where><limit>10</limit></xml>";
     }else{
       $xml_msg = 
         "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
+        $attr.
           "<where><clause>".
             "<connector>AND</connector>".
             "<phrase><distribution>".$release."</distribution></phrase>".
             "<phrase><package>".$package."</package></phrase>".
           "</clause></where>".
-        "</xml>";
+        "<limit>10</limit></xml>";
     }
 
-    $ret = array();
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML']) && is_array($entries['XML'])){
 
@@ -194,6 +213,13 @@ class gosaSupportDaemon
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML']) && is_array($entries['XML'])){
 
@@ -230,6 +256,13 @@ class gosaSupportDaemon
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML']) && is_array($entries['XML'])){
 
@@ -297,6 +330,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML']) && is_array($entries['XML'])){
 
@@ -353,6 +393,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML']) && is_array($entries['XML'])){
         foreach($entries['XML'] as $entry){
@@ -400,6 +447,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str); 
       if(isset($entries['XML'])){
         foreach($entries['XML'] as $name => $entry){
@@ -447,6 +501,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str); 
       if(isset($entries['XML'])){
         foreach($entries['XML'] as $name => $entry){
@@ -490,6 +551,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return(FALSE);
+      }
+
       $entries = $this->xml_to_array($str); 
       if( isset($entries['XML']['HEADER']) && 
           $entries['XML']['HEADER']=="answer" && 
@@ -530,6 +598,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str); 
       if( isset($entries['XML']['HEADER']) &&
           $entries['XML']['HEADER']=="answer" &&
@@ -576,6 +651,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str = $this->o_sock->read();
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return($ret);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML']) || isset($entries['COUNT'])){
         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
@@ -701,6 +783,13 @@ $xml_msg.= "
 
       $this->o_sock->write($xml_msg);
       $str      = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return(FALSE);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML'])){
         if(isset($entries['XML']['ERROR_STRING'])) {
@@ -726,6 +815,13 @@ $xml_msg.= "
     if($this->connect()){
       $this->o_sock->write($xml_msg);
       $str     = trim($this->o_sock->read());
+
+      /* Check if something went wrong while reading */
+      if($this->o_sock->is_error()){
+        $this->set_error($this->o_sock->get_error());
+        return(0);
+      }
+
       $entries = $this->xml_to_array($str);
       if(isset($entries['XML'])){
         return($entries['XML']['COUNT']);
@@ -832,6 +928,13 @@ $xml_msg.= "
       $this->o_sock->write($data);
       if ($answer_expected){
         $str = trim($this->o_sock->read());
+
+        /* Check if something went wrong while reading */
+        if($this->o_sock->is_error()){
+          $this->set_error($this->o_sock->get_error());
+          return($ret);
+        }
+
         $entries = $this->xml_to_array($str);
         if(isset($entries['XML']) && is_array($entries['XML'])){
           $ret = $entries;
index 93e49de40190e2da5eb36409d2c88536157d0ac7..1cef298c7289818374fc2fec5eafa602c6662562 100644 (file)
@@ -31,7 +31,8 @@ class Socket_Client
   private $handle      = NULL;
   private $bytes_read = 0;
   private $error = "";
-  private $b_encrypt = FALSE;
+  private $is_error   = FALSE;
+  private $b_encrypt  = FALSE;
 
   /* Crypto information */
   private $td= NULL;
@@ -44,6 +45,7 @@ class Socket_Client
     $this->host= $host;
     $this->port= $port;
     $this->timeout= $timeout;
+    $this->reset_error();
 
     /* Connect if needed */
     if($connect){
@@ -55,7 +57,7 @@ class Socket_Client
   public function setEncryptionKey($key)
   {
     if(!function_exists("mcrypt_get_iv_size")){
-      $this->error = _("The mcrypt module was not found. Please install php5-mcrypt.");
+      $this->set_error(_("The mcrypt module was not found. Please install php5-mcrypt."));
       $this->ckey = "";
       $this->b_encrypt = FALSE;
     }
@@ -99,10 +101,11 @@ class Socket_Client
 
   public function open()
   {
+    $this->reset_error();
     $this->handle = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout);
     if(!$this->handle){
       $this->handle = NULL;
-      $this->error = $this->errstr;
+      $this->set_error( $this->errstr);
     }else{
       $this->b_data_send = TRUE;
 
@@ -116,6 +119,26 @@ class Socket_Client
   }
 
 
+  public function set_error($str)
+  {
+    $this->is_error =TRUE;
+    $this->error=$str;
+  }
+
+
+  public function reset_error()
+  {
+    $this->is_error =FALSE;
+    $this->error = "";
+  }
+
+
+  public function is_error()
+  {
+    return($this->is_error);
+  }
+
+
   public function get_error()
   {
     return $this->error;
@@ -138,6 +161,7 @@ class Socket_Client
   public function read()
   {
     // Output the request results
+    $this->reset_error();
     $str = "";
     $data = "test";
     socket_set_timeout($this->handle,$this->timeout);                  
@@ -163,7 +187,8 @@ class Socket_Client
           }
         }
       }
-      if((microtime(TRUE) - $start) > $this->timeout ){      
+      if((microtime(TRUE) - $start) > $this->timeout ){     
+        $this->set_error(sprintf(_("Socket timeout of %s seconds reached."),$this->timeout));     
         break;
       }
     }