Code

msgPool
[gosa.git] / gosa-core / include / class_gosaSupportDaemon.inc
index 803ff31c943600e1d22b6d2a543ed858df47de99..a6e1c410f0de0ed3de5f8c19a146e1f98412f291 100644 (file)
@@ -41,7 +41,7 @@ class gosaSupportDaemon
     @param boolean  Connect Directly connect to daemon socket.
     @param float    Timeout The timelimit for all socket actions.
    */
-  public function __construct($connect=TRUE,$timeout=0.2)
+  public function __construct($connect=TRUE,$timeout=2)
   {
     #FIXME: bad idea about referencing global variables from within classes
     global $config;
@@ -75,6 +75,7 @@ class gosaSupportDaemon
     }else{
       $this->set_error($this->o_sock->get_error());
       $this->disconnect();
+      new log("debug","gosaSupportDaemon::connect()", "Could not connect to server.", array(),$this->get_error());
     }
     return($this->is_connected);
   }
@@ -183,6 +184,9 @@ $xml_msg.= "
       }
     }
     
+    /* Remove session ID. No one is interested in this... */
+    unset($ret['SESSION_ID']);
+
     return($ret);
   }
 
@@ -234,6 +238,53 @@ $xml_msg.= "
   }
 
 
+  /*! \brief  Returns an entry containing all requested ids.
+    @param  Array   The IDs of the entries we want to return.
+    @return Array   Of the requested entries. 
+   */
+  public function get_entries_by_mac($macs)
+  {
+    if(!is_array($macs)){
+      trigger_error("Requires an array as parameter.");
+      return;
+    }
+    $this->reset_error();
+
+    $ret = array();
+
+    $xml_msg = "<xml>
+      <header>gosa_query_jobdb</header>
+      <target>GOSA</target>
+      <source>GOSA</source>
+      <where>
+      <clause>
+      <connector>or</connector>";
+    foreach($macs as $mac){
+      $xml_msg .= "<phrase>
+        <operator>eq</operator>
+        <macaddress>".$mac."</macaddress>
+        </phrase>";
+    }
+    $xml_msg .= "</clause>
+      </where>
+      </xml>";
+
+    if($this->connect()){
+      $this->o_sock->write($xml_msg);
+      $str = trim($this->o_sock->read());
+      $entries = $this->xml_to_array($str); 
+      if(isset($entries['XML'])){
+        foreach($entries['XML'] as $name => $entry){
+          if(preg_match("/^ANSWER[0-9]*$/",$name)){
+            $ret[$name] = $entry;
+          }
+        }
+      }
+    }
+    return($ret);
+  }
+
+
   /*! \brief  Returns an entry containing all requested ids.
     @param  Array   The IDs of the entries we want to return.
     @return Array   Of the requested entries. 
@@ -398,8 +449,11 @@ $xml_msg.= "
       $this->o_sock->write($xml_msg);
       $str = $this->o_sock->read();
       $entries = $this->xml_to_array($str);
-      if(isset($entries['XML'])){
+      if(isset($entries['XML']) || isset($entries['COUNT'])){
+        new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
         return(TRUE);
+      }else{
+        new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
       }
     }
     return(FALSE);
@@ -413,30 +467,7 @@ $xml_msg.= "
    */
   public function remove_entry($id)
   {
-    $this->reset_error();
-
-    $xml_msg = "<xml>
-      <header>gosa_delete_jobdb_entry</header>
-      <target>GOSA</target>
-      <source>GOSA</source>
-      <where>
-      <clause>
-      <phrase>
-      <operator>eq</operator>
-      <id>".$id."</id>
-      </phrase>
-      </clause>
-      </where>
-      </xml>";
-    if($this->connect()){
-      $this->o_sock->write($xml_msg);
-      $str = $this->o_sock->read();
-      $entries = $this->xml_to_array($str);
-      if(isset($entries['XML'])){
-        return(TRUE);
-      }
-    }
-    return(FALSE);
+    return($this->remove_entries(array($id)));
   }
 
 
@@ -480,7 +511,7 @@ $xml_msg.= "
 
     if(!isset($params['XML'])){
       if (!array_key_exists('XML', $params)){
-        $this->set_error(_("Could not parse XML."));
+        $this->set_error(_("Cannot not parse XML!"));
       }
       $params = array("COUNT" => 0);
     }
@@ -546,8 +577,10 @@ $xml_msg.= "
       if(isset($entries['XML'])){
         if(isset($entries['XML']['ERROR_STRING'])) {
           $this->set_error($entries['XML']['ERROR_STRING']);
+          new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
           return(FALSE);
         }
+        new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
         return(TRUE);
       }
     }
@@ -623,7 +656,6 @@ $xml_msg.= "
       $request_answer = FALSE;
       if($event->get_type() == SCHEDULED_EVENT){
         $action = $event->get_schedule_action();
-        $request_answer = TRUE;
       }elseif($event->get_type() == TRIGGERED_EVENT){
         $action = $event->get_trigger_action();
       }else{
@@ -677,8 +709,13 @@ $xml_msg.= "
           $ret = $entries;
           if(isset($entries['XML']['ERROR_STRING'])) {
             $this->set_error($entries['XML']['ERROR_STRING']);
+            new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
+          }else{
+            new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
           }
         }
+      }else{
+        new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
       }
     }
     return($ret);
@@ -720,6 +757,71 @@ $xml_msg.= "
   }
 
 
+  /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
+      @param  String  $mac  The mac address for which we want to remove all jobs.      
+   */
+  function clean_queue_from_mac($mac)
+  {
+    global $config;
+
+    /* First of all we have to check which jobs are startet 
+     *  for $mac 
+     */
+    $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
+    
+    new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
+    $data = $this->_send($xml_msg,TRUE);
+    if(is_array($data) && isset($data['XML'])){
+      $already_aborted = FALSE;
+      foreach($data['XML']  as $name => $entry){
+        if(preg_match("/answer[0-9]*/i",$name)){
+          $entry['STATUS'] = strtoupper($entry['STATUS']);
+          switch($entry['STATUS']){
+
+            case 'PROCESSING' :
+
+              /* Send abort event, but only once 
+               */
+              if($already_aborted){
+                break;
+              }elseif(class_available("DaemonEvent_faireboot")){
+                $already_aborted = TRUE;
+                $tmp = new DaemonEvent_faireboot($config);
+                $tmp->add_targets(array($mac));
+                $tmp->set_type(TRIGGERED_EVENT);
+                if(!$this->append($tmp)){
+                  msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
+                  new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
+                      "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
+                }else{
+                  new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
+                      "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
+                }
+                ;break;
+              }else{
+                /* Couldn't find abort event, just remove entry */
+              }
+
+            case 'WAITING':
+            case 'ERROR':
+            default :
+            
+              /* Simply remove entries from queue. 
+               *  Failed or waiting events, can be removed without any trouble.
+               */ 
+              if(!$this->remove_entries(array($entry['ID']))){
+                msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
+              }
+              ;break;
+          }
+    
+        }
+      }
+    }
+  }
+
+
 static function ping($target)
 {
   if (tests::is_mac($target)){