Skip to content

Commit

Permalink
Implemented ImageAsyncTask
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Oct 7, 2024
1 parent 7cb9d9f commit c73c5b0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
8 changes: 5 additions & 3 deletions cli/ratchet/binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use React\EventLoop\Factory;
use React\Socket\LimitingServer;
use React\Socket\Server;
use React\Socket\SecureServer;
use Spatie\Async\Pool;

require __DIR__ . '/../../vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__.'/../../');
$dotenv->load();

$pool = Pool::create();

$logger = new Logger('log');
$logger->pushHandler(new StreamHandler(__DIR__.'/../../storage' . '/binary.log', Logger::INFO));

$clientStorage = new ClientStorage($logger);
$parser = new Parser(new Cli($pool));

$parser = new Parser(new Cli());
$clientStorage = new ClientStorage($logger);

$webSocket = (new BinaryWebSocket($parser))->init($clientStorage);

Expand Down
5 changes: 4 additions & 1 deletion cli/workerman/binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
use Dotenv\Dotenv;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Spatie\Async\Pool;

require __DIR__ . '/../../vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__.'/../../');
$dotenv->load();

$pool = Pool::create();

$logger = new Logger('binary');
$logger->pushHandler(new StreamHandler(BinaryWebSocket::STORAGE_FOLDER . '/binary.log', Logger::INFO));

$parser = new Parser(new Cli());
$parser = new Parser(new Cli($pool));

$clientStorage = new ClientStorage($logger);

Expand Down
5 changes: 3 additions & 2 deletions src/Command/Binary/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace ChessServer\Command\Binary;

use ChessServer\Command\AbstractCli;
use Spatie\Async\Pool;

class Cli extends AbstractCli
{
public function __construct()
public function __construct(Pool $pool)
{
parent::__construct();

$this->commands->attach(new ImageCommand());
$this->commands->attach((new ImageCommand())->setPool($pool));
}
}
37 changes: 37 additions & 0 deletions src/Command/Binary/ImageAsyncTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace ChessServer\Command\Binary;

use Chess\Media\BoardToPng;
use Chess\Variant\Classical\FEN\StrToBoard as ClassicalStrToBoard;
use Chess\Variant\Classical\PGN\AN\Color;
use ChessServer\Socket\AbstractSocket;
use Spatie\Async\Task;

class ImageAsyncTask extends Task
{
private array $params;

public function __construct(array $params)
{
$this->params = $params;
}

public function configure()
{
}

public function run()
{
$board = (new ClassicalStrToBoard($this->params['fen']))->create();
$filename = (new BoardToPng($board, $this->params['flip'] === Color::B))
->output(AbstractSocket::TMP_FOLDER);
$contents = file_get_contents(AbstractSocket::TMP_FOLDER . "/$filename");
$base64 = base64_encode($contents);
if (is_file(AbstractSocket::TMP_FOLDER . "/$filename")) {
unlink(AbstractSocket::TMP_FOLDER . "/$filename");
}

return $base64;
}
}
21 changes: 6 additions & 15 deletions src/Command/Binary/ImageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace ChessServer\Command\Binary;

use Chess\Media\BoardToPng;
use Chess\Variant\Classical\FEN\StrToBoard as ClassicalStrToBoard;
use Chess\Variant\Classical\PGN\AN\Color;
use ChessServer\Command\AbstractCommand;
use ChessServer\Socket\AbstractSocket;

Expand All @@ -28,17 +25,11 @@ public function run(AbstractSocket $socket, array $argv, int $id)
{
$params = json_decode(stripslashes($argv[1]), true);

$board = (new ClassicalStrToBoard($params['fen']))->create();
$filename = (new BoardToPng($board, $params['flip'] === Color::B))
->output(AbstractSocket::TMP_FOLDER);
$contents = file_get_contents(AbstractSocket::TMP_FOLDER . "/$filename");
$base64 = base64_encode($contents);
if (is_file(AbstractSocket::TMP_FOLDER . "/$filename")) {
unlink(AbstractSocket::TMP_FOLDER . "/$filename");
}

return $socket->getClientStorage()->send([$id], [
$this->name => $base64,
]);
$this->pool->add(new ImageAsyncTask($params), 81920)
->then(function ($result) use ($socket, $id) {
return $socket->getClientStorage()->send([$id], [
$this->name => $result,
]);
});
}
}

0 comments on commit c73c5b0

Please sign in to comment.