From 3127efc98155bb346bc4a772383633dd7442d04c Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Tue, 1 Nov 2011 09:52:41 -0400 Subject: [PATCH] Cleanup Removed redundant Interfaces Removed a number of unused methods --- lib/Ratchet/Command/CommandInterface.php | 1 + lib/Ratchet/Command/Runtime.php | 33 +++++++++++ lib/Ratchet/Command/SendMessage.php | 1 + lib/Ratchet/Protocol/ProtocolInterface.php | 8 +-- lib/Ratchet/Protocol/WebSocket.php | 58 ++++++++++--------- .../Protocol/WebSocket/AppInterface.php | 4 +- .../Version/{Hybi10.php => HyBi10.php} | 2 +- lib/Ratchet/ReceiverInterface.php | 20 ------- lib/Ratchet/Server.php | 48 +-------------- lib/Ratchet/ServerInterface.php | 11 ---- tests/Ratchet/Tests/Mock/Application.php | 11 +--- tests/Ratchet/Tests/Mock/Protocol.php | 11 +--- .../Ratchet/Tests/Protocol/WebSocketTest.php | 2 +- tests/Ratchet/Tests/ServerTest.php | 4 ++ 14 files changed, 85 insertions(+), 129 deletions(-) create mode 100644 lib/Ratchet/Command/Runtime.php rename lib/Ratchet/Protocol/WebSocket/Version/{Hybi10.php => HyBi10.php} (99%) delete mode 100644 lib/Ratchet/ReceiverInterface.php delete mode 100644 lib/Ratchet/ServerInterface.php diff --git a/lib/Ratchet/Command/CommandInterface.php b/lib/Ratchet/Command/CommandInterface.php index f65e21a..57db1f7 100644 --- a/lib/Ratchet/Command/CommandInterface.php +++ b/lib/Ratchet/Command/CommandInterface.php @@ -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 { /** diff --git a/lib/Ratchet/Command/Runtime.php b/lib/Ratchet/Command/Runtime.php new file mode 100644 index 0000000..83f531a --- /dev/null +++ b/lib/Ratchet/Command/Runtime.php @@ -0,0 +1,33 @@ +_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); + } + } +} \ No newline at end of file diff --git a/lib/Ratchet/Command/SendMessage.php b/lib/Ratchet/Command/SendMessage.php index ca01b47..69b49e6 100644 --- a/lib/Ratchet/Command/SendMessage.php +++ b/lib/Ratchet/Command/SendMessage.php @@ -26,6 +26,7 @@ class SendMessage implements CommandInterface { */ public function setMessage($msg) { $this->_message = (string)$msg; + return $this; } /** diff --git a/lib/Ratchet/Protocol/ProtocolInterface.php b/lib/Ratchet/Protocol/ProtocolInterface.php index 552713e..6604a20 100644 --- a/lib/Ratchet/Protocol/ProtocolInterface.php +++ b/lib/Ratchet/Protocol/ProtocolInterface.php @@ -1,12 +1,12 @@ 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']; diff --git a/lib/Ratchet/Protocol/WebSocket/AppInterface.php b/lib/Ratchet/Protocol/WebSocket/AppInterface.php index 4e04fb0..f96a9a2 100644 --- a/lib/Ratchet/Protocol/WebSocket/AppInterface.php +++ b/lib/Ratchet/Protocol/WebSocket/AppInterface.php @@ -1,12 +1,12 @@ _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 diff --git a/lib/Ratchet/ServerInterface.php b/lib/Ratchet/ServerInterface.php deleted file mode 100644 index 448a21e..0000000 --- a/lib/Ratchet/ServerInterface.php +++ /dev/null @@ -1,11 +0,0 @@ -isInstanceOf('\\Ratchet\\ReceiverInterface'); + $constraint = $this->isInstanceOf('\\Ratchet\\SocketObserver'); $this->assertThat($this->_ws, $constraint); } diff --git a/tests/Ratchet/Tests/ServerTest.php b/tests/Ratchet/Tests/ServerTest.php index dfd0002..efc82cb 100644 --- a/tests/Ratchet/Tests/ServerTest.php +++ b/tests/Ratchet/Tests/ServerTest.php @@ -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();