From dee33335786749fe28f5525d1e49ad2769cb7453 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Wed, 8 Dec 2010 16:47:04 +0000 Subject: [PATCH] [TASK] Implemented the ProtocolHandler things so it works again. [TASK] Added some comments. [TASK] Added TODOs concerning refactoring and more functions that are needed. --- Classes/Client/AbstractClient.php | 8 +++- Classes/Client/BotClient.php | 3 +- Classes/Client/ClientManager.php | 6 ++- Classes/Client/IrcClient.php | 6 +-- Classes/Connection/ConnectionPool.php | 3 +- Classes/Misc/Buffer.php | 2 +- Classes/Protocol/AbstractProtocolHandler.php | 17 +++++++++ Classes/Protocol/BotProtocolHandler.php | 39 ++++++++++++++++++-- Classes/Protocol/IrcProtocolHandler.php | 39 +++++++++++++++++++- Testcode/Client/ClientManagerTest.php | 8 ++-- 10 files changed, 114 insertions(+), 17 deletions(-) diff --git a/Classes/Client/AbstractClient.php b/Classes/Client/AbstractClient.php index f5bcfa9..8a80058 100644 --- a/Classes/Client/AbstractClient.php +++ b/Classes/Client/AbstractClient.php @@ -58,14 +58,17 @@ abstract class Client_AbstractClient { /** * This function will be the main entry point of any client. + * Any reactions will be passed to the ProtocolHandler. + * The ProtocolHandler will then pass the data back to the socket. * @param string $data - * @return string + * @return void */ abstract protected function processContentObject($contentObject); /** * This function will load the given config. * @param array $config + * @return void */ abstract public function loadConfig($config); @@ -81,6 +84,7 @@ abstract class Client_AbstractClient { /** * @param int $id + * @return void */ public function setID($id) { $this->ID = $id; @@ -88,6 +92,7 @@ abstract class Client_AbstractClient { /** * @param string $group + * @return void */ public function setGroup($group) { $this->group = $group; @@ -96,6 +101,7 @@ abstract class Client_AbstractClient { /** * Injects ClientManager * @param Client_ClientManager $clientManager + * @return void * @throws Exception_WrongDatatypeException */ public function injectClientManager($clientManager) { diff --git a/Classes/Client/BotClient.php b/Classes/Client/BotClient.php index dfd1a58..6b7bae0 100644 --- a/Classes/Client/BotClient.php +++ b/Classes/Client/BotClient.php @@ -34,7 +34,8 @@ class Client_BotClient extends Client_AbstractClient { $return = print_r($data, TRUE); } - return $return; + //TODO: implement this correctly + $this->protocolHandler->sendRaw($return); } /** diff --git a/Classes/Client/ClientManager.php b/Classes/Client/ClientManager.php index cec2f22..080ab9f 100644 --- a/Classes/Client/ClientManager.php +++ b/Classes/Client/ClientManager.php @@ -61,16 +61,18 @@ class Client_ClientManager { } /** + * TODO: Implement a function that is able to count active connections instead of sockets! * @see Connection_ConnectionPool * @return int */ - public function countConnections() { - return $this->connectionPool->countConnections(); + public function countSockets() { + return $this->connectionPool->countSockets(); } /** * Main worker function. * Processes incoming data, calls clients and much more. + * TODO: refactor this? (split it into handleFoobar() functions) * @return void */ public function work() { diff --git a/Classes/Client/IrcClient.php b/Classes/Client/IrcClient.php index b52eb07..5b711e9 100644 --- a/Classes/Client/IrcClient.php +++ b/Classes/Client/IrcClient.php @@ -39,7 +39,7 @@ class Client_IrcClient extends Client_AbstractClient { * Processes the resulting ContentObject from a ProtocolHandler. * Does all the hard work. * @param string $data - * @return string + * @return void */ public function processContentObject($contentObject) { $data = $contentObject->rawData; @@ -68,8 +68,8 @@ class Client_IrcClient extends Client_AbstractClient { $return .= "QUIT :lol\r\n"; } - //if($return !== "") echo "[SEND] ".$return; - return $return; + //workaround. will be removed soon + $this->protocolHandler->sendRaw($return); } /** diff --git a/Classes/Connection/ConnectionPool.php b/Classes/Connection/ConnectionPool.php index 6b5ebba..a9eb714 100644 --- a/Classes/Connection/ConnectionPool.php +++ b/Classes/Connection/ConnectionPool.php @@ -138,6 +138,7 @@ class Connection_ConnectionPool { throw new Exception_GeneralException("Unknown select type: '" . $selectedType . "'", 1289737080); break; } + //Put the ConnectionHandler into the array. We'll return this for further operations. $tempArray[$selectedType][] = $connectionHandler; } } @@ -176,7 +177,7 @@ class Connection_ConnectionPool { * @see Socket_SocketPool * @return int */ - public function countConnections() { + public function countSockets() { return $this->socketPool->countSockets(); } } diff --git a/Classes/Misc/Buffer.php b/Classes/Misc/Buffer.php index 0e04a6a..96dd75a 100644 --- a/Classes/Misc/Buffer.php +++ b/Classes/Misc/Buffer.php @@ -81,7 +81,7 @@ class Misc_Buffer { */ public function getNextChars($length) { //TODO: substr und so - return ""; + throw new Exception_GeneralException("This feature is not implemented yet!", 1291824224); } /** diff --git a/Classes/Protocol/AbstractProtocolHandler.php b/Classes/Protocol/AbstractProtocolHandler.php index 2f7d0d3..33ce0e7 100644 --- a/Classes/Protocol/AbstractProtocolHandler.php +++ b/Classes/Protocol/AbstractProtocolHandler.php @@ -23,6 +23,23 @@ abstract class Protocol_AbstractProtocolHandler { */ protected $buffer_outgoing; + /** + * This function will be called by the constructor. + * It will create the necessary instances of Misc_Buffer and + * put them in $buffer_incoming and $buffer_outgoing. + * @return void + */ + abstract public function createBuffers(); + + /** + * General constructor. + * Calls createBuffers() + * @return void + */ + function __construct() { + $this->createBuffers(); + } + /** * Returns whether work() can be called or not. * Depends on the protocol and its implementation. diff --git a/Classes/Protocol/BotProtocolHandler.php b/Classes/Protocol/BotProtocolHandler.php index ea2f462..0bb827c 100644 --- a/Classes/Protocol/BotProtocolHandler.php +++ b/Classes/Protocol/BotProtocolHandler.php @@ -7,11 +7,44 @@ class Protocol_BotProtocolHandler extends Protocol_AbstractProtocolHandler { /** - * @param string $data - * @return Protocol_RawProtocolContentObject + * Is called by the constructor. + * Shall create the two buffers and set them up. + * @return void */ - public function parse($data) { + public function createBuffers() { + $linebreak = "\r\n"; + $this->buffer_incoming = new Misc_Buffer($linebreak); + $this->buffer_outgoing = new Misc_Buffer($linebreak); + } + + /** + * Main worker function. It will be called in a loop. + * The returned ContentObject will be passed to the client. + * @return Protocol_BotProtocolContentObject + */ + public function work() { + $data = $this->buffer_incoming->getNextLine(); return new Protocol_BotProtocolContentObject($data); } + + /** + * Returns whether there is work to be done. + * Important in order to assure that a ContentObject is created and passed to the Client. + * @return boolean + */ + public function canWork() { + return $this->buffer_incoming->hasLines(); + } + + /** + * Will be replaced soon. Passes raw data into the outgoing buffer. + * @deprecated + * @param string $data + * @return void + */ + public function sendRaw($data) { + $this->buffer_incoming->addData($data); + } + } ?> \ No newline at end of file diff --git a/Classes/Protocol/IrcProtocolHandler.php b/Classes/Protocol/IrcProtocolHandler.php index df21ffd..9fc1b73 100644 --- a/Classes/Protocol/IrcProtocolHandler.php +++ b/Classes/Protocol/IrcProtocolHandler.php @@ -7,11 +7,46 @@ class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler { /** - * @param string $data + * Is called by the constructor. + * Shall create the two buffers and set them up. + * @return void + */ + public function createBuffers() { + $linebreak = "\r\n"; + $this->buffer_incoming = new Misc_Buffer($linebreak); + $this->buffer_outgoing = new Misc_Buffer($linebreak); + } + + /** + * Main worker function. It will be called in a loop. + * The returned ContentObject will be passed to the client. * @return Protocol_IrcProtocolContentObject */ - public function parse($data) { + public function work() { + $data = $this->buffer_incoming->getNextLine(); return new Protocol_IrcProtocolContentObject($data); } + + /** + * Returns whether there is work to be done. + * Important in order to assure that a ContentObject is created and passed to the Client. + * @return boolean + */ + public function canWork() { + return $this->buffer_incoming->hasLines(); + } + + /** + * Will put raw data into the outgoing buffer. + * This function will be removed soon. + * The ProtocolHandler shall take care of this directly. + * @deprecated + * @param string $data + * @return void + */ + public function sendRaw($data) { + $this->buffer_outgoing->addData($data); + } + } ?> \ No newline at end of file diff --git a/Testcode/Client/ClientManagerTest.php b/Testcode/Client/ClientManagerTest.php index 5791e68..89b7c09 100644 --- a/Testcode/Client/ClientManagerTest.php +++ b/Testcode/Client/ClientManagerTest.php @@ -3,7 +3,7 @@ $clientManager = new Client_ClientManager(); $clientManager->registerProtocol("irc", "Irc"); $clientManager->registerProtocol("jpt", "Bot"); -$freenode = $clientManager->createTcpConnection("freenode", "irc"); +/*$freenode = $clientManager->createTcpConnection("freenode", "irc"); $clientManager->attachConfig(array( "nick" => "Testinstanz", "userident" => "uzuguck", @@ -11,6 +11,7 @@ $clientManager->attachConfig(array( ), $freenode); $freenode->connect("irc.freenode.net", 6667); $freenode->setReconnect(TRUE); +*/ $freenode = $clientManager->createTcpConnection("freenode", "irc"); $clientManager->attachConfig(array( @@ -20,7 +21,6 @@ $clientManager->attachConfig(array( ), $freenode); $freenode->connect("irc.freenode.net", 6667); - /*$config_eloxoph = array( "nick" => "Frischmilch", "userident" => "olefolol", @@ -31,11 +31,13 @@ $eloxoph = $clientManager->createTcpConnection("eloxoph", "irc"); $clientManager->attachConfig($config_eloxoph, $eloxoph); $eloxoph->connect("irc.eloxoph.com", 6667);*/ + + $srv = $clientManager->createTcpConnection("srv", "jpt"); $srv->bind("localhost", 7777); $srv->listen(); -while($clientManager->countConnections() > 0) { +while($clientManager->countSockets() > 0) { $clientManager->work(); } ?> \ No newline at end of file