Error handling, API documentation

This commit is contained in:
Chris Boden 2011-10-24 11:32:51 -04:00
parent 5ee0d1291d
commit eefbd2be41
2 changed files with 48 additions and 16 deletions

View File

@ -49,6 +49,9 @@ class Server implements ServerInterface {
$this->_log = $logger; $this->_log = $logger;
} }
/**
* @param Logging\LoggerInterface
*/
public function setLogger(LoggerInterface $logger) { public function setLogger(LoggerInterface $logger) {
$this->_log = $logger; $this->_log = $logger;
} }
@ -59,15 +62,27 @@ class Server implements ServerInterface {
public function setClientFactory($s) { public function setClientFactory($s) {
} }
/**
* @param ReceiverInterface
* @return Server
*/
public function attatchReceiver(ReceiverInterface $receiver) { public function attatchReceiver(ReceiverInterface $receiver) {
$receiver->setUp($this); $receiver->setUp($this);
$this->_receivers[spl_object_hash($receiver)] = $receiver; $this->_receivers[spl_object_hash($receiver)] = $receiver;
return $this;
} }
/**
* @return Socket
*/
public function getMaster() { public function getMaster() {
return $this->_master; return $this->_master;
} }
/**
* @return array of Sockets
*/
public function getClients() { public function getClients() {
return $this->_connections; return $this->_connections;
} }
@ -77,6 +92,7 @@ class Server implements ServerInterface {
* @param int * @param int
* @throws Exception * @throws Exception
* @todo Validate address. Use socket_get_option, if AF_INET must be IP, if AF_UNIX must be path * @todo Validate address. Use socket_get_option, if AF_INET must be IP, if AF_UNIX must be path
* @todo Should I make handling open/close/msg an application?
*/ */
public function run($address = '127.0.0.1', $port = 1025) { public function run($address = '127.0.0.1', $port = 1025) {
if (count($this->_receivers) == 0) { if (count($this->_receivers) == 0) {
@ -86,9 +102,7 @@ class Server implements ServerInterface {
set_time_limit(0); set_time_limit(0);
ob_implicit_flush(); ob_implicit_flush();
// socket_create_listen($port); instead of create, bind, listen if (false === ($this->_master->bind($address, (int)$port))) {
if (false === ($this->_master->bind($address, (int)$port))) { // perhaps I should do some checks here...
throw new Exception; throw new Exception;
} }
@ -97,10 +111,12 @@ class Server implements ServerInterface {
} }
do { do {
try {
$changed = $this->_resources; $changed = $this->_resources;
$num_changed = @socket_select($changed, $write = NULL, $except = NULL, NULL); $num_changed = $this->_master->select($changed, $write = null, $except = null, null);
foreach($changed as $resource) { foreach($changed as $resource) {
if ($this->_master->getResource() == $resource) { if ($this->_master->getResource() === $resource) {
$this->onConnect($this->_master); $this->onConnect($this->_master);
} else { } else {
$conn = $this->_connections[$resource]; $conn = $this->_connections[$resource];
@ -117,6 +133,9 @@ class Server implements ServerInterface {
} }
} }
} }
} catch (Exception $e) {
$this->_log->error($e->getMessage());
}
} while (true); } while (true);
// $this->_master->set_nonblock(); // $this->_master->set_nonblock();
@ -150,6 +169,9 @@ class Server implements ServerInterface {
$this->_log->note('Connection closed, ' . count($this->_connections) . ' connections remain (' . count($this->_resources) . ')'); $this->_log->note('Connection closed, ' . count($this->_connections) . ' connections remain (' . count($this->_resources) . ')');
} }
/**
* @todo Remove this method, make the receivers container implement the composite pattern
*/
protected function tmpRIterator() { protected function tmpRIterator() {
$args = func_get_args(); $args = func_get_args();
$fn = array_shift($args); $fn = array_shift($args);

View File

@ -43,6 +43,10 @@ class Socket {
public function __clone() { public function __clone() {
$this->_resource = @socket_accept($this->_resource); $this->_resource = @socket_accept($this->_resource);
if (false === $this->_resource) {
throw new Exception;
}
} }
/** /**
@ -99,7 +103,13 @@ class Socket {
$write = static::mungForSelect($write); $write = static::mungForSelect($write);
$except = static::mungForSelect($except); $except = static::mungForSelect($except);
return socket_select($read, $write, $except, $tv_sec, $tv_usec); $num = socket_select($read, $write, $except, $tv_sec, $tv_usec);
if (false === $num) {
throw new Exception;
}
return $num;
} }
/** /**