Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hw5 #92

Open
wants to merge 7 commits into
base: AHarshkou/main
Choose a base branch
from
Open

hw5 #92

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# PHP_2024

https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus
### Запускк

1. docker-compose up -d
2. docker-compose run app-server php index.php server
3. docker-compose run app-client php index.php client
13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "ahor/hw5",
"type": "project",
"autoload": {
"psr-4": {
"Ahor\\Hw5\\": "src/"
}
},
"require": {
"ext-sockets": "*",
"ext-readline": "*"
}
}
18 changes: 18 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[socket]
file_name = /app/data/tmp.sock
max_len = 256
2 changes: 2 additions & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
21 changes: 21 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: "3"

services:
app-server:
build: docker/php
volumes:
- .:/app
networks:
- net
working_dir: /app/public
app-client:
build: docker/php
volumes:
- .:/app
working_dir: /app/public
networks:
- net

networks:
net:
driver: bridge
19 changes: 19 additions & 0 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM php:8.3.2-fpm

RUN apt-get update && apt-get install -y \
zlib1g-dev \
libmemcached-dev \
libssl-dev \
git

RUN docker-php-ext-install pdo pdo_mysql sockets && docker-php-ext-enable pdo pdo_mysql sockets

RUN pecl install redis-6.0.0 memcached-3.2.0 && docker-php-ext-enable redis memcached

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/bin --filename=composer --quiet --version=2.6.6

WORKDIR /app

EXPOSE 9000

CMD ["php-fpm"]
18 changes: 18 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Ahor\Hw5\App;

require('../vendor/autoload.php');


try {
$app = new App(__DIR__ . '/../config/config.ini');
$data = $app->run($argv[1] ?? null);

foreach ($data as $message) {
echo $message;
}
} catch (Exception $e) {
echo "ERROR" . PHP_EOL;
echo $e->getMessage() . PHP_EOL;
}
33 changes: 33 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Ahor\Hw5;

class App
{
public Config $config;

public function __construct(string $fileConfigName)
{
$this->config = new Config($fileConfigName);
}

public function run($arg): \Generator
{
switch ($arg) {
case 'server':
$server = new Server($this->config);
$data = $server->start();

break;
case 'client':
$client = new Client($this->config);
$data = $client->start();

break;
default:
throw new \DomainException('Ошибка аргумента, server или client' . PHP_EOL);
}

return $data;
}
}
68 changes: 68 additions & 0 deletions src/Chat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Ahor\Hw5;

use Socket;

abstract class Chat
{
private Socket $socket;

public function __construct(private readonly Config $config)
{
}

public function create($clear = false): void
{
if ($clear && file_exists($this->config->socketFile)) {
unlink($this->config->socketFile);
}

$result = socket_create(AF_UNIX, SOCK_STREAM, 0);
if ($result === false) {
throw new \DomainException("Ошибка создания сокета");
}

$this->socket = $result;
}

public function listen(): void
{
socket_listen($this->socket);
}

public function bind(): void
{
socket_bind($this->socket, $this->config->socketFile);
}

public function close(): void
{
socket_close($this->socket);
}

public function accept()
{
return socket_accept($this->socket);
}

public function receive($socket): string
{
socket_recv($socket, $message, $this->config->maxLen, 0);

return $message ?? '';
}


public function connect(): void
{
socket_connect($this->socket, $this->config->socketFile);
}

public function send(string $message): void
{
socket_write($this->socket, $message, strlen($message));
}

abstract public function start();
}
26 changes: 26 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Ahor\Hw5;

class Client extends Chat
{
public function start(): \Generator
{
yield "Старт клиента" . PHP_EOL;

$this->create();
$this->connect();

$run = true;
while ($run) {
$message = readline('Введите сообщение и нажмите Enter: ');
$this->send($message);

if ($message === 'stop') {
$run = false;
}
}

$this->close();
}
}
30 changes: 30 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Ahor\Hw5;

class Config
{
public string $socketFile;
public int $maxLen;

public function __construct(string $fileName)
{
$config = parse_ini_file($fileName, true);

if (empty($config)) {
throw new \DomainException("Файла конфига нету");
}

if (empty($config['socket']['file_name'])) {
throw new \DomainException("Имя файла сокета не найден");
}

$this->socketFile = $config['socket']['file_name'];

if (empty($config['socket']['max_len'])) {
throw new \DomainException("Максимальная длинна не найдена");
}

$this->maxLen = (int)$config['socket']['max_len'];
}
}
38 changes: 38 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Ahor\Hw5;

class Server extends Chat
{
public function __construct(Config $config)
{
parent::__construct($config);

$this->create(true);
$this->bind();
$this->listen();
}

public function start(): \Generator
{
yield "Старт сервера" . PHP_EOL;
$client = $this->accept();

$run = true;
while ($run) {
$message = $this->receive($client);

if ($message === 'stop') {
socket_close($client);
$run = false;
}

if ($message) {
yield "Пришло сообщение" . PHP_EOL;
yield $message . PHP_EOL;
}
}

$this->close();
}
}
Loading