Cleanup
Removed redundant Interfaces Removed a number of unused methods
This commit is contained in:
parent
57a4500d82
commit
3127efc981
@ -5,6 +5,7 @@ use Ratchet\SocketCollection;
|
||||
/**
|
||||
* Socket implementation of the Command Pattern
|
||||
* User created applications are to return a Command to the server for execution
|
||||
* @todo Bad format - very limited
|
||||
*/
|
||||
interface CommandInterface {
|
||||
/**
|
||||
|
33
lib/Ratchet/Command/Runtime.php
Normal file
33
lib/Ratchet/Command/Runtime.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace Ratchet\Command;
|
||||
use Ratchet\SocketCollection;
|
||||
|
||||
class Runtime implements CommandInterface {
|
||||
/**
|
||||
* @var SocketCollection
|
||||
*/
|
||||
protected $_sockets;
|
||||
|
||||
/**
|
||||
* @var Closure
|
||||
*/
|
||||
protected $_command = null;
|
||||
|
||||
public function __construct(SocketCollection $sockets) {
|
||||
$this->_socket = $sockets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Your closure should accept a single \Ratchet\Socket parameter
|
||||
* @param Closure Your closure/lambda to execute when the time comes
|
||||
*/
|
||||
public function setCommand(\Closure $callback) {
|
||||
$this->_command = $callback;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
foreach ($this->_sockets as $socket) {
|
||||
return call_user_func($this->_command, $socket);
|
||||
}
|
||||
}
|
||||
}
|
@ -26,6 +26,7 @@ class SendMessage implements CommandInterface {
|
||||
*/
|
||||
public function setMessage($msg) {
|
||||
$this->_message = (string)$msg;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,12 +1,12 @@
|
||||
<?php
|
||||
namespace Ratchet\Protocol;
|
||||
use Ratchet\ReceiverInterface;
|
||||
use Ratchet\SocketObserver;
|
||||
|
||||
interface ProtocolInterface extends ReceiverInterface {
|
||||
interface ProtocolInterface extends SocketObserver {
|
||||
/**
|
||||
* @param Ratchet\ReceiverInterface
|
||||
* @param Ratchet\SocketObserver Application to wrap in protocol
|
||||
*/
|
||||
function __construct(ReceiverInterface $application);
|
||||
function __construct(SocketObserver $application);
|
||||
|
||||
/**
|
||||
* @return Array
|
||||
|
@ -3,8 +3,10 @@ namespace Ratchet\Protocol;
|
||||
use Ratchet\Server;
|
||||
use Ratchet\Protocol\WebSocket\Client;
|
||||
use Ratchet\Protocol\WebSocket\Version;
|
||||
use Ratchet\Protocol\WebSocket\VersionInterface;
|
||||
use Ratchet\SocketInterface;
|
||||
use Ratchet\ReceiverInterface;
|
||||
use Ratchet\SocketObserver;
|
||||
use Ratchet\Command\CommandInterface;
|
||||
use Ratchet\Command\SendMessage;
|
||||
|
||||
/**
|
||||
@ -13,20 +15,17 @@ use Ratchet\Command\SendMessage;
|
||||
* @link http://ca.php.net/manual/en/ref.http.php
|
||||
* @todo Make sure this works both ways (client/server) as stack needs to exist on client for framing
|
||||
* @todo Clean up Client/Version stuff. This should be a factory making single instances of Version classes, implement chain of reponsibility for version - client should implement an interface?
|
||||
* @todo Make sure all SendMessage Commands are framed, not just ones received from onRecv
|
||||
* @todo Logic is flawed with Command/SocketCollection and framing - framing is done based on the protocol version of the received, not individual receivers...
|
||||
*/
|
||||
class WebSocket implements ProtocolInterface {
|
||||
/**
|
||||
* @type Ratchet\Server
|
||||
*/
|
||||
protected $_server;
|
||||
|
||||
/**
|
||||
* @type SplObjectStorage
|
||||
*/
|
||||
protected $_lookup;
|
||||
protected $_clients;
|
||||
|
||||
/**
|
||||
* @type Ratchet\ReceiverInterface
|
||||
* @type Ratchet\SocketObserver
|
||||
*/
|
||||
protected $_app;
|
||||
|
||||
@ -35,8 +34,8 @@ class WebSocket implements ProtocolInterface {
|
||||
, 'Hixie76' => null
|
||||
);
|
||||
|
||||
public function __construct(ReceiverInterface $application) {
|
||||
$this->_lookup = new \SplObjectStorage;
|
||||
public function __construct(SocketObserver $application) {
|
||||
$this->_clients = new \SplObjectStorage;
|
||||
$this->_app = $application;
|
||||
}
|
||||
|
||||
@ -54,25 +53,13 @@ class WebSocket implements ProtocolInterface {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName() {
|
||||
return 'WebSocket';
|
||||
}
|
||||
|
||||
public function setUp(Server $server) {
|
||||
$this->_server = $server;
|
||||
$this->_app->setUp($server);
|
||||
}
|
||||
|
||||
public function onOpen(SocketInterface $conn) {
|
||||
$this->_lookup[$conn] = new Client;
|
||||
$this->_clients[$conn] = new Client;
|
||||
return $this->_app->onOpen($conn);
|
||||
}
|
||||
|
||||
public function onRecv(SocketInterface $from, $msg) {
|
||||
$client = $this->_lookup[$from];
|
||||
$client = $this->_clients[$from];
|
||||
if (true !== $client->isHandshakeComplete()) {
|
||||
|
||||
$headers = $this->getHeaders($msg);
|
||||
@ -113,13 +100,16 @@ class WebSocket implements ProtocolInterface {
|
||||
if ($cmd instanceof SendMessage) {
|
||||
$cmd->setMessage($client->getVersion()->frame($cmd->getMessage()));
|
||||
}
|
||||
|
||||
|
||||
return $cmd;
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Wrap any SendMessage commands
|
||||
*/
|
||||
public function onClose(SocketInterface $conn) {
|
||||
$cmd = $this->_app->onClose($conn);
|
||||
unset($this->_lookup[$conn]);
|
||||
unset($this->_clients[$conn]);
|
||||
return $cmd;
|
||||
}
|
||||
|
||||
@ -129,10 +119,24 @@ class WebSocket implements ProtocolInterface {
|
||||
public function setSubProtocol($name) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Ratchet\Command\CommandInterface
|
||||
* @param Version\VersionInterface
|
||||
* @return \Ratchet\Command\CommandInterface
|
||||
*/
|
||||
protected function prepareCommand(CommandInterface $cmd, VersionInterface $version) {
|
||||
if ($cmd instanceof SendMessage) {
|
||||
$cmd->setMessage($version->frame($cmd->getMessage()));
|
||||
}
|
||||
|
||||
return $cmd;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string
|
||||
* @return array
|
||||
* @todo Consider strtolower all the header keys...right now PHP Changes Sec-WebSocket-X to Sec-Websocket-X...this could change
|
||||
* @todo Put in fallback code if http_parse_headers is not a function
|
||||
*/
|
||||
protected function getHeaders($http_message) {
|
||||
return http_parse_headers($http_message);
|
||||
@ -145,7 +149,7 @@ class WebSocket implements ProtocolInterface {
|
||||
if (isset($headers['Sec-Websocket-Version'])) { // HyBi
|
||||
if ($headers['Sec-Websocket-Version'] == '8') {
|
||||
if (null === $this->_versions['HyBi10']) {
|
||||
$this->_versions['HyBi10'] = new Version\Hybi10;
|
||||
$this->_versions['HyBi10'] = new Version\HyBi10;
|
||||
}
|
||||
|
||||
return $this->_versions['HyBi10'];
|
||||
|
@ -1,12 +1,12 @@
|
||||
<?php
|
||||
namespace Ratchet\Protocol\WebSocket;
|
||||
use Ratchet\ReceiverInterface;
|
||||
use Ratchet\SocketObserver;
|
||||
|
||||
/**
|
||||
* @todo App interfaces this (optionally) if is meant for WebSocket
|
||||
* @todo WebSocket checks if instanceof AppInterface, if so uses getSubProtocol() when doing handshake
|
||||
*/
|
||||
interface AppInterface extends ReceiverInterface {
|
||||
interface AppInterface extends SocketObserver {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
@ -5,7 +5,7 @@ namespace Ratchet\Protocol\WebSocket\Version;
|
||||
* The HyBi-10 version, identified in the headers as version 8, is currently implemented by the latest Chrome and Firefix version
|
||||
* @link http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10
|
||||
*/
|
||||
class Hybi10 implements VersionInterface {
|
||||
class HyBi10 implements VersionInterface {
|
||||
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
||||
|
||||
public function handshake(array $headers) {
|
@ -1,20 +0,0 @@
|
||||
<?php
|
||||
namespace Ratchet;
|
||||
use Ratchet\Server;
|
||||
use Ratchet\SocketObserver;
|
||||
|
||||
/**
|
||||
* Decorator interface for internal protocols
|
||||
* @todo Should probably move this into \Ratchet\Server namespace
|
||||
*/
|
||||
interface ReceiverInterface extends SocketObserver {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
function getName();
|
||||
|
||||
/**
|
||||
* @param Ratchet\Server
|
||||
*/
|
||||
function setUp(Server $server);
|
||||
}
|
@ -18,13 +18,6 @@ class Server implements SocketObserver, \IteratorAggregate {
|
||||
*/
|
||||
protected $_master = null;
|
||||
|
||||
/**
|
||||
* @todo This needs to implement the composite pattern
|
||||
* @var array of ReceiverInterface
|
||||
* @deprecated maybe?
|
||||
*/
|
||||
protected $_receivers = array();
|
||||
|
||||
/**
|
||||
* @var array of Socket Resources
|
||||
*/
|
||||
@ -41,17 +34,17 @@ class Server implements SocketObserver, \IteratorAggregate {
|
||||
protected $_log;
|
||||
|
||||
/**
|
||||
* @var ReceiverInterface
|
||||
* @var SocketObserver
|
||||
* Maybe temporary?
|
||||
*/
|
||||
protected $_app;
|
||||
|
||||
/**
|
||||
* @param Ratchet\Socket
|
||||
* @param ReceiverInterface
|
||||
* @param SocketObserver
|
||||
* @param Logging\LoggerInterface
|
||||
*/
|
||||
public function __construct(SocketInterface $host, ReceiverInterface $application, LoggerInterface $logger = null) {
|
||||
public function __construct(SocketInterface $host, SocketObserver $application, LoggerInterface $logger = null) {
|
||||
$this->_master = $host;
|
||||
$socket = $host->getResource();
|
||||
$this->_resources[] = $socket;
|
||||
@ -64,7 +57,6 @@ class Server implements SocketObserver, \IteratorAggregate {
|
||||
$this->_connections = new \ArrayIterator(array());
|
||||
|
||||
$this->_app = $application;
|
||||
$this->_app->setUp($this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,32 +78,6 @@ class Server implements SocketObserver, \IteratorAggregate {
|
||||
$this->_log = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Receive an interface that creates clients based on interface, decorator pattern for Socket
|
||||
*/
|
||||
public function setClientFactory($s) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ReceiverInterface
|
||||
* @return Server
|
||||
* @deprecated
|
||||
* @todo Consider making server chain of responsibility, currently 1-1 relation w/ receivers
|
||||
*/
|
||||
public function attatchReceiver(ReceiverInterface $receiver) {
|
||||
$receiver->setUp($this);
|
||||
$this->_receivers[spl_object_hash($receiver)] = $receiver;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SocketInterface
|
||||
*/
|
||||
public function getMaster() {
|
||||
return $this->_master;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ArrayIterator of SocketInterfaces
|
||||
*/
|
||||
@ -119,14 +85,6 @@ class Server implements SocketObserver, \IteratorAggregate {
|
||||
return $this->_connections;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string
|
||||
* @param string (note|warning|error)
|
||||
*/
|
||||
public function log($msg, $type = 'note') {
|
||||
call_user_func(array($this->_log, $type), $msg);
|
||||
}
|
||||
|
||||
/*
|
||||
* @param mixed The address to listen for incoming connections on. "0.0.0.0" to listen from anywhere
|
||||
* @param int The port to listen to connections on
|
||||
|
@ -1,11 +0,0 @@
|
||||
<?php
|
||||
namespace Ratchet;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
interface ServerInterface extends \IteratorAggregate {
|
||||
function attatchReceiver(ReceiverInterface $receiver);
|
||||
|
||||
function run($address = '127.0.0.1', $port = 1025);
|
||||
}
|
@ -1,18 +1,11 @@
|
||||
<?php
|
||||
namespace Ratchet\Tests\Mock;
|
||||
use Ratchet\ReceiverInterface;
|
||||
use Ratchet\SocketObserver;
|
||||
use Ratchet\Server;
|
||||
use Ratchet\Tests\Mock\Socket as MockSocket;
|
||||
use Ratchet\SocketInterface;
|
||||
|
||||
class Application implements ReceiverInterface {
|
||||
public function getName() {
|
||||
return 'mock_application';
|
||||
}
|
||||
|
||||
public function setUp(Server $server) {
|
||||
}
|
||||
|
||||
class Application implements SocketObserver {
|
||||
public function onOpen(SocketInterface $conn) {
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
<?php
|
||||
namespace Ratchet\Tests\Mock;
|
||||
use Ratchet\ReceiverInterface;
|
||||
use Ratchet\SocketObserver;
|
||||
use Ratchet\Protocol\ProtocolInterface;
|
||||
use Ratchet\Server;
|
||||
use Ratchet\SocketInterface;
|
||||
|
||||
class Protocol implements ProtocolInterface {
|
||||
public function __construct(ReceiverInterface $application) {
|
||||
public function __construct(SocketObserver $application) {
|
||||
}
|
||||
|
||||
public static function getDefaultConfig() {
|
||||
@ -20,13 +20,6 @@ class Protocol implements ProtocolInterface {
|
||||
);
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return 'mock_protocol';
|
||||
}
|
||||
|
||||
public function setUp(Server $server) {
|
||||
}
|
||||
|
||||
public function onOpen(SocketInterface $conn) {
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,7 @@ class WebSocketTest extends \PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
public function testServerImplementsServerInterface() {
|
||||
$constraint = $this->isInstanceOf('\\Ratchet\\ReceiverInterface');
|
||||
$constraint = $this->isInstanceOf('\\Ratchet\\SocketObserver');
|
||||
$this->assertThat($this->_ws, $constraint);
|
||||
}
|
||||
|
||||
|
@ -50,9 +50,11 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->assertSame($logger, $this->getPrivateProperty($this->_server, '_log'));
|
||||
}
|
||||
|
||||
/*
|
||||
public function testGetMasterReturnsCatalyst() {
|
||||
$this->assertSame($this->_catalyst, $this->_server->getMaster());
|
||||
}
|
||||
*/
|
||||
|
||||
public function testIteration() {
|
||||
$this->assertInstanceOf('\\Iterator', $this->_server->getIterator());
|
||||
@ -63,6 +65,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->_server->run();
|
||||
}
|
||||
|
||||
/*
|
||||
public function testAttatchedReceiverIsSet() {
|
||||
$app = new TestApp();
|
||||
|
||||
@ -71,6 +74,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->assertAttributeEquals(Array(spl_object_hash($app) => $app), '_receivers', $this->_server);
|
||||
}
|
||||
|
||||
/**/
|
||||
public function testBindToInvalidAddress() {
|
||||
return $this->markTestIncomplete();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user