]> git.mxchange.org Git - hub.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Mon, 29 May 2017 20:52:35 +0000 (22:52 +0200)
committerRoland Häder <roland@mxchange.org>
Fri, 21 Aug 2020 16:50:09 +0000 (18:50 +0200)
- implemented translateLastSocketErrorCodeToName()
- implemented getSocketRecipient<Address|Port>()
- no more socketResource here (and elsewhere will follow)
- updated core framework
- more debug messages added
- registering helper/listener instances with socket instance must be done
  outaside constructor, much easier code
- for this, registerInfoInstance() is now implemented
Signed-off-by: Roland Häder <roland@mxchange.org>
Signed-off-by: Roland Häder <roland@mxchange.org>
22 files changed:
application/hub/classes/class_BaseHubSystem.php
application/hub/classes/container/class_BaseHubContainer.php [new file with mode: 0644]
application/hub/classes/container/socket/class_SocketContainer.php
application/hub/classes/database/frontend/class_BaseHubDatabaseWrapper.php
application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php
application/hub/classes/factories/states/peer/class_PeerStateFactory.php
application/hub/classes/handler/class_BaseHubHandler.php
application/hub/classes/info/connection/class_ConnectionInfo.php
application/hub/classes/listener/class_BaseListenerDecorator.php
application/hub/classes/listener/socket/decorator/class_SocketFileListenerDecorator.php
application/hub/classes/listener/tcp/decorators/class_ClientTcpListenerDecorator.php
application/hub/classes/listener/tcp/decorators/class_HubTcpListenerDecorator.php
application/hub/classes/listener/udp/decorators/class_ClientUdpListenerDecorator.php
application/hub/classes/listener/udp/decorators/class_HubUdpListenerDecorator.php
application/hub/classes/pools/peer/class_DefaultPeerPool.php
application/hub/classes/registry/socket/class_SocketRegistry.php
application/hub/classes/resolver/state/peer/class_PeerStateResolver.php
application/hub/interfaces/class_HubInterface.php
application/hub/interfaces/container/socket/class_StorableSocket.php
application/hub/interfaces/helper/hub/class_HubHelper.php
application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php
core

