Browse Source

init common server type driver

main
yggverse 7 months ago
parent
commit
207b5f577a
  1. 139
      src/Abstract/Type/Nex.php
  2. 109
      src/Controller/Nex/Filesystem.php

139
src/Abstract/Type/Nex.php

@ -0,0 +1,139 @@
<?php
namespace Yggverse\Next\Abstract\Type;
use \Ratchet\MessageComponentInterface;
abstract class Nex implements MessageComponentInterface
{
protected \Yggverse\Next\Model\Environment $_environment;
abstract public function init();
public function __construct(
\Yggverse\Next\Model\Environment $environment
) {
if (!$environment->get('port')) $environment->set('port', 1900);
if ($environment->get('dump'))
{
print(
str_replace(
[
'{time}',
'{host}',
'{port}'
],
[
(string) date('c'),
(string) $environment->get('host'),
(string) $environment->get('port')
],
_('[{time}] [construct] server {host}:{port}')
) . PHP_EOL
);
}
$this->_environment = $environment;
$this->init();
}
public function onOpen(
\Ratchet\ConnectionInterface $connection
) {
if ($this->_environment->get('dump'))
{
print(
str_replace(
[
'{time}',
'{host}',
'{crid}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId
],
_('[{time}] [open] incoming connection {host}#{crid}')
) . PHP_EOL
);
}
}
public function onMessage(
\Ratchet\ConnectionInterface $connection,
$request
) {
if ($this->_environment->get('dump'))
{
print(
str_replace(
[
'{time}',
'{host}',
'{crid}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId
],
_('[{time}] [message] incoming connection {host}#{crid}')
) . PHP_EOL
);
}
}
public function onClose(
\Ratchet\ConnectionInterface $connection
) {
if ($this->_environment->get('dump'))
{
print(
str_replace(
[
'{time}',
'{host}',
'{crid}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId
],
_('[{time}] [close] incoming connection {host}#{crid}')
) . PHP_EOL
);
}
}
public function onError(
\Ratchet\ConnectionInterface $connection,
\Exception $exception
) {
if ($this->_environment->get('dump'))
{
print(
str_replace(
[
'{time}',
'{host}',
'{crid}',
'{info}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId,
(string) $exception->getMessage()
],
_('[{time}] [error] incoming connection {host}#{crid} reason: {info}')
) . PHP_EOL
);
}
$connection->close();
}
}

109
src/Controller/Nex/Filesystem.php

@ -2,30 +2,25 @@
namespace Yggverse\Next\Controller\Nex; namespace Yggverse\Next\Controller\Nex;
use \Ratchet\MessageComponentInterface; class Filesystem extends \Yggverse\Next\Abstract\Type\Nex
class Filesystem implements MessageComponentInterface
{ {
private \Yggverse\Next\Model\Environment $_environment;
private \Yggverse\Next\Model\Filesystem $_filesystem; private \Yggverse\Next\Model\Filesystem $_filesystem;
public function __construct( public function init()
\Yggverse\Next\Model\Environment $environment, {
\Yggverse\Next\Model\Filesystem $filesystem // Validate environment arguments defined for this type
) { if (!$this->_environment->get('root'))
// Init environment
$this->_environment = $environment;
// Init filesystem
$this->_filesystem = $filesystem;
// Check port is defined
if (!$this->_environment->get('port'))
{ {
// Set protocol defaults throw new \Exception(
$this->_environment->set('port', 1900); _('filesystem root path required!')
);
} }
// Init filesystem
$this->_filesystem = new \Yggverse\Next\Model\Filesystem(
$this->_environment->get('root')
);
// Dump event // Dump event
if ($this->_environment->get('dump')) if ($this->_environment->get('dump'))
{ {
@ -49,30 +44,6 @@ class Filesystem implements MessageComponentInterface
} }
} }
public function onOpen(
\Ratchet\ConnectionInterface $connection
) {
// Dump event
if ($this->_environment->get('dump'))
{
print(
str_replace(
[
'{time}',
'{host}',
'{crid}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId
],
_('[{time}] [open] incoming connection {host}#{crid}')
) . PHP_EOL
);
}
}
public function onMessage( public function onMessage(
\Ratchet\ConnectionInterface $connection, \Ratchet\ConnectionInterface $connection,
$request $request
@ -214,58 +185,4 @@ class Filesystem implements MessageComponentInterface
// Disconnect // Disconnect
$connection->close(); $connection->close();
} }
public function onClose(
\Ratchet\ConnectionInterface $connection
) {
// Dump event
if ($this->_environment->get('dump'))
{
print(
str_replace(
[
'{time}',
'{host}',
'{crid}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId
],
_('[{time}] [close] incoming connection {host}#{crid}')
) . PHP_EOL
);
}
}
public function onError(
\Ratchet\ConnectionInterface $connection,
\Exception $exception
) {
// Dump event
if ($this->_environment->get('dump'))
{
print(
str_replace(
[
'{time}',
'{host}',
'{crid}',
'{info}'
],
[
(string) date('c'),
(string) $connection->remoteAddress,
(string) $connection->resourceId,
(string) $exception->getMessage()
],
_('[{time}] [error] incoming connection {host}#{crid} reason: {info}')
) . PHP_EOL
);
}
// Disconnect
$connection->close();
}
} }
Loading…
Cancel
Save