From c382cfe756f2c5ffac26f9f2899fab1b56c6ed92 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 30 Nov 2010 23:25:15 +0000 Subject: [PATCH] [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. --- Classes/Client/AbstractClient.php | 12 ++++++++-- Classes/Client/IrcClient.php | 1 + Classes/Connection/ConnectionHandler.php | 24 ++++++++++---------- Classes/Exception/WrongDatatypeException.php | 9 ++++++++ Classes/Misc/Buffer.php | 19 ++++++++++++++++ 5 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 Classes/Exception/WrongDatatypeException.php diff --git a/Classes/Client/AbstractClient.php b/Classes/Client/AbstractClient.php index 478dc62..8bf3d86 100644 --- a/Classes/Client/AbstractClient.php +++ b/Classes/Client/AbstractClient.php @@ -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; } } diff --git a/Classes/Client/IrcClient.php b/Classes/Client/IrcClient.php index 8e5728c..b52eb07 100644 --- a/Classes/Client/IrcClient.php +++ b/Classes/Client/IrcClient.php @@ -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 diff --git a/Classes/Connection/ConnectionHandler.php b/Classes/Connection/ConnectionHandler.php index 9a372d5..ef4ea5e 100644 --- a/Classes/Connection/ConnectionHandler.php +++ b/Classes/Connection/ConnectionHandler.php @@ -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(); } /** diff --git a/Classes/Exception/WrongDatatypeException.php b/Classes/Exception/WrongDatatypeException.php new file mode 100644 index 0000000..bbacf7a --- /dev/null +++ b/Classes/Exception/WrongDatatypeException.php @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/Classes/Misc/Buffer.php b/Classes/Misc/Buffer.php index 2fb23e7..0e04a6a 100644 --- a/Classes/Misc/Buffer.php +++ b/Classes/Misc/Buffer.php @@ -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; + } + } ?> \ No newline at end of file