public function stop()
 {
     if (is_numeric($_REQUEST['mcid']) && $_REQUEST['mcid'] > 0) {
         $MulticastSession = new MulticastSessions($_REQUEST['mcid']);
         foreach ((array) $this->getClass('MulticastSessionsAssociationManager')->find(array('msid' => $MulticastSession->get('id'))) as $MulticastAssoc) {
             $Task = new Task($MulticastAssoc->get('taskID'));
             $Task->cancel();
         }
         $MulticastSession->set('name', null)->set('stateID', 5)->save();
         $this->FOGCore->setMessage(_('Canceled task'));
         $this->FOGCore->redirect('?node=' . $this->node . '&sub=multicast');
     }
 }
 private function serviceLoop()
 {
     while (true) {
         try {
             $StorageNode = current($this->getClass('StorageNodeManager')->find(array('isMaster' => 1, 'isEnabled' => 1, 'ip' => $this->FOGCore->getIPAddress())));
             if (!$StorageNode || !$StorageNode->isValid()) {
                 throw new Exception(sprintf(" | This is not the master node"));
             }
             $myroot = $StorageNode->get('path');
             $allTasks = MulticastTask::getAllMulticastTasks($myroot);
             $this->FOGCore->out(sprintf(" | %s task(s) found", count($allTasks)), $this->dev);
             $RMTasks = $this->getMCTasksNotInDB($KnownTasks, $allTasks);
             $jobcancelled = false;
             if (count($RMTasks)) {
                 $this->outall(sprintf(" | Cleaning %s task(s) removed from FOG Database.", count($RMTasks)));
                 foreach ((array) $RMTasks as $RMTask) {
                     $this->outall(sprintf(" | Cleaning Task (%s) %s", $RMTask->getID(), $RMTask->getName()));
                     $Assocs = $this->getClass('MulticastSessionsAssociationManager')->find(array('msID' => $RMTask->getID()));
                     $curSession = new MulticastSessions($RMTask->getID());
                     foreach ($Assocs as $Assoc) {
                         if ($Assoc && $Assoc->isValid()) {
                             if ($this->getClass('Task', $Assoc->get('taskID'))->get('stateID') == 5) {
                                 $jobcancelled = true;
                             }
                         }
                     }
                     if ($jobcancelled || $this->getClass('MulticastSessions', $RMTask->getID())->get('stateID') == 5) {
                         $RMTask->killTask();
                         $KnownTasks = $this->removeFromKnownList($KnownTasks, $RMTask->getID());
                         $this->outall(sprintf(" | Task (%s) %s has been cleaned as cancelled.", $RMTask->getID(), $RMTask->getName()));
                         $this->getClass('MulticastSessionsAssociationManager')->destroy(array('msID' => $RMTask->getID()));
                     } else {
                         $KnownTasks = $this->removeFromKnownList($KnownTasks, $RMTask->getID());
                         $this->outall(sprintf(" | Task (%s) %s has been cleaned as complete.", $RMTask->getID(), $RMTask->getName()));
                         $this->getClass('MulticastSessionsAssociationManager')->destroy(array('msID' => $RMTask->getID()));
                     }
                 }
             } else {
                 if (!$allTasks) {
                     throw new Exception(' * No Tasks Found!');
                 }
             }
             foreach ((array) $allTasks as $curTask) {
                 if ($this->isMCTaskNew($KnownTasks, $curTask->getID())) {
                     $this->outall(sprintf(" | Task (%s) %s is new!", $curTask->getID(), $curTask->getName()));
                     if (file_exists($curTask->getImagePath())) {
                         $this->outall(sprintf(" | Task (%s) %s image file found.", $curTask->getID(), $curTask->getImagePath()));
                         if ($curTask->getClientCount() > 0) {
                             $this->outall(sprintf(" | Task (%s) %s client(s) found.", $curTask->getID(), $curTask->getClientCount()));
                             if (is_numeric($curTask->getPortBase()) && $curTask->getPortBase() % 2 == 0) {
                                 $this->outall(sprintf(" | Task (%s) %s sending on base port: %s", $curTask->getID(), $curTask->getName(), $curTask->getPortBase()));
                                 $this->outall(sprintf("CMD: %s", $curTask->getCMD()));
                                 if ($curTask->startTask()) {
                                     $this->outall(sprintf(" | Task (%s) %s has started.", $curTask->getID(), $curTask->getName()));
                                     $KnownTasks[] = $curTask;
                                 } else {
                                     $this->outall(sprintf(" | Task (%s) %s failed to start!", $curTask->getID(), $curTask->getName()));
                                     $this->outall(sprintf(" | * Don't panic, check all your settings!"));
                                     $this->outall(sprintf(" |       even if the interface is incorrect the task won't start."));
                                     $this->outall(sprintf(" |       If all else fails run the following command and see what it says:"));
                                     $this->outall(sprintf(" |  %s", $curTask->getCMD()));
                                     if ($curTask->killTask()) {
                                         $this->outall(sprintf(" | Task (%s) %s has been cleaned.", $curTask->getID(), $curTask->getName()));
                                     } else {
                                         $this->outall(sprintf(" | Task (%s) %s has NOT been cleaned.", $curTask->getID(), $curTask->getName()));
                                     }
                                 }
                             } else {
                                 $this->outall(sprintf(" | Task (%s) %s failed to execute, port must be even and numeric.", $curTask->getID(), $curTask->getName()));
                             }
                         } else {
                             $this->outall(sprintf(" | Task (%s) %s failed to execute, no clients are included!", $curTask->getID(), $curTask->getName()));
                         }
                     } else {
                         $this->outall(sprintf(" | Task (%s) %s failed to execute, image file:%s not found!", $curTask->getID(), $curTask->getName(), $curTask->getImagePath()));
                     }
                 } else {
                     $runningTask = $this->getMCExistingTask($KnownTasks, $curTask->getID());
                     $curSession = new MulticastSessions($runningTask->getID());
                     $Assocs = $this->getClass('MulticastSessionsAssociationManager')->find(array('msID' => $curSession->get('id')));
                     foreach ($Assocs as $Assoc) {
                         if ($Assoc && $Assoc->isValid()) {
                             $curTaskGet = new Task($Assoc->get('taskID'));
                             if ($curTaskGet->get('stateID') == 5) {
                                 $jobcancelled = true;
                             }
                         }
                     }
                     if ($runningTask->isRunning()) {
                         $this->outall(sprintf(" | Task (%s) %s is already running PID %s", $runningTask->getID(), $runningTask->getName(), $runningTask->getPID()));
                         $runningTask->updateStats();
                     } else {
                         $this->outall(sprintf(" | Task (%s) %s is no longer running.", $runningTask->getID(), $runningTask->getName()));
                         if ($jobcancelled || $curSession->get('stateID') == 5) {
                             if ($runningTask->killTask()) {
                                 $KnownTasks = $this->removeFromKnownList($KnownTasks, $runningTask->getID());
                                 $this->outall(sprintf(" | Task (%s) %s has been cleaned as cancelled.", $runningTask->getID(), $runningTask->getName()));
                             } else {
                                 $this->outall(sprintf(" | Failed to kill task (%s) %s!", $runningTask->getID(), $runningTask->getName()));
                             }
                         } else {
                             $curSession->set('clients', 0)->set('completetime', $this->nice_date()->format('Y-m-d H:i:s'))->set('name', '')->set('stateID', 4)->save();
                             $KnownTasks = $this->removeFromKnownList($KnownTasks, $runningTask->getID());
                             $this->outall(sprintf(" | Task (%s) %s has been cleaned as complete.", $runningTask->getID(), $runningTask->getName()));
                         }
                     }
                 }
             }
         } catch (Exception $e) {
             $this->outall($e->getMessage());
         }
         $this->FOGCore->out(sprintf(" +---------------------------------------------------------"), $this->dev);
         sleep(MULTICASTSLEEPTIME);
     }
 }
            $HookManager->processEvent('EMAIL_ITEMS', array('email' => &$email, 'Host' => &$Host));
            $emailMe = '';
            foreach ($email as $key => $val) {
                $emailMe .= $key . $val;
            }
            unset($email);
            $email = $emailMe;
            if (!$Inventory->get('other1')) {
                //if there isn't an existing call number in the system
                mail($to, $Host->get('name') . " - Image Task Completed", $email, $headers);
            } else {
                mail($to, "ISSUE=" . $Inventory->get('other1') . " PROJ=1", $email, $headers);
                mail($to, $Host->get('name') . " - Image Task Completed", "{$email} \nImaged For (Call): " . $Inventory->get('other1'), $headers);
                $Inventory->set('other1', '')->save();
                //clear call number otherwise if a new "existing" call exists later on down the line it'll just update original
            }
        }
    }
    ////============================== Email Notification End	==============================
    print '##';
    // If it's a multicast job, decrement the client count, though not fully needed.
    if ($Task->get('typeID') == 8) {
        $MyMulticastTask = current($FOGCore->getClass('MulticastSessionsAssociationManager')->find(array('taskID' => $Task->get('id'))));
        if ($MyMulticastTask && $MyMulticastTask->isValid()) {
            $MulticastSession = new MulticastSessions($MyMulticastTask->get('msID'));
            $MulticastSession->set('clients', $MulticastSession->get('clients') - 1)->save();
        }
    }
} catch (Exception $e) {
    print $e->getMessage();
}
 public function updateStats()
 {
     foreach ($this->getClass('MulticastSessionsAssociationManager')->find(array('msid' => $this->intID)) as $MultiSessAssoc) {
         $Task = new Task($MultiSessAssoc->get('taskID'));
         if ($Task && $Task->isValid()) {
             $TaskPercent[] = $Task->get('percent');
         }
     }
     $TaskPercent = array_unique((array) $TaskPercent);
     $MultiSess = new MulticastSessions($this->intID);
     $MultiSess->set('percent', max((array) $TaskPercent))->save();
 }
 $Host = $FOGCore->getHostItem(false);
 // Get the task
 $Task = $Host->get('task');
 if (!$Task->isValid()) {
     throw new Exception(sprintf('%s: %s (%s)', _('No Active Task found for Host'), $Host->get('name'), $MACAddress));
 }
 if ($Task->get('typeID') == 8) {
     // Get the current Multicast Session
     $MulticastAssociation = current($FOGCore->getClass('MulticastSessionsAssociationManager')->find(array('taskID' => $Task->get('id'))));
     $MultiSess = new MulticastSessions($MulticastAssociation->get('msID'));
 }
 if ($Task->get('stateID') == 1) {
     // Check In Task for Host
     $Task->set('stateID', 2)->set('checkInTime', $FOGCore->nice_date()->format('Y-m-d H:i:s'))->save();
     // If the state is queued, meaning the client has checked in increment clients
     $Task->get('typeID') == 8 ? $MultiSess->set('clients', $MultiSess->get('clients') + 1)->save() : null;
 }
 // Get the count of total associations.
 $Task->get('typeID') == 8 ? $MSAs = $FOGCore->getClass('MulticastSessionsAssociationManager')->count(array('msID' => $MultiSess->get('id'))) : null;
 // Set the task state for this host as in-progress.
 $Task->set('stateID', 3);
 if ($Task->get('typeID') == 8) {
     // If client count is equal, place session task in-progress as it will likely start soon.
     if ($MSAs == $MultiSess->get('clients') || $MultiSess->get('sessclients') > 0 && $MultiSess->get('clients') > 0) {
         $MultiSess->set('stateID', 3);
     } else {
         $MultiSess->set('stateID', 1);
     }
 }
 // Save the info.
 if ($Task->save() && ($Task->get('typeID') == 8 ? $MultiSess->save() : true)) {
 public function remove_multicast_task()
 {
     try {
         // Remove task from associations and multicast sessions.
         $MSAs = $this->getClass('MulticastSessionsAssociationManager')->find(array('msID' => $_REQUEST['id']));
         $MulticastTask = new MulticastSessions($_REQUEST['id']);
         foreach ((array) $MSAs as $MSA) {
             $Task = new Task($MSA->get('taskID'));
             $Task->cancel();
         }
         $MulticastTask->set('completetime', $this->formatTime('now', 'Y-m-d H:i:s'))->set('stateID', 5)->save();
     } catch (Exception $e) {
     }
 }