From 16ff75eb1da87c5bd2bf7efbfdd6993b1f55a7f6 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sat, 3 Dec 2011 12:28:02 +0100 Subject: [PATCH] [TASK] Moved the project to namespaces. [TASK] Removed all the protocol handlers. [TASK] Clients are no longer part of the SocketFramework itself. [TASK] The Framework provides an "interface" to external clients - the ClientDispatcher. [TASK] Added the Core package and added a better ClassLoader. [TASK] Created a bootstrap module to include in other projects. [TASK] Cleaned up comments, reformatted them. --- .gitignore | 4 +- AutoLoader.php | 17 -- Classes/Client/AbstractClient.php | 123 ------------- .../Exception/ProtocolHandlerException.php | 9 - Classes/Exception/SocketException.php | 9 - Classes/Exception/WrongDatatypeException.php | 9 - .../AbstractProtocolContentObject.php | 35 ---- Classes/Protocol/AbstractProtocolHandler.php | 75 -------- Classes/Protocol/BotProtocolContentObject.php | 8 - Classes/Protocol/BotProtocolHandler.php | 52 ------ Classes/Protocol/RawProtocolContentObject.php | 8 - Classes/Protocol/RawProtocolHandler.php | 17 -- Doxyfile | 4 +- .../Classes}/BotClient.php | 0 .../Classes}/IrcClient.php | 0 .../Protocol/IrcProtocolContentObject.php | 0 .../Classes}/Protocol/IrcProtocolHandler.php | 0 Main.php | 2 +- SocketFramework/Bootstrap.php | 12 ++ .../Client/AbstractClientDispatcher.php | 107 ++++++++++++ .../Client/ClientDispatcherInterface.php | 74 ++++++++ .../Classes}/Client/ClientManager.php | 164 +++++++++++------- .../Classes}/Connection/ConnectionHandler.php | 133 ++++++++------ .../Classes}/Connection/ConnectionPool.php | 48 +++-- SocketFramework/Classes/Core/ClassLoader.php | 44 +++++ .../Classes}/Exception/GeneralException.php | 9 +- .../Classes/Exception/SocketException.php | 12 ++ .../Exception/WrongDatatypeException.php | 12 ++ .../Classes}/Misc/Buffer.php | 21 ++- .../Classes}/Socket/SocketHandler.php | 75 +++++--- .../Classes}/Socket/SocketPool.php | 29 +++- Testcode/Client/ClientManagerTest.php | 2 +- 32 files changed, 573 insertions(+), 541 deletions(-) delete mode 100644 AutoLoader.php delete mode 100644 Classes/Client/AbstractClient.php delete mode 100644 Classes/Exception/ProtocolHandlerException.php delete mode 100644 Classes/Exception/SocketException.php delete mode 100644 Classes/Exception/WrongDatatypeException.php delete mode 100644 Classes/Protocol/AbstractProtocolContentObject.php delete mode 100644 Classes/Protocol/AbstractProtocolHandler.php delete mode 100644 Classes/Protocol/BotProtocolContentObject.php delete mode 100644 Classes/Protocol/BotProtocolHandler.php delete mode 100644 Classes/Protocol/RawProtocolContentObject.php delete mode 100644 Classes/Protocol/RawProtocolHandler.php rename {Classes/Client => IrcClient/Classes}/BotClient.php (100%) rename {Classes/Client => IrcClient/Classes}/IrcClient.php (100%) rename {Classes => IrcClient/Classes}/Protocol/IrcProtocolContentObject.php (100%) rename {Classes => IrcClient/Classes}/Protocol/IrcProtocolHandler.php (100%) create mode 100644 SocketFramework/Bootstrap.php create mode 100644 SocketFramework/Classes/Client/AbstractClientDispatcher.php create mode 100644 SocketFramework/Classes/Client/ClientDispatcherInterface.php rename {Classes => SocketFramework/Classes}/Client/ClientManager.php (55%) rename {Classes => SocketFramework/Classes}/Connection/ConnectionHandler.php (70%) rename {Classes => SocketFramework/Classes}/Connection/ConnectionPool.php (82%) create mode 100644 SocketFramework/Classes/Core/ClassLoader.php rename {Classes => SocketFramework/Classes}/Exception/GeneralException.php (85%) create mode 100644 SocketFramework/Classes/Exception/SocketException.php create mode 100644 SocketFramework/Classes/Exception/WrongDatatypeException.php rename {Classes => SocketFramework/Classes}/Misc/Buffer.php (87%) rename {Classes => SocketFramework/Classes}/Socket/SocketHandler.php (61%) rename {Classes => SocketFramework/Classes}/Socket/SocketPool.php (72%) diff --git a/.gitignore b/.gitignore index d8fe4fa..dfd233b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -/.project +.project +.settings +.buildpath \ No newline at end of file diff --git a/AutoLoader.php b/AutoLoader.php deleted file mode 100644 index ab8072f..0000000 --- a/AutoLoader.php +++ /dev/null @@ -1,17 +0,0 @@ - \ No newline at end of file diff --git a/Classes/Client/AbstractClient.php b/Classes/Client/AbstractClient.php deleted file mode 100644 index 9ef3354..0000000 --- a/Classes/Client/AbstractClient.php +++ /dev/null @@ -1,123 +0,0 @@ -protocolHandler = $protocolHandler; - } - - /** - * 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 - */ - public function processRawData($rawData) { - $this->protocolHandler->pushRawData($rawData); - while($this->protocolHandler->canWork()) { - $this->processContentObject($this->protocolHandler->work()); - } - return $this->protocolHandler->getRawData(); - } - - /** - * 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 void - */ - abstract protected function processContentObject($contentObject); - - /** - * This function will load the given config. - * @param array $config - * @return void - */ - abstract public function loadConfig($config); - - /** - * Will reset the connectionStatus of the client. - * Implementation is optional and depends on the client. - * Should be used to reset internal variables. - * @return void - */ - public function resetConnectionStatus() { - - } - - /** - * This function gets called every time, the connection is established. - * This allows the client to send initial data. - * @return void - */ - public function initializeConnection() { - - } - - /** - * @param int $id - * @return void - */ - public function setID($id) { - $this->ID = $id; - } - - /** - * @param string $group - * @return void - */ - public function setGroup($group) { - $this->group = $group; - } - - /** - * Injects ClientManager - * @param Client_ClientManager $clientManager - * @return void - * @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; - } -} -?> \ No newline at end of file diff --git a/Classes/Exception/ProtocolHandlerException.php b/Classes/Exception/ProtocolHandlerException.php deleted file mode 100644 index 896c533..0000000 --- a/Classes/Exception/ProtocolHandlerException.php +++ /dev/null @@ -1,9 +0,0 @@ - \ No newline at end of file diff --git a/Classes/Exception/SocketException.php b/Classes/Exception/SocketException.php deleted file mode 100644 index 6d461ad..0000000 --- a/Classes/Exception/SocketException.php +++ /dev/null @@ -1,9 +0,0 @@ - \ No newline at end of file diff --git a/Classes/Exception/WrongDatatypeException.php b/Classes/Exception/WrongDatatypeException.php deleted file mode 100644 index bbacf7a..0000000 --- a/Classes/Exception/WrongDatatypeException.php +++ /dev/null @@ -1,9 +0,0 @@ - \ No newline at end of file diff --git a/Classes/Protocol/AbstractProtocolContentObject.php b/Classes/Protocol/AbstractProtocolContentObject.php deleted file mode 100644 index ff04880..0000000 --- a/Classes/Protocol/AbstractProtocolContentObject.php +++ /dev/null @@ -1,35 +0,0 @@ -rawData = $rawData; - } - - /** - * Returns raw data. - * @return string - */ - public function getRawData() { - return $this->rawData; - } -} -?> \ No newline at end of file diff --git a/Classes/Protocol/AbstractProtocolHandler.php b/Classes/Protocol/AbstractProtocolHandler.php deleted file mode 100644 index 0b5428f..0000000 --- a/Classes/Protocol/AbstractProtocolHandler.php +++ /dev/null @@ -1,75 +0,0 @@ -createBuffers(); - } - - /** - * Returns whether work() can be called or not. - * Depends on the protocol and its implementation. - * @return boolean - */ - abstract public function canWork(); - - /** - * Main worker function of the ProtocolHandler. - * Will fetch data from the bufferIncoming, process it, - * and create a ProtocolContentObject out of it that will be returned to the client. - * @return mixed instance of Protocol_AbstractProtocolContentObject - */ - abstract public function work(); - - /** - * Adds the incoming data to the internal buffer. - * @param string $rawData - * @return void - */ - public function pushRawData($rawData) { - $this->bufferIncoming->addData($rawData); - } - - /** - * Returns all outgoing data of the internal buffer. - * @return string - */ - public function getRawData() { - return $this->bufferOutgoing->getAllBufferContents(); - } -} -?> \ No newline at end of file diff --git a/Classes/Protocol/BotProtocolContentObject.php b/Classes/Protocol/BotProtocolContentObject.php deleted file mode 100644 index ba25f3d..0000000 --- a/Classes/Protocol/BotProtocolContentObject.php +++ /dev/null @@ -1,8 +0,0 @@ - \ No newline at end of file diff --git a/Classes/Protocol/BotProtocolHandler.php b/Classes/Protocol/BotProtocolHandler.php deleted file mode 100644 index bcf2ada..0000000 --- a/Classes/Protocol/BotProtocolHandler.php +++ /dev/null @@ -1,52 +0,0 @@ -bufferIncoming = new Misc_Buffer($linebreak); - $this->bufferOutgoing = 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->bufferIncoming->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->bufferIncoming->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->bufferOutgoing->addLine($data); - } - -} -?> \ No newline at end of file diff --git a/Classes/Protocol/RawProtocolContentObject.php b/Classes/Protocol/RawProtocolContentObject.php deleted file mode 100644 index 30c5c0f..0000000 --- a/Classes/Protocol/RawProtocolContentObject.php +++ /dev/null @@ -1,8 +0,0 @@ - \ No newline at end of file diff --git a/Classes/Protocol/RawProtocolHandler.php b/Classes/Protocol/RawProtocolHandler.php deleted file mode 100644 index 377f5f6..0000000 --- a/Classes/Protocol/RawProtocolHandler.php +++ /dev/null @@ -1,17 +0,0 @@ - \ No newline at end of file diff --git a/Doxyfile b/Doxyfile index db660a3..62b0b3b 100644 --- a/Doxyfile +++ b/Doxyfile @@ -38,7 +38,7 @@ PROJECT_NUMBER = # If a relative path is entered, it will be relative to the location # where doxygen was started. If left blank the current directory will be used. -OUTPUT_DIRECTORY = Documentation/Doxygen/ +OUTPUT_DIRECTORY = SocketFramework/Documentation/Doxygen/ # If the CREATE_SUBDIRS tag is set to YES, then doxygen will create # 4096 sub-directories (in 2 levels) under the output directory of each output @@ -60,7 +60,7 @@ CREATE_SUBDIRS = YES # Polish, Portuguese, Romanian, Russian, Serbian, Serbian-Cyrilic, Slovak, # Slovene, Spanish, Swedish, Ukrainian, and Vietnamese. -OUTPUT_LANGUAGE = German +OUTPUT_LANGUAGE = English # If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will # include brief member descriptions after the members that are listed in diff --git a/Classes/Client/BotClient.php b/IrcClient/Classes/BotClient.php similarity index 100% rename from Classes/Client/BotClient.php rename to IrcClient/Classes/BotClient.php diff --git a/Classes/Client/IrcClient.php b/IrcClient/Classes/IrcClient.php similarity index 100% rename from Classes/Client/IrcClient.php rename to IrcClient/Classes/IrcClient.php diff --git a/Classes/Protocol/IrcProtocolContentObject.php b/IrcClient/Classes/Protocol/IrcProtocolContentObject.php similarity index 100% rename from Classes/Protocol/IrcProtocolContentObject.php rename to IrcClient/Classes/Protocol/IrcProtocolContentObject.php diff --git a/Classes/Protocol/IrcProtocolHandler.php b/IrcClient/Classes/Protocol/IrcProtocolHandler.php similarity index 100% rename from Classes/Protocol/IrcProtocolHandler.php rename to IrcClient/Classes/Protocol/IrcProtocolHandler.php diff --git a/Main.php b/Main.php index 75462d4..9cd1c07 100644 --- a/Main.php +++ b/Main.php @@ -1,6 +1,6 @@ initialize(); +?> \ No newline at end of file diff --git a/SocketFramework/Classes/Client/AbstractClientDispatcher.php b/SocketFramework/Classes/Client/AbstractClientDispatcher.php new file mode 100644 index 0000000..79944d4 --- /dev/null +++ b/SocketFramework/Classes/Client/AbstractClientDispatcher.php @@ -0,0 +1,107 @@ +ID = $id; + } + + /** + * Sets the group. + * + * @param string $group + * @return void + */ + public function setGroup($group) { + $this->group = $group; + } + + /** + * Injects ClientManager + * + * @param \JPT\SocketFramework\Client\ClientManager $clientManager + * @return void + * @throws \JPT\SocketFramework\Exception\WrongDatatypeException + */ + public function injectClientManager($clientManager) { + if(!$clientManager instanceof \JPT\SocketFramework\Client\ClientManager) { + throw new Exception_WrongDatatypeException("Cannot inject ClientManager! Expected an instance of \JPT\SocketFramework\Client\ClientManager, '" . get_type($clientManager) . "' given.", 1291156565); + } + $this->clientManager = $clientManager; + } +} +?> \ No newline at end of file diff --git a/SocketFramework/Classes/Client/ClientDispatcherInterface.php b/SocketFramework/Classes/Client/ClientDispatcherInterface.php new file mode 100644 index 0000000..f84e235 --- /dev/null +++ b/SocketFramework/Classes/Client/ClientDispatcherInterface.php @@ -0,0 +1,74 @@ + \ No newline at end of file diff --git a/Classes/Client/ClientManager.php b/SocketFramework/Classes/Client/ClientManager.php similarity index 55% rename from Classes/Client/ClientManager.php rename to SocketFramework/Classes/Client/ClientManager.php index 05d53b3..ce7737f 100644 --- a/Classes/Client/ClientManager.php +++ b/SocketFramework/Classes/Client/ClientManager.php @@ -1,34 +1,48 @@ $classNameOfMyClientDispatcher + * This way, we can register any client dispatcher from even seperate projects, + * so we can use them within this framework. + * + * Ah, maybe this is important to understand it: + * The protocol identifier pointing to the class name of the client dispatcher + * is used to bind the client dispatcher to a connection created. + * * @var array */ - protected $registeredProtocols; + protected $registeredClientDispatchers; /** * Contains Connection-attached configurations. + * * @var array */ protected $configPool; @@ -36,21 +50,23 @@ class Client_ClientManager { /** * Default constructor. + * * @return void */ public function __construct() { - $this->connectionPool = new Connection_ConnectionPool(); - $this->clientPool = array(); + $this->connectionPool = new \JPT\SocketFramework\Connection\ConnectionPool(); + $this->clientDispatcherPool = array(); $this->registeredProtocols = array(); $this->configPool = array(); } /** * Default destructor. Closes connections before being killed. + * * @return void */ public function __destruct() { - unset($this->clientPool); + unset($this->clientDispatcherPool); unset($this->connectionPool); } @@ -64,7 +80,8 @@ class Client_ClientManager { /** * Returns the amount of connected connection handlers. - * @see Connection_ConnectionPool + * + * @see \JPT\SocketFramework\Connection\ConnectionPool * @return int */ public function countActiveConnections() { @@ -74,12 +91,13 @@ class Client_ClientManager { /** * Main worker procedure. * Processes incoming data, calls clients and much more. + * * @return void */ public function work() { //first, create clients for connections that do NOT have one already. foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) { - if(isset($this->clientPool[$connectionHandler->getID()])) continue; + if(isset($this->clientDispatcherPool[$connectionHandler->getID()])) continue; //new connections might need a client, so we'll create one here. $this->addClientForConnectionHandler($connectionHandler); //allow client to send initial stuff right after connecting to the server. @@ -113,7 +131,7 @@ class Client_ClientManager { unset($data); //call the registered client - if(isset($this->clientPool[$connectionHandler->getID()])) $outgoingData .= $this->clientPool[$connectionHandler->getID()]->processRawData($incomingData); + if(isset($this->clientDispatcherPool[$connectionHandler->getID()])) $outgoingData .= $this->clientDispatcherPool[$connectionHandler->getID()]->processRawData($incomingData); //i don't know what to do here... maybe call a connection bridge? @@ -125,10 +143,11 @@ class Client_ClientManager { /** * Calls ConnectionPool in order to create a simple connection. - * @see Connection_ConnectionPool + * + * @see \JPT\SocketFramework\Connection\ConnectionPool * @param string $group * @param boolean $IPv6 - * @return Connection_ConnectionHandler + * @return \JPT\SocketFramework\Connection\ConnectionHandler */ public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) { return $this->connectionPool->createTcpConnection($group, $protocol, $IPv6); @@ -136,39 +155,43 @@ class Client_ClientManager { /** * Closes and removes the connection. - * @param Connection_ConnectionHandler $connectionHandler + * + * @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler * @return void */ public function removeConnection($connectionHandler) { unset($this->configPool[$connectionHandler->getID()]); - unset($this->clientPool[$connectionHandler->getID()]); + unset($this->clientDispatcherPool[$connectionHandler->getID()]); $this->connectionPool->removeConnectionHandler($connectionHandler); } /** - * Registers a protocol with a specific client. - * @param string $protocol + * Registers a protocol identifier and its specific client dispatcher. + * + * @param string $protocolIdentifier * @param string $className * @return void */ - public function registerProtocol($protocol, $classNamePartial) { - $this->registeredProtocols[$protocol] = $classNamePartial; + public function registerClientDispatcherByProtocol($protocolIdentifier, $className) { + $this->registeredClientDispatchers[$protocolIdentifier] = $className; } /** - * Unregisters a protocol. + * Unregisters a client dispatcher by its protocol identifier. + * * @param string $protocol * @return void */ - public function unregisterProtocol($protocol) { - unset($this->registeredProtocols[$protocol]); + public function unregisterClientDispatcherByProtocol($protocolIdentifier) { + unset($this->registeredClientDispatchers[$protocolIdentifier]); } /** * Attaches a configuration to a connection. * Will overwrite the existing configuration for the connection. + * * @param array $config - * @param Connection_ConnectionHandler $connectionHandler + * @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler * @return void */ public function attachConfig($config, $connectionHandler) { @@ -176,8 +199,9 @@ class Client_ClientManager { } /** - * Calls Connection_Pool - * @see Connection_ConnectionPool + * Calls ConnectionPool. + * + * @see \JPT\SocketFramework\Connection\ConnectionPool * @param int $id * @param string $data * @return void @@ -188,6 +212,7 @@ class Client_ClientManager { /** * Returns a list of available connection IDs. + * * @return array */ public function getIDs() { @@ -198,6 +223,7 @@ class Client_ClientManager { /** * Returns a list of available connection groups. + * * @return array */ public function getGroups() { @@ -207,8 +233,9 @@ class Client_ClientManager { } /** - * Calls Connection_Pool - * @see Connection_ConnectionPool + * Calls Connection_Pool. + * + * @see \JPT\SocketFramework\Connection\ConnectionPool * @param string $group * @param string $data * @return void @@ -218,56 +245,59 @@ class Client_ClientManager { } /** - * Searches for the registered client for the given protocol. - * Returns a client instance or void. + * Searches the registered client dispatcher for the given protocol. + * Returns a client dispatcher instance or void. + * * @param string $protocol * @return mixed - * @throws Exception_GeneralException + * @throws \JPT\SocketFramework\Exception\GeneralException */ - protected function createClientForProtocol($protocol) { + protected function createClientDispatcherForProtocol($protocolIdentifier) { //look for the protocol - if(!in_array($protocol, $this->registeredProtocols)) { - throw new Exception_GeneralException("No client registered for protocol: '" . $protocol . "'!", 1290271651); + if(!in_array($protocolIdentifier, $this->registeredClientDispatchers)) { + throw new \JPT\SocketFramework\Exception\GeneralException("No client dispatcher is registered for protocol: '" . $protocol . "'!", 1290271651); } - $className = "Client_" . $protocol . "Client"; + $className = $this->registeredClientDispatchers[$protocolIdentifier]; //look for the class if(class_exists($className)) { //TODO: This looks ugly and wrong. Ideas, anyone? - $client = new $className(); - if(!$client instanceof Client_AbstractClient) throw new Exception_GeneralException("Class '" . $className . "' does not implement the AbstractClient-API!", 1294837055); - return $client; + $clientDispatcher = new $className(); + 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 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: '" . $protocol . "'!", 1290271773); } } /** - * Initializes the client of a given connectionHandler. + * Initializes the client dispatcher of a given connectionHandler. * This should be done after establishing a connection. - * @param Connection_ConnectionHandler $connectionHandler + * + * @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler * @return void */ protected function initializeClientForConnectionHandler($connectionHandler) { - $this->clientPool[$connectionHandler->getID()]->initializeConnection(); + $this->clientDispatcherPool[$connectionHandler->getID()]->initializeConnection(); //after initializing, have it process an empty string in order to get stuff to write. >.< - $result = $this->clientPool[$connectionHandler->getID()]->processRawData(""); + $result = $this->clientDispatcherPool[$connectionHandler->getID()]->processRawData(""); if($result !== "") $connectionHandler->write($result); } /** - * Creates a new connectionHandler, resets the clients connection status, - * copies the old config and assigns the new connectionHandler to the client. - * @param Connection_ConnectionHandler $connectionHandler + * Creates a new connectionHandler, resets the client dispatchers connection status, + * copies the old config and assigns the new connectionHandler to the clientdispatcher. + * + * @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler * @return void */ protected function handleReconnectForConnectionHandler($connectionHandler) { //have the connectionHandler do the reconnect. $newConnectionHandler = $connectionHandler->reconnect(); //get the client and reset it. - $client = $this->clientPool[$connectionHandler->getID()]; + $client = $this->clientDispatcherPool[$connectionHandler->getID()]; $client->resetConnectionStatus(); //assign the client to the new connectionHandler. - $this->clientPool[$newConnectionHandler->getID()] = $client; + $this->clientDispatcherPool[$newConnectionHandler->getID()] = $client; //get the config and assign it to the new connectionHandler, too. $config = $this->configPool[$connectionHandler->getID()]; $this->configPool[$newConnectionHandler->getID()] = $config; @@ -278,35 +308,35 @@ class Client_ClientManager { } /** - * Adds a client to our client pool. - * @param Connection_ConnectionHandler $connectionHandler + * Adds a client dispatcher to our client dispatcher pool. + * The "RAW" protocol identifier is used to not create a client. + * + * @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler * @return void */ - protected function addClientForConnectionHandler($connectionHandler) { - $protocol = $this->registeredProtocols[$connectionHandler->getProtocol()]; - //do not try this for raw connections - if($protocol === "RAW") return; - $client = $this->createClientForProtocol($protocol); + protected function addClientDispatcherForConnectionHandler($connectionHandler) { + $protocolIdentifier = $this->registeredProtocols[$connectionHandler->getProtocol()]; + //do not try this for "RAW" connections - might or might not be useful. + if($protocolIdentifier === "RAW") return; + $clientDispatcher = $this->createClientDispatcherForProtocol($protocolIdentifier); if(isset($this->configPool[$connectionHandler->getID()])) { - $client->loadConfig($this->configPool[$connectionHandler->getID()]); + $clientDispatcher->loadConfig($this->configPool[$connectionHandler->getID()]); } - $client->injectClientManager($this); - $protocolHandlerClass = "Protocol_".$protocol."ProtocolHandler"; - //TODO: This looks ugly, too. Is there a better way? - $protocolHandler = new $protocolHandlerClass(); - $client->injectProtocolHandler($protocolHandler); - $client->setID($connectionHandler->getID()); - $client->setGroup($connectionHandler->getGroup()); - $this->clientPool[$connectionHandler->getID()] = $client; + $clientDispatcher->injectClientManager($this); + $clientDispatcher->setID($connectionHandler->getID()); + $clientDispatcher->setGroup($connectionHandler->getGroup()); + $this->clientDispatcherPool[$connectionHandler->getID()] = $clientDispatcher; } /** - * Removes a client from our client pool. - * @param Connection_ConnectionHandler $connectionHandler + * Removes a client dispatcher from our client dispatcher pool. + * + * @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler * @return void + * @throws \JPT\SocketFramework\Exception\WrongDatatypeException */ protected function removeClientForConnectionHandler($connectionHandler) { - unset($this->clientPool[$connectionHandler->getID()]); + unset($this->clientDispatcherPool[$connectionHandler->getID()]); $this->connectionPool->removeConnectionHandler($connectionHandler); } diff --git a/Classes/Connection/ConnectionHandler.php b/SocketFramework/Classes/Connection/ConnectionHandler.php similarity index 70% rename from Classes/Connection/ConnectionHandler.php rename to SocketFramework/Classes/Connection/ConnectionHandler.php index 555b2a9..5319a84 100644 --- a/Classes/Connection/ConnectionHandler.php +++ b/SocketFramework/Classes/Connection/ConnectionHandler.php @@ -1,20 +1,24 @@ bufferIncoming = new Misc_Buffer(); - $this->bufferOutgoing = new Misc_Buffer(); - $this->socketHandler = new Socket_SocketHandler($socket); + $this->bufferIncoming = new \JPT\SocketFramework\Misc\Buffer(); + $this->bufferOutgoing = new \JPT\SocketFramework\Misc\Buffer(); + $this->socketHandler = new \JPT\SocketFramework\Socket\SocketHandler($socket); $this->id = $id; $this->group = $group; $this->protocol = $protocol; @@ -103,6 +113,7 @@ class Connection_ConnectionHandler { /** * Calls parent destructor. + * * @return void */ public function __destruct() { @@ -111,7 +122,8 @@ class Connection_ConnectionHandler { /** * Injector for the internal ConnectionPool access. - * @param Connection_ConnectionPool $connectionPool + * + * @param \JPT\SocketFramework\Connection\ConnectionPool $connectionPool * @return void */ public function injectConnectionPool($connectionPool) { @@ -141,6 +153,7 @@ class Connection_ConnectionHandler { /** * Returns whether this connection is a listening socket or a general client connection socket. + * * @return boolean */ public function isServer() { @@ -149,6 +162,7 @@ class Connection_ConnectionHandler { /** * Sets the IPv6-flag. + * * @param boolean $IPv6 * @return void */ @@ -158,6 +172,7 @@ class Connection_ConnectionHandler { /** * Sets reconnectOnDisconnect flag. + * * @param boolean $reconnect * @return void */ @@ -167,6 +182,7 @@ class Connection_ConnectionHandler { /** * Gets reconnectOnDisconnect flag. + * * @return boolean */ public function getReconnect() { @@ -176,6 +192,7 @@ class Connection_ConnectionHandler { /** * This function is called when socket_read() or socket_write() fail. * It creates a new ConnectionHandler that will reconnect. + * * @return void */ protected function shutdown() { @@ -186,7 +203,8 @@ class Connection_ConnectionHandler { /** * Reads from SocketHandler, writes into bufferIncoming. * Returns a boolean that will indicate whether the socket is still okay. - * @throws Exception_SocketException + * + * @throws \JPT\SocketFramework\Exception\SocketException * @return boolean */ public function readToBuffer() { @@ -203,7 +221,8 @@ class Connection_ConnectionHandler { /** * Writes the bufferOutgoing to the SocketHandler. * Returns a boolean that will indicate whether the socket is still okay. - * @throws Exception_SocketException + * + * @throws \JPT\SocketFramework\Exception\SocketException * @return boolean */ public function writeFromBuffer() { @@ -219,8 +238,9 @@ class Connection_ConnectionHandler { } /** - * Calls error() on Socket_SocketHandler. - * @throws Socket_SocketExceptions + * Calls error() on \JPT\SocketFramework\Socket\SocketHandler. + * + * @throws \JPT\SocketFramework\Exception\SocketException * @return void */ public function handleSocketError() { @@ -229,6 +249,7 @@ class Connection_ConnectionHandler { /** * Determines whether this ConnectionHandler has data to read. + * * @return boolean */ public function canRead() { @@ -237,6 +258,7 @@ class Connection_ConnectionHandler { /** * Determines whether this ConnectionHandler has data to write. + * * @return boolean */ public function canWrite() { @@ -246,6 +268,7 @@ class Connection_ConnectionHandler { /** * Reads new data into bufferIncoming. * Returns a full line from bufferIncoming. + * * @return string */ public function read() { @@ -255,6 +278,7 @@ class Connection_ConnectionHandler { /** * Writes data into bufferOutgoing. * Sends data from bufferOutgoing to the SocketHandler. + * * @param $data * @return void */ @@ -263,9 +287,10 @@ class Connection_ConnectionHandler { } /** - * Calls SocketHandler - * @see Socket_SocketHandler - * @throws Exception_SocketException + * Calls SocketHandler. + * + * @see \JPT\SocketFramework\Socket\SocketHandler + * @throws \JPT\SocketFramework\Exception\SocketException * @return ressource */ public function accept() { @@ -273,9 +298,10 @@ class Connection_ConnectionHandler { } /** - * Calls SocketHandler - * @see Socket_SocketHandler - * @throws Exception_SocketException + * Calls SocketHandler. + * + * @see \JPT\SocketFramework\Socket\SocketHandler + * @throws \JPT\SocketFramework\Exception\SocketException * @return string */ public function getRemoteName() { @@ -283,9 +309,10 @@ class Connection_ConnectionHandler { } /** - * Calls SocketHandler - * @see Socket_SocketHandler - * @throws Exception_SocketException + * Calls SocketHandler. + * + * @see \JPT\SocketFramework\Socket\SocketHandler + * @throws \JPT\SocketFramework\Exception\SocketException * @return string */ public function getLocalName() { @@ -293,9 +320,10 @@ class Connection_ConnectionHandler { } /** - * Calls SocketHandler - * @see Socket_SocketHandler - * @throws Exception_SocketException + * Calls SocketHandler. + * + * @see \JPT\SocketFramework\Socket\SocketHandler + * @throws \JPT\SocketFramework\Exception\SocketException * @return void */ public function close() { @@ -304,8 +332,9 @@ class Connection_ConnectionHandler { /** * Calls SocketHandler, stores connection data. - * @see Socket_SocketHandler - * @throws Exception_SocketException + * + * @see \JPT\SocketFramework\Socket\SocketHandler + * @throws \JPT\SocketFramework\Exception\SocketException * @param string $address * @param int $port * @return void @@ -318,15 +347,16 @@ class Connection_ConnectionHandler { /** * Calls SocketHandler, uses stored connection data to fork a new instance of itself. - * @see Socket_SocketHandler - * @throws Exception_SocketException - * @throws Exception_GeneralException - * @return Connection_ConnectionHandler + * + * @see \JPT\SocketFramework\Socket\SocketHandler + * @throws \JPT\SocketFramework\Exception\SocketException + * @throws \JPT\SocketFramework\Exception\GeneralException + * @return \JPT\SocketFramework\Connection\ConnectionHandler */ public function reconnect() { - if($this->reconnectOnDisconnect === FALSE) throw new Exception_GeneralException("Cannot reconnect: Reconnect-Flag not set!", 1290951385); - if(empty($this->host) === TRUE) throw new Exception_GeneralException("Cannot reconnect: No host specified.", 1290950818); - if(empty($this->port) === TRUE) throw new Exception_GeneralException("Cannot reconnect: No port specified.", 1290950844); + if($this->reconnectOnDisconnect === FALSE) throw new \JPT\SocketFramework\Exception\GeneralException("Cannot reconnect: Reconnect-Flag not set!", 1290951385); + if(empty($this->host) === TRUE) throw new \JPT\SocketFramework\Exception\GeneralException("Cannot reconnect: No host specified.", 1290950818); + if(empty($this->port) === TRUE) throw new \JPT\SocketFramework\Exception\GeneralException("Cannot reconnect: No port specified.", 1290950844); $newConnectionHandler = $this->connectionPool->createTcpConnection($this->group, $this->protocol, $this->IPv6); $newConnectionHandler->setReconnect($this->getReconnect()); $newConnectionHandler->connect($this->host, $this->port); @@ -334,9 +364,10 @@ class Connection_ConnectionHandler { } /** - * Calls SocketHandler - * @see Socket_SocketHandler - * @throws Exception_SocketException + * Calls SocketHandler. + * + * @see \JPT\SocketFramework\Socket\SocketHandler + * @throws \JPT\SocketFramework\Exception\SocketException * @return void */ public function bind($address, $port) { @@ -346,9 +377,10 @@ class Connection_ConnectionHandler { } /** - * Calls SocketHandler - * @see Socket_SocketHandler - * @throws Exception_SocketException + * Calls SocketHandler. + * + * @see \JPT\SocketFramework\Socket\SocketHandler + * @throws \JPT\SocketFramework\Exception\SocketException * @return void */ public function listen() { @@ -357,7 +389,7 @@ class Connection_ConnectionHandler { } /** - * @see Socket_SocketHandler + * @see \JPT\SocketFramework\Socket\SocketHandler * @return boolean */ public function isConnected() { @@ -366,7 +398,8 @@ class Connection_ConnectionHandler { /** * Sets the is_connected-flag in the socket handler. - * @see Socket_SocketHandler + * + * @see \JPT\SocketFramework\Socket\SocketHandler * @param boolean $connected * @return void */ @@ -375,7 +408,7 @@ class Connection_ConnectionHandler { } /** - * @see Socket_SocketHandler + * @see \JPT\SocketFramework\Socket\SocketHandler * @return boolean */ public function isListening() { @@ -383,9 +416,10 @@ class Connection_ConnectionHandler { } /** - * Calls SocketHandler - * @see Socket_SocketHandler - * @throws Exception_SocketException + * Calls SocketHandler. + * + * @see \JPT\SocketFramework\Socket\SocketHandler + * @throws \JPT\SocketFramework\Exception\SocketException * @return ressource */ public function getSocket() { @@ -393,15 +427,16 @@ class Connection_ConnectionHandler { } /** - * @return Socket_SocketHandler + * @return \JPT\SocketFramework\Socket\SocketHandler */ public function getSocketHandler() { return $this->socketHandler; } /** - * Calls SocketHandler - * @see Socket_SocketHandler + * Calls SocketHandler. + * + * @see \JPT\SocketFramework\Socket\SocketHandler * @return void */ public function hasBeenAccepted() { diff --git a/Classes/Connection/ConnectionPool.php b/SocketFramework/Classes/Connection/ConnectionPool.php similarity index 82% rename from Classes/Connection/ConnectionPool.php rename to SocketFramework/Classes/Connection/ConnectionPool.php index 8d91ca9..bee9e2e 100644 --- a/Classes/Connection/ConnectionPool.php +++ b/SocketFramework/Classes/Connection/ConnectionPool.php @@ -1,20 +1,25 @@ connectionHandlers = array(); - $this->socketPool = new Socket_SocketPool(); + $this->socketPool = new \JPT\SocketFramework\Socket\SocketPool(); $this->nextID = 1; } /** - * Destroys the SocketPool + * Destroys the SocketPool. + * * @return void */ public function __destruct() { @@ -44,12 +51,13 @@ class Connection_ConnectionPool { /** * Creates a new TcpConnection. + * * @param boolean $IPv6 will determine whether the socket uses IPv4 or IPv6. - * @return Connection_ConnectionHandler + * @return \JPT\SocketFramework\Connection\ConnectionHandler */ public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) { $socket = $this->socketPool->createTcpSocket($IPv6); - $connectionHandler = new Connection_ConnectionHandler($socket, $this->nextID, $group, $protocol); + $connectionHandler = new \JPT\SocketFramework\Connection\ConnectionHandler($socket, $this->nextID, $group, $protocol); $connectionHandler->setIPv6($IPv6); $connectionHandler->injectConnectionPool($this); $this->addConnectionHandler($connectionHandler); @@ -58,6 +66,7 @@ class Connection_ConnectionPool { /** * Returns the array with the current connectionHandlers. + * * @return array */ public function getConnectionHandlers() { @@ -66,7 +75,8 @@ class Connection_ConnectionPool { /** * Adds a ConnectionHandler to the pool. - * @param Connection_ConnectionHandler $addConnectionHandler + * + * @param \JPT\SocketFramework\Connection\ConnectionHandler $addConnectionHandler * @return void */ public function addConnectionHandler($addConnectionHandler) { @@ -76,7 +86,8 @@ class Connection_ConnectionPool { /** * Removes a ConnectionHandler from the pool. - * @param Connection_ConnectionHandler $removeConnectionHandler + * + * @param \JPT\SocketFramework\Connection\ConnectionHandler $removeConnectionHandler * @return void */ public function removeConnectionHandler($removeConnectionHandler) { @@ -91,8 +102,9 @@ class Connection_ConnectionPool { /** * Returns ConnectionHandler for the given socket ressource. + * * @param ressource $socketRessource - * @return Connection_ConnectionHandler + * @return \JPT\SocketFramework\Connection\ConnectionHandler */ protected function getConnectionHandlerForSocketRessource($socketRessource) { foreach($this->connectionHandlers AS $connectionHandler) { @@ -105,9 +117,10 @@ class Connection_ConnectionPool { /** * Calls select() on SocketPool and updates the ConnectionHandler. * Will also accept incoming connections and add them to the pool. - * @return array An array containing the Connection_ConnectionHandler for each Socket with new data. - * @throws Exception_GeneralException - * @throws Exception_SocketException + * + * @return array An array containing the ConnectionHandler for each Socket with new data. + * @throws \JPT\SocketFramework\Exception\GeneralException + * @throws \JPT\SocketFramework\Exception\SocketException */ public function select() { $read = array(); @@ -135,7 +148,7 @@ class Connection_ConnectionPool { case "read": if($connectionHandler->isServer() === TRUE) { $acceptedSocket = $connectionHandler->accept(); - $acceptedSocketHandler = new Connection_ConnectionHandler($acceptedSocket, $this->nextID, $connectionHandler->getGroup(), $connectionHandler->getProtocol()); + $acceptedSocketHandler = new \JPT\SocketFramework\Connection\ConnectionHandler($acceptedSocket, $this->nextID, $connectionHandler->getGroup(), $connectionHandler->getProtocol()); $acceptedSocketHandler->hasBeenAccepted(); $this->addConnectionHandler($acceptedSocketHandler); } else { @@ -163,9 +176,10 @@ class Connection_ConnectionPool { /** * Writes the given data to all sockets in $group. + * * @param string $group * @param string $data - * @throws Exception_SocketException + * @throws \JPT\SocketFramework\Exception\SocketException * @return void */ public function writeToGroup($group, $data) { @@ -178,6 +192,7 @@ class Connection_ConnectionPool { /** * Writes the given data to the socket with $id. + * * @param int $id * @param string $data * @return void @@ -191,7 +206,7 @@ class Connection_ConnectionPool { } /** - * @see Socket_SocketPool + * @see \JPT\SocketFramework\Socket\SocketPool * @return int */ public function countSockets() { @@ -201,6 +216,7 @@ class Connection_ConnectionPool { /** * Returns the amount of active connections. * A connection is active when the handler is connected. + * * @return int */ public function countActiveConnections() { diff --git a/SocketFramework/Classes/Core/ClassLoader.php b/SocketFramework/Classes/Core/ClassLoader.php new file mode 100644 index 0000000..6134757 --- /dev/null +++ b/SocketFramework/Classes/Core/ClassLoader.php @@ -0,0 +1,44 @@ + \ No newline at end of file diff --git a/Classes/Exception/GeneralException.php b/SocketFramework/Classes/Exception/GeneralException.php similarity index 85% rename from Classes/Exception/GeneralException.php rename to SocketFramework/Classes/Exception/GeneralException.php index ed8c6cf..2eb26ed 100644 --- a/Classes/Exception/GeneralException.php +++ b/SocketFramework/Classes/Exception/GeneralException.php @@ -1,14 +1,18 @@ \ No newline at end of file diff --git a/SocketFramework/Classes/Exception/WrongDatatypeException.php b/SocketFramework/Classes/Exception/WrongDatatypeException.php new file mode 100644 index 0000000..56b0f6f --- /dev/null +++ b/SocketFramework/Classes/Exception/WrongDatatypeException.php @@ -0,0 +1,12 @@ + \ No newline at end of file diff --git a/Classes/Misc/Buffer.php b/SocketFramework/Classes/Misc/Buffer.php similarity index 87% rename from Classes/Misc/Buffer.php rename to SocketFramework/Classes/Misc/Buffer.php index 5875d21..26097c5 100644 --- a/Classes/Misc/Buffer.php +++ b/SocketFramework/Classes/Misc/Buffer.php @@ -1,27 +1,33 @@ buffer) < $length) throw new Exception_GeneralException("Cannot return ".$length." chars, there are only ".strlen($this->buffer)." chars left!", 1292780386); + if(strlen($this->buffer) < $length) throw new \JPT\SocketFramework\Exception\GeneralException("Cannot return ".$length." chars, there are only ".strlen($this->buffer)." chars left!", 1292780386); $data = substr($this->buffer, 0, $length); $this->buffer = substr($this->buffer, $length); return $data; @@ -106,16 +118,18 @@ class Misc_Buffer { /** * Checks whether the buffer contains more lines to process. + * * @return boolean * @throws Exception_GeneralException */ public function hasLines() { - if($this->linebreak === "") throw new Exception_GeneralException("Cannot tell whether the buffer has lines - no linebreak set!", 1290964243); + if($this->linebreak === "") throw new \JPT\SocketFramework\Exception\GeneralException("Cannot tell whether the buffer has lines - no linebreak set!", 1290964243); return (trim(strstr($this->buffer, $this->linebreak, TRUE)) !== "") ? TRUE : FALSE; } /** * Returns TRUE when there is data in the buffer. + * * @return boolean */ public function hasData() { @@ -125,6 +139,7 @@ class Misc_Buffer { /** * Returns the full buffer contents. * Also truncates the buffer. + * * @return string */ public function getAllBufferContents() { diff --git a/Classes/Socket/SocketHandler.php b/SocketFramework/Classes/Socket/SocketHandler.php similarity index 61% rename from Classes/Socket/SocketHandler.php rename to SocketFramework/Classes/Socket/SocketHandler.php index 6df559b..6d35dcb 100644 --- a/Classes/Socket/SocketHandler.php +++ b/SocketFramework/Classes/Socket/SocketHandler.php @@ -1,14 +1,18 @@ socket = $socket; $this->isBound = FALSE; $this->isConnected = FALSE; @@ -44,6 +49,7 @@ class Socket_SocketHandler { /** * Destructor. Closes socket. + * * @return void */ public function __destruct() { @@ -51,7 +57,8 @@ class Socket_SocketHandler { } /** - * Returns socket + * Returns socket. + * * @return ressource */ public function getSocket() { @@ -67,6 +74,7 @@ class Socket_SocketHandler { /** * Sets isConnected-flag. + * * @param boolean $connected * @return void */ @@ -83,13 +91,14 @@ class Socket_SocketHandler { /** * Connects to a specified address. - * @throws Exception_SocketException + * + * @throws \JPT\SocketFramework\Exception\SocketException * @param string $address * @param int $port * @return void */ public function connect($address, $port) { - if($this->isConnected === TRUE) throw new Exception_SocketException("Socket is already connected!", 1289663170); + if($this->isConnected === TRUE) throw new \JPT\SocketFramework\Exception\SocketException("Socket is already connected!", 1289663170); $result = socket_connect($this->socket, $address, $port); if($result === FALSE) $this->handleSocketError(); $this->isConnected = TRUE; @@ -97,14 +106,15 @@ class Socket_SocketHandler { /** * Binds the socket to an address + port. - * @throws Exception_SocketException + * + * @throws \JPT\SocketFramework\Exception\SocketException * @param string $address * @param int $port * @return void */ public function bind($address, $port) { - if($this->isBound === TRUE) throw new Exception_SocketException("Socket is already bound!", 1289663212); - $result = socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1); + if($this->isBound === TRUE) throw new \JPT\SocketFramework\Exception\SocketException("Socket is already bound!", 1289663212); + $result = socket_set_option($this->socket, \SOL_SOCKET, \SO_REUSEADDR, 1); if($result === FALSE) $this->handleSocketError(); $result = socket_bind($this->socket, $address, $port); if($result === FALSE) $this->handleSocketError(); @@ -113,11 +123,12 @@ class Socket_SocketHandler { /** * Let's the socket listen for incoming connections. - * @throws Exception_SocketException + * + * @throws \JPT\SocketFramework\Exception\SocketException * @return void */ public function listen() { - if($this->isBound === FALSE) throw new Exception_SocketException("Cannot listen on unbound socket!", 1289663220); + if($this->isBound === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("Cannot listen on unbound socket!", 1289663220); $result = socket_listen($this->socket); if($result === FALSE) $this->handleSocketError(); $this->isListening = TRUE; @@ -125,6 +136,7 @@ class Socket_SocketHandler { /** * Tells the SocketHandler that he got accepted. + * * @return void */ public function hasBeenAccepted() { @@ -132,14 +144,15 @@ class Socket_SocketHandler { } /** - * Accepts a connection to a socket that is bound and listens - * The ressource has to be added to the SocketPool manually - * @throws Exception_SocketException + * Accepts a connection to a socket that is bound and listens. + * The ressource has to be added to the SocketPool manually. + * + * @throws \JPT\SocketFramework\Exception\SocketException * @return ressource */ public function accept() { - if($this->isBound === FALSE) throw new Exception_SocketException("Cannot accept connections from unbound socket!", 1289663239); - if($this->isListening === FALSE) throw new Exception_SocketException("Cannot accept connections from socket that is not listening!", 1289663241); + if($this->isBound === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("Cannot accept connections from unbound socket!", 1289663239); + if($this->isListening === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("Cannot accept connections from socket that is not listening!", 1289663241); $accept = socket_accept($this->socket); if($accept === FALSE) $this->handleSocketError(); return $accept; @@ -147,11 +160,12 @@ class Socket_SocketHandler { /** * Returns ip + port of the remote socket. - * @throws Exception_SocketException + * + * @throws \JPT\SocketFramework\Exception\SocketException * @return string */ public function getRemoteName() { - if($this->isConnected === FALSE) throw new Exception_SocketException("Socket not connected, cannot retrieve remote name!", 1289928192); + if($this->isConnected === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("Socket not connected, cannot retrieve remote name!", 1289928192); $result = socket_getpeername($this->socket, $address, $port); if($result === FALSE) $this->handleSocketError(); return $address . ":" . $port; @@ -159,11 +173,12 @@ class Socket_SocketHandler { /** * Returns ip + port of the local socket. - * @throws Exception_SocketException + * + * @throws \JPT\SocketFramework\Exception\SocketException * @return string */ public function getLocalName() { - if($this->isBound === FALSE ^ $this->isConnected === FALSE) throw new Exception_SocketException("Socket is not bound or connected, no local name available!", 1289928256); + if($this->isBound === FALSE ^ $this->isConnected === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("Socket is not bound or connected, no local name available!", 1289928256); $result = socket_getsockname($this->socket, $address, $port); if($result === FALSE) $this->handleSocketError(); return $address . ":" . $port; @@ -171,7 +186,8 @@ class Socket_SocketHandler { /** * Writes data to the socket. - * @throws Exception_SocketException + * + * @throws \JPT\SocketFramework\Exception\SocketException * @param string $data * @return void */ @@ -182,23 +198,25 @@ class Socket_SocketHandler { /** * Reads data from the socket. - * @throws Exception_SocketException + * + * @throws \JPT\SocketFramework\Exception\SocketException * @param int $length * @return string */ public function read($length = 16384) { - $result = socket_read($this->socket, $length, PHP_BINARY_READ); + $result = socket_read($this->socket, $length, \PHP_BINARY_READ); if($result === FALSE) $this->handleSocketError(); return $result; } /** * Shuts the socket down after waiting a bit (waiting might be optional). - * @throws Exception_SocketException + * + * @throws \JPT\SocketFramework\Exception\SocketException * @return void */ public function close() { - usleep(100000); + usleep(100000); //we'll wait here since socket_shutdown _might_ fail without it. if($this->isConnected === TRUE) { $result = socket_shutdown($this->socket); if($result === FALSE) $this->handleSocketError(); @@ -212,16 +230,17 @@ class Socket_SocketHandler { /** * Gets last error from socket. - * @throws Exception_SocketException + * + * @throws \JPT\SocketFramework\Exception\SocketException * @return void */ protected function handleSocketError() { - if(is_resource($this->socket) === FALSE) throw new Exception_SocketException("No socket resource available!", 1290954177); + if(is_resource($this->socket) === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("No socket resource available!", 1290954177); $errno = socket_last_error($this->socket); $error = socket_strerror($errno); socket_clear_error(); $errormsg = "[" . $errno . "] " . $error; - throw new Exception_SocketException("A socket error occured: " . $errormsg, 1289663360); + throw new \JPT\SocketFramework\Exception\SocketException("A socket error occured: " . $errormsg, 1289663360); } } ?> \ No newline at end of file diff --git a/Classes/Socket/SocketPool.php b/SocketFramework/Classes/Socket/SocketPool.php similarity index 72% rename from Classes/Socket/SocketPool.php rename to SocketFramework/Classes/Socket/SocketPool.php index 80b2a1f..fd18d69 100644 --- a/Classes/Socket/SocketPool.php +++ b/SocketFramework/Classes/Socket/SocketPool.php @@ -1,20 +1,25 @@ addSocket($socket); return $socket; } /** * Returns an array of sockets one can read from. + * * @param array $read * @param array $write * @param array $except * @param int $timeout - * @throws Exception_SocketException + * @throws \JPT\SocketFramework\Exception\SocketException * @return array */ public function select($read, $write, $except, $timeout = NULL) { $n = socket_select($read, $write, $except, $timeout); - if($n === FALSE) throw new Exception_SocketException("socket_select() failed! - Error: " . socket_strerror(socket_last_error()), 1290273693); + if($n === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("socket_select() failed! - Error: " . socket_strerror(socket_last_error()), 1290273693); if($n === 0) return array("read" => array(), "write" => array(), "except" => array()); return array("read" => $read, "write" => $write, "except" => $except); } diff --git a/Testcode/Client/ClientManagerTest.php b/Testcode/Client/ClientManagerTest.php index c80bb64..fd53484 100644 --- a/Testcode/Client/ClientManagerTest.php +++ b/Testcode/Client/ClientManagerTest.php @@ -1,5 +1,5 @@ registerProtocol("irc", "Irc"); $clientManager->registerProtocol("jpt", "Bot");