Removed redundant Interfaces
Removed a number of unused methods
This commit is contained in:
Chris Boden 2011-11-01 09:52:41 -04:00
parent 57a4500d82
commit 3127efc981
14 changed files with 85 additions and 129 deletions

View File

@ -5,6 +5,7 @@ use Ratchet\SocketCollection;
/** /**
* Socket implementation of the Command Pattern * Socket implementation of the Command Pattern
* User created applications are to return a Command to the server for execution * User created applications are to return a Command to the server for execution
* @todo Bad format - very limited
*/ */
interface CommandInterface { interface CommandInterface {
/** /**

View 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);
}
}
}

View File

@ -26,6 +26,7 @@ class SendMessage implements CommandInterface {
*/ */
public function setMessage($msg) { public function setMessage($msg) {
$this->_message = (string)$msg; $this->_message = (string)$msg;
return $this;
} }
/** /**

View File

@ -1,12 +1,12 @@
<?php <?php
namespace Ratchet\Protocol; 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 * @return Array

View File

@ -3,8 +3,10 @@ namespace Ratchet\Protocol;
use Ratchet\Server; use Ratchet\Server;
use Ratchet\Protocol\WebSocket\Client; use Ratchet\Protocol\WebSocket\Client;
use Ratchet\Protocol\WebSocket\Version; use Ratchet\Protocol\WebSocket\Version;
use Ratchet\Protocol\WebSocket\VersionInterface;
use Ratchet\SocketInterface; use Ratchet\SocketInterface;
use Ratchet\ReceiverInterface; use Ratchet\SocketObserver;
use Ratchet\Command\CommandInterface;
use Ratchet\Command\SendMessage; use Ratchet\Command\SendMessage;
/** /**
@ -13,20 +15,17 @@ use Ratchet\Command\SendMessage;
* @link http://ca.php.net/manual/en/ref.http.php * @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 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 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 { class WebSocket implements ProtocolInterface {
/**
* @type Ratchet\Server
*/
protected $_server;
/** /**
* @type SplObjectStorage * @type SplObjectStorage
*/ */
protected $_lookup; protected $_clients;
/** /**
* @type Ratchet\ReceiverInterface * @type Ratchet\SocketObserver
*/ */
protected $_app; protected $_app;
@ -35,8 +34,8 @@ class WebSocket implements ProtocolInterface {
, 'Hixie76' => null , 'Hixie76' => null
); );
public function __construct(ReceiverInterface $application) { public function __construct(SocketObserver $application) {
$this->_lookup = new \SplObjectStorage; $this->_clients = new \SplObjectStorage;
$this->_app = $application; $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) { public function onOpen(SocketInterface $conn) {
$this->_lookup[$conn] = new Client; $this->_clients[$conn] = new Client;
return $this->_app->onOpen($conn); return $this->_app->onOpen($conn);
} }
public function onRecv(SocketInterface $from, $msg) { public function onRecv(SocketInterface $from, $msg) {
$client = $this->_lookup[$from]; $client = $this->_clients[$from];
if (true !== $client->isHandshakeComplete()) { if (true !== $client->isHandshakeComplete()) {
$headers = $this->getHeaders($msg); $headers = $this->getHeaders($msg);
@ -113,13 +100,16 @@ class WebSocket implements ProtocolInterface {
if ($cmd instanceof SendMessage) { if ($cmd instanceof SendMessage) {
$cmd->setMessage($client->getVersion()->frame($cmd->getMessage())); $cmd->setMessage($client->getVersion()->frame($cmd->getMessage()));
} }
return $cmd; return $cmd;
} }
/**
* @todo Wrap any SendMessage commands
*/
public function onClose(SocketInterface $conn) { public function onClose(SocketInterface $conn) {
$cmd = $this->_app->onClose($conn); $cmd = $this->_app->onClose($conn);
unset($this->_lookup[$conn]); unset($this->_clients[$conn]);
return $cmd; return $cmd;
} }
@ -129,10 +119,24 @@ class WebSocket implements ProtocolInterface {
public function setSubProtocol($name) { 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 * @param string
* @return array * @return array
* @todo Consider strtolower all the header keys...right now PHP Changes Sec-WebSocket-X to Sec-Websocket-X...this could change * @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) { protected function getHeaders($http_message) {
return http_parse_headers($http_message); return http_parse_headers($http_message);
@ -145,7 +149,7 @@ class WebSocket implements ProtocolInterface {
if (isset($headers['Sec-Websocket-Version'])) { // HyBi if (isset($headers['Sec-Websocket-Version'])) { // HyBi
if ($headers['Sec-Websocket-Version'] == '8') { if ($headers['Sec-Websocket-Version'] == '8') {
if (null === $this->_versions['HyBi10']) { if (null === $this->_versions['HyBi10']) {
$this->_versions['HyBi10'] = new Version\Hybi10; $this->_versions['HyBi10'] = new Version\HyBi10;
} }
return $this->_versions['HyBi10']; return $this->_versions['HyBi10'];

View File

@ -1,12 +1,12 @@
<?php <?php
namespace Ratchet\Protocol\WebSocket; namespace Ratchet\Protocol\WebSocket;
use Ratchet\ReceiverInterface; use Ratchet\SocketObserver;
/** /**
* @todo App interfaces this (optionally) if is meant for WebSocket * @todo App interfaces this (optionally) if is meant for WebSocket
* @todo WebSocket checks if instanceof AppInterface, if so uses getSubProtocol() when doing handshake * @todo WebSocket checks if instanceof AppInterface, if so uses getSubProtocol() when doing handshake
*/ */
interface AppInterface extends ReceiverInterface { interface AppInterface extends SocketObserver {
/** /**
* @return string * @return string
*/ */

View File

@ -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 * 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 * @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'; const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
public function handshake(array $headers) { public function handshake(array $headers) {

View File

@ -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);
}

View File

@ -18,13 +18,6 @@ class Server implements SocketObserver, \IteratorAggregate {
*/ */
protected $_master = null; 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 * @var array of Socket Resources
*/ */
@ -41,17 +34,17 @@ class Server implements SocketObserver, \IteratorAggregate {
protected $_log; protected $_log;
/** /**
* @var ReceiverInterface * @var SocketObserver
* Maybe temporary? * Maybe temporary?
*/ */
protected $_app; protected $_app;
/** /**
* @param Ratchet\Socket * @param Ratchet\Socket
* @param ReceiverInterface * @param SocketObserver
* @param Logging\LoggerInterface * @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; $this->_master = $host;
$socket = $host->getResource(); $socket = $host->getResource();
$this->_resources[] = $socket; $this->_resources[] = $socket;
@ -64,7 +57,6 @@ class Server implements SocketObserver, \IteratorAggregate {
$this->_connections = new \ArrayIterator(array()); $this->_connections = new \ArrayIterator(array());
$this->_app = $application; $this->_app = $application;
$this->_app->setUp($this);
} }
/** /**
@ -86,32 +78,6 @@ class Server implements SocketObserver, \IteratorAggregate {
$this->_log = $logger; $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 * @return ArrayIterator of SocketInterfaces
*/ */
@ -119,14 +85,6 @@ class Server implements SocketObserver, \IteratorAggregate {
return $this->_connections; 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 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 * @param int The port to listen to connections on

View File

@ -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);
}

View File

@ -1,18 +1,11 @@
<?php <?php
namespace Ratchet\Tests\Mock; namespace Ratchet\Tests\Mock;
use Ratchet\ReceiverInterface; use Ratchet\SocketObserver;
use Ratchet\Server; use Ratchet\Server;
use Ratchet\Tests\Mock\Socket as MockSocket; use Ratchet\Tests\Mock\Socket as MockSocket;
use Ratchet\SocketInterface; use Ratchet\SocketInterface;
class Application implements ReceiverInterface { class Application implements SocketObserver {
public function getName() {
return 'mock_application';
}
public function setUp(Server $server) {
}
public function onOpen(SocketInterface $conn) { public function onOpen(SocketInterface $conn) {
} }

View File

@ -1,12 +1,12 @@
<?php <?php
namespace Ratchet\Tests\Mock; namespace Ratchet\Tests\Mock;
use Ratchet\ReceiverInterface; use Ratchet\SocketObserver;
use Ratchet\Protocol\ProtocolInterface; use Ratchet\Protocol\ProtocolInterface;
use Ratchet\Server; use Ratchet\Server;
use Ratchet\SocketInterface; use Ratchet\SocketInterface;
class Protocol implements ProtocolInterface { class Protocol implements ProtocolInterface {
public function __construct(ReceiverInterface $application) { public function __construct(SocketObserver $application) {
} }
public static function getDefaultConfig() { 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) { public function onOpen(SocketInterface $conn) {
} }

View File

@ -15,7 +15,7 @@ class WebSocketTest extends \PHPUnit_Framework_TestCase {
} }
public function testServerImplementsServerInterface() { public function testServerImplementsServerInterface() {
$constraint = $this->isInstanceOf('\\Ratchet\\ReceiverInterface'); $constraint = $this->isInstanceOf('\\Ratchet\\SocketObserver');
$this->assertThat($this->_ws, $constraint); $this->assertThat($this->_ws, $constraint);
} }

View File

@ -50,9 +50,11 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
$this->assertSame($logger, $this->getPrivateProperty($this->_server, '_log')); $this->assertSame($logger, $this->getPrivateProperty($this->_server, '_log'));
} }
/*
public function testGetMasterReturnsCatalyst() { public function testGetMasterReturnsCatalyst() {
$this->assertSame($this->_catalyst, $this->_server->getMaster()); $this->assertSame($this->_catalyst, $this->_server->getMaster());
} }
*/
public function testIteration() { public function testIteration() {
$this->assertInstanceOf('\\Iterator', $this->_server->getIterator()); $this->assertInstanceOf('\\Iterator', $this->_server->getIterator());
@ -63,6 +65,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
$this->_server->run(); $this->_server->run();
} }
/*
public function testAttatchedReceiverIsSet() { public function testAttatchedReceiverIsSet() {
$app = new TestApp(); $app = new TestApp();
@ -71,6 +74,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
$this->assertAttributeEquals(Array(spl_object_hash($app) => $app), '_receivers', $this->_server); $this->assertAttributeEquals(Array(spl_object_hash($app) => $app), '_receivers', $this->_server);
} }
/**/
public function testBindToInvalidAddress() { public function testBindToInvalidAddress() {
return $this->markTestIncomplete(); return $this->markTestIncomplete();