From 9bc0cbce2504b3c0d2c28bc24f458ef08b652b73 Mon Sep 17 00:00:00 2001
From: Chris Boden <cboden@gmail.com>
Date: Mon, 5 Sep 2011 19:39:37 -0400
Subject: [PATCH] Changed attempt to use decorator pattern for everything to
 chain of command pattern

---
 lib/Ratchet/ApplicationInterface.php          | 15 --------
 lib/Ratchet/Protocol/ProtocolInterface.php    |  4 +--
 lib/Ratchet/Protocol/WebSocket.php            | 34 +++++++++++++++++++
 lib/Ratchet/Protocol/WebSocket/Server.php     | 32 -----------------
 lib/Ratchet/ReceiverInterface.php             | 15 ++++++++
 lib/Ratchet/Server.php                        | 10 +++---
 lib/Ratchet/ServerInterface.php               |  2 +-
 tests/Ratchet/Tests/Mock/Application.php      | 10 +++---
 .../Tests/Protocol/WebSocket/ServerTest.php   | 30 ----------------
 .../Ratchet/Tests/Protocol/WebSocketTest.php  | 29 ++++++++++++++++
 tests/Ratchet/Tests/ServerTest.php            |  9 ++---
 11 files changed, 96 insertions(+), 94 deletions(-)
 delete mode 100644 lib/Ratchet/ApplicationInterface.php
 create mode 100644 lib/Ratchet/Protocol/WebSocket.php
 delete mode 100644 lib/Ratchet/Protocol/WebSocket/Server.php
 create mode 100644 lib/Ratchet/ReceiverInterface.php
 delete mode 100644 tests/Ratchet/Tests/Protocol/WebSocket/ServerTest.php
 create mode 100644 tests/Ratchet/Tests/Protocol/WebSocketTest.php

diff --git a/lib/Ratchet/ApplicationInterface.php b/lib/Ratchet/ApplicationInterface.php
deleted file mode 100644
index 1241c5f..0000000
--- a/lib/Ratchet/ApplicationInterface.php
+++ /dev/null
@@ -1,15 +0,0 @@
-<?php
-namespace Ratchet;
-
-interface ApplicationInterface {
-    /**
-     * @return string
-     */
-    function getName();
-
-    function onConnect();
-
-    function onMessage();
-
-    function onClose();
-}
\ No newline at end of file
diff --git a/lib/Ratchet/Protocol/ProtocolInterface.php b/lib/Ratchet/Protocol/ProtocolInterface.php
index cdad88d..f70e635 100644
--- a/lib/Ratchet/Protocol/ProtocolInterface.php
+++ b/lib/Ratchet/Protocol/ProtocolInterface.php
@@ -1,8 +1,8 @@
 <?php
 namespace Ratchet\Protocol;
-use Ratchet\ServerInterface;
+use Ratchet\ReceiverInterface;
 