index dff2ac434d2c18d61c8bb09f8d6cbd0351f600ea..10c9710ecc1bdaa9313feb0c72a2c864e1d3c164 100644 (file)
@@ -361,7 +361,7 @@ class BaseHubSystem extends BaseFrameworkSystem implements HubInterface {
         *
         * @return      $listenerInstance       A Listenable instance
         */
-       protected final function getListenerInstance () {
+       public final function getListenerInstance () {
                return $this->listenerInstance;
        }
 
diff --git a/application/hub/classes/container/class_BaseHubContainer.php b/application/hub/classes/container/class_BaseHubContainer.php
new file mode 100644 (file)
index 0000000..85489ec
--- /dev/null
@@ -0,0 +1,253 @@
+<?php
+// Own namespace
+namespace Hub\Container;
+
+// Import application-specific stuff
+use Hub\Container\Socket\StorableSocket;
+use Hub\Handler\Network\RawData\BaseRawDataHandler;
+use Hub\Generic\HubInterface;
+use Hub\Information\ShareableInfo;
+use Hub\Listener\Listenable;
+use Hub\Locator\Node\LocateableNode;
+use Hub\Pool\Poolable;
+
+// Import framework stuff
+use CoreFramework\Container\BaseContainer;
+
+/**
+ * A general hub container
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+class BaseHubContainer extends BaseContainer implements HubInterface {
+       /**
+        * Listener instance
+        */
+       private $listenerInstance = NULL;
+
+       /**
+        * 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);
+       }
+
+       /**
+        * Setter for listener instance
+        *
+        * @param       $listenerInstance       A Listenable instance
+        * @return      void
+        */
+       protected final function setListenerInstance (Listenable $listenerInstance) {
+               $this->listenerInstance = $listenerInstance;
+       }
+
+       /**
+        * Getter for listener instance
+        *
+        * @return      $listenerInstance       A Listenable instance
+        */
+       public final function getListenerInstance () {
+               return $this->listenerInstance;
+       }
+
+}
index 52ef686dcd6cd5adf901b0740dc3694cf587304c..7a2d594d3ee36128d6e5494a3bfb06b1f1669983 100644 (file)
@@ -3,17 +3,18 @@
 namespace Hub\Container\Socket;
 
 // Import application-specific stuff
+use Hub\Container\BaseHubContainer;
 use Hub\Handler\Network\RawData\BaseRawDataHandler;
 use Hub\Factory\Network\Locator\UniversalNodeLocatorFactory;
 use Hub\Factory\Socket\SocketFactory;
 use Hub\Helper\Connection\BaseConnectionHelper;
+use Hub\Helper\Connection\ConnectionHelper;
 use Hub\Information\ShareableInfo;
 use Hub\Listener\BaseListener;
 use Hub\Listener\Listenable;
 use Hub\Network\Package\NetworkPackage;
 
 // Import framework stuff
-use CoreFramework\Container\BaseContainer;
 use CoreFramework\Factory\ObjectFactory;
 use CoreFramework\Registry\Registerable;
 use CoreFramework\Socket\InvalidSocketException;
@@ -51,7 +52,7 @@ use \LogicException;
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class SocketContainer extends BaseContainer implements StorableSocket, Visitable, Registerable {
+class SocketContainer extends BaseHubContainer implements StorableSocket, Visitable, Registerable {
        /**
         * Socket protocol:
         * - 'tcp' for TCP/IPv4 connections
@@ -95,12 +96,11 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable
         * @param       $socketResource         A valid socket resource
         * @param       $socketProtocol         Socket protocol
         * @param       $packageData            Raw package data
-        * @param       $infoInstance           An instance of a  ShareableInfo class
         * @return      $socketInstance         An instance of this Container class
         */
-       public static final function createSocketContainer ($socketResource, $socketProtocol, array $packageData, ShareableInfo $infoInstance = NULL) {
+       public static final function createSocketContainer ($socketResource, $socketProtocol, array $packageData) {
                // Trace message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET: socketResource=%s,socketProtocol=%s,packageData()=%d,infoInstance[]=%s - CALLED!', $socketResource, $socketProtocol, count($packageData), gettype($infoInstance)));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET: socketResource=%s,socketProtocol=%s,packageData()=%d - CALLED!', $socketResource, $socketProtocol, count($packageData)));
                /* DEBUG-PRINT: */ printf('[%s:%d]: packageData=%s', __METHOD__, __LINE__, print_r($packageData, TRUE));
 
                // Get a new instance
@@ -113,31 +113,6 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable
                unset($packageData[NetworkPackage::PACKAGE_DATA_CONTENT]);
                unset($packageData[NetworkPackage::PACKAGE_DATA_HASH]);
 
-               // Is the info instance set?
-               if ($infoInstance instanceof ShareableInfo) {
-                       // Get listener/helper from info class
-                       $listenerInstance = $infoInstance->getListenerInstance();
-                       $helperInstance = $infoInstance->getHelperInstance();
-
-                       // Debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: listenerInstance[]=' . gettype($listenerInstance));
-
-                       // Is there a listener instance set?
-                       if ($listenerInstance instanceof Listenable) {
-                               // Debug message
-                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...');
-
-                               // Set it here for later usage
-                               $socketInstance->setListenerInstance($listenerInstance);
-                       } elseif ($helperInstance instanceof ConnectionHelper) {
-                               // Debug message
-                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting helperInstance=' . $helperInstance->__toString() . ' ...');
-
-                               // Set it here for later usage
-                               $socketInstance->setHelperInstance($helperInstance);
-                       }
-               } // END - if
-
                // Set the resource ...
                $socketInstance->setSocketResource($socketResource);
 
@@ -298,8 +273,15 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable
         *
         * @return      $recipient      Recipient array element
         * @throws      LogicException  If 'recipient' array element is not found
+        * @throws      InvalidSocketException  If socket is invalid
         */
        public function getSocketRecipient () {
+               // Should be valid socket
+               if (!$this->isValidSocket()) {
+                       // Throw exception
+                       throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
+               } // END - if
+
                // Get package data
                $packageData = $this->getPackageData();
 
@@ -313,6 +295,59 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable
                return $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT];
        }
 
+       /**
+        * Getter for socket recipient's address part
+        *
+        * @return      $recipientAddress       Recipient's address part
+        * @throws      InvalidSocketException  If socket is invalid
+        */
+       public function getSocketRecipientAddress () {
+               // Should be valid socket
+               if (!$this->isValidSocket()) {
+                       // Throw exception
+                       throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
+               } // END - if
+
+               // Get recipient
+               $recipient = $this->getSocketRecipient();
+
+               // Generate UNL instance for it
+               $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient);
+
+               // Get address part
+               $recipientAddress = $unlInstance->getUnlAddress();
+
+               // Return it
+               return $recipientAddress;
+       }
+
+       /**
+        * Getter for socket recipient's port part
+        *
+        * @return      $recipientPort  Recipient's port part
+        * @throws      InvalidSocketException  If socket is invalid
+        */
+       public function getSocketRecipientPort () {
+               // Should be valid socket
+               if (!$this->isValidSocket()) {
+                       // Throw exception
+                       throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
+               } // END - if
+
+               // Get recipient
+               $recipient = $this->getSocketRecipient();
+
+               // Generate UNL instance for it
+               $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($recipient);
+
+               // Get port part
+               $recipientPort = $unlInstance->getUnlPort();
+
+               // Return it
+               return $recipientPort;
+
+       }
+
        /**
         * Validates stored stocket
         *
@@ -376,6 +411,22 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable
                return $errorMessage;
        }
 
+       /**
+        * Translates last socket error code into a human-readable form
+        *
+        * @return      $codeName       Code name to used e.g. with some_$codeName_socket_error_class or so
+        */
+       public function translateLastSocketErrorCodeToName () {
+               // Get last error code
+               $errorCode = $this->getLastSocketErrorCode();
+
+               // Call "translate" method
+               $codeName = $this->translateSocketErrorCodeToName($errorCode);
+
+               // Return it
+               return $codeName;
+       }
+
        /**
         * Tries to bind the socket.
         *
@@ -702,6 +753,40 @@ class SocketContainer extends BaseContainer implements StorableSocket, Visitable
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!');
        }
 
+       /**
+        * Registers whether helper or listener instance found in given info
+        * instance with this socket container.
+        *
+        * @param       $infoInstance   An instance of a ShareableInfo class
+        * @return      void
+        */
+       public function registerInfoInstance (ShareableInfo $infoInstance) {
+               // Trace message
+               /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling infoInstance->getListenerInstance() ...', strtoupper($this->getSocketProtocol())));
+
+               // Get listener/helper from info class
+               $listenerInstance = $infoInstance->getListenerInstance();
+               $helperInstance = $infoInstance->getHelperInstance();
+
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance));
+
+               // Is there a listener instance set?
+               if ($listenerInstance instanceof Listenable) {
+                       // Debug message
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting listenerInstance=' . $listenerInstance->__toString() . ' ...');
+
+                       // Set it here for later usage
+                       $this->setListenerInstance($listenerInstance);
+               } elseif ($helperInstance instanceof ConnectionHelper) {
+                       // Debug message
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Setting helperInstance=' . $helperInstance->__toString() . ' ...');
+
+                       // Set it here for later usage
+                       $this->setHelperInstance($helperInstance);
+               }
+       }
+
        /**
         * Handles socket error for given socket resource and peer data. This method
         * validates socket resource stored in given container if it is a valid
index de83717bf1f3e9f3680ee3315ca988acf253c5de..0afec33b0e4682269dce61d28bbaa9702ef96152 100644 (file)
@@ -14,7 +14,7 @@ use Hub\Pool\Poolable;
 use CoreFramework\Database\Frontend\BaseDatabaseWrapper;
 
 /**
- * A database wrapper for cruncher work/test units
+ * A general hub database wrapper
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
@@ -36,6 +36,10 @@ use CoreFramework\Database\Frontend\BaseDatabaseWrapper;
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class BaseHubDatabaseWrapper extends BaseDatabaseWrapper implements HubInterface {
+       /**
+        * Listener instance
+        */
+       private $listenerInstance = NULL;
 
        /**
         * Listener pool instance
@@ -226,4 +230,23 @@ class BaseHubDatabaseWrapper extends BaseDatabaseWrapper implements HubInterface
                throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
        }
 
+       /**
+        * Setter for listener instance
+        *
+        * @param       $listenerInstance       A Listenable instance
+        * @return      void
+        */
+       protected final function setListenerInstance (Listenable $listenerInstance) {
+               $this->listenerInstance = $listenerInstance;
+       }
+
+       /**
+        * Getter for listener instance
+        *
+        * @return      $listenerInstance       A Listenable instance
+        */
+       public final function getListenerInstance () {
+               return $this->listenerInstance;
+       }
+
 }
