From 7ba41afc4617f3d0cb8abe1699c49e48f8d96d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Garnier?= Date: Sun, 8 Jan 2023 21:30:52 +0100 Subject: [PATCH] test thread --- WEB/Client.php | 85 +++++++++++++++---------------- WEB/Client/Client.php | 47 +++++++++-------- WEB/Client/MyTask.php | 73 ++++++++++++++++++++++++++ WEB/Controller/UserController.php | 2 +- WEB/Model/UserModel.php | 8 +-- 5 files changed, 142 insertions(+), 73 deletions(-) create mode 100644 WEB/Client/MyTask.php diff --git a/WEB/Client.php b/WEB/Client.php index 683b4384..e63df91e 100644 --- a/WEB/Client.php +++ b/WEB/Client.php @@ -1,51 +1,48 @@ -findUsersInQueue(); + $numberOfPlayer = count($userGroup); + header('Location : ' . $rep . $vues['queue']); } - $response=""; - while(strcmp($response,"launchGame")!=0){ - $response=socket_read($socket, $BUFFER_SIZE); - if($response!=false) - { - echo "socket_read a réussi.\n"; - if(strcmp($response,"refreshQueue")==0) - { - $userGroup = $userModel->findUsersInQueue(); - $numberOfPlayer=count($userGroup); - header('Location : '.$rep.$vues['queue']); - } - } - else{ - echo "socket_read a échoué : " . socket_strerror(socket_last_error()) . "\n"; - break; - } + } else { + echo "socket_read a échoué : " . socket_strerror(socket_last_error()) . "\n"; + break; } - } \ No newline at end of file + } +} \ No newline at end of file diff --git a/WEB/Client/Client.php b/WEB/Client/Client.php index 7b044828..1d413816 100644 --- a/WEB/Client/Client.php +++ b/WEB/Client/Client.php @@ -1,60 +1,59 @@ -findUsersInQueue(); - $numberOfPlayer=count($userGroup); - header('Location : '.$rep.$vues['queue']); + $numberOfPlayer = count($userGroup); + header('Location : ' . $rep . $vues['queue']); } - } - else{ + } else { echo "socket_read a échoué : " . socket_strerror(socket_last_error()) . "\n"; break; } - } + } } } } \ No newline at end of file diff --git a/WEB/Client/MyTask.php b/WEB/Client/MyTask.php new file mode 100644 index 00000000..12646f6a --- /dev/null +++ b/WEB/Client/MyTask.php @@ -0,0 +1,73 @@ +m_inputs = $inputs; + $this->m_outputs = new Threaded(); // we will store the results in here. + } + + + public function run() + { + foreach ($this->m_inputs as $input) + { + // casting the array to an array is not a mistake + // but actually super important for this to work + // https://github.com/krakjoe/pthreads/issues/813#issuecomment-361955116 + $this->m_outputs[] = (array)array( + 'property1' => $input * 2, + 'property2' => ($input + 2), + ); + } + } + + + # Accessors + public function getResults() { return $this->m_outputs; } +} + + + +function main() +{ + $inputs = range(0,10000); + $numInputsPerTask = 20; + $inputGroups = array_chunk($inputs, $numInputsPerTask); + $numCpus = 4; // I would nomrally dynamically fetch this and sometimes large (e.g. aws C5 instances) + $numTasks = count($inputGroups); + $numThreads = min($numTasks, $numCpus); // don't need to spawn more threads than tasks. + $pool = new Pool($numThreads); + $tasks = array(); // collection to hold all the tasks to get the results from afterwards. + + foreach ($inputGroups as $inputsForTask) + { + $task = new MyTask($inputsForTask); + $tasks[] = $task; + $pool->submit($task); + } + + + while ($pool->collect()); + + # We could submit more stuff here, the Pool is still waiting for work to be submitted. + + $pool->shutdown(); + + # All tasks should have been completed at this point. Get the results! + $results = array(); + foreach ($tasks as $task) + { + $results[] = $task->getResults(); + } + + print "results: " . print_r($results, true); +} + +main(); + diff --git a/WEB/Controller/UserController.php b/WEB/Controller/UserController.php index b0f05b48..678087ff 100644 --- a/WEB/Controller/UserController.php +++ b/WEB/Controller/UserController.php @@ -171,11 +171,11 @@ class UserController { try { global $rep, $vues, $error; //$serverAdress,$port; - require($rep . $vues['queue']); $model = new UserModel(); $model->addToQueue(); $client = Client::getInstance(); $client->Queuing(); + require($rep . $vues['queue']); } catch (Exception $e) { $error = $e->getMessage(); require($rep . $vues['erreur']); diff --git a/WEB/Model/UserModel.php b/WEB/Model/UserModel.php index 3c1ab9df..b178ccb4 100644 --- a/WEB/Model/UserModel.php +++ b/WEB/Model/UserModel.php @@ -27,12 +27,12 @@ class UserModel public function addToQueue() { echo '1'; - if ($this->utilisateur_gateway->isAlreadyInqueue($_SESSION['utilisateur'])) { + $utilisateur = $_SESSION['utilisateur']; + if ($this->utilisateur_gateway->isAlreadyInqueue($utilisateur)) { var_dump($this->utilisateur_gateway->queueFilled()); if($this->utilisateur_gateway->queueFilled()) { - $_REQUEST['action']='launchGame'; - var_dump($_REQUEST['action']); + header('Location: index.php?action=launchGame'); } return; } @@ -52,7 +52,7 @@ class UserModel echo '7'; } echo '8'; - $this->utilisateur_gateway->addToQueue($_SESSION['utilisateur'], $partie); + $this->utilisateur_gateway->addToQueue($utilisateur, $partie); } public function findUserGroup() : int {