-interface ProtocolInterface extends ServerInterface {
+interface ProtocolInterface extends ReceiverInterface {
     /**
      * @return Array
      */
diff --git a/lib/Ratchet/Protocol/WebSocket.php b/lib/Ratchet/Protocol/WebSocket.php
new file mode 100644
index 0000000..2d41df4
--- /dev/null
+++ b/lib/Ratchet/Protocol/WebSocket.php
@@ -0,0 +1,34 @@
+<?php
+namespace Ratchet\Protocol;
+
+class WebSocket implements ProtocolInterface {
+    /**
+     * @return Array
+     */
+    public static function getDefaultConfig() {
+        return Array(
+            'domain'   => AF_INET
+          , 'type'     => SOCK_STREAM
+          , 'protocol' => SOL_TCP
+          , 'options'  => Array(
+                SOL_SOCKET => Array(SO_REUSEADDR => 1)
+            )
+        );
+    }
+
+    /**
+     * @return string
+     */
+    function getName() {
+        return __CLASS__;
+    }
+
+    function handleConnect() {
+    }
+
+    function handleMessage() {
+    }
+
+    function handleClose() {
+    }
+}
\ No newline at end of file
diff --git a/lib/Ratchet/Protocol/WebSocket/Server.php b/lib/Ratchet/Protocol/WebSocket/Server.php
deleted file mode 100644
index b896111..0000000
--- a/lib/Ratchet/Protocol/WebSocket/Server.php
+++ /dev/null
@@ -1,32 +0,0 @@
-<?php
-namespace Ratchet\Protocol\WebSocket;
-use Ratchet\ServerInterface;
-use Ratchet\Protocol\ProtocolInterface;
-use Ratchet\ApplicationInterface;
-
-class Server implements ProtocolInterface {
-    protected $_server = null;
-
-    public function __construct(ServerInterface $server) {
-        $this->_server = $server;
-    }
-
-    /**
-     * @return Array
-     */
-    public static function getDefaultConfig() {
-        return Array(
-            'domain'   => AF_INET
-          , 'type'     => SOCK_STREAM
-          , 'protocol' => SOL_TCP
-          , 'options'  => Array(
-                SOL_SOCKET => Array(SO_REUSEADDR => 1)
-            )
-        );
-    }
-
-    public function attatchApplication(ApplicationInterface $app) {}
-
-    public function run($address = '127.0.0.1', $port = 1025) {
-    }
-}
\ No newline at end of file
diff --git a/lib/Ratchet/ReceiverInterface.php b/lib/Ratchet/ReceiverInterface.php
new file mode 100644
index 0000000..feadd46
--- /dev/null
+++ b/lib/Ratchet/ReceiverInterface.php
@@ -0,0 +1,15 @@
+<?php
+namespace Ratchet;
+
+interface ReceiverInterface {
+    /**
+     * @return string
+     */
+    function getName();
+
+    function handleConnect();
+
+    function handleMessage();
+
+    function handleClose();
+}
\ No newline at end of file
diff --git a/lib/Ratchet/Server.php b/lib/Ratchet/Server.php
index 68db6db..570f4da 100644
--- a/lib/Ratchet/Server.php
+++ b/lib/Ratchet/Server.php
@@ -4,9 +4,9 @@ use Ratchet\Protocol\ProtocolInterface;
 
 class Server implements ServerInterface {
     protected $_master = null;
-    protected $_app    = null;
     protected $_debug  = false;
 
+    protected $_receivers   = Array();
     protected $_connections = Array();
 
     /**
@@ -18,8 +18,8 @@ class Server implements ServerInterface {
         $this->_debug  = (boolean)$debug;
     }
 
-    public function attatchApplication(ApplicationInterface $app) {
-        $this->_app = $app;
+    public function attatchReceiver(ReceiverInterface $receiver) {
+        $this->_receivers[spl_object_hash($receiver)] = $receiver;
     }
 
     /*
@@ -28,8 +28,8 @@ class Server implements ServerInterface {
      * @throws Ratchet\Exception
      */
     public function run($address = '127.0.0.1', $port = 1025) {
-        if (!($this->_app instanceof ApplicationInterface)) {
-            throw new \RuntimeException("No application has been bound to the server");
+        if (count($this->_receivers) == 0) {
+            throw new \RuntimeException("No receiver has been attatched to the server");
         }
 
         set_time_limit(0);
diff --git a/lib/Ratchet/ServerInterface.php b/lib/Ratchet/ServerInterface.php
index 0566126..b39d3ee 100644
--- a/lib/Ratchet/ServerInterface.php
+++ b/lib/Ratchet/ServerInterface.php
@@ -2,7 +2,7 @@
 namespace Ratchet;
 
 interface ServerInterface {
-    function attatchApplication(ApplicationInterface $app);
+    function attatchReceiver(ReceiverInterface $receiver);
 
     function run($address = '127.0.0.1', $port = 1025);
 }
\ No newline at end of file
diff --git a/tests/Ratchet/Tests/Mock/Application.php b/tests/Ratchet/Tests/Mock/Application.php
index 43cb3c6..08e8c79 100644
--- a/tests/Ratchet/Tests/Mock/Application.php
+++ b/tests/Ratchet/Tests/Mock/Application.php
@@ -1,18 +1,18 @@
 <?php
 namespace Ratchet\Tests\Mock;
-use Ratchet\ApplicationInterface;
+use Ratchet\ReceiverInterface;
 
-class Application implements ApplicationInterface {
+class Application implements ReceiverInterface {
     public function getName() {
         return 'mock_application';
     }
 
-    public function onConnect() {
+    public function handleConnect() {
     }
 
-    public function onMessage() {
+    public function handleMessage() {
     }
 
-    public function onClose() {
+    public function handleClose() {
     }
 }
\ No newline at end of file
diff --git a/tests/Ratchet/Tests/Protocol/WebSocket/ServerTest.php b/tests/Ratchet/Tests/Protocol/WebSocket/ServerTest.php
deleted file mode 100644
index 9acdad7..0000000
--- a/tests/Ratchet/Tests/Protocol/WebSocket/ServerTest.php
+++ /dev/null
@@ -1,30 +0,0 @@
-<?php
-namespace Ratchet\Tests\Protocol\WebSocket;
-use Ratchet\Server;
-use Ratchet\Protocol\WebSocket\Server as WebServer;
-use Ratchet\Tests\Mock\Socket;
-
-/**
- * @covers Ratchet\Protocol\WebSocket\Server
- */
-class ServerTest extends \PHPUnit_Framework_TestCase {
-    protected $_server;
-
-    public function setUp() {
-        $this->_server = new WebServer(new Server(new Socket()));
-    }
-
-    public function testServerImplementsServerInterface() {
-        $constraint = $this->isInstanceOf('\\Ratchet\\ServerInterface');
-        $this->assertThat($this->_server, $constraint);
-    }
-
-    public function testServerImplementsProtocolInterface() {
-        $constraint = $this->isInstanceOf('\\Ratchet\\Protocol\ProtocolInterface');
-        $this->assertThat($this->_server, $constraint);
-    }
-
-    public function testGetConfigReturnsArray() {
-        $this->assertInternalType('array', $this->_server->getDefaultConfig());
-    }
-}
\ No newline at end of file
diff --git a/tests/Ratchet/Tests/Protocol/WebSocketTest.php b/tests/Ratchet/Tests/Protocol/WebSocketTest.php
new file mode 100644
index 0000000..3aabc49
--- /dev/null
+++ b/tests/Ratchet/Tests/Protocol/WebSocketTest.php
@@ -0,0 +1,29 @@
+<?php
+namespace Ratchet\Tests\Protocol;
+use Ratchet\Protocol\WebSocket;
+use Ratchet\Tests\Mock\Socket;
+
+/**
+ * @covers Ratchet\Protocol\WebSocket
+ */
+class ServerTest extends \PHPUnit_Framework_TestCase {
+    protected $_ws;
+
+    public function setUp() {
+        $this->_ws = new WebSocket();
+    }
+
+    public function testServerImplementsServerInterface() {
+        $constraint = $this->isInstanceOf('\\Ratchet\\ReceiverInterface');
+        $this->assertThat($this->_ws, $constraint);
+    }
+
+    public function testServerImplementsProtocolInterface() {
+        $constraint = $this->isInstanceOf('\\Ratchet\\Protocol\ProtocolInterface');
+        $this->assertThat($this->_ws, $constraint);
+    }
+
+    public function testGetConfigReturnsArray() {
+        $this->assertInternalType('array', $this->_ws->getDefaultConfig());
+    }
+}
\ No newline at end of file
diff --git a/tests/Ratchet/Tests/ServerTest.php b/tests/Ratchet/Tests/ServerTest.php
index f271e92..b7d193e 100644
--- a/tests/Ratchet/Tests/ServerTest.php
+++ b/tests/Ratchet/Tests/ServerTest.php
@@ -24,17 +24,18 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
         $this->_server->run();
     }
 
-    public function testAttatchedApplicationIsSet() {
+    public function testAttatchedReceiverIsSet() {
         $app = new TestApp();
 
-        $this->_server->attatchApplication($app);
-        $this->assertAttributeEquals($app, '_app', $this->_server);
+        $this->_server->attatchReceiver($app);
+// todo, use proper assertions...can't look them up atm, no internet
+        $this->assertAttributeEquals(Array(spl_object_hash($app) => $app), '_receivers', $this->_server);
     }
 
     public function testBindToInvalidAddress() {
         $app = new TestApp();
 
-        $this->_server->attatchApplication($app);
+        $this->_server->attatchReceiver($app);
         $this->setExpectedException('\\Ratchet\\Exception');
 
         $this->_server->run('la la la', 80);