Changed attempt to use decorator pattern for everything to chain of command pattern
This commit is contained in:
parent
30ce6c0386
commit
9bc0cbce25
@ -1,15 +0,0 @@
|
||||
<?php
|
||||
namespace Ratchet;
|
||||
|
||||
interface ApplicationInterface {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
function getName();
|
||||
|
||||
function onConnect();
|
||||
|
||||
function onMessage();
|
||||
|
||||
function onClose();
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
<?php
|
||||
namespace Ratchet\Protocol;
|
||||
use Ratchet\ServerInterface;
|
||||
use Ratchet\ReceiverInterface;
|
||||
|
||||
interface ProtocolInterface extends ServerInterface {
|
||||
interface ProtocolInterface extends ReceiverInterface {
|
||||
/**
|
||||
* @return Array
|
||||
*/
|
||||
|
34
lib/Ratchet/Protocol/WebSocket.php
Normal file
34
lib/Ratchet/Protocol/WebSocket.php
Normal file
@ -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() {
|
||||
}
|
||||
}
|
@ -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) {
|
||||
}
|
||||
}
|
15
lib/Ratchet/ReceiverInterface.php
Normal file
15
lib/Ratchet/ReceiverInterface.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace Ratchet;
|
||||
|
||||
interface ReceiverInterface {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
function getName();
|
||||
|
||||
function handleConnect();
|
||||
|
||||
function handleMessage();
|
||||
|
||||
function handleClose();
|
||||
}
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
@ -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() {
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
29
tests/Ratchet/Tests/Protocol/WebSocketTest.php
Normal file
29
tests/Ratchet/Tests/Protocol/WebSocketTest.php
Normal file
@ -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());
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user