From e7021248957d0cccbfadecadb6b5c11fc9edd951 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sat, 3 Dec 2011 20:58:08 +0100 Subject: [PATCH] [TASK] Finalized modifications on ClientManager. [TASK] Added support for further class paths to ClassLoader. [TASK] Started to implement the irc client dispatcher. (partially works now) --- .../{IrcClient.php => ClientDispatcher.php} | 73 ++++++++++--------- Main.php | 2 + .../Client/AbstractClientDispatcher.php | 4 +- .../Client/ClientDispatcherInterface.php | 2 +- .../Classes/Client/ClientManager.php | 15 ++-- SocketFramework/Classes/Core/ClassLoader.php | 43 +++++++++-- Testcode/Client/ClientManagerTest.php | 5 +- 7 files changed, 90 insertions(+), 54 deletions(-) rename IrcClient/Classes/{IrcClient.php => ClientDispatcher.php} (70%) diff --git a/IrcClient/Classes/IrcClient.php b/IrcClient/Classes/ClientDispatcher.php similarity index 70% rename from IrcClient/Classes/IrcClient.php rename to IrcClient/Classes/ClientDispatcher.php index 9433342..75cff26 100644 --- a/IrcClient/Classes/IrcClient.php +++ b/IrcClient/Classes/ClientDispatcher.php @@ -1,42 +1,44 @@ channels = array(); $this->resetConnectionStatus(); } - + /** * Will reset the clients internal variables concerning the connection status. * @return void @@ -57,7 +59,7 @@ class Client_IrcClient extends Client_AbstractClient { $this->joined = FALSE; $this->authed = FALSE; } - + /** * This function gets called every time, the connection is established. * This allows the client to send initial data. @@ -68,46 +70,45 @@ class Client_IrcClient extends Client_AbstractClient { $data = "USER poweruser as as :JPTs Bot\r\nNICK " . $this->nick . "\r\n"; $this->authed = TRUE; } - $this->protocolHandler->sendRaw($data); + //$this->protocolHandler->sendRaw($data); } - + /** - * Processes the resulting ContentObject from a ProtocolHandler. - * Does all the hard work. - * @param string $data - * @return void + * Forwards incoming data to the ProtocolHandler + * Let's the ProtocolHandler do all the work and forward its results to the Clients. + * Return resulting raw data. + * + * @param string $rawData + * @return string */ - protected function processContentObject($contentObject) { - $data = $contentObject->getRawData(); - //DEBUG - var_dump($contentObject); + public function processRawData($rawData) { + $data = $rawData; var_dump($data); - - //respond to pings - if($contentObject->getCommand() === "PING") $this->protocolHandler->pong($contentObject->getParams()); - + + //respond to pings + //if($contentObject->getCommand() === "PING") $this->protocolHandler->pong($contentObject->getParams()); + $this->clientManager->sendToGroup("srv", "[#".$this->ID."] ".$data); - - if($contentObject->getCommand() === "001") $this->got001 = TRUE; - + + //if($contentObject->getCommand() === "001") $this->got001 = TRUE; + $return = ""; - + $this->lines++; - + if(!$this->joined && $this->got001) { $this->joined = TRUE; foreach($this->channels AS $channel) $return .= "JOIN " . $channel . "\r\n"; } - + if(strpos($data, "musdsdsafgagltivitamin") !== FALSE) { $return .= "PRIVMSG ".$this->channels[0]." :roger that :D\r\n"; $return .= "QUIT :lol\r\n"; } - //workaround. will be removed soon - $this->protocolHandler->sendRaw($return); + return $return; } - + /** * Loads the given configuration. * @param array $config @@ -118,6 +119,6 @@ class Client_IrcClient extends Client_AbstractClient { $this->channels = $config["channels"]; $this->userident = $config["userident"]; } - + } ?> \ No newline at end of file diff --git a/Main.php b/Main.php index 9cd1c07..a6ebe61 100644 --- a/Main.php +++ b/Main.php @@ -2,6 +2,8 @@ error_reporting(E_ALL); require_once('SocketFramework' . DIRECTORY_SEPARATOR . 'Bootstrap.php'); +$classLoader->addClassPathMapping('JPT\IrcClient', 'IrcClient' . \DIRECTORY_SEPARATOR . 'Classes'); + try { require_once('Testcode' . DIRECTORY_SEPARATOR . 'Client' . DIRECTORY_SEPARATOR . 'ClientManagerTest.php'); } diff --git a/SocketFramework/Classes/Client/AbstractClientDispatcher.php b/SocketFramework/Classes/Client/AbstractClientDispatcher.php index 79944d4..b9f8927 100644 --- a/SocketFramework/Classes/Client/AbstractClientDispatcher.php +++ b/SocketFramework/Classes/Client/AbstractClientDispatcher.php @@ -47,7 +47,9 @@ abstract class AbstractClientDispatcher implements \JPT\SocketFramework\Client\C * @param array $config * @return void */ - abstract public function loadConfig($config); + public function loadConfig($config) { + + } /** * Will reset the connectionStatus of the client. diff --git a/SocketFramework/Classes/Client/ClientDispatcherInterface.php b/SocketFramework/Classes/Client/ClientDispatcherInterface.php index f84e235..7ae7919 100644 --- a/SocketFramework/Classes/Client/ClientDispatcherInterface.php +++ b/SocketFramework/Classes/Client/ClientDispatcherInterface.php @@ -26,7 +26,7 @@ interface ClientDispatcherInterface { * @param array $config * @return void */ - abstract public function loadConfig($config); + public function loadConfig($config); /** * Will reset the connectionStatus of the client. diff --git a/SocketFramework/Classes/Client/ClientManager.php b/SocketFramework/Classes/Client/ClientManager.php index ce7737f..9a19294 100644 --- a/SocketFramework/Classes/Client/ClientManager.php +++ b/SocketFramework/Classes/Client/ClientManager.php @@ -99,7 +99,7 @@ class ClientManager { foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) { if(isset($this->clientDispatcherPool[$connectionHandler->getID()])) continue; //new connections might need a client, so we'll create one here. - $this->addClientForConnectionHandler($connectionHandler); + $this->addClientDispatcherForConnectionHandler($connectionHandler); //allow client to send initial stuff right after connecting to the server. $this->initializeClientForConnectionHandler($connectionHandler); } @@ -120,7 +120,7 @@ class ClientManager { } //handle accepted sockets, adopt them and treat them with care :-) if($connectionHandler->isListening() === TRUE) { - $this->addClientForConnectionHandler($connectionHandler); + $this->addClientDispatcherForConnectionHandler($connectionHandler); $this->initializeClientForConnectionHandler($connectionHandler); } @@ -233,7 +233,7 @@ class ClientManager { } /** - * Calls Connection_Pool. + * Calls \JPT\SocketFramework\Connection\ConnectionPool. * * @see \JPT\SocketFramework\Connection\ConnectionPool * @param string $group @@ -254,8 +254,8 @@ class ClientManager { */ protected function createClientDispatcherForProtocol($protocolIdentifier) { //look for the protocol - if(!in_array($protocolIdentifier, $this->registeredClientDispatchers)) { - throw new \JPT\SocketFramework\Exception\GeneralException("No client dispatcher is registered for protocol: '" . $protocol . "'!", 1290271651); + if(isset($this->registeredClientDispatchers[$protocolIdentifier]) === FALSE) { + throw new \JPT\SocketFramework\Exception\GeneralException("No client dispatcher is registered for protocol: '" . $protocolIdentifier . "'!", 1290271651); } $className = $this->registeredClientDispatchers[$protocolIdentifier]; //look for the class @@ -265,7 +265,7 @@ class ClientManager { if(!$clientDispatcher instanceof \JPT\SocketFramework\Client\AbstractClientDispatcher) throw new \JPT\SocketFramework\Exception\GeneralException("Class '" . $className . "' does not implement the AbstractClientDispatcher-API!", 1294837055); return $clientDispatcher; } else { - throw new \JPT\SocketFramework\Exception\GeneralException("Registered class name '" . $className . "' does not exist for protocol: '" . $protocol . "'!", 1290271773); + throw new \JPT\SocketFramework\Exception\GeneralException("Registered class name '" . $className . "' does not exist for protocol: '" . $protocolIdentifier . "'!", 1290271773); } } @@ -277,6 +277,7 @@ class ClientManager { * @return void */ protected function initializeClientForConnectionHandler($connectionHandler) { + if(!isset($this->clientDispatcherPool[$connectionHandler->getID()])) return; $this->clientDispatcherPool[$connectionHandler->getID()]->initializeConnection(); //after initializing, have it process an empty string in order to get stuff to write. >.< $result = $this->clientDispatcherPool[$connectionHandler->getID()]->processRawData(""); @@ -315,7 +316,7 @@ class ClientManager { * @return void */ protected function addClientDispatcherForConnectionHandler($connectionHandler) { - $protocolIdentifier = $this->registeredProtocols[$connectionHandler->getProtocol()]; + $protocolIdentifier = $connectionHandler->getProtocol(); //do not try this for "RAW" connections - might or might not be useful. if($protocolIdentifier === "RAW") return; $clientDispatcher = $this->createClientDispatcherForProtocol($protocolIdentifier); diff --git a/SocketFramework/Classes/Core/ClassLoader.php b/SocketFramework/Classes/Core/ClassLoader.php index 6134757..85241c4 100644 --- a/SocketFramework/Classes/Core/ClassLoader.php +++ b/SocketFramework/Classes/Core/ClassLoader.php @@ -8,20 +8,51 @@ namespace JPT\SocketFramework\Core; */ class ClassLoader { + /** + * Contains class name prefix and path prefix for all class paths it shall handle + * + * @var array + */ + protected $classNameMapping = array(); + + /** + * Default constructor. + * Registers the main class path for the socket framework. + */ + public function __construct() { + $this->classNameMapping = array( + 'JPT\SocketFramework' => 'SocketFramework' . \DIRECTORY_SEPARATOR . 'Classes' + ); + } + + /** + * Registers a new class path for the autoloader + * + * @param string $classNamePrefix + * @param string $pathPrefix + */ + public function addClassPathMapping($classNamePrefix, $pathPrefix) { + $this->classNameMapping[$classNamePrefix] = $pathPrefix; + } + /** * Autoloader function. - * All classes will be in Classes/$Package$/$ClassName$.php * * @throws \Exception * @param string $className * @return void */ public function loadClass($className) { - if(substr($className, 0, 19) !== "JPT\\SocketFramework") return; - $className = substr($className, 20); - $fileName = "SocketFramework" . \DIRECTORY_SEPARATOR . "Classes"; - $fileName .= \DIRECTORY_SEPARATOR . str_replace("\\", \DIRECTORY_SEPARATOR, $className) . ".php"; - + $matchingClassNamePrefix = FALSE; + foreach($this->classNameMapping AS $classNamePrefix => $pathPrefix) { + if(substr($className, 0, strlen($classNamePrefix)) !== $classNamePrefix) continue; + $matchingClassNamePrefix = TRUE; + $classNameSuffix = substr($className, strlen($classNamePrefix), strlen($className)); + $fileName = $this->classNameMapping[$classNamePrefix]; + $fileName .= \DIRECTORY_SEPARATOR . str_replace('\\', \DIRECTORY_SEPARATOR, $classNameSuffix) . ".php"; + break; + } + if($matchingClassNamePrefix === FALSE) return; if(file_exists($fileName) === TRUE) { require_once($fileName); } else { diff --git a/Testcode/Client/ClientManagerTest.php b/Testcode/Client/ClientManagerTest.php index fd53484..1290616 100644 --- a/Testcode/Client/ClientManagerTest.php +++ b/Testcode/Client/ClientManagerTest.php @@ -1,7 +1,6 @@ registerProtocol("irc", "Irc"); -$clientManager->registerProtocol("jpt", "Bot"); +$clientManager->registerClientDispatcherByProtocol("irc", "\JPT\IrcClient\ClientDispatcher"); $freenode = $clientManager->createTcpConnection("freenode", "irc"); $clientManager->attachConfig(array( @@ -46,7 +45,7 @@ $eloxoph->connect("irc.eloxoph.com", 6667);*/ -$srv = $clientManager->createTcpConnection("srv", "jpt"); +$srv = $clientManager->createTcpConnection("srv", "RAW"); $srv->bind("localhost", 7777); $srv->listen();