Created
November 26, 2021 15:43
-
-
Save martinobettucci/9f2a95e603645dc7e88b0b702ac0cd90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__ . '/../vendor/autoload.php'; | |
use Phacterl\Runtime\Scheduler; | |
use Phacterl\Actor\Actor; | |
use Phacterl\Message\Message; | |
// Le puppet master | |
$scheduler = new Scheduler(); | |
class Passage extends Actor { | |
protected $state; | |
public function init($args) { | |
echo "Passage.init", "\n"; | |
return array('etat' => $args['etat']); | |
} | |
public function receive() { | |
echo "Passage.receive", "\n"; | |
return array('open', 'close'); | |
} | |
public function handle_open($msg, $state) | |
{ | |
echo "Feu.handle_open: open", "\n"; | |
$state['etat'] = 'open'; | |
return $state; | |
} | |
public function handle_close($msg, $state) | |
{ | |
echo "Feu.handle_open: closed for ", $msg['seconds'], " seconds", "\n"; | |
$state['etat'] = 'closed'; | |
// attends le passage du train | |
sleep($msg['seconds']); | |
// reouvre le portail | |
$this->handle_close($msg, $state); | |
return $state; | |
} | |
} | |
$passage1 = $scheduler->spawn('Passage', array('etat' => 'open')); | |
$passage2 = $scheduler->spawn('Passage', array('etat' => 'open')); | |
$passage3 = $scheduler->spawn('Passage', array('etat' => 'open')); | |
$passage4 = $scheduler->spawn('Passage', array('etat' => 'open')); | |
class Train extends Actor { | |
public function init($args) { | |
return $args; | |
} | |
public function receive() { | |
return array('passing'); | |
} | |
public function handle_passing($msg, $state) { | |
echo "Train.handle_passing: ", $msg['sender'], "\n"; | |
// temps de passage du train | |
$seconds = $state['speed'] * $state['length']; | |
foreach ($state['passages'] as $passageDestinataire) { | |
$this->send($passageDestinataire, new Message('close', array('seconds' => $seconds, "id" => $passageDestinataire))); | |
} | |
return $state; | |
} | |
} | |
$train1 = $scheduler->spawn('Train', array('speed' => 1, 'length' => 5, 'passages' => array($passage1))); | |
$train2 = $scheduler->spawn('Train', array('speed' => 1, 'length' => 5, 'passages' => array($passage2))); | |
$train3 = $scheduler->spawn('Train', array('speed' => 1, 'length' => 5, 'passages' => array($passage3, $passage4))); | |
$scheduler->send($train1, new Message('passing', null)); | |
$scheduler->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment