From 60a8a04e4062c7331900281f8901a6733134d597 Mon Sep 17 00:00:00 2001 From: Chris Boden <cboden@gmail.com> Date: Mon, 7 Nov 2011 11:55:07 -0500 Subject: [PATCH] Cleanup Fixed bad interface implementation in Close Command Removed old code from Composite (now in Factory) Removed done @todo's Cleaned up Ping/Pong Added HyBi-10 frame/unframe test --- .../Command/Action/CloseConnection.php | 4 ++-- lib/Ratchet/Command/Composite.php | 22 ------------------- lib/Ratchet/Protocol/WebSocket.php | 8 ++++--- .../WebSocket/Command/Action/Ping.php | 4 +--- .../WebSocket/Command/Action/Pong.php | 4 +--- .../Protocol/WebSocket/Version/HyBi10.php | 3 ++- lib/Ratchet/Server.php | 1 - .../Protocol/WebSocket/Version/HyBi10Test.php | 8 +++++++ 8 files changed, 19 insertions(+), 35 deletions(-) diff --git a/lib/Ratchet/Command/Action/CloseConnection.php b/lib/Ratchet/Command/Action/CloseConnection.php index cd7b374..a9ca45b 100644 --- a/lib/Ratchet/Command/Action/CloseConnection.php +++ b/lib/Ratchet/Command/Action/CloseConnection.php @@ -12,8 +12,8 @@ class CloseConnection implements CommandInterface { */ protected $_socket; - public function __construct(SocketInterface $sockets) { - $this->_socket = $sockets; + public function __construct(SocketInterface $socket) { + $this->_socket = $socket; } function execute() { diff --git a/lib/Ratchet/Command/Composite.php b/lib/Ratchet/Command/Composite.php index 28b3a58..56f5ca1 100644 --- a/lib/Ratchet/Command/Composite.php +++ b/lib/Ratchet/Command/Composite.php @@ -3,28 +3,6 @@ namespace Ratchet\Command; use Ratchet\SocketInterface; class Composite extends \SplQueue { - /** - * @param string - * @param Ratchet\SocketInterface - * @return CommandInterface - */ - public function NOPEcreate($name, SocketInterface $socket) { - $class = __NAMESPACE__ . "\\{$name}\\"; - if (!class_exists($class)) { - throw new \UnexpectedValueException("Command {$name} not found"); - } - - $cmd = new $class($socket); - - if ($cmd instanceof CommandInterface) { - throw new RuntimeException("{$name} is not a valid command"); - } - - $this->enqueue($cmd); - - return $cmd; - } - public function enqueue(CommandInterface $command) { return parent::enqueue($command); } diff --git a/lib/Ratchet/Protocol/WebSocket.php b/lib/Ratchet/Protocol/WebSocket.php index 4af38ab..b3b5ae9 100644 --- a/lib/Ratchet/Protocol/WebSocket.php +++ b/lib/Ratchet/Protocol/WebSocket.php @@ -15,7 +15,6 @@ use Ratchet\Protocol\WebSocket\Util\HTTP; * This is a mediator between the Server and your application to handle real-time messaging through a web browser * @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 */ class WebSocket implements ProtocolInterface { @@ -36,7 +35,7 @@ class WebSocket implements ProtocolInterface { public function __construct(SocketObserver $application) { $this->_clients = new \SplObjectStorage; - $this->_app = $application; + $this->_app = $application; } /** @@ -58,6 +57,9 @@ class WebSocket implements ProtocolInterface { return $this->_app->onOpen($conn); } + /** + * @todo Cleanup spaghetti code + */ public function onRecv(SocketInterface $from, $msg) { $client = $this->_clients[$from]; if (true !== $client->isHandshakeComplete()) { @@ -102,7 +104,7 @@ class WebSocket implements ProtocolInterface { if ($cmds instanceof Composite) { foreach ($cmds as $cmd) { if ($cmd instanceof SendMessage) { - $sock = $cmd->_socket; + $sock = $cmd->_socket; // bad $clnt = $this->_clients[$sock]; $cmd->setMessage($clnt->getVersion()->frame($cmd->getMessage())); diff --git a/lib/Ratchet/Protocol/WebSocket/Command/Action/Ping.php b/lib/Ratchet/Protocol/WebSocket/Command/Action/Ping.php index b6e1dc2..419d7d4 100644 --- a/lib/Ratchet/Protocol/WebSocket/Command/Action/Ping.php +++ b/lib/Ratchet/Protocol/WebSocket/Command/Action/Ping.php @@ -1,10 +1,8 @@ <?php namespace Ratchet\Command; use Ratchet\SocketInterface; +use Ratchet\Command\CommandInterface; -/** - * @todo Move this command to the WebSocket protocol namespace - */ class Ping implements CommandInterface { public function __construct(SocketInterface $socket) { } diff --git a/lib/Ratchet/Protocol/WebSocket/Command/Action/Pong.php b/lib/Ratchet/Protocol/WebSocket/Command/Action/Pong.php index a96e6a4..78eded5 100644 --- a/lib/Ratchet/Protocol/WebSocket/Command/Action/Pong.php +++ b/lib/Ratchet/Protocol/WebSocket/Command/Action/Pong.php @@ -1,10 +1,8 @@ <?php namespace Ratchet\Command; use Ratchet\SocketInterface; +use Ratchet\Command\CommandInterface; -/** - * @todo Move this command to the WebSocket protocol namespace - */ class Pong implements CommandInterface { public function __construct(SocketInterface $socket) { } diff --git a/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php b/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php index 37f9621..f618df7 100644 --- a/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php +++ b/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php @@ -30,8 +30,9 @@ class HyBi10 implements VersionInterface { * Thanks to @lemmingzshadow for the code on decoding a HyBi-10 frame * @link https://github.com/lemmingzshadow/php-websocket * @param string - * @return string + * @return array * @throws UnexpectedValueException + * @todo return a common interface instead of array */ public function unframe($message) { $data = $message; diff --git a/lib/Ratchet/Server.php b/lib/Ratchet/Server.php index 38d6da1..3330e00 100644 --- a/lib/Ratchet/Server.php +++ b/lib/Ratchet/Server.php @@ -9,7 +9,6 @@ use Ratchet\Command\Composite; /** * Creates an open-ended socket to listen on a port for incomming connections. Events are delegated through this to attached applications * @todo Consider using _connections as master reference and passing iterator_to_array(_connections) to socket_select - * @todo Move SocketObserver methods to separate class, create, wrap class in __construct * @todo Currently passing Socket object down the decorated chain - should be sending reference to it instead; Receivers do not interact with the Socket directly, they do so through the Command pattern */ class Server implements SocketObserver, \IteratorAggregate { diff --git a/tests/Ratchet/Tests/Protocol/WebSocket/Version/HyBi10Test.php b/tests/Ratchet/Tests/Protocol/WebSocket/Version/HyBi10Test.php index 9c04d77..79e6b2e 100644 --- a/tests/Ratchet/Tests/Protocol/WebSocket/Version/HyBi10Test.php +++ b/tests/Ratchet/Tests/Protocol/WebSocket/Version/HyBi10Test.php @@ -47,4 +47,12 @@ class HyBi10Test extends \PHPUnit_Framework_TestCase { , array("The quick brown fox jumps over the lazy dog. All work and no play makes Chris a dull boy. I'm trying to get past 128 characters for a unit test here...", 'gf4Amahb14P8M7Kj2S6+4MN7tfHHLLmjzjSvo8IuuvPbe7j1zSn398A+9+/JIa6jzDSwrYh7lu/Ee6Ds2jD34sY/9+3He6fvySL37skwsvCIGL/xwSj34og/ou/Ee7Xs0XX3o+F8uqPcKa7qxjz398d7sObce6fi2y/3sppj9+DAOqXiyy+y8dt7sezae7aj3TW+94gvsvDce7/m2j75rYY=') ); } + + public function testUnframeMatchesPreFraming() { + $string = 'Hello World!'; + $framed = $this->_version->frame($string); + $unframed = $this->_version->unframe($framed); + + $this->assertEquals($string, $unframed['payload']); + } } \ No newline at end of file