"Fossies" - the Fresh Open Source Software Archive

Member "fogproject-1.5.9/packages/web/lib/service/multicasttask.class.php" (13 Sep 2020, 19055 Bytes) of package /linux/misc/fogproject-1.5.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "multicasttask.class.php": 1.5.8_vs_1.5.9.

    1 <?php
    2 /**
    3  * Multicast task generator/finder
    4  *
    5  * PHP version 5
    6  *
    7  * @category MulticastTask
    8  * @package  FOGProject
    9  * @author   Tom Elliott <tommygunsster@gmail.com>
   10  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
   11  * @link     https://fogproject.org
   12  */
   13 /**
   14  * Multicast task generator/finder
   15  *
   16  * @category MulticastTask
   17  * @package  FOGProject
   18  * @author   Tom Elliott <tommygunsster@gmail.com>
   19  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
   20  * @link     https://fogproject.org
   21  */
   22 class MulticastTask extends FOGService
   23 {
   24     /**
   25      * Gets all the multicast tasks
   26      *
   27      * @param string $root            root to look for items
   28      * @param int    $myStorageNodeID this services storage id
   29      * @param string $queuedStates    the queued states.
   30      *
   31      * @return array
   32      */
   33     public static function getAllMulticastTasks(
   34         $root,
   35         $myStorageNodeID,
   36         $queuedStates
   37     ) {
   38         Route::indiv(
   39             'storagenode',
   40             $myStorageNodeID
   41         );
   42         $StorageNode = json_decode(
   43             Route::getData()
   44         );
   45         self::$HookManager->processEvent(
   46             'CHECK_NODE_MASTER',
   47             array(
   48                 'StorageNode' => &$StorageNode,
   49                 'FOGServiceClass' => __CLASS__
   50             )
   51         );
   52         if (!$StorageNode->isMaster) {
   53             return;
   54         }
   55         $Interface = self::getMasterInterface(
   56             self::resolveHostname(
   57                 $StorageNode->ip
   58             )
   59         );
   60         unset($StorageNode);
   61         Route::active('multicastsession');
   62         $Tasks = json_decode(
   63             Route::getData()
   64         );
   65         $NewTasks = [];
   66         foreach ($Tasks->multicastsessions as &$Task) {
   67             $find = ['msID' => $Task->id];
   68             Route::ids(
   69                 'multicastsessionassociation',
   70                 $find,
   71                 'taskID'
   72             );
   73             $taskIDs = json_decode(Route::getData(), true);
   74             $count = count($taskIDs ?: []);
   75             if ($count < 1) {
   76                 $count = $Task->sessclients;
   77             }
   78             if ($count < 1) {
   79                 self::getClass('MulticastSessionManager')->update(
   80                     ['id' => $Task->id],
   81                     '',
   82                     [
   83                         'stateID' => self::getCancelledState(),
   84                         'name' => ''
   85                     ]
   86                 );
   87                 self::outall(
   88                     _('Task not created as there are no associated tasks')
   89                 );
   90                 self::outall(
   91                     _('Or there was no number defined for joining session')
   92                 );
   93                 continue;
   94             }
   95             Route::indiv(
   96                 'image',
   97                 $Task->image->id
   98             );
   99             $Image = json_decode(
  100                 Route::getData()
  101             );
  102             $fullPath = sprintf('%s/%s', $root, $Task->logpath);
  103             if (!file_exists($fullPath)) {
  104                 self::outall(_(' | Unable to find image path'));
  105                 continue;
  106             }
  107             $NewTasks[] = new self(
  108                 $Task->id,
  109                 $Task->name,
  110                 $Task->port,
  111                 $fullPath,
  112                 $Interface,
  113                 $count,
  114                 $Task->isDD,
  115                 $Image->osID,
  116                 ($Task->clients == -2 ? 1 : 0),
  117                 $taskIDs
  118             );
  119             unset($Task);
  120         }
  121         return array_filter($NewTasks);
  122     }
  123     /**
  124      * Session ID
  125      *
  126      * @var int
  127      */
  128     private $_intID;
  129     /**
  130      * The session name
  131      *
  132      * @var string
  133      */
  134     private $_strName;
  135     /**
  136      * The session port
  137      *
  138      * @var int
  139      */
  140     private $_intPort;
  141     /**
  142      * The session image
  143      *
  144      * @var string
  145      */
  146     private $_strImage;
  147     /**
  148      * The session interface to use
  149      *
  150      * @var string
  151      */
  152     private $_strEth;
  153     /**
  154      * The number of clients
  155      *
  156      * @var int
  157      */
  158     private $_intClients;
  159     /**
  160      * The sessions task ids
  161      *
  162      * @var array
  163      */
  164     private $_taskIDs;
  165     /**
  166      * The sessions image type
  167      *
  168      * @var int
  169      */
  170     private $_intImageType;
  171     /**
  172      * The sessions osid
  173      *
  174      * @var int
  175      */
  176     private $_intOSID;
  177     /**
  178      * Is this session a joined session
  179      *
  180      * @var bool
  181      */
  182     private $_isNameSess;
  183     /**
  184      * The multicast session class
  185      *
  186      * @var object
  187      */
  188     private $_MultiSess;
  189     /**
  190      * This tasks process reference
  191      *
  192      * @var resource
  193      */
  194     public $procRef;
  195     /**
  196      * This tasks process piped info
  197      *
  198      * @var resource
  199      */
  200     public $procPipes;
  201     /**
  202      * Initializes the task so multicast man can process
  203      *
  204      * @param int    $id        the id
  205      * @param string $name      the name
  206      * @param int    $port      the port
  207      * @param string $image     the image
  208      * @param string $eth       the interface
  209      * @param int    $clients   the number of clients
  210      * @param int    $imagetype the image type
  211      * @param int    $osid      the os id
  212      * @param bool   $nameSess  the named session
  213      * @param array  $taskIDs   the task ids
  214      *
  215      * @return void
  216      */
  217     public function __construct(
  218         $id = '',
  219         $name = '',
  220         $port = '',
  221         $image = '',
  222         $eth = '',
  223         $clients = '',
  224         $imagetype = '',
  225         $osid = '',
  226         $nameSess = '',
  227         $taskIDs = ''
  228     ) {
  229         parent::__construct();
  230         $overridePort = self::getSetting('FOG_MULTICAST_PORT_OVERRIDE');
  231         $this->_intID = $id;
  232         $this->_strName = $name;
  233         if ($overridePort) {
  234             $this->_intPort = $overridePort;
  235         } else {
  236             $this->_intPort = $port;
  237         }
  238         $this->_strImage = $image;
  239         $this->_strEth = $eth;
  240         $this->_intClients = $clients;
  241         $this->_intImageType = $imagetype;
  242         $this->_intOSID = $osid;
  243         $this->_isNameSess = $nameSess;
  244         $this->_taskIDs = $taskIDs;
  245         $this->_MultiSess = new MulticastSession($this->getID());
  246     }
  247     /**
  248      * Is this a named session
  249      *
  250      * @return bool
  251      */
  252     public function isNamedSessionFinished()
  253     {
  254         if ($this->_isNameSess
  255                 && $this->_MultiSess->get('clients') == 0
  256                 && !$this->isRunning($this->procRef)) {
  257             return true;
  258         } else {
  259             return false;
  260         }
  261     }
  262     /**
  263      * Returns the task ids
  264      *
  265      * @return array
  266      */
  267     public function getTaskIDs()
  268     {
  269         return $this->_taskIDs;
  270     }
  271     /**
  272      * Returns the id
  273      *
  274      * @return int
  275      */
  276     public function getID()
  277     {
  278         return $this->_intID;
  279     }
  280     /**
  281      * Returns the name
  282      *
  283      * @return string
  284      */
  285     public function getName()
  286     {
  287         return $this->_strName;
  288     }
  289     /**
  290      * Returns the image path
  291      *
  292      * @return string
  293      */
  294     public function getImagePath()
  295     {
  296         return $this->_strImage;
  297     }
  298     /**
  299      * Returns the image type
  300      *
  301      * @return int
  302      */
  303     public function getImageType()
  304     {
  305         return $this->_intImageType;
  306     }
  307     /**
  308      * Returns the client count
  309      *
  310      * @return int
  311      */
  312     public function getClientCount()
  313     {
  314         return $this->_intClients;
  315     }
  316     /**
  317      * Returns the port
  318      *
  319      * @return int
  320      */
  321     public function getPortBase()
  322     {
  323         return $this->_intPort;
  324     }
  325     /**
  326      * Returns the interface
  327      *
  328      * @return string
  329      */
  330     public function getInterface()
  331     {
  332         return $this->_strEth;
  333     }
  334     /**
  335      * Returns the os id
  336      *
  337      * @return int
  338      */
  339     public function getOSID()
  340     {
  341         return $this->_intOSID;
  342     }
  343     /**
  344      * Returns the udpcast log file
  345      *
  346      * @return string
  347      */
  348     public function getUDPCastLogFile()
  349     {
  350         list(
  351             $filenam,
  352             $logpath
  353         ) = self::getSubObjectIDs(
  354             'Service',
  355             array(
  356                 'name' => array(
  357                     'MULTICASTLOGFILENAME',
  358                     'SERVICE_LOG_PATH',
  359                 )
  360             ),
  361             'value',
  362             false,
  363             'AND',
  364             'name',
  365             false,
  366             ''
  367         );
  368         return $this->altLog = sprintf(
  369             '/%s/%s.udpcast.%s',
  370             trim($logpath, '/'),
  371             $filenam,
  372             $this->getID()
  373         );
  374     }
  375     /**
  376      * Returns the bitrate max
  377      *
  378      * @return string
  379      */
  380     public function getBitrate()
  381     {
  382         return self::getClass(
  383             'Image',
  384             $this->_MultiSess->get('image')
  385         )->getStorageGroup()
  386         ->getMasterStorageNode()
  387         ->get('bitrate');
  388     }
  389     /**
  390      * Returns the rexmit hello interval
  391      *
  392      * @return string
  393      */
  394     public function getHelloInterval()
  395     {
  396         return self::getClass(
  397             'Image',
  398             $this->_MultiSess->get('image')
  399         )->getStorageGroup()
  400         ->getMasterStorageNode()
  401         ->get('helloInterval');
  402     }
  403 
  404 
  405     /**
  406      * Returns the partition id to be cloned, 0 for all
  407      *
  408      * @return int
  409      */
  410     public function getPartitions()
  411     {
  412         return (int)self::getClass(
  413             'Image',
  414             $this->_MultiSess->get('image')
  415         )->getPartitionType();
  416     }
  417     /**
  418      * Returns the session class
  419      *
  420      * @return object
  421      */
  422     public function getSess()
  423     {
  424         return $this->_MultiSess;
  425     }
  426     /**
  427      * Sets/Gets the command needed to start the tasking
  428      *
  429      * @return string
  430      */
  431     public function getCMD()
  432     {
  433         unset(
  434             $filelist,
  435             $buildcmd,
  436             $cmd
  437         );
  438         list(
  439             $address,
  440             $duplex,
  441             $multicastrdv,
  442             $maxwait
  443         ) = self::getSubObjectIDs(
  444             'Service',
  445             array(
  446                 'name' => array(
  447                     'FOG_MULTICAST_ADDRESS',
  448                     'FOG_MULTICAST_DUPLEX',
  449                     'FOG_MULTICAST_RENDEZVOUS',
  450                     'FOG_UDPCAST_MAXWAIT'
  451                 )
  452             ),
  453             'value',
  454             false,
  455             'AND',
  456             'name',
  457             false,
  458             ''
  459         );
  460         if ($address) {
  461             $address = long2ip(
  462                 ip2long($address) + (
  463                     (
  464                         $this->getPortBase() / 2 + 1
  465                     ) % self::getSetting('FOG_MULTICAST_MAX_SESSIONS')
  466                 )
  467             );
  468         }
  469         $buildcmd = array(
  470             UDPSENDERPATH,
  471             (
  472                 $this->getHelloInterval() ?
  473                 sprintf(' --rexmit-hello-interval %s', $this->getHelloInterval()) :
  474                 null
  475             ),
  476             (
  477                 $this->getBitrate() ?
  478                 sprintf(' --max-bitrate %s', $this->getBitrate()) :
  479                 null
  480             ),
  481             (
  482                 $this->getInterface() ?
  483                 sprintf(' --interface %s', $this->getInterface()) :
  484                 null
  485             ),
  486             sprintf(
  487                 ' --min-receivers %d',
  488                 (
  489                     $this->getClientCount() ?
  490                     $this->getClientCount():
  491                     self::getClass('HostManager')->count()
  492                 )
  493             ),
  494             sprintf(' --max-wait %s', '%d'),
  495             (
  496                 $address ?
  497                 sprintf(' --mcast-data-address %s', $address) :
  498                 null
  499             ),
  500             (
  501                 $multicastrdv ?
  502                 sprintf(' --mcast-rdv-address %s', $multicastrdv) :
  503                 null
  504             ),
  505             sprintf(' --portbase %s', $this->getPortBase()),
  506             sprintf(' %s', $duplex),
  507             ' --ttl 32',
  508             ' --nokbd',
  509             ' --nopointopoint',
  510         );
  511         $buildcmd = array_values(array_filter($buildcmd));
  512         switch ($this->getImageType()) {
  513         case 1:
  514             switch ($this->getOSID()) {
  515             case 1:
  516             case 2:
  517                 if (is_file($this->getImagePath())) {
  518                     $filelist[] = $this->getImagePath();
  519                     break;
  520                 }
  521                 // no break
  522             case 5:
  523             case 6:
  524             case 7:
  525                 $files = scandir($this->getImagePath());
  526                 $sys = preg_grep('#(sys\.img\..*$)#i', $files);
  527                 $rec = preg_grep('#(rec\.img\..*$)#i', $files);
  528                 if (count($sys) || count($rec)) {
  529                     if (count($sys)) {
  530                         $filelist[] = 'sys.img.*';
  531                     }
  532                     if (count($rec)) {
  533                         $filelist[] = 'rec.img.*';
  534                     }
  535                 } else {
  536                     $filename = 'd1p%d.%s';
  537                     $iterator = new DirectoryIterator(
  538                         $this->getImagePath()
  539                     );
  540                     foreach ($iterator as $fileInfo) {
  541                         if ($fileInfo->isDot()) {
  542                             continue;
  543                         }
  544                         sscanf(
  545                             $fileInfo->getFilename(),
  546                             $filename,
  547                             $part,
  548                             $ext
  549                         );
  550                         if ($ext == 'img') {
  551                             $filelist[] = $fileInfo->getFilename();
  552                         }
  553                         unset($part, $ext);
  554                     }
  555                     unset($iterator);
  556                 }
  557                 unset($files, $sys, $rec);
  558                 break;
  559             default:
  560                 $filename = 'd1p%d.%s';
  561                 $iterator = new DirectoryIterator(
  562                     $this->getImagePath()
  563                 );
  564                 foreach ($iterator as $fileInfo) {
  565                     if ($fileInfo->isDot()) {
  566                         continue;
  567                     }
  568                     sscanf(
  569                         $fileInfo->getFilename(),
  570                         $filename,
  571                         $part,
  572                         $ext
  573                     );
  574                     if ($ext == 'img') {
  575                         $filelist[] = $fileInfo->getFilename();
  576                     }
  577                     unset($part, $ext);
  578                 }
  579                 unset($iterator);
  580                 break;
  581             }
  582             break;
  583         case 2:
  584             $filename = 'd1p%d.%s';
  585             $iterator = new DirectoryIterator(
  586                 $this->getImagePath()
  587             );
  588             foreach ($iterator as $fileInfo) {
  589                 if ($fileInfo->isDot()) {
  590                     continue;
  591                 }
  592                 sscanf(
  593                     $fileInfo->getFilename(),
  594                     $filename,
  595                     $part,
  596                     $ext
  597                 );
  598                 if ($ext == 'img') {
  599                     $filelist[] = $fileInfo->getFilename();
  600                 }
  601                 unset($part, $ext);
  602             }
  603             unset($iterator);
  604             break;
  605         case 3:
  606             $filename = 'd%dp%d.%s';
  607             $iterator = new DirectoryIterator(
  608                 $this->getImagePath()
  609             );
  610             foreach ($iterator as $fileInfo) {
  611                 if ($fileInfo->isDot()) {
  612                     continue;
  613                 }
  614                 sscanf(
  615                     $fileInfo->getFilename(),
  616                     $filename,
  617                     $device,
  618                     $part,
  619                     $ext
  620                 );
  621                 if ($ext == 'img') {
  622                     $filelist[] = $fileInfo->getFilename();
  623                 }
  624                 unset($device, $part, $ext);
  625             }
  626             unset($iterator);
  627             break;
  628         case 4:
  629             $iterator = new DirectoryIterator(
  630                 $this->getImagePath()
  631             );
  632             foreach ($iterator as $fileInfo) {
  633                 if ($fileInfo->isDot()) {
  634                     continue;
  635                 }
  636                 $filelist[] = $fileInfo->getFilename();
  637             }
  638             unset($iterator);
  639             break;
  640         }
  641         natcasesort($filelist);
  642         $partid = self::getPartitions();
  643         if ($partid < 1) {
  644             $filelist = array_values((array)$filelist);
  645         } else {
  646             $filelist = array_values(
  647                 preg_grep("/^d[0-9]p$partid\.img$/", (array)$filelist)
  648             );
  649         }
  650         ob_start();
  651         foreach ($filelist as $i => &$file) {
  652             printf(
  653                 '%s --file %s%s%s;',
  654                 sprintf(
  655                     implode($buildcmd),
  656                     (
  657                         $i == 0 ?
  658                         $maxwait * 60 :
  659                         10
  660                     )
  661                 ),
  662                 rtrim(
  663                     $this->getImagePath(),
  664                     DS
  665                 ),
  666                 DS,
  667                 $file
  668             );
  669             unset($file);
  670         }
  671         unset($filelist, $buildcmd);
  672         return ob_get_clean();
  673     }
  674     /**
  675      * Starts our tasking as needed
  676      *
  677      * @return bool
  678      */
  679     public function startTask()
  680     {
  681         if (file_exists($this->getUDPCastLogFile())) {
  682             unlink($this->getUDPCastLogFile());
  683         }
  684         $this->startTasking($this->getCMD(), $this->getUDPCastLogFile());
  685         $this->procRef = array_shift($this->procRef);
  686         $this->_MultiSess
  687             ->set('stateID', self::getQueuedState())
  688             ->save();
  689         return $this->isRunning($this->procRef);
  690     }
  691     /**
  692      * Kills the tasking as needed
  693      *
  694      * @return bool
  695      */
  696     public function killTask()
  697     {
  698         $this->killTasking();
  699         if (file_exists($this->getUDPCastLogFile())) {
  700             unlink($this->getUDPCastLogFile());
  701         }
  702         return true;
  703     }
  704     /**
  705      * Updates the stats of the tasking
  706      *
  707      * @return void
  708      */
  709     public function updateStats()
  710     {
  711         Route::listem(
  712             'multicastsessionassociation',
  713             ['msID' => $this->_intID]
  714         );
  715         $MSAssocs = json_decode(
  716             Route::getData()
  717         );
  718         $TaskPercent = [];
  719         foreach ($MSAssocs as &$Task) {
  720             $TaskPercent[] = self::getClass('Task', $Task->taskID)->get('percent');
  721             unset($Task);
  722         }
  723         $TaskPercent = array_unique((array)$TaskPercent);
  724         $this->_MultiSess
  725             ->set('percent', @max($TaskPercent))
  726             ->save();
  727     }
  728     /**
  729      * Updates task ID list in case of MC session joins via PXE menu
  730      *
  731      * @return void
  732      */
  733     public function setTaskIDs($newTaskIDs)
  734     {
  735         $this->_taskIDs = $newTaskIDs;
  736     }
  737 }