Server building

This commit is contained in:
Chris Boden 2011-09-05 18:39:36 -04:00
parent 45fb69d68b
commit 30ce6c0386
4 changed files with 40 additions and 6 deletions

View File

@ -27,6 +27,6 @@ class Server implements ProtocolInterface {
public function attatchApplication(ApplicationInterface $app) {} public function attatchApplication(ApplicationInterface $app) {}
public function run() { public function run($address = '127.0.0.1', $port = 1025) {
} }
} }

View File

@ -5,16 +5,29 @@ use Ratchet\Protocol\ProtocolInterface;
class Server implements ServerInterface { class Server implements ServerInterface {
protected $_master = null; protected $_master = null;
protected $_app = null; protected $_app = null;
protected $_debug = false;
public function __construct(Socket $socket) { protected $_connections = Array();
/**
* @param Ratchet\Socket
* @param boolean True, enables debug mode and the server doesn't infiniate loop
*/
public function __construct(Socket $socket, $debug = false) {
$this->_master = $socket; $this->_master = $socket;
$this->_debug = (boolean)$debug;
} }
public function attatchApplication(ApplicationInterface $app) { public function attatchApplication(ApplicationInterface $app) {
$this->_app = $app; $this->_app = $app;
} }
public function run() { /*
* @param mixed
* @param int
* @throws Ratchet\Exception
*/
public function run($address = '127.0.0.1', $port = 1025) {
if (!($this->_app instanceof ApplicationInterface)) { if (!($this->_app instanceof ApplicationInterface)) {
throw new \RuntimeException("No application has been bound to the server"); throw new \RuntimeException("No application has been bound to the server");
} }
@ -22,9 +35,21 @@ class Server implements ServerInterface {
set_time_limit(0); set_time_limit(0);
ob_implicit_flush(); ob_implicit_flush();
do { if (false === ($this->_master->bind($address, (int)$port))) { // perhaps I should do some checks here...
throw new Exception();
}
if (false === ($this->_master->listen())) {
throw new Exception();
}
do {
$changed = $this->_connections;
$num_changed = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
// foreach($changed as $)
} while (!$this->_debug);
} while (true);
// $this->_master->set_nonblock(); // $this->_master->set_nonblock();
// declare(ticks = 1); // declare(ticks = 1);
} }

View File

@ -4,5 +4,5 @@ namespace Ratchet;
interface ServerInterface { interface ServerInterface {
function attatchApplication(ApplicationInterface $app); function attatchApplication(ApplicationInterface $app);
function run(); function run($address = '127.0.0.1', $port = 1025);
} }

View File

@ -30,4 +30,13 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
$this->_server->attatchApplication($app); $this->_server->attatchApplication($app);
$this->assertAttributeEquals($app, '_app', $this->_server); $this->assertAttributeEquals($app, '_app', $this->_server);
} }
public function testBindToInvalidAddress() {
$app = new TestApp();
$this->_server->attatchApplication($app);
$this->setExpectedException('\\Ratchet\\Exception');
$this->_server->run('la la la', 80);
}
} }