From d0de3823a92a1f36686ad231577ae92f169a3faf Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 4 Dec 2011 14:31:18 +0100 Subject: [PATCH] [TASK] Added an outputBuffer to the AbstractClientDispatcher. [TASK] Added support for the new outputBuffer - it can be used in addition to the default behaviour "return a string on processRawData()". --- ...{BotClient.php => BotClientDispatcher.php} | 36 +++++++++---------- ...Dispatcher.php => IrcClientDispatcher.php} | 10 +++--- .../Client/AbstractClientDispatcher.php | 36 +++++++++++++++++++ .../Classes/Client/ClientManager.php | 7 +++- Testcode/Client/ClientManagerTest.php | 5 +-- 5 files changed, 69 insertions(+), 25 deletions(-) rename IrcClient/Classes/{BotClient.php => BotClientDispatcher.php} (51%) rename IrcClient/Classes/{ClientDispatcher.php => IrcClientDispatcher.php} (87%) diff --git a/IrcClient/Classes/BotClient.php b/IrcClient/Classes/BotClientDispatcher.php similarity index 51% rename from IrcClient/Classes/BotClient.php rename to IrcClient/Classes/BotClientDispatcher.php index b1b37aa..396be31 100644 --- a/IrcClient/Classes/BotClient.php +++ b/IrcClient/Classes/BotClientDispatcher.php @@ -1,31 +1,31 @@ getRawData(); if(trim($rawData) === "list") { - $return = print_r(array("IDs" => $this->clientManager->getIDs(), "Groups" => $this->clientManager->getGroups()), TRUE); + $return = print_r(array( + "IDs" => $this->clientManager->getIDs(), + "Groups" => $this->clientManager->getGroups() + ), TRUE); } if(strpos($rawData, "-") !== FALSE ) { @@ -42,7 +42,7 @@ class Client_BotClient extends Client_AbstractClient { //TODO: implement this correctly $return = str_replace("\n", "\r\n", $return); - $this->protocolHandler->sendRaw($return); + return $return; } /** diff --git a/IrcClient/Classes/ClientDispatcher.php b/IrcClient/Classes/IrcClientDispatcher.php similarity index 87% rename from IrcClient/Classes/ClientDispatcher.php rename to IrcClient/Classes/IrcClientDispatcher.php index 75cff26..a44902a 100644 --- a/IrcClient/Classes/ClientDispatcher.php +++ b/IrcClient/Classes/IrcClientDispatcher.php @@ -2,12 +2,13 @@ namespace JPT\IrcClient; /** - * The ClientDispatcher for this IrcBot. + * The IrcClientDispatcher for this IrcBot. * * @author jpt * @package Client + * @depends \JPT\SocketFramework\Client\AbstractClientDispatcher */ -class ClientDispatcher extends \JPT\SocketFramework\Client\AbstractClientDispatcher { +class IrcClientDispatcher extends \JPT\SocketFramework\Client\AbstractClientDispatcher { /** * @var boolean @@ -44,6 +45,7 @@ class ClientDispatcher extends \JPT\SocketFramework\Client\AbstractClientDispatc * @return void */ public function __construct() { + $this->outputBuffer = new \JPT\SocketFramework\Misc\Buffer("\r\n"); $this->nick = "Serena"; $this->channels = array(); $this->resetConnectionStatus(); @@ -70,7 +72,7 @@ class ClientDispatcher extends \JPT\SocketFramework\Client\AbstractClientDispatc $data = "USER poweruser as as :JPTs Bot\r\nNICK " . $this->nick . "\r\n"; $this->authed = TRUE; } - //$this->protocolHandler->sendRaw($data); + $this->send($data); } /** @@ -106,7 +108,7 @@ class ClientDispatcher extends \JPT\SocketFramework\Client\AbstractClientDispatc $return .= "QUIT :lol\r\n"; } - return $return; + $this->send($return); } /** diff --git a/SocketFramework/Classes/Client/AbstractClientDispatcher.php b/SocketFramework/Classes/Client/AbstractClientDispatcher.php index b9f8927..2ce7063 100644 --- a/SocketFramework/Classes/Client/AbstractClientDispatcher.php +++ b/SocketFramework/Classes/Client/AbstractClientDispatcher.php @@ -21,6 +21,13 @@ abstract class AbstractClientDispatcher implements \JPT\SocketFramework\Client\C * @var string */ protected $group; + + /** + * Contains data that the ClientDispatcher sends. + * + * @var \JPT\SocketFramework\Misc\Buffer + */ + protected $outputBuffer; /** * Contains a reference to the ClientManager in order to change stuff on the fly. @@ -29,6 +36,35 @@ abstract class AbstractClientDispatcher implements \JPT\SocketFramework\Client\C */ protected $clientManager; + + /** + * Default constructor. + * Creates the outputBuffer. + */ + public function __construct() { + $this->outputBuffer = new \JPT\SocketFramework\Misc\Buffer(); + } + + /** + * Adds data that shall be sent to the outputBuffer. + * Feel free to overwrite this if you need something different. + * + * @param string $rawData + */ + protected function send($rawData) { + $this->outputBuffer->addData($rawData); + } + + /** + * Returns all data from the outputBuffer. + * Feel free to overwrite this if you need something different. + * + * @return string + */ + public function getOutputData() { + return $this->outputBuffer->getAllBufferContents(); + } + /** * Forwards incoming data to the ProtocolHandler * Let's the ProtocolHandler do all the work and forward its results to the Clients. diff --git a/SocketFramework/Classes/Client/ClientManager.php b/SocketFramework/Classes/Client/ClientManager.php index 9a19294..053e578 100644 --- a/SocketFramework/Classes/Client/ClientManager.php +++ b/SocketFramework/Classes/Client/ClientManager.php @@ -131,7 +131,12 @@ class ClientManager { unset($data); //call the registered client - if(isset($this->clientDispatcherPool[$connectionHandler->getID()])) $outgoingData .= $this->clientDispatcherPool[$connectionHandler->getID()]->processRawData($incomingData); + if(isset($this->clientDispatcherPool[$connectionHandler->getID()])) { + //other output data has priority. + $outgoingData .= $this->clientDispatcherPool[$connectionHandler->getID()]->getOutputData(); + //returned data from the processing will be sent. + $outgoingData .= $this->clientDispatcherPool[$connectionHandler->getID()]->processRawData($incomingData); + } //i don't know what to do here... maybe call a connection bridge? diff --git a/Testcode/Client/ClientManagerTest.php b/Testcode/Client/ClientManagerTest.php index 1290616..458b1f7 100644 --- a/Testcode/Client/ClientManagerTest.php +++ b/Testcode/Client/ClientManagerTest.php @@ -1,6 +1,7 @@ registerClientDispatcherByProtocol("irc", "\JPT\IrcClient\ClientDispatcher"); +$clientManager->registerClientDispatcherByProtocol("irc", "\JPT\IrcClient\IrcClientDispatcher"); +$clientManager->registerClientDispatcherByProtocol("jpt_control", "\JPT\IrcClient\BotClientDispatcher"); $freenode = $clientManager->createTcpConnection("freenode", "irc"); $clientManager->attachConfig(array( @@ -45,7 +46,7 @@ $eloxoph->connect("irc.eloxoph.com", 6667);*/ -$srv = $clientManager->createTcpConnection("srv", "RAW"); +$srv = $clientManager->createTcpConnection("srv", "jpt_control"); $srv->bind("localhost", 7777); $srv->listen();