index 1f305f78024b7f2e99955d44e15283e2f43cff45..00db969f7fb3f638475f960773d90042df41d669 100644 (file)
@@ -166,9 +166,9 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L
                $peerPort    = '0';
 
                // Get peer name
-               if (!@socket_getpeername($socketInstance, $peerAddress, $peerPort)) {
+               if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) {
                        // Get last error
-                       $lastError = socket_last_error($socketInstance);
+                       $lastError = $socketInstance->getLastSocketErrorCode();
 
                        // ... and cleartext message from it and put both into criteria
                        $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_CODE, $lastError);
@@ -234,21 +234,10 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L
         *
         * @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 purgeOldEntriesBysocketInstance (StorableSocket $socketInstance) {
-               // Get peer name
-               if (!@socket_getpeername($socketInstance, $peerAddress, $peerPort)) {
-                       // Get last error
-                       $lastError = socket_last_error($socketInstance);
-
-                       // Doesn't work!
-                       throw new InvalidSocketException(array($this, $socketInstance, $lastError, socket_strerror($lastError)), self::EXCEPTION_INVALID_SOCKET);
-               } // END - if
-
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: peerAddress=' . $peerAddress . ',peerPort=' . $peerPort . ' - UNFINISHED!');
+       public function purgeOldEntriesBySocketInstance (StorableSocket $socketInstance) {
+               $this->partialStub('Please finish this method.');
        }
 
        /**
@@ -290,7 +279,5 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L
                // Return it
                return $isSamePeerState;
        }
-}
 
-// [EOF]
-?>
+}
index 6e414948f72b8ad439ee13ef3558e65954969469..746ccf24b2e20ea1385c5011b978589f6866ac10 100644 (file)
@@ -3,6 +3,7 @@
 namespace Hub\Factory\State\Peer;
 
 // Import application-specific stuff
+use Hub\Container\Socket\StorableSocket;
 use Hub\Helper\Connection\ConnectionHelper;
 
 // Import framework stuff
@@ -71,11 +72,11 @@ class PeerStateFactory extends ObjectFactory {
         *
         * @param       $helperInstance         An instance of a ConnectionHelper class
         * @param       $packageData            Raw package data
-        * @param       $socketResource         A valid socket resource
+        * @param       $socketInstance         A valid socket resource
         * @param       $errorCode                      The last error code
         * @return      $stateInstance          A Stateable class instance
         */
-       public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, array $packageData, $socketResource, $errorCode) {
+       public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, array $packageData, StorableSocket $socketInstance, $errorCode) {
                // Init state instance, this is better coding practice
                $stateInstance = NULL;
 
@@ -88,7 +89,7 @@ class PeerStateFactory extends ObjectFactory {
                 */
                try {
                        // Purge old entries
-                       $tableInstance->purgeOldEntriesBySocketResource($socketResource);
+                       $tableInstance->purgeOldEntriesBySocketInstance($socketInstance);
                } catch (InvalidSocketException $e) {
                        // Just log all errors
                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] Purging of old entries failed. Message from exception: ' . $e->getMessage());
@@ -100,7 +101,7 @@ class PeerStateFactory extends ObjectFactory {
                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-FACTORY[' . __LINE__ . ':] errorCode=' . $errorCode);
 
                        // Register the new peer with its session id
-                       $tableInstance->registerPeerByPackageData($packageData, $socketResource);
+                       $tableInstance->registerPeerByPackageData($packageData, $socketInstance);
 
                        /*
                         * It is a new peer so create the state instance based on error
index a0d4861099620eb8da8630c7db640514d7ae2d65..675eeb0854b6380ed1470665b0f044f222043122 100644 (file)
@@ -7,6 +7,7 @@ use Hub\Container\Socket\StorableSocket;
 use Hub\Handler\Network\RawData\BaseRawDataHandler;
 use Hub\Generic\HubInterface;
 use Hub\Information\ShareableInfo;
+use Hub\Listener\Listenable;
 use Hub\Locator\Node\LocateableNode;
 use Hub\Pool\Poolable;
 
@@ -37,6 +38,11 @@ use CoreFramework\Handler\Handleable;
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 class BaseHubHandler extends BaseHandler implements Handleable, HubInterface {
+       /**
+        * Listener instance
+        */
+       private $listenerInstance = NULL;
+
        /**
         * Listener pool instance
         */
@@ -226,4 +232,23 @@ class BaseHubHandler extends BaseHandler implements Handleable, HubInterface {
                throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
        }
 
+       /**
+        * Setter for listener instance
+        *
+        * @param       $listenerInstance       A Listenable instance
+        * @return      void
+        */
+       protected final function setListenerInstance (Listenable $listenerInstance) {
+               $this->listenerInstance = $listenerInstance;
+       }
+
+       /**
+        * Getter for listener instance
+        *
+        * @return      $listenerInstance       A Listenable instance
+        */
+       public function getListenerInstance () {
+               return $this->listenerInstance;
+       }
+
 }
index 1d6c48b10da07987ca54880a063bb54bfdf2d9e9..50319ec8f5d23141f99feee292bed89e7789cadf 100644 (file)
@@ -92,8 +92,8 @@ class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable {
 
                // Fill the generic array with several data from the listener:
                $this->setProtocolName($helperInstance->getSocketInstance()->getSocketProtocol());
-               $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $helperInstance->getSocketInstance()->getRecipientAddress());
-               $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT    , $helperInstance->getSocketInstance()->getRecipientPort());
+               $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $helperInstance->getSocketInstance()->getSocketRecipientAddress());
+               $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT    , $helperInstance->getSocketInstance()->getSocketRecipientPort());
 
                // Set helper here
                $this->setHelperInstance($helperInstance);
