From: Roland Häder Date: Mon, 29 May 2017 19:22:54 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=cf39be5d5645b195e967ccf8bb16f2da5a5ff37a;p=hub.git Continued: - method renamed to getConnectionClassNameFromSocket() which now only accepts a StorableSocket class - resolveStateByPackage() now accepts StorableSocket instead of socket resource - also extend HubInterface - introduced BaseHubHandler to implement HubInterface (or what is missing + protected setter/getter) - introduced BaseHubDatabaseWrapper to implement HubInterface - more debug messages - other stuff improved - imported (Base|Peer)StateResolver - imported StorableSocket - imported (Lookupable)PeerState - imported PeerStateable - created new namespaces - moved classes around Signed-off-by: Roland Häder --- diff --git a/application/hub/classes/class_BaseHubSystem.php b/application/hub/classes/class_BaseHubSystem.php index dbad5a778..dff2ac434 100644 --- a/application/hub/classes/class_BaseHubSystem.php +++ b/application/hub/classes/class_BaseHubSystem.php @@ -9,6 +9,7 @@ use Hub\Handler\Network\RawData\BaseRawDataHandler; use Hub\Information\ShareableInfo; use Hub\Listener\BaseListener; use Hub\Listener\Listenable; +use Hub\Locator\Node\LocateableNode; use Hub\Network\Deliver\Deliverable; use Hub\Network\Package\Assembler\Assembler; use Hub\Network\Package\Fragment\Fragmentable; @@ -114,6 +115,11 @@ class BaseHubSystem extends BaseFrameworkSystem implements HubInterface { */ private $socketInstance = NULL; + /** + * An instance of a LocateableNode class + */ + private $universalNodeLocatorInstance = NULL; + /** * Name of used protocol */ @@ -416,4 +422,25 @@ class BaseHubSystem extends BaseFrameworkSystem implements HubInterface { return $this->socketInstance; } + /** + * Setter for UNL instance + * + * @para $unlInstance An instance of a LocateableNode class + * @return void + */ + protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { + // Set new UNL data array + $this->universalNodeLocatorInstance = $unlInstance; + } + + /** + * Getter for UNL instance + * + * @return $unlData An instance of a LocateableNode class + */ + public final function getUniversalNodeLocatorInstance () { + // Return UNL data array + return $this->universalNodeLocatorInstance; + } + } diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index c4d88636c..52ef686dc 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -465,6 +465,9 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * @throws InvalidSocketException If stored socket is invalid */ public function connectToSocketRecipient () { + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol()))); + // Should be valid socket if (!$this->isValidSocket()) { // Throw exception @@ -480,6 +483,9 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable // Try to connect to it $result = socket_connect($this->getSocketResource(), $unlInstance->getUnlAddress(), $unlInstance->getUnlPort()); + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result))); + // Return result return $result; } @@ -489,7 +495,7 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable * * @return void * @throws SocketShutdownException If the current socket could not be shut down - * @throws BaseMethodCallException If this method is possibly called twice + * @throws BadMethodCallException If this method is possibly called twice * @todo We may want to implement a filter for ease notification of other objects like our pool * @todo rewrite this! */ @@ -497,7 +503,7 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable // Should be valid socket if (!$this->isValidSocket()) { // Throw exception - throw new BaseMethodCallException(sprintf('[%s:%d]: Shutdown on invalid socket. Maybe called already?', __METHOD__, __LINE__), self::EXCEPTION_INVALID_SOCKET); + throw new BadMethodCallException(sprintf('[%s:%d]: Shutdown on invalid socket. Maybe called already?', __METHOD__, __LINE__), self::EXCEPTION_INVALID_SOCKET); } // END - if // Debug message diff --git a/application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php b/application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php new file mode 100644 index 000000000..de83717bf --- /dev/null +++ b/application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php @@ -0,0 +1,229 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2011 - 2014 Cruncher Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class BaseHubDatabaseWrapper extends BaseDatabaseWrapper implements HubInterface { + + /** + * Listener pool instance + */ + private $listenerPoolInstance = NULL; + + /** + * Info instance + */ + private $infoInstance = NULL; + + /** + * A StorableSocket instance + */ + private $socketInstance = NULL; + + /** + * An instance of a LocateableNode class + */ + private $universalNodeLocatorInstance = NULL; + + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + } + + /** + * Checks whether start/end marker are set + * + * @param $data Data to be checked + * @return $isset Whether start/end marker are set + */ + public final function ifStartEndMarkersSet ($data) { + // Determine it + $isset = ((substr($data, 0, strlen(BaseRawDataHandler::STREAM_START_MARKER)) == BaseRawDataHandler::STREAM_START_MARKER) && (substr($data, -1 * strlen(BaseRawDataHandler::STREAM_END_MARKER), strlen(BaseRawDataHandler::STREAM_END_MARKER)) == BaseRawDataHandler::STREAM_END_MARKER)); + + // ... and return it + return $isset; + } + + /** + * Setter for listener pool instance + * + * @param $listenerPoolInstance The new listener pool instance + * @return void + */ + protected final function setListenerPoolInstance (Poolable $listenerPoolInstance) { + $this->listenerPoolInstance = $listenerPoolInstance; + } + + /** + * Getter for listener pool instance + * + * @return $listenerPoolInstance Our current listener pool instance + */ + public final function getListenerPoolInstance () { + return $this->listenerPoolInstance; + } + + /** + * Setter for info instance + * + * @param $infoInstance A ShareableInfo instance + * @return void + */ + protected final function setInfoInstance (ShareableInfo $infoInstance) { + $this->infoInstance = $infoInstance; + } + + /** + * Getter for info instance + * + * @return $infoInstance An instance of a ShareableInfo class + */ + public final function getInfoInstance () { + return $this->infoInstance; + } + + /** + * Setter for socket instance + * + * @param $socketInstance A StorableSocket instance + * @return void + */ + public final function setSocketInstance (StorableSocket $socketInstance) { + $this->socketInstance = $socketInstance; + } + + /** + * Getter for socket instance + * + * @return $socketInstance An instance of a StorableSocket class + */ + public final function getSocketInstance () { + return $this->socketInstance; + } + + /** + * Setter for UNL instance + * + * @para $unlInstance An instance of a LocateableNode class + * @return void + */ + protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { + // Set new UNL data array + $this->universalNodeLocatorInstance = $unlInstance; + } + + /** + * Getter for UNL instance + * + * @return $unlData An instance of a LocateableNode class + */ + public final function getUniversalNodeLocatorInstance () { + // Return UNL data array + return $this->universalNodeLocatorInstance; + } + + /** + * Setter for node id + * + * @param $nodeId The new node id + * @return void + */ + protected final function setNodeId ($nodeId) { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for node id + * + * @return $nodeId Current node id + */ + public final function getNodeId () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Setter for private key + * + * @param $privateKey The new private key + * @return void + */ + protected final function setPrivateKey ($privateKey) { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for private key + * + * @return $privateKey Current private key + */ + public final function getPrivateKey () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Setter for private key hash + * + * @param $privateKeyHash The new private key hash + * @return void + */ + protected final function setPrivateKeyHash ($privateKeyHash) { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for private key hash + * + * @return $privateKeyHash Current private key hash + */ + public final function getPrivateKeyHash () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for session id + * + * @return $sessionId Current session id + */ + public final function getSessionId () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + +} diff --git a/application/hub/classes/database/frontend/cruncher/class_CruncherUnitDatabaseWrapper.php b/application/hub/classes/database/frontend/cruncher/class_CruncherUnitDatabaseWrapper.php index ce93c37f9..f48f6e4a2 100644 --- a/application/hub/classes/database/frontend/cruncher/class_CruncherUnitDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/cruncher/class_CruncherUnitDatabaseWrapper.php @@ -2,8 +2,10 @@ // Own namespace namespace Hub\Database\Frontend\Cruncher\Unit; +// Import application-specific stuff +use Hub\Database\Frontend\BaseHubDatabaseWrapper; + // Import framework stuff -use CoreFramework\Database\Frontend\BaseDatabaseWrapper; use CoreFramework\Factory\ObjectFactory; use CoreFramework\Registry\Registerable; @@ -29,7 +31,7 @@ use CoreFramework\Registry\Registerable; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class CruncherUnitDatabaseWrapper extends BaseDatabaseWrapper implements UnitDatabaseWrapper, Registerable { +class CruncherUnitDatabaseWrapper extends BaseHubDatabaseWrapper implements UnitDatabaseWrapper, Registerable { // Constants for database table names const DB_TABLE_CRUNCHER_UNITS = 'cruncher_units'; diff --git a/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php b/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php index b8c64aa69..a4a8ce8d7 100644 --- a/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php @@ -3,6 +3,7 @@ namespace Hub\Database\Frontend\Node\Dht; // Import application-specific stuff +use Hub\Database\Frontend\BaseHubDatabaseWrapper; use Hub\Database\Frontend\Node\Information\NodeInformationDatabaseWrapper; use Hub\Factory\Node\NodeObjectFactory; use Hub\Network\Package\NetworkPackage; @@ -12,7 +13,6 @@ use Hub\Node\BaseHubNode; use CoreFramework\Bootstrap\FrameworkBootstrap; use CoreFramework\Criteria\Local\LocalSearchCriteria; use CoreFramework\Criteria\Storing\StoreableCriteria; -use CoreFramework\Database\Frontend\BaseDatabaseWrapper; use CoreFramework\Factory\ObjectFactory; use CoreFramework\Handler\DataSet\HandleableDataSet; use CoreFramework\Registry\Registerable; @@ -39,7 +39,7 @@ use CoreFramework\Registry\Registerable; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implements NodeDhtWrapper, Registerable { +class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper implements NodeDhtWrapper, Registerable { /** * "Cached" results for dabase for looking for unpublished entries */ diff --git a/application/hub/classes/database/frontend/node/class_NodeInformationDatabaseWrapper.php b/application/hub/classes/database/frontend/node/class_NodeInformationDatabaseWrapper.php index 92fb1b5d3..a7e40a57c 100644 --- a/application/hub/classes/database/frontend/node/class_NodeInformationDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/node/class_NodeInformationDatabaseWrapper.php @@ -3,6 +3,7 @@ namespace Hub\Database\Frontend\Node\Information; // Import application-specific stuff +use Hub\Database\Frontend\BaseHubDatabaseWrapper; use Hub\Database\Frontend\Node\NodeInformationWrapper; use Hub\Node\BaseHubNode; use Hub\Helper\Node\NodeHelper; @@ -10,7 +11,6 @@ use Hub\Helper\Node\NodeHelper; // Import framework stuff use CoreFramework\Bootstrap\FrameworkBootstrap; use CoreFramework\Criteria\Local\LocalSearchCriteria; -use CoreFramework\Database\Frontend\BaseDatabaseWrapper; use CoreFramework\Factory\ObjectFactory; use CoreFramework\Registry\Registerable; use CoreFramework\Request\Requestable; @@ -37,7 +37,7 @@ use CoreFramework\Request\Requestable; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class NodeInformationDatabaseWrapper extends BaseDatabaseWrapper implements NodeInformationWrapper, Registerable { +class NodeInformationDatabaseWrapper extends BaseHubDatabaseWrapper implements NodeInformationWrapper, Registerable { // Constants for database table names const DB_TABLE_NODE_INFORMATION = 'node_data'; diff --git a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php index 594416bc4..1f305f780 100644 --- a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php @@ -3,14 +3,18 @@ namespace Hub\Database\Frontend\Node\PeerState; // Import application-specific sutff +use Hub\Container\Socket\StorableSocket; +use Hub\Database\Frontend\BaseHubDatabaseWrapper; use Hub\Helper\Connection\ConnectionHelper; use Hub\Network\Package\NetworkPackage; +use Hub\LookupTable\Lookupable; +use Hub\State\Peer\Lookup\LookupablePeerState; +use Hub\State\Peer\PeerStateable; use Hub\Tools\HubTools; // Import framework stuff use CoreFramework\Criteria\Local\LocalSearchCriteria; use CoreFramework\Criteria\Storing\StoreableCriteria; -use CoreFramework\Database\Frontend\BaseDatabaseWrapper; use CoreFramework\Factory\ObjectFactory; /** @@ -35,7 +39,7 @@ use CoreFramework\Factory\ObjectFactory; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements LookupablePeerState { +class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements LookupablePeerState { // Exception constants const EXCEPTION_PEER_ALREADY_REGISTERED = 0x300; @@ -137,11 +141,11 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements Look * Registers a new peer with given package data. We use the session id from it. * * @param $packageData Raw package data - * @param $socketResource A valid socket resource + * @param $socketResource An instance of a StorableSocket class * @return void * @throws PeerAlreadyRegisteredException If a peer is already registered */ - public function registerPeerByPackageData (array $packageData, $socketResource) { + public function registerPeerByPackageData (array $packageData, StorableSocket $socketInstance) { // Make sure only new peers can be registered with package data if (!$this->isSenderNewPeer($packageData)) { // Throw an exception because this should normally not happen @@ -162,9 +166,9 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements Look $peerPort = '0'; // Get peer name - if (!@socket_getpeername($socketResource, $peerAddress, $peerPort)) { + if (!@socket_getpeername($socketInstance, $peerAddress, $peerPort)) { // Get last error - $lastError = socket_last_error($socketResource); + $lastError = socket_last_error($socketInstance); // ... and cleartext message from it and put both into criteria $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_CODE, $lastError); @@ -228,19 +232,19 @@ class PeerStateLookupDatabaseWrapper extends BaseDatabaseWrapper implements Look /** * Purges old entries of given socket resource. We use the IP address from that resource. * - * @param $socketResource A valid socket resource + * @param $socketInstance An instance of a StorableSocket class * @return void * @throws InvalidSocketException If the socket resource was invalid * @todo Unfinished area, please rewrite! */ - public function purgeOldEntriesBySocketResource ($socketResource) { + public function purgeOldEntriesBysocketInstance (StorableSocket $socketInstance) { // Get peer name - if (!@socket_getpeername($socketResource, $peerAddress, $peerPort)) { + if (!@socket_getpeername($socketInstance, $peerAddress, $peerPort)) { // Get last error - $lastError = socket_last_error($socketResource); + $lastError = socket_last_error($socketInstance); // Doesn't work! - throw new InvalidSocketException(array($this, $socketResource, $lastError, socket_strerror($lastError)), self::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($this, $socketInstance, $lastError, socket_strerror($lastError)), self::EXCEPTION_INVALID_SOCKET); } // END - if // Debug message diff --git a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php index dc1205db1..6f80c04f5 100644 --- a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php +++ b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php @@ -13,6 +13,7 @@ use Hub\Generic\BaseHubSystem; use Hub\Handler\Protocol\HandleableProtocol; use Hub\Listener\Listenable; use Hub\Network\Package\NetworkPackage; +use Hub\Resolver\State\Peer\PeerStateResolver; // Import framework stuff use CoreFramework\Generic\NullPointerException; diff --git a/application/hub/classes/handler/answer-status/.htaccess b/application/hub/classes/handler/answer-status/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/answer-status/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/answer-status/announcement/.htaccess b/application/hub/classes/handler/answer-status/announcement/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/answer-status/announcement/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/answer-status/announcement/class_NodeAnnouncementAnswerOkayHandler.php b/application/hub/classes/handler/answer-status/announcement/class_NodeAnnouncementAnswerOkayHandler.php deleted file mode 100644 index fae30321d..000000000 --- a/application/hub/classes/handler/answer-status/announcement/class_NodeAnnouncementAnswerOkayHandler.php +++ /dev/null @@ -1,148 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class NodeAnnouncementAnswerOkayHandler extends BaseAnserStatusHandler implements HandleableAnswerStatus, Registerable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Init array - $this->searchData = array( - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS, - ); - - // Set handler name - $this->setHandlerName('announcement_answer_okay'); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a HandleableMessage class - */ - public final static function createNodeAnnouncementAnswerOkayHandler () { - // Get new instance - $handlerInstance = new NodeAnnouncementAnswerOkayHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Handles given message data array - * - * @param $messageData An array of message data - * @param $packageInstance An instance of a Receivable class - * @return void - * @todo Do some more here: Handle karma, et cetera? - */ - public function handleAnswerMessageData (array $messageData, Receivable $packageInstance) { - /* - * Query DHT and force update (which will throw an exception if the - * node is not found). - */ - DhtObjectFactory::createDhtInstance('node')->registerNodeByMessageData($messageData, $this, TRUE); - - // Get handler instance - $handlerInstance = Registry::getRegistry()->getInstance('task_handler'); - - // Generate DHT bootstrap task - $taskInstance = ObjectFactory::createObjectByConfiguredName('node_dht_late_bootstrap_task_class'); - - // Register it as well - $handlerInstance->registerTask('dht_late_bootstrap', $taskInstance); - - // Get the node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - - // Change state - $nodeInstance->getStateInstance()->nodeAnnouncementSuccessful(); - - // Prepare next message - $this->prepareNextMessage($messageData, $packageInstance); - } - - /** - * Initializes configuration data from given message data array - * - * The following array is being handled over: - * - * my-external-address => 1.2.3.4 - * my-internal-address => 5.6.7.8 - * my-status => reachable - * my-node-id => aaabbbcccdddeeefff123456789 - * my-session-id => aaabbbcccdddeeefff123456789 - * my-tcp-port => 9060 - * my-udp-port => 9060 - * answer-status => OKAY - * message_type => announcement_answer - * - * @param $messageData An array with all message data - * @return void - */ - protected function initMessageConfigurationData (array $messageData) { - // Get node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - - // Get an array of all accepted object types - $objectList = $nodeInstance->getListFromAcceptedObjectTypes(); - - // Add missing (temporary) configuration 'accepted_object_types' - $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPTED_OBJECTS, implode(BaseHubNode::OBJECT_LIST_SEPARATOR, $objectList)); - } - - /** - * Removes configuration data with given message data array from global - * configuration. For content of $messageData see method comment above. - * - * @param $messageData An array with all message data - * @return void - */ - protected function removeMessageConfigurationData (array $messageData) { - // Remove temporay configuration - $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPTED_OBJECTS); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/answer-status/class_ b/application/hub/classes/handler/answer-status/class_ deleted file mode 100644 index b792e7b53..000000000 --- a/application/hub/classes/handler/answer-status/class_ +++ /dev/null @@ -1,63 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class ???Handler extends BaseAnserStatusHandler implements HandleableAnswerStatus, Registerable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('|||_answer_==='); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a HandleableMessage class - */ - public final static function create???Handler () { - // Get new instance - $handlerInstance = new ???Handler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Handles given message data array - * - * @param $messageData An array of message data - * @return void - */ - public function handleAnswerMessageData (array $messageData) { - $this->partialStub('Please implement this method.'); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/answer-status/class_BaseAnserStatusHandler.php b/application/hub/classes/handler/answer-status/class_BaseAnserStatusHandler.php deleted file mode 100644 index 540bb1c45..000000000 --- a/application/hub/classes/handler/answer-status/class_BaseAnserStatusHandler.php +++ /dev/null @@ -1,49 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -abstract class BaseAnserStatusHandler extends BaseDataHandler { - /** - * Protected constructor - * - * @param $className Name of the class - * @return void - */ - protected function __construct ($className) { - // Call parent constructor - parent::__construct($className); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/answer-status/requests/.htaccess b/application/hub/classes/handler/answer-status/requests/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/answer-status/requests/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/answer-status/requests/class_RequestNodeListAnswerOkayHandler.php b/application/hub/classes/handler/answer-status/requests/class_RequestNodeListAnswerOkayHandler.php deleted file mode 100644 index f3c4a47a3..000000000 --- a/application/hub/classes/handler/answer-status/requests/class_RequestNodeListAnswerOkayHandler.php +++ /dev/null @@ -1,133 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class RequestNodeListAnswerOkayHandler extends BaseAnserStatusHandler implements HandleableAnswerStatus, Registerable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Init array - $this->searchData = array( - XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_SESSION_ID, - ); - - // Set handler name - $this->setHandlerName('request_node_list_answer_okay'); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a HandleableMessage class - */ - public final static function createRequestNodeListAnswerOkayHandler () { - // Get new instance - $handlerInstance = new RequestNodeListAnswerOkayHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Handles given message data array - * - * @param $messageData An array of message data - * @param $packageInstance An instance of a Receivable class - * @return void - * @throws NodeSessionIdVerficationException If the provided session id is not matching - * @todo Do some more here: Handle karma, et cetera? - */ - public function handleAnswerMessageData (array $messageData, Receivable $packageInstance) { - // Make sure node-list is found in array - assert(isset($messageData[XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_NODE_LIST])); - - // Save node list - $nodeList = json_decode(base64_decode($messageData[XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_NODE_LIST])); - - // Make sure it is completely decoded - assert(is_array($nodeList)); - - // ... and remove it as it should not be included now - unset($messageData[XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_NODE_LIST]); - - // Write node list to DHT - DhtObjectFactory::createDhtInstance('node')->insertNodeList($nodeList); - - /* - * Query DHT and force update (which will throw an exception if the - * node is not found). - */ - DhtObjectFactory::createDhtInstance('node')->registerNodeByMessageData($messageData, $this, TRUE); - - // Prepare next message ("hello" message to all returned nodes) - //$this->prepareNextMessage($messageData, $packageInstance); - } - - /** - * Initializes configuration data from given message data array - * - * The following array is being handled over: - * - * session-id => aaabbbcccdddeeefff123456789 - * node-list => aabb:ccdd:eeff - * answer-status => OKAY - * message_type => request_node_list_answer - * - * @param $messageData An array with all message data - * @return void - * @todo 0% done - */ - protected function initMessageConfigurationData (array $messageData) { - $this->partialStub('Please implement this method.'); - } - - /** - * Removes configuration data with given message data array from global - * configuration. For content of $messageData see method comment above. - * - * @param $messageData An array with all message data - * @return void - * @todo 0% done - */ - protected function removeMessageConfigurationData (array $messageData) { - $this->partialStub('Please implement this method.'); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/chunks/class_ChunkHandler.php b/application/hub/classes/handler/chunks/class_ChunkHandler.php index f9469078a..e83314387 100644 --- a/application/hub/classes/handler/chunks/class_ChunkHandler.php +++ b/application/hub/classes/handler/chunks/class_ChunkHandler.php @@ -4,11 +4,11 @@ namespace Hub\Handler\Network\RawData\Chunks; // Import application-specific stuff use Hub\Factory\Fragmenter\FragmenterFactory; +use Hub\Handler\BaseHubHandler; use Hub\Network\Package\Fragment\PackageFragmenter; // Import framework stuff use CoreFramework\Factory\ObjectFactory; -use CoreFramework\Handler\BaseHandler; use CoreFramework\Registry\Registerable; /** @@ -33,7 +33,7 @@ use CoreFramework\Registry\Registerable; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class ChunkHandler extends BaseHandler implements HandleableChunks, Registerable { +class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registerable { /** * Stacker for chunks with final EOP */ diff --git a/application/hub/classes/handler/class_BaseHubHandler.php b/application/hub/classes/handler/class_BaseHubHandler.php new file mode 100644 index 000000000..a0d486109 --- /dev/null +++ b/application/hub/classes/handler/class_BaseHubHandler.php @@ -0,0 +1,229 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class BaseHubHandler extends BaseHandler implements Handleable, HubInterface { + /** + * Listener pool instance + */ + private $listenerPoolInstance = NULL; + + /** + * Info instance + */ + private $infoInstance = NULL; + + /** + * A StorableSocket instance + */ + private $socketInstance = NULL; + + /** + * An instance of a LocateableNode class + */ + private $universalNodeLocatorInstance = NULL; + + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + } + + /** + * Checks whether start/end marker are set + * + * @param $data Data to be checked + * @return $isset Whether start/end marker are set + */ + public final function ifStartEndMarkersSet ($data) { + // Determine it + $isset = ((substr($data, 0, strlen(BaseRawDataHandler::STREAM_START_MARKER)) == BaseRawDataHandler::STREAM_START_MARKER) && (substr($data, -1 * strlen(BaseRawDataHandler::STREAM_END_MARKER), strlen(BaseRawDataHandler::STREAM_END_MARKER)) == BaseRawDataHandler::STREAM_END_MARKER)); + + // ... and return it + return $isset; + } + + /** + * Setter for listener pool instance + * + * @param $listenerPoolInstance The new listener pool instance + * @return void + */ + protected final function setListenerPoolInstance (Poolable $listenerPoolInstance) { + $this->listenerPoolInstance = $listenerPoolInstance; + } + + /** + * Getter for listener pool instance + * + * @return $listenerPoolInstance Our current listener pool instance + */ + public final function getListenerPoolInstance () { + return $this->listenerPoolInstance; + } + + /** + * Setter for info instance + * + * @param $infoInstance A ShareableInfo instance + * @return void + */ + protected final function setInfoInstance (ShareableInfo $infoInstance) { + $this->infoInstance = $infoInstance; + } + + /** + * Getter for info instance + * + * @return $infoInstance An instance of a ShareableInfo class + */ + public final function getInfoInstance () { + return $this->infoInstance; + } + + /** + * Setter for socket instance + * + * @param $socketInstance A StorableSocket instance + * @return void + */ + public final function setSocketInstance (StorableSocket $socketInstance) { + $this->socketInstance = $socketInstance; + } + + /** + * Getter for socket instance + * + * @return $socketInstance An instance of a StorableSocket class + */ + public final function getSocketInstance () { + return $this->socketInstance; + } + + /** + * Setter for UNL instance + * + * @para $unlInstance An instance of a LocateableNode class + * @return void + */ + protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { + // Set new UNL data array + $this->universalNodeLocatorInstance = $unlInstance; + } + + /** + * Getter for UNL instance + * + * @return $unlData An instance of a LocateableNode class + */ + public final function getUniversalNodeLocatorInstance () { + // Return UNL data array + return $this->universalNodeLocatorInstance; + } + + /** + * Setter for node id + * + * @param $nodeId The new node id + * @return void + */ + protected final function setNodeId ($nodeId) { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for node id + * + * @return $nodeId Current node id + */ + public final function getNodeId () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Setter for private key + * + * @param $privateKey The new private key + * @return void + */ + protected final function setPrivateKey ($privateKey) { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for private key + * + * @return $privateKey Current private key + */ + public final function getPrivateKey () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Setter for private key hash + * + * @param $privateKeyHash The new private key hash + * @return void + */ + protected final function setPrivateKeyHash ($privateKeyHash) { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for private key hash + * + * @return $privateKeyHash Current private key hash + */ + public final function getPrivateKeyHash () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Getter for session id + * + * @return $sessionId Current session id + */ + public final function getSessionId () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + +} diff --git a/application/hub/classes/handler/data/.htaccess b/application/hub/classes/handler/data/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/data/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/data/answer-status/.htaccess b/application/hub/classes/handler/data/answer-status/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/data/answer-status/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/data/answer-status/announcement/.htaccess b/application/hub/classes/handler/data/answer-status/announcement/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/data/answer-status/announcement/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/data/answer-status/announcement/class_NodeAnnouncementAnswerOkayHandler.php b/application/hub/classes/handler/data/answer-status/announcement/class_NodeAnnouncementAnswerOkayHandler.php new file mode 100644 index 000000000..fae30321d --- /dev/null +++ b/application/hub/classes/handler/data/answer-status/announcement/class_NodeAnnouncementAnswerOkayHandler.php @@ -0,0 +1,148 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class NodeAnnouncementAnswerOkayHandler extends BaseAnserStatusHandler implements HandleableAnswerStatus, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Init array + $this->searchData = array( + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS, + ); + + // Set handler name + $this->setHandlerName('announcement_answer_okay'); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function createNodeAnnouncementAnswerOkayHandler () { + // Get new instance + $handlerInstance = new NodeAnnouncementAnswerOkayHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles given message data array + * + * @param $messageData An array of message data + * @param $packageInstance An instance of a Receivable class + * @return void + * @todo Do some more here: Handle karma, et cetera? + */ + public function handleAnswerMessageData (array $messageData, Receivable $packageInstance) { + /* + * Query DHT and force update (which will throw an exception if the + * node is not found). + */ + DhtObjectFactory::createDhtInstance('node')->registerNodeByMessageData($messageData, $this, TRUE); + + // Get handler instance + $handlerInstance = Registry::getRegistry()->getInstance('task_handler'); + + // Generate DHT bootstrap task + $taskInstance = ObjectFactory::createObjectByConfiguredName('node_dht_late_bootstrap_task_class'); + + // Register it as well + $handlerInstance->registerTask('dht_late_bootstrap', $taskInstance); + + // Get the node instance + $nodeInstance = NodeObjectFactory::createNodeInstance(); + + // Change state + $nodeInstance->getStateInstance()->nodeAnnouncementSuccessful(); + + // Prepare next message + $this->prepareNextMessage($messageData, $packageInstance); + } + + /** + * Initializes configuration data from given message data array + * + * The following array is being handled over: + * + * my-external-address => 1.2.3.4 + * my-internal-address => 5.6.7.8 + * my-status => reachable + * my-node-id => aaabbbcccdddeeefff123456789 + * my-session-id => aaabbbcccdddeeefff123456789 + * my-tcp-port => 9060 + * my-udp-port => 9060 + * answer-status => OKAY + * message_type => announcement_answer + * + * @param $messageData An array with all message data + * @return void + */ + protected function initMessageConfigurationData (array $messageData) { + // Get node instance + $nodeInstance = NodeObjectFactory::createNodeInstance(); + + // Get an array of all accepted object types + $objectList = $nodeInstance->getListFromAcceptedObjectTypes(); + + // Add missing (temporary) configuration 'accepted_object_types' + $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPTED_OBJECTS, implode(BaseHubNode::OBJECT_LIST_SEPARATOR, $objectList)); + } + + /** + * Removes configuration data with given message data array from global + * configuration. For content of $messageData see method comment above. + * + * @param $messageData An array with all message data + * @return void + */ + protected function removeMessageConfigurationData (array $messageData) { + // Remove temporay configuration + $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPTED_OBJECTS); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/data/answer-status/class_ b/application/hub/classes/handler/data/answer-status/class_ new file mode 100644 index 000000000..b792e7b53 --- /dev/null +++ b/application/hub/classes/handler/data/answer-status/class_ @@ -0,0 +1,63 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class ???Handler extends BaseAnserStatusHandler implements HandleableAnswerStatus, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('|||_answer_==='); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function create???Handler () { + // Get new instance + $handlerInstance = new ???Handler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles given message data array + * + * @param $messageData An array of message data + * @return void + */ + public function handleAnswerMessageData (array $messageData) { + $this->partialStub('Please implement this method.'); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/data/answer-status/class_BaseAnserStatusHandler.php b/application/hub/classes/handler/data/answer-status/class_BaseAnserStatusHandler.php new file mode 100644 index 000000000..540bb1c45 --- /dev/null +++ b/application/hub/classes/handler/data/answer-status/class_BaseAnserStatusHandler.php @@ -0,0 +1,49 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +abstract class BaseAnserStatusHandler extends BaseDataHandler { + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/data/answer-status/requests/.htaccess b/application/hub/classes/handler/data/answer-status/requests/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/data/answer-status/requests/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/data/answer-status/requests/class_RequestNodeListAnswerOkayHandler.php b/application/hub/classes/handler/data/answer-status/requests/class_RequestNodeListAnswerOkayHandler.php new file mode 100644 index 000000000..f3c4a47a3 --- /dev/null +++ b/application/hub/classes/handler/data/answer-status/requests/class_RequestNodeListAnswerOkayHandler.php @@ -0,0 +1,133 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class RequestNodeListAnswerOkayHandler extends BaseAnserStatusHandler implements HandleableAnswerStatus, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Init array + $this->searchData = array( + XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_SESSION_ID, + ); + + // Set handler name + $this->setHandlerName('request_node_list_answer_okay'); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function createRequestNodeListAnswerOkayHandler () { + // Get new instance + $handlerInstance = new RequestNodeListAnswerOkayHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles given message data array + * + * @param $messageData An array of message data + * @param $packageInstance An instance of a Receivable class + * @return void + * @throws NodeSessionIdVerficationException If the provided session id is not matching + * @todo Do some more here: Handle karma, et cetera? + */ + public function handleAnswerMessageData (array $messageData, Receivable $packageInstance) { + // Make sure node-list is found in array + assert(isset($messageData[XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_NODE_LIST])); + + // Save node list + $nodeList = json_decode(base64_decode($messageData[XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_NODE_LIST])); + + // Make sure it is completely decoded + assert(is_array($nodeList)); + + // ... and remove it as it should not be included now + unset($messageData[XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_NODE_LIST]); + + // Write node list to DHT + DhtObjectFactory::createDhtInstance('node')->insertNodeList($nodeList); + + /* + * Query DHT and force update (which will throw an exception if the + * node is not found). + */ + DhtObjectFactory::createDhtInstance('node')->registerNodeByMessageData($messageData, $this, TRUE); + + // Prepare next message ("hello" message to all returned nodes) + //$this->prepareNextMessage($messageData, $packageInstance); + } + + /** + * Initializes configuration data from given message data array + * + * The following array is being handled over: + * + * session-id => aaabbbcccdddeeefff123456789 + * node-list => aabb:ccdd:eeff + * answer-status => OKAY + * message_type => request_node_list_answer + * + * @param $messageData An array with all message data + * @return void + * @todo 0% done + */ + protected function initMessageConfigurationData (array $messageData) { + $this->partialStub('Please implement this method.'); + } + + /** + * Removes configuration data with given message data array from global + * configuration. For content of $messageData see method comment above. + * + * @param $messageData An array with all message data + * @return void + * @todo 0% done + */ + protected function removeMessageConfigurationData (array $messageData) { + $this->partialStub('Please implement this method.'); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/data/class_ b/application/hub/classes/handler/data/class_ new file mode 100644 index 000000000..9a2846c67 --- /dev/null +++ b/application/hub/classes/handler/data/class_ @@ -0,0 +1,77 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class ???NetworkPackageHandler extends BaseNetworkPackageHandler implements Networkable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('!!!'); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a Networkable class + */ + public final static function create???NetworkPackageHandler () { + // Get new instance + $handlerInstance = new ???NetworkPackageHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Processes a package from given resource. This is mostly useful for TCP + * package handling and is implemented in the TcpListener class + * + * @param $resource A valid resource identifier + * @return void + * @throws InvalidResourceException If the given resource is invalid + * @todo 0% + */ + public function processResourcePackage ($resource) { + // Check the resource + if (!is_resource($resource)) { + // Throw an exception + throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); + } // END - if + + // Implement processing here + $this->partialStub('Please implement this method.'); + } + +} diff --git a/application/hub/classes/handler/data/class_BaseDataHandler.php b/application/hub/classes/handler/data/class_BaseDataHandler.php new file mode 100644 index 000000000..744a14c9c --- /dev/null +++ b/application/hub/classes/handler/data/class_BaseDataHandler.php @@ -0,0 +1,202 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +abstract class BaseDataHandler extends BaseHubHandler implements HubInterface { + /** + * Last exception instance from database layer or NULL (default) + */ + private $lastException = NULL; + + /** + * Array with search criteria elements + */ + protected $searchData = array(); + + /** + * Array with all data XML nodes (which hold the actual data) and their values + */ + protected $messageDataElements = array(); + + /** + * Array for translating message data elements (other node's data mostly) + * into configuration elements. + */ + protected $messageToConfig = array(); + + /** + * Array for copying configuration entries + */ + protected $configCopy = array(); + + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + } + + /** + * Getter for search data array + * + * @return $searchData Search data array + */ + public final function getSearchData () { + return $this->searchData; + } + + /** + * Getter for last exception + * + * @return $lastException Last thrown exception + */ + public final function getLastException () { + return $this->lastException; + } + + /** + * Setter for last exception + * + * @param $lastException Last thrown exception + * @return void + */ + public final function setLastException (FrameworkException $exceptionInstance = NULL) { + $this->lastException = $exceptionInstance; + } + + /** + * Prepares a message as answer for given message data for delivery. + * + * @param $messageData An array with all message data + * @param $packageInstance An instance of a Deliverable instance + * @return void + */ + protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) { + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER: Going to send an answer message for ' . $this->getHandlerName() . ' ...'); + + // Get a helper instance based on this handler's name + $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData)); + + // Get node instance + $nodeInstance = NodeObjectFactory::createNodeInstance(); + + // Load descriptor XML + $helperInstance->loadDescriptorXml($nodeInstance); + + /* + * Set missing (temporary) configuration data, mostly it needs to be + * copied from message data array. + */ + $this->initMessageConfigurationData($messageData); + + // Compile any configuration variables + $helperInstance->getTemplateInstance()->compileConfigInVariables(); + + // Deliver the package + $helperInstance->sendPackage($nodeInstance); + + /* + * Remove temporary configuration + */ + $this->removeMessageConfigurationData($messageData); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER: Answer message has been prepared.'); + } + + /** + * Prepares the next message + * + * @param $messageData An array with all message data + * @param $packageInstance An instance of a Deliverable instance + * @return void + */ + protected function prepareNextMessage (array $messageData, Deliverable $packageInstance) { + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER: Going to send next message ...'); + + // Get a helper instance based on this handler's name + $helperInstance = ObjectFactory::createObjectByConfiguredName('node_next_' . $this->getHandlerName() . '_helper_class', array($messageData)); + + // Get node instance + $nodeInstance = NodeObjectFactory::createNodeInstance(); + + // Load descriptor XML + $helperInstance->loadDescriptorXml($nodeInstance); + + /* + * Set missing (temporary) configuration data, mostly it needs to be + * copied from message data array. + */ + $this->initMessageConfigurationData($messageData); + + // Compile any configuration variables + $helperInstance->getTemplateInstance()->compileConfigInVariables(); + + // Deliver the package + $helperInstance->sendPackage($nodeInstance); + + /* + * Remove temporary configuration + */ + $this->removeMessageConfigurationData($messageData); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER: Next message has been prepared.'); + } + + /** + * Initializes configuration data from given message data array + * + * @param $messageData An array with all message data + * @return void + */ + abstract protected function initMessageConfigurationData (array $messageData); + + /** + * Removes configuration data with given message data array from global + * configuration + * + * @param $messageData An array with all message data + * @return void + */ + abstract protected function removeMessageConfigurationData (array $messageData); + +} diff --git a/application/hub/classes/handler/data/message-types/.htaccess b/application/hub/classes/handler/data/message-types/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/data/message-types/announcement/.htaccess b/application/hub/classes/handler/data/message-types/announcement/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/announcement/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/data/message-types/announcement/class_NodeMessageAnnouncementHandler.php b/application/hub/classes/handler/data/message-types/announcement/class_NodeMessageAnnouncementHandler.php new file mode 100644 index 000000000..1882fb2e3 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/announcement/class_NodeMessageAnnouncementHandler.php @@ -0,0 +1,209 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('message_announcement'); + + // Init message data array + $this->messageDataElements = array( + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS, + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS, + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS, + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_MODE, + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID, + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH, + ); + + // Init message-data->configuration translation array + $this->messageToConfig = array( + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address', + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address', + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID => 'your_node_id', + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id', + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash', + ); + + // Init config-copy array + $this->configCopy = array( + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address', + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address', + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS => 'node_status', + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'session_id', + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash', + ); + + // Init array + $this->searchData = array( + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS + ); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function createNodeMessageAnnouncementHandler () { + // Get new instance + $handlerInstance = new NodeMessageAnnouncementHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles data array of the message + * + * @param $messageData An array with message data to handle + * @param $packageInstance An instance of a Receivable class + * @return void + * @throws AnnouncementNotAcceptedException If this node does not accept announcements + */ + public function handleMessageData (array $messageData, Receivable $packageInstance) { + // Get node instance + $nodeInstance = NodeObjectFactory::createNodeInstance(); + + // Is this node accepting announcements? + if (!$nodeInstance->isAcceptingAnnouncements()) { + /* + * This node is not accepting announcements, then someone wants to + * announce his node to a non-bootstrap and non-master node. + */ + throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageData), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED); + } // END - if + + // Register the announcing node with this node + $this->registerNodeByMessageData($messageData); + + // Prepare answer message to be delivered back to the other node + $this->prepareAnswerMessage($messageData, $packageInstance); + } + + /** + * Adds all required elements from given array into data set instance + * + * @param $dataSetInstance An instance of a StoreableCriteria class + * @param $messageData An array with all message data + * @return void + */ + public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { + // Add generic first + parent::addArrayToDataSet($dataSetInstance, $messageData); + + // Add all ements + foreach ($this->messageDataElements as $key) { + // Is it there? + assert(isset($messageData[$key])); + + // Add it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...'); + $dataSetInstance->addCriteria($key, $messageData[$key]); + } // END - foreach + } + + /** + * Initializes configuration data from given message data array + * + * @param $messageData An array with all message data + * @return void + */ + protected function initMessageConfigurationData (array $messageData) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: messageData=' . print_r($messageData, TRUE)); + + // "Walk" throught the translation array + foreach ($this->messageToConfig as $messageKey => $configKey) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Setting messageKey=' . $messageKey . ',configKey=' . $configKey . ':' . $messageData[$messageKey]); + + // Set the element in configuration + $this->getConfigInstance()->setConfigEntry($configKey, $messageData[$messageKey]); + } // END - foreach + + // "Walk" throught the config-copy array + foreach ($this->configCopy as $targetKey => $sourceKey) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...'); + + // Copy from source to targetKey + $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey)); + } // END - foreach + + // Translate last exception into a status code + $statusCode = $this->getTranslatedStatusFromLastException(); + + // Set it in configuration (temporarily) + $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode); + } + + /** + * Removes configuration data with given message data array from global + * configuration + * + * @param $messageData An array with all message data + * @return void + */ + protected function removeMessageConfigurationData (array $messageData) { + // "Walk" throught the translation array again + foreach ($this->messageToConfig as $dummy => $configKey) { + // Now unset this configuration entry (to save some memory) + $this->getConfigInstance()->unsetConfigEntry($configKey); + } // END - foreach + + // "Walk" throught the config-copy array again + foreach ($this->configCopy as $configKey => $dummy) { + // Now unset this configuration entry (to save some memory again) + $this->getConfigInstance()->unsetConfigEntry($configKey); + } // END - foreach + + // Remove NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS as well + $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/data/message-types/answer/.htaccess b/application/hub/classes/handler/data/message-types/answer/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/answer/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/data/message-types/answer/class_NodeMessageAnnouncementAnswerHandler.php b/application/hub/classes/handler/data/message-types/answer/class_NodeMessageAnnouncementAnswerHandler.php new file mode 100644 index 000000000..932b647b1 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/answer/class_NodeMessageAnnouncementAnswerHandler.php @@ -0,0 +1,179 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class NodeMessageAnnouncementAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('message_announcement_answer'); + + // Init message data array + $this->messageDataElements = array( + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS, + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS, + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS, + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID, + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH, + BaseXmlAnswerTemplateEngine::ANSWER_STATUS, + ); + + // Init message-data->configuration translation array + $this->messageToConfig = array( + /* + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address', + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address', + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id' + XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash' + */ + ); + + // Init config-copy array + $this->configCopy = array( + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address', + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address', + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS => 'node_status', + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'session_id', + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash', + ); + + // Init array + $this->searchData = array( + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, + XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS, + ); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function createNodeMessageAnnouncementAnswerHandler () { + // Get new instance + $handlerInstance = new NodeMessageAnnouncementAnswerHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles data array of the message + * + * @param $messageData An array with message data to handle + * @param $packageInstance An instance of a Receivable class + * @return void + * @throws NoAnnouncementAttemptedException If this node has not attempted to announce itself + */ + public function handleMessageData (array $messageData, Receivable $packageInstance) { + // Get node instance + $nodeInstance = NodeObjectFactory::createNodeInstance(); + + // Has this node attempted to announce itself? + if (!$nodeInstance->ifNodeIsAnnouncing()) { + /* + * This node has never announced itself, so it doesn't expect + * announcement answer messages. + */ + throw new NoAnnouncementAttemptedException(array($this, $nodeInstance, $messageData), self::EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED); + } // END - if + + // Register the announcing node with this node + $this->registerNodeByMessageData($messageData); + + // Handle the answer status element + $nodeInstance->handleAnswerStatusByMessageData($messageData, $packageInstance); + } + + /** + * Adds all required elements from given array into data set instance + * + * @param $dataSetInstance An instance of a StoreableCriteria class + * @param $messageData An array with all message data + * @return void + */ + public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { + // Add generic first + parent::addArrayToDataSet($dataSetInstance, $messageData); + + // Add all ements + foreach ($this->messageDataElements as $key) { + // Is it there? + assert(isset($messageData[$key])); + + /* + * Add it, but remove any 'my-' prefixes as they are not used in + * database layer. + */ + $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]); + } // END - foreach + } + + /** + * Initializes configuration data from given message data array + * + * @param $messageData An array with all message data + * @return void + * @throws UnsupportedOperationException If this method is called + */ + protected function initMessageConfigurationData (array $messageData) { + // Please don't call this method + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Removes configuration data with given message data array from global + * configuration + * + * @param $messageData An array with all message data + * @return void + * @throws UnsupportedOperationException If this method is called + */ + protected function removeMessageConfigurationData (array $messageData) { + // Please don't call this method + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/data/message-types/answer/class_NodeMessageDhtBootstrapAnswerHandler.php b/application/hub/classes/handler/data/message-types/answer/class_NodeMessageDhtBootstrapAnswerHandler.php new file mode 100644 index 000000000..e47c57a42 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/answer/class_NodeMessageDhtBootstrapAnswerHandler.php @@ -0,0 +1,175 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class NodeMessageDhtBootstrapAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('message_announcement_answer'); + + // Init message data array + $this->messageDataElements = array( + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS, + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS, + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS, + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID, + BaseXmlAnswerTemplateEngine::ANSWER_STATUS, + ); + + // Init message-data->configuration translation array + $this->messageToConfig = array( + /* + @TODO Why commented out? + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'your_external_address', + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'your_internal_address', + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID => 'your_session_id' + */ + ); + + // Init config-copy array + $this->configCopy = array( + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'external_address', + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'internal_address', + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS => 'node_status', + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID => 'session_id', + ); + + // Init array + $this->searchData = array( + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID, + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS, + ); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function createNodeMessageDhtBootstrapAnswerHandler () { + // Get new instance + $handlerInstance = new NodeMessageDhtBootstrapAnswerHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles data array of the message + * + * @param $messageData An array with message data to handle + * @param $packageInstance An instance of a Receivable class + * @return void + * @throws NoDhtBootstrapAttemptedException If this DHT has not attempted to bootstrap + * @todo ~30% done + */ + public function handleMessageData (array $messageData, Receivable $packageInstance) { + // Get DHT instance + $dhtInstance = DhtObjectFactory::createDhtInstance('node'); + + // Has this DHT attempted to bootstrap? + if (!$dhtInstance->ifDhtIsBooting()) { + /* + * This DHT has never bootstrapped, so it doesn't expect + * announcement answer messages. + */ + throw new NoDhtBootstrapAttemptedException(array($this, $dhtInstance, $messageData), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED); + } // END - if + + // Unfinished + $this->partialStub('UNHANDLED: messageData=' . print_r($messageData, TRUE)); + } + + /** + * Adds all required elements from given array into data set instance + * + * @param $dataSetInstance An instance of a StoreableCriteria class + * @param $messageData An array with all message data + * @return void + */ + public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { + // Add generic first + parent::addArrayToDataSet($dataSetInstance, $messageData); + + // Add all ements + foreach ($this->messageDataElements as $key) { + // Is it there? + assert(isset($messageData[$key])); + + /* + * Add it, but remove any 'my-' prefixes as they are not used in + * database layer. + */ + $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]); + } // END - foreach + } + + /** + * Initializes configuration data from given message data array + * + * @param $messageData An array with all message data + * @return void + * @throws UnsupportedOperationException If this method is called + */ + protected function initMessageConfigurationData (array $messageData) { + // Please don't call this method + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Removes configuration data with given message data array from global + * configuration + * + * @param $messageData An array with all message data + * @return void + * @throws UnsupportedOperationException If this method is called + */ + protected function removeMessageConfigurationData (array $messageData) { + // Please don't call this method + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/data/message-types/answer/class_NodeMessageRequestNodeListAnswerHandler.php b/application/hub/classes/handler/data/message-types/answer/class_NodeMessageRequestNodeListAnswerHandler.php new file mode 100644 index 000000000..f2a4aa328 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/answer/class_NodeMessageRequestNodeListAnswerHandler.php @@ -0,0 +1,168 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class NodeMessageRequestNodeListAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('message_request_node_list_answer'); + + // Init message data array + $this->messageDataElements = array( + XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_SESSION_ID, + XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_NODE_LIST, + BaseXmlAnswerTemplateEngine::ANSWER_STATUS, + ); + + // Init message-data->configuration translation array + $this->messageToConfig = array( + /* + XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID => 'your_session_id' + */ + ); + + // Init search data array + $this->searchData = array( + XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_SESSION_ID, + ); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function createNodeMessageRequestNodeListAnswerHandler () { + // Get new instance + $handlerInstance = new NodeMessageRequestNodeListAnswerHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles data array of the message + * + * @param $messageData An array with message data to handle + * @param $packageInstance An instance of a Receivable class + * @return void + * @throws NoRequestNodeListAttemptedException If this node has not attempted to announce itself + */ + public function handleMessageData (array $messageData, Receivable $packageInstance) { + // Get node instance + $nodeInstance = NodeObjectFactory::createNodeInstance(); + + // Has this node attempted to announce itself? + if (!$nodeInstance->ifNodeHasAnnouncementCompleted()) { + /* + * This node has never announced itself, so it doesn't expect + * request-node-list answer messages. + */ + throw new NoRequestNodeListAttemptedException(array($this, $nodeInstance, $messageData), self::EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED); + } // END - if + + // Register the announcing node with this node + $this->registerNodeByMessageData($messageData); + + // Handle the answer status element + $nodeInstance->handleAnswerStatusByMessageData($messageData, $packageInstance); + } + + /** + * Adds all required elements from given array into data set instance + * + * @param $dataSetInstance An instance of a StoreableCriteria class + * @param $messageData An array with all message data + * @return void + */ + public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { + // Add generic first + parent::addArrayToDataSet($dataSetInstance, $messageData); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANSWER-HANDLER: messageData=' . print_r($messageData, TRUE)); + + // Add all ements + foreach ($this->messageDataElements as $key) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANSWER-HANDLER: messageData[' . $key . ']=' . $messageData[$key]); + + // Is it there? + assert(isset($messageData[$key])); + + /* + * Add it, but remove any 'my-' prefixes as they are not used in + * database layer. + */ + $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]); + } // END - foreach + } + + /** + * Initializes configuration data from given message data array + * + * @param $messageData An array with all message data + * @return void + * @throws UnsupportedOperationException If this method is called + */ + protected function initMessageConfigurationData (array $messageData) { + // Please don't call this method + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Removes configuration data with given message data array from global + * configuration + * + * @param $messageData An array with all message data + * @return void + * @throws UnsupportedOperationException If this method is called + */ + protected function removeMessageConfigurationData (array $messageData) { + // Please don't call this method + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/data/message-types/class_ b/application/hub/classes/handler/data/message-types/class_ new file mode 100644 index 000000000..0a4077e16 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/class_ @@ -0,0 +1,121 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class NodeMessage???Handler extends BaseMessageHandler implements HandleableMessage, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('message_==='); + + // Init message data array + $this->messageDataElements = array( + Xml???TemplateEngine::!!!_DATA_EXTERNAL_ADDRESS, + Xml???TemplateEngine::!!!_DATA_NODE_STATUS, + Xml???TemplateEngine::!!!_DATA_SESSION_ID, + Xml???TemplateEngine::!!!_DATA_LISTEN_PORT, + ); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function createNodeMessage???Handler () { + // Get new instance + $handlerInstance = new NodeMessage???Handler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles data array of the message + * + * @param $messageData An array with message data to handle + * @param $packageInstance An instance of a Receivable class + * @return void + */ + public function handleMessageData (array $messageData, Receivable $packageInstance) { + // Register the announcing node with this node + $this->registerNodeByMessageData($messageData); + + // Prepare answer message to be delivered back to the other node + $this->prepareAnswerMessage($messageData, $packageInstance); + } + + /** + * Adds all required elements from given array into data set instance + * + * @param $dataSetInstance An instance of a StoreableCriteria class + * @param $messageData An array with all message data + * @return void + */ + public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { + // Add all ements + foreach ($this->messageDataElements as $key) { + // Is it there? + assert(isset($messageData[$key])); + + // Add it + $dataSetInstance->addCriteria($key, $messageData[$key]); + } // END - foreach + } + + /** + * Initializes configuration data from given message data array + * + * @param $messageData An array with all message data + * @return void + */ + protected function initMessageConfigurationData (array $messageData) { + $this->partialStub('Please implement this method.'); + } + + /** + * Removes configuration data with given message data array from global + * configuration + * + * @param $messageData An array with all message data + * @return void + */ + protected function removeMessageConfigurationData (array $messageData) { + $this->partialStub('Please implement this method.'); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/data/message-types/class_BaseMessageHandler.php b/application/hub/classes/handler/data/message-types/class_BaseMessageHandler.php new file mode 100644 index 000000000..4d5563741 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/class_BaseMessageHandler.php @@ -0,0 +1,101 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +abstract class BaseMessageHandler extends BaseDataHandler { + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + } + + /** + * "Getter" for a translated last exception as a status code + * + * @return $statusCode Translated status code from last exception + */ + protected function getTranslatedStatusFromLastException () { + // Default is all fine + $statusCode = BaseHubSystem::MESSAGE_STATUS_CODE_OKAY; + + // Is the last exception not NULL? + if ($this->getLastException() instanceof FrameworkException) { + // "Determine" the right status code (may differ from exception to exception) + $this->debugInstance(sprintf('[%s:%d]: lastException=%s,message=%s is unfinished.', __METHOD__, __LINE__, $this->getLastException()->__toString(), $this->getLastException()->getMessage())); + } // END - if + + // Return the status code + return $statusCode; + } + + /** + * Registers an other node with this node by given message data. The + * following data must always be present: + * + * - session-id (for finding the node's record together with below data) + * - external-address (hostname or IP number) + * - listen-port (TCP/UDP listen port for inbound connections) + * + * @param $messageArray An array with all minimum message data + * @return void + */ + protected function registerNodeByMessageData (array $messageData) { + // Check if searchData has entries + assert(count($this->getSearchData()) > 0); + + // Let the DHT facade do the work + DhtObjectFactory::createDhtInstance('node')->registerNodeByMessageData($messageData, $this); + } + + /** + * Posty-handles data array of the message + * + * @param $messageData An array with message data to handle + * @param $packageInstance An instance of a Receivable class + * @return void + */ + public function postHandleMessageData (array $messageData, Receivable $packageInstance) { + /* + * Feed hash to miner by handling over the whole array as also the + * sender and tags are needed. + */ + $packageInstance->feedHashToMiner($messageData); + } + +} diff --git a/application/hub/classes/handler/data/message-types/dht/.htaccess b/application/hub/classes/handler/data/message-types/dht/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/dht/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/data/message-types/dht/class_NodeMessageDhtBootstrapHandler.php b/application/hub/classes/handler/data/message-types/dht/class_NodeMessageDhtBootstrapHandler.php new file mode 100644 index 000000000..dfaa234bc --- /dev/null +++ b/application/hub/classes/handler/data/message-types/dht/class_NodeMessageDhtBootstrapHandler.php @@ -0,0 +1,215 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements HandleableMessage, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('message_dht_bootstrap'); + + // Init message data array + $this->messageDataElements = array( + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS, + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS, + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS, + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_MODE, + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID, + ); + + // Init message-data->configuration translation array + $this->messageToConfig = array( + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'your_external_address', + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'your_internal_address', + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID => 'your_session_id' + ); + + // Init config-copy array + $this->configCopy = array( + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'external_address', + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'internal_address', + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS => 'node_status', + XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID => 'session_id', + ); + + // Init array + $this->searchData = array( + XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS, + ); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function createNodeMessageDhtBootstrapHandler () { + // Get new instance + $handlerInstance = new NodeMessageDhtBootstrapHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles data array of the message + * + * @param $messageData An array with message data to handle + * @param $packageInstance An instance of a Receivable class + * @return void + * @throws DhtBootstrapNotAcceptedException If this node does not accept DHT bootstrap requests + */ + public function handleMessageData (array $messageData, Receivable $packageInstance) { + // Is this node accepting DHT bootstrap requests? + if (!NodeObjectFactory::createNodeInstance()->isAcceptingDhtBootstrap()) { + /* + * This node is not accepting DHT bootstrap requests. + */ + throw new DhtBootstrapNotAcceptedException(array($this, $messageData), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ACCEPTED); + } // END - if + + // Register the DHT bootstrap requesting node with this node + $this->registerNodeByMessageData($messageData); + + // Prepare answer message to be delivered back to the other node + $this->prepareAnswerMessage($messageData, $packageInstance); + } + + /** + * Adds all required elements from given array into data set instance + * + * @param $dataSetInstance An instance of a StoreableCriteria class + * @param $messageData An array with all message data + * @return void + */ + public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { + // Add generic first + parent::addArrayToDataSet($dataSetInstance, $messageData); + + // Add all ements + foreach ($this->messageDataElements as $key) { + // Is it there? + assert(isset($messageData[$key])); + + // Add it + $dataSetInstance->addCriteria($key, $messageData[$key]); + } // END - foreach + } + + /** + * Initializes configuration data from given message data array + * + * @param $messageData An array with all message data + * @return void + */ + protected function initMessageConfigurationData (array $messageData) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-BOOTSTRAP-HANDLER: messageData=' . print_r($messageData, TRUE)); + + // "Walk" throught the translation array + foreach ($this->messageToConfig as $messageKey => $configKey) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-BOOTSTRAP-HANDLER: Setting messageKey=' . $messageKey . ',configKey=' . $configKey . ':' . $messageData[$messageKey]); + + // Set the element in configuration + $this->getConfigInstance()->setConfigEntry($configKey, $messageData[$messageKey]); + } // END - foreach + + // "Walk" throught the config-copy array + foreach ($this->configCopy as $targetKey => $sourceKey) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-BOOTSTRAP-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...'); + + // Copy from source to target key + $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey)); + } // END - foreach + + // Translate last exception into a status code + $statusCode = $this->getTranslatedStatusFromLastException(); + + // Set it in configuration (temporarily) + $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode); + + /* + * Use the DHT instance to get a list of recipients. This means that all + * DHT nodes that accept bootstrap requests are read from the DHT + * database. + */ + $nodeList = DhtObjectFactory::createDhtInstance('node')->findRecipientsByKey(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPT_BOOTSTRAP, 'Y'); + + // Make sure it is an array and has at least one entry + assert(is_array($nodeList)); + assert(count($nodeList) > 0); + + // Set it in configuration + $this->getConfigInstance()->setConfigEntry('dht_nodes', base64_encode(json_encode($nodeList))); + } + + /** + * Removes configuration data with given message data array from global + * configuration + * + * @param $messageData An array with all message data + * @return void + */ + protected function removeMessageConfigurationData (array $messageData) { + // "Walk" throught the translation array again + foreach ($this->messageToConfig as $dummy => $configKey) { + // Now unset this configuration entry (to save some memory) + $this->getConfigInstance()->unsetConfigEntry($configKey); + } // END - foreach + + // "Walk" throught the config-copy array again + foreach ($this->configCopy as $configKey => $dummy) { + // Now unset this configuration entry (to save some memory again) + $this->getConfigInstance()->unsetConfigEntry($configKey); + } // END - foreach + + // Remove temporary "special" values as well + $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS); + $this->getConfigInstance()->unsetConfigEntry('dht_nodes'); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/data/message-types/requests/.htaccess b/application/hub/classes/handler/data/message-types/requests/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/requests/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/data/message-types/requests/class_NodeMessageRequestNodeListHandler.php b/application/hub/classes/handler/data/message-types/requests/class_NodeMessageRequestNodeListHandler.php new file mode 100644 index 000000000..e74527a67 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/requests/class_NodeMessageRequestNodeListHandler.php @@ -0,0 +1,190 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class NodeMessageRequestNodeListHandler extends BaseMessageHandler implements HandleableMessage, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('message_request_node_list'); + + // Init message data array + $this->messageDataElements = array( + XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES, + ); + + // Init config-copy array + $this->configCopy = array( + ); + + // Init search data array + $this->searchData = array( + XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID, + ); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function createNodeMessageRequestNodeListHandler () { + // Get new instance + $handlerInstance = new NodeMessageRequestNodeListHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles data array of the message + * + * @param $messageData An array with message data to handle + * @param $packageInstance An instance of a Receivable class + * @return void + * @throws RequestNotAcceptedException If this node does not accept this request + */ + public function handleMessageData (array $messageData, Receivable $packageInstance) { + // Get node instance + $nodeInstance = NodeObjectFactory::createNodeInstance(); + + // Is this node accepting announcements? + if (!$nodeInstance->isAcceptingNodeListRequests()) { + /* + * This node is not accepting node list requests. Throw an + * exception to abort any further processing. + */ + throw new RequestNotAcceptedException(array($this, $nodeInstance, $messageData), self::EXCEPTION_REQUEST_NOT_ACCEPTED); + } // END - if + + // Register the announcing node with this node + $this->registerNodeByMessageData($messageData); + + // Prepare answer message to be delivered back to the other node + $this->prepareAnswerMessage($messageData, $packageInstance); + } + + /** + * Adds all required elements from given array into data set instance + * + * @param $dataSetInstance An instance of a StoreableCriteria class + * @param $messageData An array with all message data + * @return void + */ + public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { + // Add generic first + parent::addArrayToDataSet($dataSetInstance, $messageData); + + // Add all ements + foreach ($this->messageDataElements as $key) { + // Is it there? + assert(isset($messageData[$key])); + + // Add it + $dataSetInstance->addCriteria($key, $messageData[$key]); + } // END - foreach + } + + /** + * Initializes configuration data from given message data array + * + * @param $messageData An array with all message data + * @return void + */ + protected function initMessageConfigurationData (array $messageData) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: messageData=' . print_r($messageData, TRUE)); + + // "Walk" throught the config-copy array + foreach ($this->configCopy as $targetKey => $sourceKey) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...'); + + // Copy from source to targetKey + $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey)); + } // END - foreach + + // Query local DHT for nodes except given session id + $nodeList = DhtObjectFactory::createDhtInstance('node')->queryLocalNodeListExceptByMessageData( + $messageData, + $this, + XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID, + XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES, + BaseHubNode::OBJECT_LIST_SEPARATOR + ); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Got a node list of ' . count($nodeList) . ' entry/-ies back.'); + + // Set it serialized in configuration (temporarily) + $this->getConfigInstance()->setConfigEntry('node_list', base64_encode(json_encode($nodeList))); + + // Translate last exception into a status code + $statusCode = $this->getTranslatedStatusFromLastException(); + + // Set it in configuration (temporarily) + $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode); + } + + /** + * Removes configuration data with given message data array from global + * configuration + * + * @param $messageData An array with all message data + * @return void + */ + protected function removeMessageConfigurationData (array $messageData) { + // "Walk" throught the config-copy array again + foreach ($this->configCopy as $configKey => $dummy) { + // Now unset this configuration entry (to save some memory again) + $this->getConfigInstance()->unsetConfigEntry($configKey); + } // END - foreach + + // Remove answer status/node list as well + $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS); + $this->getConfigInstance()->unsetConfigEntry('node_list'); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/data/message-types/self-connect/.htaccess b/application/hub/classes/handler/data/message-types/self-connect/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/self-connect/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/data/message-types/self-connect/class_NodeMessageSelfConnectHandler.php b/application/hub/classes/handler/data/message-types/self-connect/class_NodeMessageSelfConnectHandler.php new file mode 100644 index 000000000..b485f7b19 --- /dev/null +++ b/application/hub/classes/handler/data/message-types/self-connect/class_NodeMessageSelfConnectHandler.php @@ -0,0 +1,130 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class NodeMessageSelfConnectHandler extends BaseMessageHandler implements HandleableMessage, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('message_self_connect'); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a HandleableMessage class + */ + public final static function createNodeMessageSelfConnectHandler () { + // Get new instance + $handlerInstance = new NodeMessageSelfConnectHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Handles data array of the message + * + * @param $messageData An array with message data to handle + * @param $packageInstance An instance of a Receivable class + * @return void + */ + public function handleMessageData (array $messageData, Receivable $packageInstance) { + // Get node instance + $nodeInstance = NodeObjectFactory::createNodeInstance(); + + // Are node id and session id the same? + if (($messageData[XmlSelfConnectTemplateEngine::SELF_CONNECT_DATA_NODE_ID] == $nodeInstance->getNodeId()) && ($messageData[XmlSelfConnectTemplateEngine::SELF_CONNECT_DATA_SESSION_ID] == $nodeInstance->getSessionId())) { + // Both are equal + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SELF-CONNECT: Have connected to myself, both node and session id are equal!'); + + // ... and change state + $nodeInstance->getStateInstance()->nodeHasSelfConnected(); + } else { + // Something really horrible happened + // @TODO Throw an exception here instead of dying + $this->debugInstance(sprintf('[%s:%d]: ids mismatching! messageData=%s', __METHOD__, __LINE__, print_r($messageData, TRUE))); + } + } + + /** + * Adds all required elements from given array into data set instance + * + * @param $dataSetInstance An instance of a StoreableCriteria class + * @param $messageData An array with all message data + * @return void + * @throws UnsupportedOperationException If this method is called + */ + public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { + // Add generic first + parent::addArrayToDataSet($dataSetInstance, $messageData); + + // Please don't call this method! + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Initializes configuration data from given message data array + * + * @param $messageData An array with all message data + * @return void + * @throws UnsupportedOperationException If this method is called + */ + protected function initMessageConfigurationData (array $messageData) { + // Please don't call this method! + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Removes configuration data with given message data array from global configuration + * + * @param $messageData An array with all message data + * @return void + * @throws UnsupportedOperationException If this method is called + */ + protected function removeMessageConfigurationData (array $messageData) { + // Please don't call this method! + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/message-types/.htaccess b/application/hub/classes/handler/message-types/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/message-types/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/message-types/announcement/.htaccess b/application/hub/classes/handler/message-types/announcement/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/message-types/announcement/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/message-types/announcement/class_NodeMessageAnnouncementHandler.php b/application/hub/classes/handler/message-types/announcement/class_NodeMessageAnnouncementHandler.php deleted file mode 100644 index 1882fb2e3..000000000 --- a/application/hub/classes/handler/message-types/announcement/class_NodeMessageAnnouncementHandler.php +++ /dev/null @@ -1,209 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class NodeMessageAnnouncementHandler extends BaseMessageHandler implements HandleableMessage, Registerable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('message_announcement'); - - // Init message data array - $this->messageDataElements = array( - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS, - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS, - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS, - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_MODE, - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID, - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH, - ); - - // Init message-data->configuration translation array - $this->messageToConfig = array( - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address', - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address', - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID => 'your_node_id', - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id', - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash', - ); - - // Init config-copy array - $this->configCopy = array( - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address', - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address', - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS => 'node_status', - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'session_id', - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash', - ); - - // Init array - $this->searchData = array( - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS - ); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a HandleableMessage class - */ - public final static function createNodeMessageAnnouncementHandler () { - // Get new instance - $handlerInstance = new NodeMessageAnnouncementHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Handles data array of the message - * - * @param $messageData An array with message data to handle - * @param $packageInstance An instance of a Receivable class - * @return void - * @throws AnnouncementNotAcceptedException If this node does not accept announcements - */ - public function handleMessageData (array $messageData, Receivable $packageInstance) { - // Get node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - - // Is this node accepting announcements? - if (!$nodeInstance->isAcceptingAnnouncements()) { - /* - * This node is not accepting announcements, then someone wants to - * announce his node to a non-bootstrap and non-master node. - */ - throw new AnnouncementNotAcceptedException(array($this, $nodeInstance, $messageData), BaseHubSystem::EXCEPTION_ANNOUNCEMENT_NOT_ACCEPTED); - } // END - if - - // Register the announcing node with this node - $this->registerNodeByMessageData($messageData); - - // Prepare answer message to be delivered back to the other node - $this->prepareAnswerMessage($messageData, $packageInstance); - } - - /** - * Adds all required elements from given array into data set instance - * - * @param $dataSetInstance An instance of a StoreableCriteria class - * @param $messageData An array with all message data - * @return void - */ - public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { - // Add generic first - parent::addArrayToDataSet($dataSetInstance, $messageData); - - // Add all ements - foreach ($this->messageDataElements as $key) { - // Is it there? - assert(isset($messageData[$key])); - - // Add it - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Adding messageData[' . $key . ']=' . $messageData[$key] . ' ...'); - $dataSetInstance->addCriteria($key, $messageData[$key]); - } // END - foreach - } - - /** - * Initializes configuration data from given message data array - * - * @param $messageData An array with all message data - * @return void - */ - protected function initMessageConfigurationData (array $messageData) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: messageData=' . print_r($messageData, TRUE)); - - // "Walk" throught the translation array - foreach ($this->messageToConfig as $messageKey => $configKey) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Setting messageKey=' . $messageKey . ',configKey=' . $configKey . ':' . $messageData[$messageKey]); - - // Set the element in configuration - $this->getConfigInstance()->setConfigEntry($configKey, $messageData[$messageKey]); - } // END - foreach - - // "Walk" throught the config-copy array - foreach ($this->configCopy as $targetKey => $sourceKey) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANNOUNCEMENT-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...'); - - // Copy from source to targetKey - $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey)); - } // END - foreach - - // Translate last exception into a status code - $statusCode = $this->getTranslatedStatusFromLastException(); - - // Set it in configuration (temporarily) - $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode); - } - - /** - * Removes configuration data with given message data array from global - * configuration - * - * @param $messageData An array with all message data - * @return void - */ - protected function removeMessageConfigurationData (array $messageData) { - // "Walk" throught the translation array again - foreach ($this->messageToConfig as $dummy => $configKey) { - // Now unset this configuration entry (to save some memory) - $this->getConfigInstance()->unsetConfigEntry($configKey); - } // END - foreach - - // "Walk" throught the config-copy array again - foreach ($this->configCopy as $configKey => $dummy) { - // Now unset this configuration entry (to save some memory again) - $this->getConfigInstance()->unsetConfigEntry($configKey); - } // END - foreach - - // Remove NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS as well - $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/message-types/answer/.htaccess b/application/hub/classes/handler/message-types/answer/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/message-types/answer/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/message-types/answer/class_NodeMessageAnnouncementAnswerHandler.php b/application/hub/classes/handler/message-types/answer/class_NodeMessageAnnouncementAnswerHandler.php deleted file mode 100644 index 932b647b1..000000000 --- a/application/hub/classes/handler/message-types/answer/class_NodeMessageAnnouncementAnswerHandler.php +++ /dev/null @@ -1,179 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class NodeMessageAnnouncementAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('message_announcement_answer'); - - // Init message data array - $this->messageDataElements = array( - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS, - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS, - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS, - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_ID, - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH, - BaseXmlAnswerTemplateEngine::ANSWER_STATUS, - ); - - // Init message-data->configuration translation array - $this->messageToConfig = array( - /* - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'your_external_address', - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'your_internal_address', - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'your_session_id' - XmlAnnouncementTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'your_private_key_hash' - */ - ); - - // Init config-copy array - $this->configCopy = array( - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS => 'external_address', - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_INTERNAL_ADDRESS => 'internal_address', - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_NODE_STATUS => 'node_status', - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID => 'session_id', - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_PRIVATE_KEY_HASH => 'private_key_hash', - ); - - // Init array - $this->searchData = array( - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_SESSION_ID, - XmlAnnouncementAnswerTemplateEngine::ANNOUNCEMENT_DATA_EXTERNAL_ADDRESS, - ); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a HandleableMessage class - */ - public final static function createNodeMessageAnnouncementAnswerHandler () { - // Get new instance - $handlerInstance = new NodeMessageAnnouncementAnswerHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Handles data array of the message - * - * @param $messageData An array with message data to handle - * @param $packageInstance An instance of a Receivable class - * @return void - * @throws NoAnnouncementAttemptedException If this node has not attempted to announce itself - */ - public function handleMessageData (array $messageData, Receivable $packageInstance) { - // Get node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - - // Has this node attempted to announce itself? - if (!$nodeInstance->ifNodeIsAnnouncing()) { - /* - * This node has never announced itself, so it doesn't expect - * announcement answer messages. - */ - throw new NoAnnouncementAttemptedException(array($this, $nodeInstance, $messageData), self::EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED); - } // END - if - - // Register the announcing node with this node - $this->registerNodeByMessageData($messageData); - - // Handle the answer status element - $nodeInstance->handleAnswerStatusByMessageData($messageData, $packageInstance); - } - - /** - * Adds all required elements from given array into data set instance - * - * @param $dataSetInstance An instance of a StoreableCriteria class - * @param $messageData An array with all message data - * @return void - */ - public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { - // Add generic first - parent::addArrayToDataSet($dataSetInstance, $messageData); - - // Add all ements - foreach ($this->messageDataElements as $key) { - // Is it there? - assert(isset($messageData[$key])); - - /* - * Add it, but remove any 'my-' prefixes as they are not used in - * database layer. - */ - $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]); - } // END - foreach - } - - /** - * Initializes configuration data from given message data array - * - * @param $messageData An array with all message data - * @return void - * @throws UnsupportedOperationException If this method is called - */ - protected function initMessageConfigurationData (array $messageData) { - // Please don't call this method - throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); - } - - /** - * Removes configuration data with given message data array from global - * configuration - * - * @param $messageData An array with all message data - * @return void - * @throws UnsupportedOperationException If this method is called - */ - protected function removeMessageConfigurationData (array $messageData) { - // Please don't call this method - throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/message-types/answer/class_NodeMessageDhtBootstrapAnswerHandler.php b/application/hub/classes/handler/message-types/answer/class_NodeMessageDhtBootstrapAnswerHandler.php deleted file mode 100644 index e47c57a42..000000000 --- a/application/hub/classes/handler/message-types/answer/class_NodeMessageDhtBootstrapAnswerHandler.php +++ /dev/null @@ -1,175 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class NodeMessageDhtBootstrapAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('message_announcement_answer'); - - // Init message data array - $this->messageDataElements = array( - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS, - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS, - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS, - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID, - BaseXmlAnswerTemplateEngine::ANSWER_STATUS, - ); - - // Init message-data->configuration translation array - $this->messageToConfig = array( - /* - @TODO Why commented out? - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'your_external_address', - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'your_internal_address', - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID => 'your_session_id' - */ - ); - - // Init config-copy array - $this->configCopy = array( - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'external_address', - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'internal_address', - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS => 'node_status', - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID => 'session_id', - ); - - // Init array - $this->searchData = array( - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID, - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS, - ); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a HandleableMessage class - */ - public final static function createNodeMessageDhtBootstrapAnswerHandler () { - // Get new instance - $handlerInstance = new NodeMessageDhtBootstrapAnswerHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Handles data array of the message - * - * @param $messageData An array with message data to handle - * @param $packageInstance An instance of a Receivable class - * @return void - * @throws NoDhtBootstrapAttemptedException If this DHT has not attempted to bootstrap - * @todo ~30% done - */ - public function handleMessageData (array $messageData, Receivable $packageInstance) { - // Get DHT instance - $dhtInstance = DhtObjectFactory::createDhtInstance('node'); - - // Has this DHT attempted to bootstrap? - if (!$dhtInstance->ifDhtIsBooting()) { - /* - * This DHT has never bootstrapped, so it doesn't expect - * announcement answer messages. - */ - throw new NoDhtBootstrapAttemptedException(array($this, $dhtInstance, $messageData), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ATTEMPTED); - } // END - if - - // Unfinished - $this->partialStub('UNHANDLED: messageData=' . print_r($messageData, TRUE)); - } - - /** - * Adds all required elements from given array into data set instance - * - * @param $dataSetInstance An instance of a StoreableCriteria class - * @param $messageData An array with all message data - * @return void - */ - public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { - // Add generic first - parent::addArrayToDataSet($dataSetInstance, $messageData); - - // Add all ements - foreach ($this->messageDataElements as $key) { - // Is it there? - assert(isset($messageData[$key])); - - /* - * Add it, but remove any 'my-' prefixes as they are not used in - * database layer. - */ - $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]); - } // END - foreach - } - - /** - * Initializes configuration data from given message data array - * - * @param $messageData An array with all message data - * @return void - * @throws UnsupportedOperationException If this method is called - */ - protected function initMessageConfigurationData (array $messageData) { - // Please don't call this method - throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); - } - - /** - * Removes configuration data with given message data array from global - * configuration - * - * @param $messageData An array with all message data - * @return void - * @throws UnsupportedOperationException If this method is called - */ - protected function removeMessageConfigurationData (array $messageData) { - // Please don't call this method - throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/message-types/answer/class_NodeMessageRequestNodeListAnswerHandler.php b/application/hub/classes/handler/message-types/answer/class_NodeMessageRequestNodeListAnswerHandler.php deleted file mode 100644 index f2a4aa328..000000000 --- a/application/hub/classes/handler/message-types/answer/class_NodeMessageRequestNodeListAnswerHandler.php +++ /dev/null @@ -1,168 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class NodeMessageRequestNodeListAnswerHandler extends BaseMessageHandler implements HandleableMessage, Registerable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('message_request_node_list_answer'); - - // Init message data array - $this->messageDataElements = array( - XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_SESSION_ID, - XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_NODE_LIST, - BaseXmlAnswerTemplateEngine::ANSWER_STATUS, - ); - - // Init message-data->configuration translation array - $this->messageToConfig = array( - /* - XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID => 'your_session_id' - */ - ); - - // Init search data array - $this->searchData = array( - XmlRequestNodeListAnswerTemplateEngine::REQUEST_DATA_SESSION_ID, - ); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a HandleableMessage class - */ - public final static function createNodeMessageRequestNodeListAnswerHandler () { - // Get new instance - $handlerInstance = new NodeMessageRequestNodeListAnswerHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Handles data array of the message - * - * @param $messageData An array with message data to handle - * @param $packageInstance An instance of a Receivable class - * @return void - * @throws NoRequestNodeListAttemptedException If this node has not attempted to announce itself - */ - public function handleMessageData (array $messageData, Receivable $packageInstance) { - // Get node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - - // Has this node attempted to announce itself? - if (!$nodeInstance->ifNodeHasAnnouncementCompleted()) { - /* - * This node has never announced itself, so it doesn't expect - * request-node-list answer messages. - */ - throw new NoRequestNodeListAttemptedException(array($this, $nodeInstance, $messageData), self::EXCEPTION_ANNOUNCEMENT_NOT_ATTEMPTED); - } // END - if - - // Register the announcing node with this node - $this->registerNodeByMessageData($messageData); - - // Handle the answer status element - $nodeInstance->handleAnswerStatusByMessageData($messageData, $packageInstance); - } - - /** - * Adds all required elements from given array into data set instance - * - * @param $dataSetInstance An instance of a StoreableCriteria class - * @param $messageData An array with all message data - * @return void - */ - public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { - // Add generic first - parent::addArrayToDataSet($dataSetInstance, $messageData); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANSWER-HANDLER: messageData=' . print_r($messageData, TRUE)); - - // Add all ements - foreach ($this->messageDataElements as $key) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('ANSWER-HANDLER: messageData[' . $key . ']=' . $messageData[$key]); - - // Is it there? - assert(isset($messageData[$key])); - - /* - * Add it, but remove any 'my-' prefixes as they are not used in - * database layer. - */ - $dataSetInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]); - } // END - foreach - } - - /** - * Initializes configuration data from given message data array - * - * @param $messageData An array with all message data - * @return void - * @throws UnsupportedOperationException If this method is called - */ - protected function initMessageConfigurationData (array $messageData) { - // Please don't call this method - throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); - } - - /** - * Removes configuration data with given message data array from global - * configuration - * - * @param $messageData An array with all message data - * @return void - * @throws UnsupportedOperationException If this method is called - */ - protected function removeMessageConfigurationData (array $messageData) { - // Please don't call this method - throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/message-types/class_ b/application/hub/classes/handler/message-types/class_ deleted file mode 100644 index 0a4077e16..000000000 --- a/application/hub/classes/handler/message-types/class_ +++ /dev/null @@ -1,121 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class NodeMessage???Handler extends BaseMessageHandler implements HandleableMessage, Registerable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('message_==='); - - // Init message data array - $this->messageDataElements = array( - Xml???TemplateEngine::!!!_DATA_EXTERNAL_ADDRESS, - Xml???TemplateEngine::!!!_DATA_NODE_STATUS, - Xml???TemplateEngine::!!!_DATA_SESSION_ID, - Xml???TemplateEngine::!!!_DATA_LISTEN_PORT, - ); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a HandleableMessage class - */ - public final static function createNodeMessage???Handler () { - // Get new instance - $handlerInstance = new NodeMessage???Handler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Handles data array of the message - * - * @param $messageData An array with message data to handle - * @param $packageInstance An instance of a Receivable class - * @return void - */ - public function handleMessageData (array $messageData, Receivable $packageInstance) { - // Register the announcing node with this node - $this->registerNodeByMessageData($messageData); - - // Prepare answer message to be delivered back to the other node - $this->prepareAnswerMessage($messageData, $packageInstance); - } - - /** - * Adds all required elements from given array into data set instance - * - * @param $dataSetInstance An instance of a StoreableCriteria class - * @param $messageData An array with all message data - * @return void - */ - public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { - // Add all ements - foreach ($this->messageDataElements as $key) { - // Is it there? - assert(isset($messageData[$key])); - - // Add it - $dataSetInstance->addCriteria($key, $messageData[$key]); - } // END - foreach - } - - /** - * Initializes configuration data from given message data array - * - * @param $messageData An array with all message data - * @return void - */ - protected function initMessageConfigurationData (array $messageData) { - $this->partialStub('Please implement this method.'); - } - - /** - * Removes configuration data with given message data array from global - * configuration - * - * @param $messageData An array with all message data - * @return void - */ - protected function removeMessageConfigurationData (array $messageData) { - $this->partialStub('Please implement this method.'); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/message-types/class_BaseMessageHandler.php b/application/hub/classes/handler/message-types/class_BaseMessageHandler.php deleted file mode 100644 index 4d5563741..000000000 --- a/application/hub/classes/handler/message-types/class_BaseMessageHandler.php +++ /dev/null @@ -1,101 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -abstract class BaseMessageHandler extends BaseDataHandler { - /** - * Protected constructor - * - * @param $className Name of the class - * @return void - */ - protected function __construct ($className) { - // Call parent constructor - parent::__construct($className); - } - - /** - * "Getter" for a translated last exception as a status code - * - * @return $statusCode Translated status code from last exception - */ - protected function getTranslatedStatusFromLastException () { - // Default is all fine - $statusCode = BaseHubSystem::MESSAGE_STATUS_CODE_OKAY; - - // Is the last exception not NULL? - if ($this->getLastException() instanceof FrameworkException) { - // "Determine" the right status code (may differ from exception to exception) - $this->debugInstance(sprintf('[%s:%d]: lastException=%s,message=%s is unfinished.', __METHOD__, __LINE__, $this->getLastException()->__toString(), $this->getLastException()->getMessage())); - } // END - if - - // Return the status code - return $statusCode; - } - - /** - * Registers an other node with this node by given message data. The - * following data must always be present: - * - * - session-id (for finding the node's record together with below data) - * - external-address (hostname or IP number) - * - listen-port (TCP/UDP listen port for inbound connections) - * - * @param $messageArray An array with all minimum message data - * @return void - */ - protected function registerNodeByMessageData (array $messageData) { - // Check if searchData has entries - assert(count($this->getSearchData()) > 0); - - // Let the DHT facade do the work - DhtObjectFactory::createDhtInstance('node')->registerNodeByMessageData($messageData, $this); - } - - /** - * Posty-handles data array of the message - * - * @param $messageData An array with message data to handle - * @param $packageInstance An instance of a Receivable class - * @return void - */ - public function postHandleMessageData (array $messageData, Receivable $packageInstance) { - /* - * Feed hash to miner by handling over the whole array as also the - * sender and tags are needed. - */ - $packageInstance->feedHashToMiner($messageData); - } - -} diff --git a/application/hub/classes/handler/message-types/dht/.htaccess b/application/hub/classes/handler/message-types/dht/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/message-types/dht/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/message-types/dht/class_NodeMessageDhtBootstrapHandler.php b/application/hub/classes/handler/message-types/dht/class_NodeMessageDhtBootstrapHandler.php deleted file mode 100644 index dfaa234bc..000000000 --- a/application/hub/classes/handler/message-types/dht/class_NodeMessageDhtBootstrapHandler.php +++ /dev/null @@ -1,215 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements HandleableMessage, Registerable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('message_dht_bootstrap'); - - // Init message data array - $this->messageDataElements = array( - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS, - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS, - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS, - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_MODE, - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID, - ); - - // Init message-data->configuration translation array - $this->messageToConfig = array( - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'your_external_address', - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'your_internal_address', - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID => 'your_session_id' - ); - - // Init config-copy array - $this->configCopy = array( - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'external_address', - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'internal_address', - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS => 'node_status', - XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID => 'session_id', - ); - - // Init array - $this->searchData = array( - XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS, - ); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a HandleableMessage class - */ - public final static function createNodeMessageDhtBootstrapHandler () { - // Get new instance - $handlerInstance = new NodeMessageDhtBootstrapHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Handles data array of the message - * - * @param $messageData An array with message data to handle - * @param $packageInstance An instance of a Receivable class - * @return void - * @throws DhtBootstrapNotAcceptedException If this node does not accept DHT bootstrap requests - */ - public function handleMessageData (array $messageData, Receivable $packageInstance) { - // Is this node accepting DHT bootstrap requests? - if (!NodeObjectFactory::createNodeInstance()->isAcceptingDhtBootstrap()) { - /* - * This node is not accepting DHT bootstrap requests. - */ - throw new DhtBootstrapNotAcceptedException(array($this, $messageData), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ACCEPTED); - } // END - if - - // Register the DHT bootstrap requesting node with this node - $this->registerNodeByMessageData($messageData); - - // Prepare answer message to be delivered back to the other node - $this->prepareAnswerMessage($messageData, $packageInstance); - } - - /** - * Adds all required elements from given array into data set instance - * - * @param $dataSetInstance An instance of a StoreableCriteria class - * @param $messageData An array with all message data - * @return void - */ - public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { - // Add generic first - parent::addArrayToDataSet($dataSetInstance, $messageData); - - // Add all ements - foreach ($this->messageDataElements as $key) { - // Is it there? - assert(isset($messageData[$key])); - - // Add it - $dataSetInstance->addCriteria($key, $messageData[$key]); - } // END - foreach - } - - /** - * Initializes configuration data from given message data array - * - * @param $messageData An array with all message data - * @return void - */ - protected function initMessageConfigurationData (array $messageData) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-BOOTSTRAP-HANDLER: messageData=' . print_r($messageData, TRUE)); - - // "Walk" throught the translation array - foreach ($this->messageToConfig as $messageKey => $configKey) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-BOOTSTRAP-HANDLER: Setting messageKey=' . $messageKey . ',configKey=' . $configKey . ':' . $messageData[$messageKey]); - - // Set the element in configuration - $this->getConfigInstance()->setConfigEntry($configKey, $messageData[$messageKey]); - } // END - foreach - - // "Walk" throught the config-copy array - foreach ($this->configCopy as $targetKey => $sourceKey) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-BOOTSTRAP-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...'); - - // Copy from source to target key - $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey)); - } // END - foreach - - // Translate last exception into a status code - $statusCode = $this->getTranslatedStatusFromLastException(); - - // Set it in configuration (temporarily) - $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode); - - /* - * Use the DHT instance to get a list of recipients. This means that all - * DHT nodes that accept bootstrap requests are read from the DHT - * database. - */ - $nodeList = DhtObjectFactory::createDhtInstance('node')->findRecipientsByKey(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPT_BOOTSTRAP, 'Y'); - - // Make sure it is an array and has at least one entry - assert(is_array($nodeList)); - assert(count($nodeList) > 0); - - // Set it in configuration - $this->getConfigInstance()->setConfigEntry('dht_nodes', base64_encode(json_encode($nodeList))); - } - - /** - * Removes configuration data with given message data array from global - * configuration - * - * @param $messageData An array with all message data - * @return void - */ - protected function removeMessageConfigurationData (array $messageData) { - // "Walk" throught the translation array again - foreach ($this->messageToConfig as $dummy => $configKey) { - // Now unset this configuration entry (to save some memory) - $this->getConfigInstance()->unsetConfigEntry($configKey); - } // END - foreach - - // "Walk" throught the config-copy array again - foreach ($this->configCopy as $configKey => $dummy) { - // Now unset this configuration entry (to save some memory again) - $this->getConfigInstance()->unsetConfigEntry($configKey); - } // END - foreach - - // Remove temporary "special" values as well - $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS); - $this->getConfigInstance()->unsetConfigEntry('dht_nodes'); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/message-types/requests/.htaccess b/application/hub/classes/handler/message-types/requests/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/message-types/requests/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/message-types/requests/class_NodeMessageRequestNodeListHandler.php b/application/hub/classes/handler/message-types/requests/class_NodeMessageRequestNodeListHandler.php deleted file mode 100644 index e74527a67..000000000 --- a/application/hub/classes/handler/message-types/requests/class_NodeMessageRequestNodeListHandler.php +++ /dev/null @@ -1,190 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class NodeMessageRequestNodeListHandler extends BaseMessageHandler implements HandleableMessage, Registerable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('message_request_node_list'); - - // Init message data array - $this->messageDataElements = array( - XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES, - ); - - // Init config-copy array - $this->configCopy = array( - ); - - // Init search data array - $this->searchData = array( - XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID, - ); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a HandleableMessage class - */ - public final static function createNodeMessageRequestNodeListHandler () { - // Get new instance - $handlerInstance = new NodeMessageRequestNodeListHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Handles data array of the message - * - * @param $messageData An array with message data to handle - * @param $packageInstance An instance of a Receivable class - * @return void - * @throws RequestNotAcceptedException If this node does not accept this request - */ - public function handleMessageData (array $messageData, Receivable $packageInstance) { - // Get node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - - // Is this node accepting announcements? - if (!$nodeInstance->isAcceptingNodeListRequests()) { - /* - * This node is not accepting node list requests. Throw an - * exception to abort any further processing. - */ - throw new RequestNotAcceptedException(array($this, $nodeInstance, $messageData), self::EXCEPTION_REQUEST_NOT_ACCEPTED); - } // END - if - - // Register the announcing node with this node - $this->registerNodeByMessageData($messageData); - - // Prepare answer message to be delivered back to the other node - $this->prepareAnswerMessage($messageData, $packageInstance); - } - - /** - * Adds all required elements from given array into data set instance - * - * @param $dataSetInstance An instance of a StoreableCriteria class - * @param $messageData An array with all message data - * @return void - */ - public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { - // Add generic first - parent::addArrayToDataSet($dataSetInstance, $messageData); - - // Add all ements - foreach ($this->messageDataElements as $key) { - // Is it there? - assert(isset($messageData[$key])); - - // Add it - $dataSetInstance->addCriteria($key, $messageData[$key]); - } // END - foreach - } - - /** - * Initializes configuration data from given message data array - * - * @param $messageData An array with all message data - * @return void - */ - protected function initMessageConfigurationData (array $messageData) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: messageData=' . print_r($messageData, TRUE)); - - // "Walk" throught the config-copy array - foreach ($this->configCopy as $targetKey => $sourceKey) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...'); - - // Copy from source to targetKey - $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey)); - } // END - foreach - - // Query local DHT for nodes except given session id - $nodeList = DhtObjectFactory::createDhtInstance('node')->queryLocalNodeListExceptByMessageData( - $messageData, - $this, - XmlRequestNodeListTemplateEngine::REQUEST_DATA_SESSION_ID, - XmlRequestNodeListTemplateEngine::REQUEST_DATA_ACCEPTED_OBJECT_TYPES, - BaseHubNode::OBJECT_LIST_SEPARATOR - ); - - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REQUEST-HANDLER: Got a node list of ' . count($nodeList) . ' entry/-ies back.'); - - // Set it serialized in configuration (temporarily) - $this->getConfigInstance()->setConfigEntry('node_list', base64_encode(json_encode($nodeList))); - - // Translate last exception into a status code - $statusCode = $this->getTranslatedStatusFromLastException(); - - // Set it in configuration (temporarily) - $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode); - } - - /** - * Removes configuration data with given message data array from global - * configuration - * - * @param $messageData An array with all message data - * @return void - */ - protected function removeMessageConfigurationData (array $messageData) { - // "Walk" throught the config-copy array again - foreach ($this->configCopy as $configKey => $dummy) { - // Now unset this configuration entry (to save some memory again) - $this->getConfigInstance()->unsetConfigEntry($configKey); - } // END - foreach - - // Remove answer status/node list as well - $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS); - $this->getConfigInstance()->unsetConfigEntry('node_list'); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/message-types/self-connect/.htaccess b/application/hub/classes/handler/message-types/self-connect/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/message-types/self-connect/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/message-types/self-connect/class_NodeMessageSelfConnectHandler.php b/application/hub/classes/handler/message-types/self-connect/class_NodeMessageSelfConnectHandler.php deleted file mode 100644 index b485f7b19..000000000 --- a/application/hub/classes/handler/message-types/self-connect/class_NodeMessageSelfConnectHandler.php +++ /dev/null @@ -1,130 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class NodeMessageSelfConnectHandler extends BaseMessageHandler implements HandleableMessage, Registerable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('message_self_connect'); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a HandleableMessage class - */ - public final static function createNodeMessageSelfConnectHandler () { - // Get new instance - $handlerInstance = new NodeMessageSelfConnectHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Handles data array of the message - * - * @param $messageData An array with message data to handle - * @param $packageInstance An instance of a Receivable class - * @return void - */ - public function handleMessageData (array $messageData, Receivable $packageInstance) { - // Get node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - - // Are node id and session id the same? - if (($messageData[XmlSelfConnectTemplateEngine::SELF_CONNECT_DATA_NODE_ID] == $nodeInstance->getNodeId()) && ($messageData[XmlSelfConnectTemplateEngine::SELF_CONNECT_DATA_SESSION_ID] == $nodeInstance->getSessionId())) { - // Both are equal - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SELF-CONNECT: Have connected to myself, both node and session id are equal!'); - - // ... and change state - $nodeInstance->getStateInstance()->nodeHasSelfConnected(); - } else { - // Something really horrible happened - // @TODO Throw an exception here instead of dying - $this->debugInstance(sprintf('[%s:%d]: ids mismatching! messageData=%s', __METHOD__, __LINE__, print_r($messageData, TRUE))); - } - } - - /** - * Adds all required elements from given array into data set instance - * - * @param $dataSetInstance An instance of a StoreableCriteria class - * @param $messageData An array with all message data - * @return void - * @throws UnsupportedOperationException If this method is called - */ - public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData) { - // Add generic first - parent::addArrayToDataSet($dataSetInstance, $messageData); - - // Please don't call this method! - throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); - } - - /** - * Initializes configuration data from given message data array - * - * @param $messageData An array with all message data - * @return void - * @throws UnsupportedOperationException If this method is called - */ - protected function initMessageConfigurationData (array $messageData) { - // Please don't call this method! - throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); - } - - /** - * Removes configuration data with given message data array from global configuration - * - * @param $messageData An array with all message data - * @return void - * @throws UnsupportedOperationException If this method is called - */ - protected function removeMessageConfigurationData (array $messageData) { - // Please don't call this method! - throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/network/.htaccess b/application/hub/classes/handler/network/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/network/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/network/socket/.htaccess b/application/hub/classes/handler/network/socket/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/network/socket/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/network/socket/class_SocketRawDataHandler.php b/application/hub/classes/handler/network/socket/class_SocketRawDataHandler.php deleted file mode 100644 index e02f2f10f..000000000 --- a/application/hub/classes/handler/network/socket/class_SocketRawDataHandler.php +++ /dev/null @@ -1,71 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class SocketRawDataHandler extends BaseRawDataHandler implements Networkable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('socket'); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a Networkable class - */ - public static final function createSocketRawDataHandler () { - // Get new instance - $handlerInstance = new SocketRawDataHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Processes raw data from given resource. This is mostly useful for TCP - * package handling and is implemented in the ???Listener class - * - * @param $resource A valid socket resource array - * @return void - */ - public function processRawDataFromResource (array $socketArray) { - $this->partialStub('socketArray=' . print_r($socketArray, TRUE)); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/network/tcp/.htaccess b/application/hub/classes/handler/network/tcp/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/network/tcp/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/network/tcp/class_ b/application/hub/classes/handler/network/tcp/class_ deleted file mode 100644 index c09a61e2a..000000000 --- a/application/hub/classes/handler/network/tcp/class_ +++ /dev/null @@ -1,220 +0,0 @@ -while (count($clients) > 0) { - // create a copy, so $clients doesn't get modified by socket_select() - $read = $clients; - - // get a list of all the clients that have data to be read from - // if there are no clients with data, go to next iteration - $left = @socket_select($read, $write = null, $except = null, 0, 150); - if ($left < 1) { - continue; - } - - // check if there is a client trying to connect - if (in_array($mainSocket, $read)) { - // accept the client, and add him to the $clients array - $new_sock = socket_accept($mainSocket); - $clients[] = $new_sock; - - // send the client a welcome message - socket_write($new_sock, "No noobs, but I'll make an exception. :)\n". - "There are ".(count($clients) - 1)." client(s) connected to the server.\n"); - - socket_getpeername($new_sock, $ip); - out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:New client connected: {$ip}"); - - // Notify all chatter - if (count($clients) > 2) { - foreach ($clients as $send_sock) { - if ($send_sock != $mainSocket && $send_sock != $new_sock) { - socket_write($send_sock, "Server: Chatter has joined from {$ip}. There are now ".(count($clients) - 1)." clients.\n"); - } - } - } - - // remove the listening socket from the clients-with-data array - $key = array_search($mainSocket, $read); - unset($read[$key]); - } - - // loop through all the clients that have data to read from - foreach ($read as $read_sock) { - // Get client data - socket_getpeername($read_sock, $ip); - - // read until newline or 1024 bytes - // socket_read while show errors when the client is disconnected, so silence the error messages - $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ); - - // check if the client is disconnected - if (($data === FALSE) || (in_array(strtolower(trim($data)), $leaving))) { - - // remove client for $clients array - $key = array_search($read_sock, $clients); - unset($clients[$key]); - out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client from {$ip} disconnected. Left: ".(count($clients) - 1).""); - - // Notify all chatter - if (count($clients) > 1) { - foreach ($clients as $send_sock) { - if ($send_sock != $mainSocket) { - socket_write($send_sock, "Server: Chatter from {$ip} has logged out. ".(count($clients) - 1)." client(s) left.\n"); - } - } - } - - // continue to the next client to read from, if any - socket_write($read_sock, "Server: Good bye.\n"); - socket_shutdown($read_sock, 2); - socket_close($read_sock); - continue; - } elseif (in_array(trim($data), $shutdown)) { - // Is he allowed to shutdown? - if (!in_array($ip, $masters)) { - out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has tried to shutdown the server!"); - socket_write($read_sock, "Server: You are not allowed to shutdown the server!\n"); - $data = ""; - continue; - } - - // Close all connections a leave here - foreach ($clients as $client) { - // Send message to client - if ($client !== $mainSocket && $client != $read_sock) { - socket_write($client, "Server: Shutting down! Thank you for joining us.\n"); - } - - // Quit him - socket_shutdown($client, 2); - socket_close($client); - } // end foreach - - // Leave the loop - $data = ""; - $clients = array(); - continue; - } - - // trim off the trailing/beginning white spaces - $data = trim($data); - - // Test for HTML codes - $tags = strip_tags($data); - - // check if there is any data after trimming off the spaces - if (!empty($data) && $tags == $data && count($clients) > 2) { - // Send confirmation to "chatter" - socket_write($read_sock, "\nServer: Message accepted.\n"); - - // send this to all the clients in the $clients array (except the first one, which is a listening socket) - foreach ($clients as $send_sock) { - - // if its the listening sock or the client that we got the message from, go to the next one in the list - if ($send_sock == $mainSocket || $send_sock == $read_sock) { - continue; - } // END - if - - // write the message to the client -- add a newline character to the end of the message - socket_write($send_sock, "{$ip}:{$data}\n"); - - } // end of broadcast foreach - } elseif ($tags != $data) { - // HTML codes are not allowed - out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has entered HTML code!"); - socket_write($read_sock, "Server: HTML is forbidden!\n"); - } elseif ((count($clients) == 2) && ($read_sock != $mainSocket)) { - // No one else will hear the "chatter" - out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} speaks with himself."); - socket_write($read_sock, "Server: No one will hear you!\n"); - } - } // end of reading foreach -} - -// close the listening socket -socket_close($mainSocket); - -?> -// Own namespace -namespace Hub\Listener\; - -// Import application-specific stuff -use Hub\Helper\Node\NodeHelper; -use Hub\Listener\BaseListener; -use Hub\Listener\Listenable; - -// Import framework stuff -use CoreFramework\Visitor\Visitable; - - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class ???Listener extends BaseListener implements Listenable, Visitable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - } - - /** - * Creates an instance of this class - * - * @param $nodeInstance A NodeHelper instance - * @return $listenerInstance An instance a prepared listener class - */ - public final static function create???Listener (NodeHelper $nodeInstance) { - // Get new instance - $listenerInstance = new ???Listener(); - - // Set the application instance - $listenerInstance->setNodeInstance($nodeInstance); - - // Return the prepared instance - return $listenerInstance; - } - - /** - * Initializes the listener by setting up the required socket server - * - * @return void - * @todo 0% done - */ - public function initListener() { - $this->partialStub('Need to implement this method.'); - } - - /** - * "Listens" for incoming network packages - * - * @return void - * @todo 0% done - */ - public function doListen() { - $this->partialStub('Need to implement this method.'); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/network/tcp/class_TcpRawDataHandler.php b/application/hub/classes/handler/network/tcp/class_TcpRawDataHandler.php deleted file mode 100644 index 3832dc37b..000000000 --- a/application/hub/classes/handler/network/tcp/class_TcpRawDataHandler.php +++ /dev/null @@ -1,125 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class TcpRawDataHandler extends BaseRawDataHandler implements Networkable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('tcp'); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a Networkable class - */ - public static final function createTcpRawDataHandler () { - // Get new instance - $handlerInstance = new TcpRawDataHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Processes raw data from given resource. This is mostly useful for TCP - * package handling and is implemented in the TcpListener class - * - * @param $resource A valid socket resource array - * @return void - */ - public function processRawDataFromResource (array $socketArray) { - // Check the resource - if ((!isset($socketArray[Poolable::SOCKET_ARRAY_INSTANCE])) || (!is_resource($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]))) { - // Throw an exception - throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); - } // END - if - - // Reset error code to unhandled - $this->setErrorCode(self::SOCKET_ERROR_UNHANDLED); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: Handling TCP package from resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE] . ',type=' . $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE] . ',last error=' . socket_strerror($this->lastSocketError)); - - /* - * Read the raw data from socket. If you change PHP_BINARY_READ to - * PHP_NORMAL_READ, this line will endless block. This script does only - * provide simultanous threads, not real. - */ - $rawData = socket_read($socketArray[Poolable::SOCKET_ARRAY_INSTANCE], $this->getConfigInstance()->getConfigEntry('tcp_buffer_length'), PHP_BINARY_READ); - - // Get socket error code back - $this->lastSocketError = socket_last_error($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]); - - // Debug output of read data length - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: rawData[' . gettype($rawData) . ']=' . strlen($rawData) . ',MD5=' . md5($rawData) . ',resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE] . ',error=' . socket_strerror($this->lastSocketError)); - //* NOISY-DEBUG: */ if ($rawData !== FALSE) self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: rawData=' . $rawData); - - // Is it valid? - if ($this->lastSocketError == 11) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: Ignoring error 11 (Resource temporary unavailable) from socket resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]); - - /* - * Error code 11 (Resource temporary unavailable) can be safely - * ignored on non-blocking sockets. The socket is currently not - * sending any data. - */ - socket_clear_error($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]); - - // Skip any further processing - return; - } elseif (($rawData === FALSE) || ($this->lastSocketError > 0)) { - // Network error or connection lost - $this->setErrorCode($this->lastSocketError); - } elseif (empty($rawData)) { - // The peer did send nothing to us which is now being ignored - return; - } else { - /* - * All is fine at this point. So it is okay to add the raw data to - * the stacker. Here it doesn't matter if the raw data is a - * well-formed BASE64-encoded message with start and markers. This - * will be checked later on. - */ - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: Adding ' . strlen($rawData) . ' bytes to stacker ...'); - $this->addRawDataToStacker($rawData); - } - } - -} diff --git a/application/hub/classes/handler/network/udp/.htaccess b/application/hub/classes/handler/network/udp/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/network/udp/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/network/udp/class_UdpRawDataHandler.php b/application/hub/classes/handler/network/udp/class_UdpRawDataHandler.php deleted file mode 100644 index f1aaa667f..000000000 --- a/application/hub/classes/handler/network/udp/class_UdpRawDataHandler.php +++ /dev/null @@ -1,81 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class UdpRawDataHandler extends BaseRawDataHandler implements Networkable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('udp'); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a Networkable class - */ - public static final function createUdpRawDataHandler () { - // Get new instance - $handlerInstance = new UdpRawDataHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Processes raw data from given resource. This is mostly useful for UDP - * package handling and is implemented in the UdpListener class - * - * @param $socketArray A valid socket resource array - * @return void - * @throws InvalidResourceException If the given resource is invalid - * @todo 0% - */ - public function processRawDataFromResource (array $socketArray) { - // Check the resource - if ((!isset($socketArray[Poolable::SOCKET_ARRAY_INSTANCE])) || (!is_resource($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]))) { - // Throw an exception - throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); - } // END - if - - // Implement processing here - $this->partialStub('Please implement this method. resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE] . ',type=' . $socketArray[Poolable::SOCKET_ARRRAY_CONN_TYPE]); - } -} - -// [EOF] -?> diff --git a/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php b/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php index 8353fa0c9..a66f951c4 100644 --- a/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php +++ b/application/hub/classes/handler/protocol/class_BaseProtocolHandler.php @@ -3,13 +3,10 @@ namespace Hub\Handler\Protocol; // Import application-specific sutff +use Hub\Handler\BaseHubHandler; use Hub\Handler\Protocol\HandleableProtocol; -use Hub\Locator\Node\LocateableNode; use Hub\Tools\HubTools; -// Import framework stuff -use CoreFramework\Handler\BaseHandler; - /** * A general handler for protocols such as TCP, UDP and others. * @@ -32,12 +29,7 @@ use CoreFramework\Handler\BaseHandler; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -abstract class BaseProtocolHandler extends BaseHandler implements HandleableProtocol { - /** - * Whole UNL instance - */ - private $universalNodeLocatorInstance = NULL; - +abstract class BaseProtocolHandler extends BaseHubHandler implements HandleableProtocol { /** * Name of used protocol */ @@ -54,17 +46,6 @@ abstract class BaseProtocolHandler extends BaseHandler implements HandleableProt parent::__construct($className); } - /** - * Setter for UNL instance - * - * @para $unlInstance An instance of a LocateableNode class - * @return void - */ - protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { - // Set new UNL data array - $this->universalNodeLocatorInstance = $unlInstance; - } - /** * Validates given UNL very basicly by given regular expression. You * normally don't need/want to overwrite this method as this is a very basic @@ -104,16 +85,6 @@ abstract class BaseProtocolHandler extends BaseHandler implements HandleableProt $this->protocolName = $protocolName; } - /** - * Getter for UNL instance - * - * @return $unlData An instance of a LocateableNode class - */ - public final function getUniversalNodeLocatorInstance () { - // Return UNL data array - return $this->universalNodeLocatorInstance; - } - /** * Default implementation for returning address part, may not be suitable * for IPv4/IPv6 protocol handlers. So you have to overwrite (NOT CHANGE!) this method. diff --git a/application/hub/classes/handler/raw_data/class_ b/application/hub/classes/handler/raw_data/class_ index 9a2846c67..0b5036feb 100644 --- a/application/hub/classes/handler/raw_data/class_ +++ b/application/hub/classes/handler/raw_data/class_ @@ -1,18 +1,19 @@ + * @author Roland Haeder * @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Hub Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,7 +28,7 @@ use Hub\Network\Networkable; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class ???NetworkPackageHandler extends BaseNetworkPackageHandler implements Networkable { +class ???RawDataHandler extends BaseRawDataHandler implements Networkable { /** * Protected constructor * @@ -38,40 +39,30 @@ class ???NetworkPackageHandler extends BaseNetworkPackageHandler implements Netw parent::__construct(__CLASS__); // Set handler name - $this->setHandlerName('!!!'); + $this->setHandlerName('|||'); } /** * Creates an instance of this class * - * @return $handlerInstance An instance of a Networkable class + * @return $handlerInstance An instance of a Networkable class */ - public final static function create???NetworkPackageHandler () { + public static final function create???RawDataHandler () { // Get new instance - $handlerInstance = new ???NetworkPackageHandler(); + $handlerInstance = new ???RawDataHandler(); // Return the prepared instance return $handlerInstance; } /** - * Processes a package from given resource. This is mostly useful for TCP - * package handling and is implemented in the TcpListener class + * Processes raw data from given resource. This is mostly useful for TCP + * package handling and is implemented in the ???Listener class * - * @param $resource A valid resource identifier + * @param $resource A valid socket resource array * @return void - * @throws InvalidResourceException If the given resource is invalid - * @todo 0% */ - public function processResourcePackage ($resource) { - // Check the resource - if (!is_resource($resource)) { - // Throw an exception - throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); - } // END - if - - // Implement processing here - $this->partialStub('Please implement this method.'); + public function processRawDataFromResource (array $socketArray) { } } diff --git a/application/hub/classes/handler/raw_data/class_BaseDataHandler.php b/application/hub/classes/handler/raw_data/class_BaseDataHandler.php deleted file mode 100644 index 6b06d8972..000000000 --- a/application/hub/classes/handler/raw_data/class_BaseDataHandler.php +++ /dev/null @@ -1,201 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -abstract class BaseDataHandler extends BaseHandler { - /** - * Last exception instance from database layer or NULL (default) - */ - private $lastException = NULL; - - /** - * Array with search criteria elements - */ - protected $searchData = array(); - - /** - * Array with all data XML nodes (which hold the actual data) and their values - */ - protected $messageDataElements = array(); - - /** - * Array for translating message data elements (other node's data mostly) - * into configuration elements. - */ - protected $messageToConfig = array(); - - /** - * Array for copying configuration entries - */ - protected $configCopy = array(); - - /** - * Protected constructor - * - * @param $className Name of the class - * @return void - */ - protected function __construct ($className) { - // Call parent constructor - parent::__construct($className); - } - - /** - * Getter for search data array - * - * @return $searchData Search data array - */ - public final function getSearchData () { - return $this->searchData; - } - - /** - * Getter for last exception - * - * @return $lastException Last thrown exception - */ - public final function getLastException () { - return $this->lastException; - } - - /** - * Setter for last exception - * - * @param $lastException Last thrown exception - * @return void - */ - public final function setLastException (FrameworkException $exceptionInstance = NULL) { - $this->lastException = $exceptionInstance; - } - - /** - * Prepares a message as answer for given message data for delivery. - * - * @param $messageData An array with all message data - * @param $packageInstance An instance of a Deliverable instance - * @return void - */ - protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER: Going to send an answer message for ' . $this->getHandlerName() . ' ...'); - - // Get a helper instance based on this handler's name - $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData)); - - // Get node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - - // Load descriptor XML - $helperInstance->loadDescriptorXml($nodeInstance); - - /* - * Set missing (temporary) configuration data, mostly it needs to be - * copied from message data array. - */ - $this->initMessageConfigurationData($messageData); - - // Compile any configuration variables - $helperInstance->getTemplateInstance()->compileConfigInVariables(); - - // Deliver the package - $helperInstance->sendPackage($nodeInstance); - - /* - * Remove temporary configuration - */ - $this->removeMessageConfigurationData($messageData); - - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER: Answer message has been prepared.'); - } - - /** - * Prepares the next message - * - * @param $messageData An array with all message data - * @param $packageInstance An instance of a Deliverable instance - * @return void - */ - protected function prepareNextMessage (array $messageData, Deliverable $packageInstance) { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER: Going to send next message ...'); - - // Get a helper instance based on this handler's name - $helperInstance = ObjectFactory::createObjectByConfiguredName('node_next_' . $this->getHandlerName() . '_helper_class', array($messageData)); - - // Get node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - - // Load descriptor XML - $helperInstance->loadDescriptorXml($nodeInstance); - - /* - * Set missing (temporary) configuration data, mostly it needs to be - * copied from message data array. - */ - $this->initMessageConfigurationData($messageData); - - // Compile any configuration variables - $helperInstance->getTemplateInstance()->compileConfigInVariables(); - - // Deliver the package - $helperInstance->sendPackage($nodeInstance); - - /* - * Remove temporary configuration - */ - $this->removeMessageConfigurationData($messageData); - - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER: Next message has been prepared.'); - } - - /** - * Initializes configuration data from given message data array - * - * @param $messageData An array with all message data - * @return void - */ - abstract protected function initMessageConfigurationData (array $messageData); - - /** - * Removes configuration data with given message data array from global - * configuration - * - * @param $messageData An array with all message data - * @return void - */ - abstract protected function removeMessageConfigurationData (array $messageData); - -} diff --git a/application/hub/classes/handler/raw_data/class_BaseRawDataHandler.php b/application/hub/classes/handler/raw_data/class_BaseRawDataHandler.php new file mode 100644 index 000000000..b05eb66e1 --- /dev/null +++ b/application/hub/classes/handler/raw_data/class_BaseRawDataHandler.php @@ -0,0 +1,177 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class BaseRawDataHandler extends BaseHubHandler implements HubInterface { + // - Package errors + const PACKAGE_ERROR_INVALID_DATA = 'invalid_data'; // Invalid data in package found + const PACKAGE_ERROR_INCOMPLETE_DATA = 'incomplete_data'; // Incomplete data sent (e.g. field is missing) + const PACKAGE_ERROR_INVALID_CONTENT = 'invalid_content'; // Content is invalid (e.g. not well-formed) + const PACKAGE_ERROR_RECIPIENT_MISMATCH = 'recipient_error'; // Recipient is not us + const PACKAGE_LEVEL_CHECK_OKAY = 'checked_package'; // Package is fine + + // Package data + const PACKAGE_RAW_DATA = 'raw_data'; + const PACKAGE_ERROR_CODE = 'error_code'; + + // Start/end marker + const STREAM_START_MARKER = '[[S]]'; + const STREAM_END_MARKER = '[[E]]'; + + /** + * Stacker for raw data + */ + const STACKER_NAME_RAW_DATA = 'raw_data'; + + /** + * Error code from socket + */ + private $errorCode = -1; + + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + + // Set error code to 'unknown' + $this->setErrorCode(StorableSocket::SOCKET_ERROR_UNKNOWN); + + // Init stacker instance for processed raw data + $stackInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_stacker_class'); + + // Remember this in this package handler + $this->setStackInstance($stackInstance); + + // Init stacker + $this->initStack(); + } + + /** + * Initializes the stacker for raw data + * + * @return void + */ + protected function initStack () { + $this->getStackInstance()->initStack(self::STACKER_NAME_RAW_DATA); + } + + /** + * Adds given raw data to the raw data stacker + * + * @param $rawData raw data from the socket resource + * @return void + */ + protected function addRawDataToStacker ($rawData) { + /* + * Add the deocoded data and error code to the stacker so other classes + * (e.g. NetworkPackage) can "pop" it from the stacker. + */ + $this->getStackInstance()->pushNamed(self::STACKER_NAME_RAW_DATA, array( + self::PACKAGE_RAW_DATA => $rawData, + self::PACKAGE_ERROR_CODE => $this->getErrorCode() + )); + } + + /** + * Checks whether raw data is pending for further processing. + * + * @return $isPending Whether raw data is pending + */ + public function isRawDataPending () { + // Does the stacker have some entries (not empty)? + $isPending = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_RAW_DATA)); + + // Return it + return $isPending; + } + + /** + * "Getter" for next raw data from the stacker + * + * @return $rawData Raw data from the stacker + */ + public function getNextRawData () { + // "Pop" the raw data from the stacker + $rawData = $this->getStackInstance()->popNamed(self::STACKER_NAME_RAW_DATA); + + // And return it + return $rawData; + } + + /** + * Checks whether the 'recipient' field matches our own an universal node + * locator. + * + * @param $packageData Raw package data + * @return $matches Whether it matches + * @todo This method will be moved to a better place + */ + protected function ifRecipientMatchesOwnUniversalNodeLocator (array $packageData) { + // Construct own address first + $ownAddress = NodeObjectFactory::createNodeInstance()->determineUniversalNodeLocator(); + + // Does it match? + $matches = ($ownAddress === $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + + // Return result + return $matches; + } + + /** + * Setter for error code + * + * @param $errorCode The error code we shall set + * @return void + */ + public final function setErrorCode ($errorCode) { + $this->errorCode = $errorCode; + } + + /** + * Getter for error code + * + * @return $errorCode The error code + */ + public final function getErrorCode () { + return $this->errorCode; + } + +} diff --git a/application/hub/classes/handler/raw_data/network/.htaccess b/application/hub/classes/handler/raw_data/network/.htaccess deleted file mode 100644 index 3a4288278..000000000 --- a/application/hub/classes/handler/raw_data/network/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/application/hub/classes/handler/raw_data/network/class_ b/application/hub/classes/handler/raw_data/network/class_ deleted file mode 100644 index 0b5036feb..000000000 --- a/application/hub/classes/handler/raw_data/network/class_ +++ /dev/null @@ -1,68 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class ???RawDataHandler extends BaseRawDataHandler implements Networkable { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Set handler name - $this->setHandlerName('|||'); - } - - /** - * Creates an instance of this class - * - * @return $handlerInstance An instance of a Networkable class - */ - public static final function create???RawDataHandler () { - // Get new instance - $handlerInstance = new ???RawDataHandler(); - - // Return the prepared instance - return $handlerInstance; - } - - /** - * Processes raw data from given resource. This is mostly useful for TCP - * package handling and is implemented in the ???Listener class - * - * @param $resource A valid socket resource array - * @return void - */ - public function processRawDataFromResource (array $socketArray) { - } - -} diff --git a/application/hub/classes/handler/raw_data/network/class_BaseRawDataHandler.php b/application/hub/classes/handler/raw_data/network/class_BaseRawDataHandler.php deleted file mode 100644 index 914baddf6..000000000 --- a/application/hub/classes/handler/raw_data/network/class_BaseRawDataHandler.php +++ /dev/null @@ -1,175 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Hub Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class BaseRawDataHandler extends BaseHandler { - // - Package errors - const PACKAGE_ERROR_INVALID_DATA = 'invalid_data'; // Invalid data in package found - const PACKAGE_ERROR_INCOMPLETE_DATA = 'incomplete_data'; // Incomplete data sent (e.g. field is missing) - const PACKAGE_ERROR_INVALID_CONTENT = 'invalid_content'; // Content is invalid (e.g. not well-formed) - const PACKAGE_ERROR_RECIPIENT_MISMATCH = 'recipient_error'; // Recipient is not us - const PACKAGE_LEVEL_CHECK_OKAY = 'checked_package'; // Package is fine - - // Package data - const PACKAGE_RAW_DATA = 'raw_data'; - const PACKAGE_ERROR_CODE = 'error_code'; - - // Start/end marker - const STREAM_START_MARKER = '[[S]]'; - const STREAM_END_MARKER = '[[E]]'; - - /** - * Stacker for raw data - */ - const STACKER_NAME_RAW_DATA = 'raw_data'; - - /** - * Error code from socket - */ - private $errorCode = -1; - - /** - * Protected constructor - * - * @param $className Name of the class - * @return void - */ - protected function __construct ($className) { - // Call parent constructor - parent::__construct($className); - - // Set error code to 'unknown' - $this->setErrorCode(StorableSocket::SOCKET_ERROR_UNKNOWN); - - // Init stacker instance for processed raw data - $stackInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_stacker_class'); - - // Remember this in this package handler - $this->setStackInstance($stackInstance); - - // Init stacker - $this->initStack(); - } - - /** - * Initializes the stacker for raw data - * - * @return void - */ - protected function initStack () { - $this->getStackInstance()->initStack(self::STACKER_NAME_RAW_DATA); - } - - /** - * Adds given raw data to the raw data stacker - * - * @param $rawData raw data from the socket resource - * @return void - */ - protected function addRawDataToStacker ($rawData) { - /* - * Add the deocoded data and error code to the stacker so other classes - * (e.g. NetworkPackage) can "pop" it from the stacker. - */ - $this->getStackInstance()->pushNamed(self::STACKER_NAME_RAW_DATA, array( - self::PACKAGE_RAW_DATA => $rawData, - self::PACKAGE_ERROR_CODE => $this->getErrorCode() - )); - } - - /** - * Checks whether raw data is pending for further processing. - * - * @return $isPending Whether raw data is pending - */ - public function isRawDataPending () { - // Does the stacker have some entries (not empty)? - $isPending = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_RAW_DATA)); - - // Return it - return $isPending; - } - - /** - * "Getter" for next raw data from the stacker - * - * @return $rawData Raw data from the stacker - */ - public function getNextRawData () { - // "Pop" the raw data from the stacker - $rawData = $this->getStackInstance()->popNamed(self::STACKER_NAME_RAW_DATA); - - // And return it - return $rawData; - } - - /** - * Checks whether the 'recipient' field matches our own an universal node - * locator. - * - * @param $packageData Raw package data - * @return $matches Whether it matches - * @todo This method will be moved to a better place - */ - protected function ifRecipientMatchesOwnUniversalNodeLocator (array $packageData) { - // Construct own address first - $ownAddress = NodeObjectFactory::createNodeInstance()->determineUniversalNodeLocator(); - - // Does it match? - $matches = ($ownAddress === $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); - - // Return result - return $matches; - } - - /** - * Setter for error code - * - * @param $errorCode The error code we shall set - * @return void - */ - public final function setErrorCode ($errorCode) { - $this->errorCode = $errorCode; - } - - /** - * Getter for error code - * - * @return $errorCode The error code - */ - public final function getErrorCode () { - return $this->errorCode; - } - -} diff --git a/application/hub/classes/handler/raw_data/socket/.htaccess b/application/hub/classes/handler/raw_data/socket/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/raw_data/socket/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/raw_data/socket/class_SocketRawDataHandler.php b/application/hub/classes/handler/raw_data/socket/class_SocketRawDataHandler.php new file mode 100644 index 000000000..e02f2f10f --- /dev/null +++ b/application/hub/classes/handler/raw_data/socket/class_SocketRawDataHandler.php @@ -0,0 +1,71 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class SocketRawDataHandler extends BaseRawDataHandler implements Networkable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('socket'); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a Networkable class + */ + public static final function createSocketRawDataHandler () { + // Get new instance + $handlerInstance = new SocketRawDataHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Processes raw data from given resource. This is mostly useful for TCP + * package handling and is implemented in the ???Listener class + * + * @param $resource A valid socket resource array + * @return void + */ + public function processRawDataFromResource (array $socketArray) { + $this->partialStub('socketArray=' . print_r($socketArray, TRUE)); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/raw_data/tcp/.htaccess b/application/hub/classes/handler/raw_data/tcp/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/raw_data/tcp/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/raw_data/tcp/class_ b/application/hub/classes/handler/raw_data/tcp/class_ new file mode 100644 index 000000000..c09a61e2a --- /dev/null +++ b/application/hub/classes/handler/raw_data/tcp/class_ @@ -0,0 +1,220 @@ +while (count($clients) > 0) { + // create a copy, so $clients doesn't get modified by socket_select() + $read = $clients; + + // get a list of all the clients that have data to be read from + // if there are no clients with data, go to next iteration + $left = @socket_select($read, $write = null, $except = null, 0, 150); + if ($left < 1) { + continue; + } + + // check if there is a client trying to connect + if (in_array($mainSocket, $read)) { + // accept the client, and add him to the $clients array + $new_sock = socket_accept($mainSocket); + $clients[] = $new_sock; + + // send the client a welcome message + socket_write($new_sock, "No noobs, but I'll make an exception. :)\n". + "There are ".(count($clients) - 1)." client(s) connected to the server.\n"); + + socket_getpeername($new_sock, $ip); + out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:New client connected: {$ip}"); + + // Notify all chatter + if (count($clients) > 2) { + foreach ($clients as $send_sock) { + if ($send_sock != $mainSocket && $send_sock != $new_sock) { + socket_write($send_sock, "Server: Chatter has joined from {$ip}. There are now ".(count($clients) - 1)." clients.\n"); + } + } + } + + // remove the listening socket from the clients-with-data array + $key = array_search($mainSocket, $read); + unset($read[$key]); + } + + // loop through all the clients that have data to read from + foreach ($read as $read_sock) { + // Get client data + socket_getpeername($read_sock, $ip); + + // read until newline or 1024 bytes + // socket_read while show errors when the client is disconnected, so silence the error messages + $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ); + + // check if the client is disconnected + if (($data === FALSE) || (in_array(strtolower(trim($data)), $leaving))) { + + // remove client for $clients array + $key = array_search($read_sock, $clients); + unset($clients[$key]); + out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client from {$ip} disconnected. Left: ".(count($clients) - 1).""); + + // Notify all chatter + if (count($clients) > 1) { + foreach ($clients as $send_sock) { + if ($send_sock != $mainSocket) { + socket_write($send_sock, "Server: Chatter from {$ip} has logged out. ".(count($clients) - 1)." client(s) left.\n"); + } + } + } + + // continue to the next client to read from, if any + socket_write($read_sock, "Server: Good bye.\n"); + socket_shutdown($read_sock, 2); + socket_close($read_sock); + continue; + } elseif (in_array(trim($data), $shutdown)) { + // Is he allowed to shutdown? + if (!in_array($ip, $masters)) { + out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has tried to shutdown the server!"); + socket_write($read_sock, "Server: You are not allowed to shutdown the server!\n"); + $data = ""; + continue; + } + + // Close all connections a leave here + foreach ($clients as $client) { + // Send message to client + if ($client !== $mainSocket && $client != $read_sock) { + socket_write($client, "Server: Shutting down! Thank you for joining us.\n"); + } + + // Quit him + socket_shutdown($client, 2); + socket_close($client); + } // end foreach + + // Leave the loop + $data = ""; + $clients = array(); + continue; + } + + // trim off the trailing/beginning white spaces + $data = trim($data); + + // Test for HTML codes + $tags = strip_tags($data); + + // check if there is any data after trimming off the spaces + if (!empty($data) && $tags == $data && count($clients) > 2) { + // Send confirmation to "chatter" + socket_write($read_sock, "\nServer: Message accepted.\n"); + + // send this to all the clients in the $clients array (except the first one, which is a listening socket) + foreach ($clients as $send_sock) { + + // if its the listening sock or the client that we got the message from, go to the next one in the list + if ($send_sock == $mainSocket || $send_sock == $read_sock) { + continue; + } // END - if + + // write the message to the client -- add a newline character to the end of the message + socket_write($send_sock, "{$ip}:{$data}\n"); + + } // end of broadcast foreach + } elseif ($tags != $data) { + // HTML codes are not allowed + out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has entered HTML code!"); + socket_write($read_sock, "Server: HTML is forbidden!\n"); + } elseif ((count($clients) == 2) && ($read_sock != $mainSocket)) { + // No one else will hear the "chatter" + out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} speaks with himself."); + socket_write($read_sock, "Server: No one will hear you!\n"); + } + } // end of reading foreach +} + +// close the listening socket +socket_close($mainSocket); + +?> +// Own namespace +namespace Hub\Listener\; + +// Import application-specific stuff +use Hub\Helper\Node\NodeHelper; +use Hub\Listener\BaseListener; +use Hub\Listener\Listenable; + +// Import framework stuff +use CoreFramework\Visitor\Visitable; + + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class ???Listener extends BaseListener implements Listenable, Visitable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this class + * + * @param $nodeInstance A NodeHelper instance + * @return $listenerInstance An instance a prepared listener class + */ + public final static function create???Listener (NodeHelper $nodeInstance) { + // Get new instance + $listenerInstance = new ???Listener(); + + // Set the application instance + $listenerInstance->setNodeInstance($nodeInstance); + + // Return the prepared instance + return $listenerInstance; + } + + /** + * Initializes the listener by setting up the required socket server + * + * @return void + * @todo 0% done + */ + public function initListener() { + $this->partialStub('Need to implement this method.'); + } + + /** + * "Listens" for incoming network packages + * + * @return void + * @todo 0% done + */ + public function doListen() { + $this->partialStub('Need to implement this method.'); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/handler/raw_data/tcp/class_TcpRawDataHandler.php b/application/hub/classes/handler/raw_data/tcp/class_TcpRawDataHandler.php new file mode 100644 index 000000000..3832dc37b --- /dev/null +++ b/application/hub/classes/handler/raw_data/tcp/class_TcpRawDataHandler.php @@ -0,0 +1,125 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class TcpRawDataHandler extends BaseRawDataHandler implements Networkable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('tcp'); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a Networkable class + */ + public static final function createTcpRawDataHandler () { + // Get new instance + $handlerInstance = new TcpRawDataHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Processes raw data from given resource. This is mostly useful for TCP + * package handling and is implemented in the TcpListener class + * + * @param $resource A valid socket resource array + * @return void + */ + public function processRawDataFromResource (array $socketArray) { + // Check the resource + if ((!isset($socketArray[Poolable::SOCKET_ARRAY_INSTANCE])) || (!is_resource($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]))) { + // Throw an exception + throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); + } // END - if + + // Reset error code to unhandled + $this->setErrorCode(self::SOCKET_ERROR_UNHANDLED); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: Handling TCP package from resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE] . ',type=' . $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE] . ',last error=' . socket_strerror($this->lastSocketError)); + + /* + * Read the raw data from socket. If you change PHP_BINARY_READ to + * PHP_NORMAL_READ, this line will endless block. This script does only + * provide simultanous threads, not real. + */ + $rawData = socket_read($socketArray[Poolable::SOCKET_ARRAY_INSTANCE], $this->getConfigInstance()->getConfigEntry('tcp_buffer_length'), PHP_BINARY_READ); + + // Get socket error code back + $this->lastSocketError = socket_last_error($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]); + + // Debug output of read data length + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: rawData[' . gettype($rawData) . ']=' . strlen($rawData) . ',MD5=' . md5($rawData) . ',resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE] . ',error=' . socket_strerror($this->lastSocketError)); + //* NOISY-DEBUG: */ if ($rawData !== FALSE) self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: rawData=' . $rawData); + + // Is it valid? + if ($this->lastSocketError == 11) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: Ignoring error 11 (Resource temporary unavailable) from socket resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]); + + /* + * Error code 11 (Resource temporary unavailable) can be safely + * ignored on non-blocking sockets. The socket is currently not + * sending any data. + */ + socket_clear_error($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]); + + // Skip any further processing + return; + } elseif (($rawData === FALSE) || ($this->lastSocketError > 0)) { + // Network error or connection lost + $this->setErrorCode($this->lastSocketError); + } elseif (empty($rawData)) { + // The peer did send nothing to us which is now being ignored + return; + } else { + /* + * All is fine at this point. So it is okay to add the raw data to + * the stacker. Here it doesn't matter if the raw data is a + * well-formed BASE64-encoded message with start and markers. This + * will be checked later on. + */ + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-HANDLER: Adding ' . strlen($rawData) . ' bytes to stacker ...'); + $this->addRawDataToStacker($rawData); + } + } + +} diff --git a/application/hub/classes/handler/raw_data/udp/.htaccess b/application/hub/classes/handler/raw_data/udp/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/raw_data/udp/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/handler/raw_data/udp/class_UdpRawDataHandler.php b/application/hub/classes/handler/raw_data/udp/class_UdpRawDataHandler.php new file mode 100644 index 000000000..f1aaa667f --- /dev/null +++ b/application/hub/classes/handler/raw_data/udp/class_UdpRawDataHandler.php @@ -0,0 +1,81 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class UdpRawDataHandler extends BaseRawDataHandler implements Networkable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set handler name + $this->setHandlerName('udp'); + } + + /** + * Creates an instance of this class + * + * @return $handlerInstance An instance of a Networkable class + */ + public static final function createUdpRawDataHandler () { + // Get new instance + $handlerInstance = new UdpRawDataHandler(); + + // Return the prepared instance + return $handlerInstance; + } + + /** + * Processes raw data from given resource. This is mostly useful for UDP + * package handling and is implemented in the UdpListener class + * + * @param $socketArray A valid socket resource array + * @return void + * @throws InvalidResourceException If the given resource is invalid + * @todo 0% + */ + public function processRawDataFromResource (array $socketArray) { + // Check the resource + if ((!isset($socketArray[Poolable::SOCKET_ARRAY_INSTANCE])) || (!is_resource($socketArray[Poolable::SOCKET_ARRAY_INSTANCE]))) { + // Throw an exception + throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); + } // END - if + + // Implement processing here + $this->partialStub('Please implement this method. resource=' . $socketArray[Poolable::SOCKET_ARRAY_INSTANCE] . ',type=' . $socketArray[Poolable::SOCKET_ARRRAY_CONN_TYPE]); + } +} + +// [EOF] +?> diff --git a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php index 355c76685..5fe5b8341 100644 --- a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php +++ b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php @@ -3,6 +3,7 @@ namespace Hub\Helper\Connection; // Import application-specific stuff +use Hub\Container\Socket\StorableSocket; use Hub\Factory\Fragmenter\FragmenterFactory; use Hub\Factory\Network\NetworkPackageFactory; use Hub\Factory\State\Peer\PeerStateFactory; @@ -109,7 +110,7 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Visitable, Reg */ public final function __toString () { // Class name representation - $class = self::getConnectionClassName($this->getSocketInstance(), parent::__toString()); + $class = self::getConnectionClassNameFromSocket($this->getSocketInstance(), parent::__toString()); // Return it return $class; @@ -137,12 +138,11 @@ class BaseConnectionHelper extends BaseHubSystemHelper implements Visitable, Reg /** * Static "getter" for this connection class' name * - * @param $address IP address - * @param $port Port number - * @param $className Original class name + * @param $socketInstance An instance of a StorableSocket class * @return $class Expanded class name */ - public static function getConnectionClassName ($address, $port, $className) { + public static function getConnectionClassNameFromSocket (StorableSocket $socketInstance) { + die(__METHOD__.':socketInstance='.print_r($socketInstance, TRUE)); // Construct it $class = $address . ':' . $port . ':' . $className; diff --git a/application/hub/classes/listener/class_BaseListenerDecorator.php b/application/hub/classes/listener/class_BaseListenerDecorator.php index 0d8564113..46fecd8ce 100644 --- a/application/hub/classes/listener/class_BaseListenerDecorator.php +++ b/application/hub/classes/listener/class_BaseListenerDecorator.php @@ -6,10 +6,12 @@ namespace Hub\Listener; use Hub\Container\Socket\StorableSocket; use Hub\Factory\Network\NetworkPackageFactory; use Hub\Listener\Listenable; +use Hub\Locator\Node\LocateableNode; use Hub\Network\Networkable; // Import framework stuff use CoreFramework\Generic\BaseDecorator; +use CoreFramework\Generic\UnsupportedOperationException; use CoreFramework\Visitor\Visitable; use CoreFramework\Visitor\Visitor; @@ -284,4 +286,25 @@ class BaseListenerDecorator extends BaseDecorator implements Visitable { $this->protocolName = $protocolName; } + /** + * Setter for UNL instance + * + * @para $unlInstance An instance of a LocateableNode class + * @return void + * @throws UnsupportedOperationException If this method is called + */ + protected final function setUniversalNodeLocatorInstance (LocateableNode $unlInstance) { + throw new UnsupportedOperationException('This method should not be called.'); + } + + /** + * Getter for UNL instance + * + * @return $unlData An instance of a LocateableNode class + * @throws UnsupportedOperationException If this method is called + */ + public final function getUniversalNodeLocatorInstance () { + throw new UnsupportedOperationException('This method should not be called.'); + } + } diff --git a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php index f526eebf2..8fec88c5c 100644 --- a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php +++ b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php @@ -6,6 +6,8 @@ namespace Hub\Resolver\State\Peer; use Hub\Container\Socket\StorableSocket; use Hub\Factory\State\Peer\PeerStateFactory; use Hub\Helper\Connection\ConnectionHelper; +use Hub\Resolver\State\BaseStateResolver; +use Hub\Resolver\State\StateResolver; // Import framework stuff use CoreFramework\Socket\InvalidSocketException; diff --git a/application/hub/classes/states/peer/class_ b/application/hub/classes/states/peer/class_ index e9e2ac927..13cbe515c 100644 --- a/application/hub/classes/states/peer/class_ +++ b/application/hub/classes/states/peer/class_ @@ -4,6 +4,7 @@ namespace Hub\State\Peer; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; +use Hub\State\Peer\PeerStateable; // Import framework stuff use CoreFramework\State\Stateable; diff --git a/application/hub/classes/states/peer/connected/class_ConnectedPeerState.php b/application/hub/classes/states/peer/connected/class_ConnectedPeerState.php index 9b71fe1e3..45e82c18a 100644 --- a/application/hub/classes/states/peer/connected/class_ConnectedPeerState.php +++ b/application/hub/classes/states/peer/connected/class_ConnectedPeerState.php @@ -4,6 +4,7 @@ namespace Hub\State\Peer; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; +use Hub\State\Peer\PeerStateable; // Import framework stuff use CoreFramework\State\Stateable; diff --git a/application/hub/classes/states/peer/errors/class_ConnectionRefusedPeerState.php b/application/hub/classes/states/peer/errors/class_ConnectionRefusedPeerState.php index f12869ae6..cf42313a9 100644 --- a/application/hub/classes/states/peer/errors/class_ConnectionRefusedPeerState.php +++ b/application/hub/classes/states/peer/errors/class_ConnectionRefusedPeerState.php @@ -4,6 +4,7 @@ namespace Hub\State\Peer; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; +use Hub\State\Peer\PeerStateable; // Import framework stuff use CoreFramework\State\Stateable; @@ -56,7 +57,5 @@ class ConnectionRefusedPeerState extends BasePeerState implements PeerStateable // Return the prepared instance return $stateInstance; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/states/peer/errors/class_ConnectionTimedOutPeerState.php b/application/hub/classes/states/peer/errors/class_ConnectionTimedOutPeerState.php index 7313e9390..2de0616eb 100644 --- a/application/hub/classes/states/peer/errors/class_ConnectionTimedOutPeerState.php +++ b/application/hub/classes/states/peer/errors/class_ConnectionTimedOutPeerState.php @@ -4,6 +4,7 @@ namespace Hub\State\Peer; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; +use Hub\State\Peer\PeerStateable; // Import framework stuff use CoreFramework\State\Stateable; @@ -56,7 +57,5 @@ class ConnectionTimedOutPeerState extends BasePeerState implements PeerStateable // Return the prepared instance return $stateInstance; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/states/peer/errors/class_NoRouteToHostPeerState.php b/application/hub/classes/states/peer/errors/class_NoRouteToHostPeerState.php index edaa29f83..b90fb9ebe 100644 --- a/application/hub/classes/states/peer/errors/class_NoRouteToHostPeerState.php +++ b/application/hub/classes/states/peer/errors/class_NoRouteToHostPeerState.php @@ -4,6 +4,7 @@ namespace Hub\State\Peer; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; +use Hub\State\Peer\PeerStateable; // Import framework stuff use CoreFramework\State\Stateable; @@ -56,7 +57,5 @@ class NoRouteToHostPeerState extends BasePeerState implements PeerStateable { // Return the prepared instance return $stateInstance; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/states/peer/errors/class_OperationAlreadyProgressPeerState.php b/application/hub/classes/states/peer/errors/class_OperationAlreadyProgressPeerState.php index 99d39acc9..f33ed8994 100644 --- a/application/hub/classes/states/peer/errors/class_OperationAlreadyProgressPeerState.php +++ b/application/hub/classes/states/peer/errors/class_OperationAlreadyProgressPeerState.php @@ -4,6 +4,7 @@ namespace Hub\State\Peer; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; +use Hub\State\Peer\PeerStateable; // Import framework stuff use CoreFramework\State\Stateable; @@ -56,7 +57,5 @@ class OperationAlreadyProgressPeerState extends BasePeerState implements PeerSta // Return the prepared instance return $stateInstance; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/states/peer/errors/class_ProblemPeerState.php b/application/hub/classes/states/peer/errors/class_ProblemPeerState.php index ca6d00f2d..51fce8833 100644 --- a/application/hub/classes/states/peer/errors/class_ProblemPeerState.php +++ b/application/hub/classes/states/peer/errors/class_ProblemPeerState.php @@ -4,6 +4,7 @@ namespace Hub\State\Peer; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; +use Hub\State\Peer\PeerStateable; // Import framework stuff use CoreFramework\State\Stateable; @@ -56,7 +57,5 @@ class ProblemPeerState extends BasePeerState implements PeerStateable { // Return the prepared instance return $stateInstance; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/states/peer/errors/class_TransportEndpointGonePeerState.php b/application/hub/classes/states/peer/errors/class_TransportEndpointGonePeerState.php index 30aa74d6c..3a6430742 100644 --- a/application/hub/classes/states/peer/errors/class_TransportEndpointGonePeerState.php +++ b/application/hub/classes/states/peer/errors/class_TransportEndpointGonePeerState.php @@ -4,6 +4,7 @@ namespace Hub\State\Peer; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; +use Hub\State\Peer\PeerStateable; // Import framework stuff use CoreFramework\State\Stateable; @@ -56,7 +57,5 @@ class TransportEndpointGonePeerState extends BasePeerState implements PeerStatea // Return the prepared instance return $stateInstance; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/states/peer/init/class_InitPeerState.php b/application/hub/classes/states/peer/init/class_InitPeerState.php index da101a976..a3aab4723 100644 --- a/application/hub/classes/states/peer/init/class_InitPeerState.php +++ b/application/hub/classes/states/peer/init/class_InitPeerState.php @@ -53,7 +53,5 @@ class InitPeerState extends BasePeerState implements PeerStateable { // Return the prepared instance return $stateInstance; } -} -// [EOF] -?> +} diff --git a/application/hub/config.php b/application/hub/config.php index e0a9a1704..391824b0e 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -61,7 +61,7 @@ $cfg->setConfigEntry('node_dht_db_wrapper_class', 'Hub\Database\Frontend\Node\Dh $cfg->setConfigEntry('node_dht_list_limit', 20); // CFG: PEER-LOOKUP-DB-WRAPPER-CLASS -$cfg->setConfigEntry('peer_state_lookup_db_wrapper_class', 'PeerStateLookupDatabaseWrapper'); +$cfg->setConfigEntry('peer_state_lookup_db_wrapper_class', 'Hub\Database\Frontend\Node\PeerState\PeerStateLookupDatabaseWrapper'); // CFG: WEB-CONTENT-TYPE $cfg->setConfigEntry('web_content_type', ''); diff --git a/application/hub/interfaces/class_HubInterface.php b/application/hub/interfaces/class_HubInterface.php index 68b7d2147..2abd9c85c 100644 --- a/application/hub/interfaces/class_HubInterface.php +++ b/application/hub/interfaces/class_HubInterface.php @@ -98,4 +98,11 @@ interface HubInterface extends FrameworkInterface { */ function getSocketInstance (); + /** + * Getter for UNL instance + * + * @return $unlInstance An instance of a LocateableNode class + */ + function getUniversalNodeLocatorInstance (); + } diff --git a/application/hub/interfaces/handler/answer-status/class_HandleableAnswerStatus.php b/application/hub/interfaces/handler/answer-status/class_HandleableAnswerStatus.php index b8e7c78f4..74f64de7e 100644 --- a/application/hub/interfaces/handler/answer-status/class_HandleableAnswerStatus.php +++ b/application/hub/interfaces/handler/answer-status/class_HandleableAnswerStatus.php @@ -3,6 +3,7 @@ namespace Hub\AnswerStatus\Node; // Import application-specific stuff +use Hub\Generic\HubInterface; use Hub\Network\Receive\Receivable; // Import framework stuff @@ -31,7 +32,7 @@ use CoreFramework\Handler\DataSet\HandleableDataSet; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface HandleableAnswerStatus extends HandleableDataSet { +interface HandleableAnswerStatus extends HandleableDataSet, HubInterface { /** * Handles given message data array * @@ -49,7 +50,5 @@ interface HandleableAnswerStatus extends HandleableDataSet { * @return void */ function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $messageData); -} -// [EOF] -?> +} diff --git a/application/hub/interfaces/handler/chunks/class_HandleableChunks.php b/application/hub/interfaces/handler/chunks/class_HandleableChunks.php index 1d0a90a09..f8f920d37 100644 --- a/application/hub/interfaces/handler/chunks/class_HandleableChunks.php +++ b/application/hub/interfaces/handler/chunks/class_HandleableChunks.php @@ -2,6 +2,9 @@ // Own namespace namespace Hub\Handler\Network\RawData\Chunks; +// Import application-specific stuff +use Hub\Generic\HubInterface; + // Import framework stuff use CoreFramework\Handler\Handleable; @@ -27,7 +30,7 @@ use CoreFramework\Handler\Handleable; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface HandleableChunks extends Handleable { +interface HandleableChunks extends Handleable, HubInterface { /** * Adds all chunks if the last one verifies as a 'final chunk'. * @@ -101,7 +104,5 @@ interface HandleableChunks extends Handleable { * @return void */ function handledAssembledRawPackageData (); -} -// [EOF] -?> +} diff --git a/application/hub/interfaces/handler/message-types/class_HandleableMessage.php b/application/hub/interfaces/handler/message-types/class_HandleableMessage.php index 5ad7ee260..ba9653576 100644 --- a/application/hub/interfaces/handler/message-types/class_HandleableMessage.php +++ b/application/hub/interfaces/handler/message-types/class_HandleableMessage.php @@ -3,6 +3,7 @@ namespace Hub\Handler\Message; // Import application-specific stuff +use Hub\Generic\HubInterface; use Hub\Network\Receive\Receivable; // Import framework stuff @@ -30,7 +31,7 @@ use CoreFramework\Handler\DataSet\HandleableDataSet; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface HandleableMessage extends HandleableDataSet { +interface HandleableMessage extends HandleableDataSet, HubInterface { /** * Handles data array of the message * @@ -48,7 +49,5 @@ interface HandleableMessage extends HandleableDataSet { * @return void */ function postHandleMessageData (array $messageData, Receivable $packageInstance); -} -// [EOF] -?> +} diff --git a/application/hub/interfaces/handler/network/class_Networkable.php b/application/hub/interfaces/handler/network/class_Networkable.php index caf56cf9e..4cd6ed37c 100644 --- a/application/hub/interfaces/handler/network/class_Networkable.php +++ b/application/hub/interfaces/handler/network/class_Networkable.php @@ -2,6 +2,9 @@ // Own namespace namespace Hub\Network; +// Import application-specific stuff +use Hub\Generic\HubInterface; + // Import framework stuff use CoreFramework\Handler\DataSet\HandleableDataSet; @@ -27,7 +30,7 @@ use CoreFramework\Handler\DataSet\HandleableDataSet; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface Networkable extends HandleableDataSet { +interface Networkable extends HandleableDataSet, HubInterface { /** * Processes raw data from given resource. This is mostly useful for TCP * package handling and is implemented in the TcpListener class @@ -51,7 +54,5 @@ interface Networkable extends HandleableDataSet { * @return $decodedData Raw data from the stacker */ function getNextRawData (); -} -// [EOF] -?> +} diff --git a/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php b/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php index 79575b8b8..33528ffe5 100644 --- a/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php +++ b/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php @@ -2,6 +2,9 @@ // Own namespace namespace Hub\Handler\Protocol; +// Import application-specific stuff +use Hub\Generic\HubInterface; + // Import framework stuff use CoreFramework\Handler\Handleable; @@ -27,14 +30,7 @@ use CoreFramework\Handler\Handleable; * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -interface HandleableProtocol extends Handleable { - /** - * Getter for UNL instance - * - * @return $unlInstance An instance of a LocateableNode class - */ - function getUniversalNodeLocatorInstance (); - +interface HandleableProtocol extends Handleable, HubInterface { /** * Getter for port number to satify HandleableProtocol * diff --git a/application/hub/interfaces/helper/connections/class_ConnectionHelper.php b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php index be49b0561..511bd8f6a 100644 --- a/application/hub/interfaces/helper/connections/class_ConnectionHelper.php +++ b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php @@ -3,6 +3,7 @@ namespace Hub\Helper\Connection; // Import application-specific stuff +use Hub\Container\Socket\StorableSocket; use Hub\Helper\HubHelper; /** @@ -50,12 +51,10 @@ interface ConnectionHelper extends HubHelper { /** * Static "getter" for this connection class' name * - * @param $address IP address - * @param $port Port number - * @param $className Original class name + * @param $socketInstance An instance of a StorableSocket class * @return $class Expanded class name */ - static function getConnectionClassName ($address, $port, $className); + static function getConnectionClassNameFromSocket (StorableSocket $socketInstance); /** * Getter for shuttedDown diff --git a/application/hub/interfaces/lookup/node_states/class_LookupableNodeState.php b/application/hub/interfaces/lookup/node_states/class_LookupableNodeState.php index d3f24b44f..c651cf691 100644 --- a/application/hub/interfaces/lookup/node_states/class_LookupableNodeState.php +++ b/application/hub/interfaces/lookup/node_states/class_LookupableNodeState.php @@ -1,4 +1,10 @@ diff --git a/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php b/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php index 7385a22e5..01b8a3bcc 100644 --- a/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php +++ b/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php @@ -5,6 +5,8 @@ namespace Hub\State\Peer\Lookup; // Import application-specific stuff use Hub\Container\Socket\StorableSocket; use Hub\Helper\Connection\ConnectionHelper; +use Hub\LookupTable\Lookupable; +use Hub\State\Peer\PeerStateable; // Import framework stuff use CoreFramework\State\Stateable; @@ -61,10 +63,10 @@ interface LookupablePeerState extends Lookupable { /** * Purges old entries of given socket resource. We use the IP address from that resource. * - * @param $socketResource A valid socket resource + * @param $socketInstance An instance of a StorableSocket class * @return void */ - function purgeOldEntriesBySocketResource ($socketResource); + function purgeOldEntriesBysocketInstance (StorableSocket $socketInstance); /** * Checks whether a given peer state (in helper instance) is same as stored @@ -75,7 +77,5 @@ interface LookupablePeerState extends Lookupable { * @return $isSamePeerState Whether the peer's state is the same */ function isSamePeerState (ConnectionHelper $helperInstance, array $packageData); -} -// [EOF] -?> +} diff --git a/application/hub/interfaces/resolver/state/class_StateResolver.php b/application/hub/interfaces/resolver/state/class_StateResolver.php index dabe7930d..85554f55f 100644 --- a/application/hub/interfaces/resolver/state/class_StateResolver.php +++ b/application/hub/interfaces/resolver/state/class_StateResolver.php @@ -3,6 +3,7 @@ namespace Hub\Resolver\State; // Import application-specific stuff +use Hub\Container\Socket\StorableSocket; use Hub\Helper\Connection\ConnectionHelper; // Import framework stuff @@ -36,10 +37,10 @@ interface StateResolver extends Resolver { * * @param $helperInstance An instance of a ConnectionHelper class * @param $packageData Raw package data - * @param $socketResource A valid socket resource + * @param $socketInstance An instance of a StorableSocket class * @return $stateInstance An instance of the resolved state */ - static function resolveStateByPackage (ConnectionHelper $helperInstance, array $packageData, $socketResource); + static function resolveStateByPackage (ConnectionHelper $helperInstance, array $packageData, StorableSocket $socketInstance); /** * Checks whether the given state is valid diff --git a/application/hub/interfaces/states/peer/class_PeerStateable.php b/application/hub/interfaces/states/peer/class_PeerStateable.php index e1de0cb0f..57ab4fdc9 100644 --- a/application/hub/interfaces/states/peer/class_PeerStateable.php +++ b/application/hub/interfaces/states/peer/class_PeerStateable.php @@ -29,6 +29,3 @@ use CoreFramework\State\Stateable; */ interface PeerStateable extends Stateable { } - -// [EOF] -?>