[TASK] Removed the linebreak from the buffers in the ConnectionHandler.

[TASK] Added a WrongDatatypeException
[TASK] Started to implement the ProtocolHandler in a different way.
[TASK] Started to put buffers into the ProtocolHandler.
[!!!] Committing broken code.
This commit is contained in:
Jan Philipp Timme 2010-11-30 23:25:15 +00:00
parent 5cb0714834
commit c382cfe756
5 changed files with 51 additions and 14 deletions

View File

@ -32,8 +32,12 @@ abstract class Client_AbstractClient {
* Injects ProtocolHandler
* @param mixed $protocolHandler instance of Protocol_AbstractProtocolHandler
* @return void
* @throws Exception_WrongDatatypeException
*/
public function injectProtocolHandler($protocolHandler) {
if(!$protocolHandler instanceof Protocol_AbstractProtocolHandler) {
throw new Exception_WrongDatatypeException("Cannot inject ProtocolHandler! Expected an instance of Protocol_AbstractProtocolHandler, '" . get_type($protocolHandler) . "' given.", 1291156562);
}
$this->protocolHandler = $protocolHandler;
}
@ -44,8 +48,8 @@ abstract class Client_AbstractClient {
* @return string
*/
public function processRawData($rawData) {
$contentObject = $this->protocolHandler->parse($rawData);
return $this->processContentObject($contentObject);
$contentObject = $this->protocolHandler->pushRawData($rawData);
return $this->processContentObject($this->protocolHandler->);
}
/**
@ -88,8 +92,12 @@ abstract class Client_AbstractClient {
/**
* Injects ClientManager
* @param Client_ClientManager $clientManager
* @throws Exception_WrongDatatypeException
*/
public function injectClientManager($clientManager) {
if(!$clientManager instanceof Client_ClientManager) {
throw new Exception_WrongDatatypeException("Cannot inject ClientManager! Expected an instance of Client_ClientManager, '" . get_type($clientManager) . "' given.", 1291156565);
}
$this->clientManager = $clientManager;
}
}

View File

@ -36,6 +36,7 @@ class Client_IrcClient extends Client_AbstractClient {
}
/**
* Processes the resulting ContentObject from a ProtocolHandler.
* Does all the hard work.
* @param string $data
* @return string

View File

@ -13,6 +13,7 @@ class Connection_ConnectionHandler {
/**
* Buffer that contains incoming data.
* Contents were received from the SocketHandler.
* This buffer does not use a linebreak, it's a temporary store for data.
* @var Misc_Buffer
*/
protected $buffer_incoming;
@ -20,6 +21,7 @@ class Connection_ConnectionHandler {
/**
* Buffer that contains outgoing data.
* Contents will be sent to the SocketHandler.
* This buffer does not use a linebreak, it's a temporary store for data.
* @var Misc_Buffer
*/
protected $buffer_outgoing;
@ -85,9 +87,9 @@ class Connection_ConnectionHandler {
* @param $linebreak
* @return void
*/
function __construct($socket, $id, $group = "", $protocol = "", $linebreak = "\r\n") {
$this->buffer_incoming = new Misc_Buffer($linebreak);
$this->buffer_outgoing = new Misc_Buffer($linebreak);
function __construct($socket, $id, $group = "", $protocol = "") {
$this->buffer_incoming = new Misc_Buffer();
$this->buffer_outgoing = new Misc_Buffer();
$this->socketHandler = new Socket_SocketHandler($socket);
$this->id = $id;
$this->group = $group;
@ -202,12 +204,10 @@ class Connection_ConnectionHandler {
* @return boolean
*/
public function writeFromBuffer() {
while($this->buffer_outgoing->hasLines()) {
$result = $this->socketHandler->write($this->buffer_outgoing->getNextLine());
if($result === FALSE) {
$this->shutdown();
return FALSE;
}
$result = $this->socketHandler->write($this->buffer_outgoing->getAllBufferContents());
if($result === FALSE) {
$this->shutdown();
return FALSE;
}
return TRUE;
}
@ -226,7 +226,7 @@ class Connection_ConnectionHandler {
* @return boolean
*/
public function canRead() {
return $this->buffer_incoming->hasLines();
return $this->buffer_incoming->hasData();
}
/**
@ -234,7 +234,7 @@ class Connection_ConnectionHandler {
* @return boolean
*/
public function canWrite() {
return $this->buffer_outgoing->hasLines();
return $this->buffer_outgoing->hasData();
}
/**
@ -243,7 +243,7 @@ class Connection_ConnectionHandler {
* @return string
*/
public function read() {
return $this->buffer_incoming->getNextLine();
return $this->buffer_incoming->getAllBufferContents();
}
/**

View File

@ -0,0 +1,9 @@
<?php
/**
* WrongDatatypeException
* @author jpt
* @package Exception
* @exception
*/
class Exception_WrongDatatypeException extends Exception_GeneralException {}
?>

View File

@ -94,5 +94,24 @@ class Misc_Buffer {
return (trim(strstr($this->buffer, $this->linebreak, TRUE)) !== "") ? TRUE : FALSE;
}
/**
* Returns TRUE when there is data in the buffer.
* @return boolean
*/
public function hasData() {
return (strlen($this->buffer) > 0) ? TRUE : FALSE;
}
/**
* Returns the full buffer contents.
* Also truncates the buffer.
* @return string
*/
public function getAllBufferContents() {
$return = $this->buffer;
$this->buffer = "";
return $return;
}
}
?>