index 46fecd8ce6497fecf8d4d0d50f6ec5b7faff3209..b94ee018082d9b72ee2d4f2ec84966e70db46961 100644 (file)
@@ -70,6 +70,8 @@ class BaseListenerDecorator extends BaseDecorator implements Visitable {
         * @return      $listenAddress  The address this listener should listen on
         */
        public final function getListenAddress () {
+               // Trace message
+               /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('LISTENER-DECORATOR: Calling this->getListenerInstance()->getListenAddress() ...');
                return $this->getListenerInstance()->getListenAddress();
        }
 
@@ -79,6 +81,8 @@ class BaseListenerDecorator extends BaseDecorator implements Visitable {
         * @return      $listenPort             The port this listener should listen on
         */
        public final function getListenPort () {
+               // Trace message
+               /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('LISTENER-DECORATOR: Calling this->getListenerInstance()->getListenPort() ...');
                return $this->getListenerInstance()->getListenPort();
        }
 
@@ -263,7 +267,7 @@ class BaseListenerDecorator extends BaseDecorator implements Visitable {
         *
         * @return      $listenerInstance       A Listenable instance
         */
-       protected final function getListenerInstance () {
+       public final function getListenerInstance () {
                return $this->listenerInstance;
        }
 
index 6d54616a7573a22dad3a9b7103d069b4674ae24e..a56d29c14de7807b17681f31f4517ce246fc580d 100644 (file)
@@ -78,6 +78,9 @@ class SocketFileListenerDecorator extends BaseListenerDecorator implements Liste
         * @return      void
         */
        public function doListen () {
+               // Trace message
+               /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Calling this->getListenerInstance() ...');
+
                // Handle generic TCP package
                $this->getListenerInstance()->doListen();
 
index a38d4d2fd6f3801fbfa07b8612e7dbe0b02fd85c..82d61a4fc888f40308728af9cba0e81e1af8bbc9 100644 (file)
@@ -78,6 +78,9 @@ class ClientTcpListenerDecorator extends BaseListenerDecorator implements Listen
         * @return      void
         */
        public function doListen () {
+               // Trace message
+               /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CLIENT-TCP-LISTENER: Calling this->getListenerInstance() ...');
+
                // Handle generic TCP package
                $this->getListenerInstance()->doListen();
 
index f5f2c08b019dc39e121661bd17c3df52ef3d2697..748a5cf8e0325e774aa66a864009f1582de01578 100644 (file)
@@ -78,6 +78,9 @@ class HubTcpListenerDecorator extends BaseListenerDecorator implements Listenabl
         * @return      void
         */
        public function doListen () {
+               // Trace message
+               /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-TCP-LISTENER: Calling this->getListenerInstance() ...');
+
                // Handle generic TCP package
                $this->getListenerInstance()->doListen();
 
index 15008105e6fcd51129294d4c344e5667df82f751..f43a3b96ce6cbf532afbbabc851dbdc6e0ccdd15 100644 (file)
@@ -78,6 +78,9 @@ class ClientUdpListenerDecorator extends BaseListenerDecorator implements Listen
         * @return      void
         */
        public function doListen () {
+               // Trace message
+               /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CLIENT-UDP-LISTENER: Calling this->getListenerInstance() ...');
+
                // Handle generic UDP packages first
                $this->getListenerInstance()->doListen();
 
index 15cd60583fbe3e612a0ce449b4f31fbfc2a3ce60..a47f0e02e1b7c82b733c0d795e1e3148589fbf87 100644 (file)
@@ -78,6 +78,9 @@ class HubUdpListenerDecorator extends BaseListenerDecorator implements Listenabl
         * @return      void
         */
        public function doListen () {
+               // Trace message
+               /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HUB-UDP-LISTENER: Calling this->getListenerInstance() ...');
+
                // Handle generic UDP package first
                $this->getListenerInstance()->doListen();
 
index e646b71386c20c26ab84ed323bdbd6f592db2fbb..5312daa987dbc5a78daba5e089aa4577da283b32 100644 (file)
@@ -121,6 +121,9 @@ class DefaultPeerPool extends BasePool implements PoolablePeer {
                $peerAddress = '0.0.0.0';
                $peerPort    = '0';
 
+               // Trace message
+               /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Calling this->getListenerInstance() ...');
+
                // The socket resource should not match server socket
                if (!$this->getListenerInstance()->getSocketInstance()->equals($socketInstance)) {
                        // Try to determine the peer's IP number
index d191433c7e27fa9549b183ec62d11008d1908231..3c526d8631619e9c21fd54a544a5a7295d5c3127 100644 (file)
@@ -5,6 +5,7 @@ namespace Hub\Registry\Socket;
 // Import application-specific stuff
 use Hub\Container\Socket\StorableSocket;
 use Hub\Factory\Information\Connection\ConnectionInfoFactory;
+use Hub\Helper\Connection\ConnectionHelper;
 use Hub\Information\ShareableInfo;
 use Hub\Listener\BaseListener;
 use Hub\Listener\Listenable;
@@ -242,6 +243,9 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke
                // We have a sub-registry, the socket key and the socket, now we need to put all together
                //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: socketKey=' . $socketKey . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ' - adding socket container instance ...');
                $registryInstance->addInstance($socketKey, $socketInstance);
+
+               // Also register all instances from info instance in socket
+               $socketInstance->registerInfoInstance($infoInstance);
        }
 
        /**
@@ -297,18 +301,21 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke
                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',registryInstance=' . $registryInstance->__toString());
 
                        // This is always a SubRegistry instance
-                       foreach ($registryInstance->getInstanceRegistry() as $subKey => $containerInstance) {
+                       foreach ($registryInstance->getInstanceRegistry() as $subKey => $socketInstance) {
                                // Debug message
-                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',subKey=' . $subKey . ',containerInstance=' . $containerInstance->__toString());
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',subKey=' . $subKey . ',socketInstance=' . $socketInstance->__toString());
 
                                // Is this a StorableSocket instance and is the address the same?
-                               if (($containerInstance instanceof StorableSocket) && ($containerInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) {
+                               if (($socketInstance instanceof StorableSocket) && ($socketInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) {
                                        // Debug die
-                                       //* DEBUG-DIE: */ die(__METHOD__ . ': containerInstance=' . print_r($containerInstance, TRUE));
+                                       //* DEBUG-DIE: */ die(__METHOD__ . ': socketInstance=' . print_r($socketInstance, TRUE));
+
+                                       // Trace message
+                                       /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: Calling socketInstance->getListenerInstance() ...');
 
                                        // Get listener and helper instances
-                                       $listenerInstance = $containerInstance->getListenerInstance();
-                                       $helperInstance = $containerInstance->getHelperInstance();
+                                       $listenerInstance = $socketInstance->getListenerInstance();
+                                       $helperInstance = $socketInstance->getHelperInstance();
 
                                        // Debug message
                                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',subKey=' . $subKey . ',listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance));
@@ -328,7 +335,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke
                                                $infoInstance->fillWithConnectionHelperInformation($helperInstance);
                                        } else {
                                                // Not supported state!
-                                               $this->debugInstance(sprintf('[%s:%d]: Invalid state found, please report this to the developers with full debug infos.', __METHOD__, __LINE__));
+                                               $this->debugInstance(sprintf('[%s:%d]: Invalid script-state found, please report this to the developers with full debug infos. socketInstance=%s', __METHOD__, __LINE__, print_r($socketInstance, TRUE)));
                                        }
 
                                        // Debug message
index 8fec88c5c202d4c595aeb20c22e5d724176b8c96..97147445e94ab88242dddc573987b2f2b84923e9 100644 (file)
@@ -92,7 +92,7 @@ class PeerStateResolver extends BaseStateResolver implements StateResolver {
                } // END - if
 
                // Translate the error code to an own name
-               $errorName = $socketInstance->translateSocketLastErrorCodeToName();
+               $errorName = $socketInstance->translateLastSocketErrorCodeToName();
 
                // Create a state instance based on $errorCode. This factory does the hard work for us
                $stateInstance = PeerStateFactory::createPeerStateInstanceBySocketStatusCode($helperInstance, $packageData, $socketInstance, $errorName);
index 2abd9c85cf18fd923e8ba8a780b8085e05170b44..1e08ea121021a7cf5bd491deea7fadf4172015d9 100644 (file)
@@ -105,4 +105,11 @@ interface HubInterface extends FrameworkInterface {
         */
        function getUniversalNodeLocatorInstance ();
 
+       /**
+        * Getter for listener instance
+        *
+        * @return      $listenerInstance       A Listenable instance
+        */
+       function getListenerInstance ();
+
 }
index b217b079953b467a37cc698f76a9bd18773ff89e..68348ef4b26d167d9fe027d392c9b97b99b6c144 100644 (file)
@@ -2,6 +2,9 @@
 // Own namespace
 namespace Hub\Container\Socket;
 
+// Import application-related stuff
+use Hub\Information\ShareableInfo;
+
 // Inport frameworks stuff
 use CoreFramework\Generic\FrameworkInterface;
 
@@ -165,9 +168,26 @@ interface StorableSocket extends FrameworkInterface {
         *
         * @return      $recipient      Recipient array element
         * @throws      LogicException  If 'recipient' array element is not found
+        * @throws      InvalidSocketException  If stored socket is invalid
         */
        function getSocketRecipient ();
 
+       /**
+        * Getter for socket recipient's address part
+        *
+        * @return      $recipientAddress       Recipient's address part
+        * @throws      InvalidSocketException  If stored socket is invalid
+        */
+       function getSocketRecipientAddress ();
+
+       /**
+        * Getter for socket recipient's port part
+        *
+        * @return      $recipientPort  Recipient's port part
+        * @throws      InvalidSocketException  If stored socket is invalid
+        */
+       function getSocketRecipientPort ();
+
        /**
         * Checks whether the given Universal Node Locator matches with the one from package data
         *
@@ -223,6 +243,13 @@ interface StorableSocket extends FrameworkInterface {
         */
        function getLastSocketErrorMessage ();
 
+       /**
+        * Translates last socket error code into a human-readable form
+        *
+        * @return      $codeName       Code name to used e.g. with some_$codeName_socket_error_class or so
+        */
+       function translateLastSocketErrorCodeToName ();
+
        /**
         * Handles socket error for given socket resource and peer data. This method
         * validates socket resource stored in given container if it is a valid
@@ -264,4 +291,13 @@ interface StorableSocket extends FrameworkInterface {
         */
        function clearLastSocketError ();
 
+       /**
+        * Registers whether helper or listener instance found in given info
+        * instance with this socket container.
+        *
+        * @param       $infoInstance   An instance of a ShareableInfo class
+        * @return      void
+        */
+       function registerInfoInstance (ShareableInfo $infoInstance);
+
 }
index f036ed444b1246db03abef826a5c6a31613a865b..ae2f8608c2ec625be06aab4d834ac11493f7bb19 100644 (file)
@@ -2,6 +2,9 @@
 // Own namespace
 namespace Hub\Helper;
 
+// Import application-specific stuff
+use Hub\Generic\HubInterface;
+
 // Import framework stuff
 use CoreFramework\Helper\Helper;
 
@@ -27,7 +30,7 @@ use CoreFramework\Helper\Helper;
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-interface HubHelper extends Helper {
+interface HubHelper extends Helper, HubInterface {
        /**
         * Tries to determine the used protocol for this package (this helper is helping to send out)
         *
index 01b8a3bcc7d9bc04d64f84e21c61feea262def0c..62a6444706484f2dbde4d15f7e59818d2a86a93d 100644 (file)
@@ -66,7 +66,7 @@ interface LookupablePeerState extends Lookupable {
         * @param       $socketInstance         An instance of a StorableSocket class
         * @return      void
         */
-       function purgeOldEntriesBysocketInstance (StorableSocket $socketInstance);
+       function purgeOldEntriesBySocketInstance (StorableSocket $socketInstance);
 
        /**
         * Checks whether a given peer state (in helper instance) is same as stored
diff --git a/core b/core
index e9e5242797adec674d25da4e641965f8d6dcbecd..dc46df7fb4a0358cb7554c33b6f08d9ff8418c66 160000 (submodule)
--- a/core
+++ b/core
@@ -1 +1 @@
-Subproject commit e9e5242797adec674d25da4e641965f8d6dcbecd
+Subproject commit dc46df7fb4a0358cb7554c33b6f08d9ff8418c66