]> git.mxchange.org Git - hub.git/commitdiff
Continued a bit:
authorRoland Häder <roland@mxchange.org>
Tue, 30 May 2017 20:12:23 +0000 (22:12 +0200)
committerRoland Häder <roland@mxchange.org>
Fri, 21 Aug 2020 16:50:10 +0000 (18:50 +0200)
- splitted sender address and port to have both being copyable (needed later on)
  to ShareableInfo class
- used some lesser socket_strerr() but socketInstance calls

Signed-off-by: Roland Häder <roland@mxchange.org>
application/hub/classes/container/socket/class_SocketContainer.php
application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php
application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php
application/hub/classes/decoder/package/class_PackageDecoder.php
application/hub/classes/listener/class_BaseListener.php
application/hub/classes/package/class_NetworkPackage.php
application/hub/classes/package/fragmenter/class_PackageFragmenter.php
application/hub/exceptions/package/class_InvalidDataChecksumException.php
application/hub/exceptions/package/class_UnexpectedPackageStatusException.php
application/hub/exceptions/peer/class_PeerAlreadyRegisteredException.php

index 5eabaaaad0fdd5ed7974744694f65765f0a44abf..851041b06cdc83f58a3ffa756f2fa48b6afd2de9 100644 (file)
@@ -454,7 +454,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita
                $socketResource = $this->getSocketResource();
 
                // Get error code and message
-               $errorMessage = socket_strerror(socket_last_error($socketResource));
+               $errorMessage = socket_strerror($this->getLastSocketErrorCode());
 
                // Return it
                return $errorMessage;
index b92b6f2bf69dac845d7ed829aba64f6472b41a82..1b90bde4390f51d01650f5bdf70fbdfadb6d7b70 100644 (file)
@@ -620,7 +620,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper imp
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
 
                // Assert on required array field
-               assert(isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER]));
+               assert(isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]));
 
                // Get max recipients
                $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
@@ -629,7 +629,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper imp
                $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
 
                // Then exclude 'sender' field as the sender is the current (*this*) node
-               $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
+               $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]);
 
                // Set limit to maximum DHT recipients
                $searchInstance->setLimit($maxRecipients);
index 07a9a5000485d60ef9fe275abbc0f7d79dadab66..3591a81f3e6bc3ee220045519354be09d283215e 100644 (file)
@@ -88,11 +88,11 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L
         */
        private function getSenderSearchInstanceFromPackageData (array $packageData) {
                // Get the instance
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Searching for sender ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Searching for sender ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]);
                $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
 
                // Add 'sender' as the peer's IP address
-               $searchInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
+               $searchInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]);
                $searchInstance->setLimit(1);
 
                // Return the instance
@@ -111,7 +111,7 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: ' . __FUNCTION__ . ' called with packageData()=' . count($packageData) . ' - CALLED!');
 
                // Is the package valid?
-               if (!isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER])) {
+               if (!isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS])) {
                        // Invalid package found, please report this
                        $this->debugBackTrace(':  packageData=' . print_r($packageData, TRUE));
                } // END - if
@@ -159,7 +159,7 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L
                $dataSetInstance->setUniqueKey(self::DB_COLUMN_PEER_SESSION_ID);
 
                // Add session id
-               $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
+               $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]);
 
                // Init peer address/port
                $peerAddress = '0.0.0.0';
@@ -167,12 +167,9 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L
 
                // Get peer name
                if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) {
-                       // Get last error
-                       $lastError = $socketInstance->getLastSocketErrorCode();
-
                        // ... and cleartext message from it and put both into criteria
-                       $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_CODE, $lastError);
-                       $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_MSG , socket_strerror($lastError));
+                       $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_CODE, $socketInstance->getLastSocketErrorCode());
+                       $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_MSG , $socketInstance->getLastSocketErrorMessage());
                } // END - if
 
                // Add ip address and port
@@ -183,7 +180,7 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L
                $this->queryInsertDataSet($dataSetInstance, array(self::DB_COLUMN_PEER_SESSION_ID => TRUE));
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER] . ' has been registered.');
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS] . ' has been registered.');
        }
 
        /**
@@ -203,11 +200,11 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L
                $dataSetInstance->setUniqueKey(self::DB_COLUMN_PEER_SESSION_ID);
 
                // Add session id
-               $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
+               $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]);
                $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_STATE     , $stateInstance->getStateName());
 
                // Try to resolve sender's session id
-               $senderData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_SENDER]));
+               $senderData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]));
 
                // Just make sure that 'invalid:invalid' is not being processed
                assert(($senderData[0] != 'invalid') && ($senderData[1] != 'invalid') && ($senderData[2] != 'invalid'));
@@ -226,7 +223,7 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L
                }
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER] . ' has been registered/updated with state ' . $stateInstance->getStateName());
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS] . ' has been registered/updated with state ' . $stateInstance->getStateName());
        }
 
        /**
index 69004a39d2bcb42864891b4686cd9b774d5ce174..32e0ee8945cc18ac40adcda4db0ae4b21cd62845 100644 (file)
@@ -113,7 +113,8 @@ class PackageDecoder extends BaseDecoder implements Decodeable, Registerable {
                // Some checks
                assert(
                        (is_array($decodedData)) &&
-                       (isset($decodedData[NetworkPackage::PACKAGE_DATA_SENDER])) &&
+                       (isset($decodedData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS])) &&
+                       (isset($decodedData[NetworkPackage::PACKAGE_DATA_SENDER_PORT])) &&
                        (isset($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) &&
                        (isset($decodedData[NetworkPackage::PACKAGE_DATA_CONTENT])) &&
                        (isset($decodedData[NetworkPackage::PACKAGE_DATA_STATUS]))
index a557a59cc64a3ec02105345a7bb9b73d283da7ba..d983ad5b93deb579ea1b2b6d1e80b04b1327e204 100644 (file)
@@ -170,6 +170,7 @@ class BaseListener extends BaseHubSystem implements Visitable {
         * @param       $socketInstance         An instance of a StorableSocket class
         * @return      void
         * @throws      SocketAlreadyRegisteredException        If the given resource is already registered
+        * @throws      LogicException  If no info instance can be created
         */
        protected function registerServerSocketInstance (StorableSocket $socketInstance) {
                // Trace message
@@ -187,6 +188,12 @@ class BaseListener extends BaseHubSystem implements Visitable {
                // Get a connection info instance
                $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener');
 
+               // Is the instance set?
+               if (!($infoInstance instanceof ShareableInfo)) {
+                       // Should not happen
+                       throw new LogicException(sprintf('infoInstance[]=%s does not implement ShareableInfo', gettype($infoInstance)));
+               } // END - if
+
                // Will the info instance with listener data
                $infoInstance->fillWithListenerInformation($this);
 
@@ -284,6 +291,7 @@ class BaseListener extends BaseHubSystem implements Visitable {
         *
         * @param       $peerSuffix             Suffix for peer name (e.g. :0 for TCP(/UDP?) connections)
         * @return      void
+        * @throws      LogicException  If no info instance can be created
         */
        protected function doListenSocketSelect ($peerSuffix) {
                // Trace message
@@ -326,16 +334,23 @@ class BaseListener extends BaseHubSystem implements Visitable {
 
                // Create a faked package data array
                $packageData = array(
-                       NetworkPackage::PACKAGE_DATA_SENDER    => $peerAddress . $peerSuffix,
-                       NetworkPackage::PACKAGE_DATA_RECIPIENT => $nodeInstance->getSessionId(),
-                       NetworkPackage::PACKAGE_DATA_STATUS    => NetworkPackage::PACKAGE_STATUS_FAKED
+                       NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS => $peerAddress . $peerSuffix,
+                       NetworkPackage::PACKAGE_DATA_SENDER_PORT    => $peerPort,
+                       NetworkPackage::PACKAGE_DATA_RECIPIENT      => $nodeInstance->getSessionId(),
+                       NetworkPackage::PACKAGE_DATA_STATUS         => NetworkPackage::PACKAGE_STATUS_FAKED
                );
 
                // Get a connection info instance
                $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener');
 
+               // Is the instance set?
+               if (!($infoInstance instanceof ShareableInfo)) {
+                       // Should not happen
+                       throw new LogicException(sprintf('infoInstance[]=%s does not implement ShareableInfo', gettype($infoInstance)));
+               } // END - if
+
                // Will the info instance with listener data
-               $infoInstance->fillWithListenerInformation($this);
+               $infoInstance->fillWithSocketPeerInformation($socketInstance);
 
                // Get a socket registry
                $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
index 2ffd307713d56e2b54968084b53d22fae074a619..9f70d1ab0ae1b1ee96feca6b6dfcca4bdb536312 100644 (file)
@@ -137,7 +137,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
        /**
         * Named array elements for package data
         */
-       const PACKAGE_DATA_SENDER           = 'sender';
+       const PACKAGE_DATA_SENDER_ADDRESS   = 'sender_address';
+       const PACKAGE_DATA_SENDER_PORT      = 'sender_port';
        const PACKAGE_DATA_RECIPIENT        = 'recipient';
        const PACKAGE_DATA_CONTENT          = 'content';
        const PACKAGE_DATA_STATUS           = 'status';
@@ -466,7 +467,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
         */
        private function isChecksumValid (array $decodedContent, array $decodedData) {
                // Get checksum
-               $checksum = $this->getHashFromContentSessionId($decodedContent, $decodedData[self::PACKAGE_DATA_SENDER]);
+               $checksum = $this->getHashFromContentSessionId($decodedContent, $decodedData[self::PACKAGE_DATA_SENDER_ADDRESS]);
 
                // Is it the same?
                $isChecksumValid = ($checksum == $decodedContent[self::PACKAGE_CONTENT_CHECKSUM]);
@@ -824,7 +825,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
 
                // Now prepare the temporary array and push it on the 'undeclared' stack
                $this->getStackInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
-                       self::PACKAGE_DATA_SENDER           => $this->getSessionId(),
+                       self::PACKAGE_DATA_SENDER_ADDRESS   => $this->getSessionId(),
+                       self::PACKAGE_DATA_SENDER_PORT      => '0',
                        self::PACKAGE_DATA_RECIPIENT        => $helperInstance->getRecipientType(),
                        self::PACKAGE_DATA_CONTENT          => $packageContent,
                        self::PACKAGE_DATA_STATUS           => self::PACKAGE_STATUS_NEW,
@@ -1342,7 +1344,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                 * Create 'decodedData' array with all assoziative array elements.
                 */
                $decodedData = array(
-                       self::PACKAGE_DATA_SENDER           => $decodedArray[self::INDEX_PACKAGE_SENDER],
+                       self::PACKAGE_DATA_SENDER_ADDRESS   => $decodedArray[self::INDEX_PACKAGE_SENDER],
+                       self::PACKAGE_DATA_SENDER_PORT      => '0',
                        self::PACKAGE_DATA_RECIPIENT        => $decodedArray[self::INDEX_PACKAGE_RECIPIENT],
                        self::PACKAGE_DATA_CONTENT          => $decodedArray[self::INDEX_PACKAGE_CONTENT],
                        self::PACKAGE_DATA_STATUS           => self::PACKAGE_STATUS_DECODED,
@@ -1387,7 +1390,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R
                        // Checksum of the _decoded_ data
                        self::PACKAGE_CONTENT_CHECKSUM         => $decodedContent[self::INDEX_CHECKSUM],
                        // Sender's id
-                       self::PACKAGE_CONTENT_SENDER           => $decodedData[self::PACKAGE_DATA_SENDER],
+                       self::PACKAGE_CONTENT_SENDER           => $decodedData[self::PACKAGE_DATA_SENDER_ADDRESS],
                        // Hash from decoded raw data
                        self::PACKAGE_CONTENT_HASH             => $decodedData[self::PACKAGE_DATA_HASH],
                        // Hash of private key
index e9848fc280396636f5decc5f00f7f77679ad756f..ab7d86789cff8493624ce1a74551f97ad44ff893 100644 (file)
@@ -170,9 +170,9 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera
         */
        private function getProcessedPackagesIndex (array $packageData) {
                return (
-                       $packageData[NetworkPackage::PACKAGE_DATA_SENDER]    . NetworkPackage::PACKAGE_DATA_SEPARATOR .
-                       $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . NetworkPackage::PACKAGE_DATA_SEPARATOR .
-                       $packageData[NetworkPackage::PACKAGE_DATA_CONTENT]   . NetworkPackage::PACKAGE_DATA_SEPARATOR
+                       $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS] . NetworkPackage::PACKAGE_DATA_SEPARATOR .
+                       $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]      . NetworkPackage::PACKAGE_DATA_SEPARATOR .
+                       $packageData[NetworkPackage::PACKAGE_DATA_CONTENT]        . NetworkPackage::PACKAGE_DATA_SEPARATOR
                );
        }
 
index 4eab57e8fa2df76b8d89e71032696d337dd577ff..fac07507871977148d57bde24157d29929c31294 100644 (file)
@@ -44,7 +44,7 @@ class InvalidDataChecksumException extends FrameworkException {
                $message = sprintf('[%s:%d] The checksum %s doesnt match the checksum of the content: %s',
                        $messageArray[0]->__toString(),
                        $this->getLine(),
-                       $messageArray[0]->getHashFromContentSessionId($messageArray[1], $messageArray[2][NetworkPackage::PACKAGE_DATA_SENDER]),
+                       $messageArray[0]->getHashFromContentSessionId($messageArray[1], $messageArray[2][NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]),
                        $messageArray[1][NetworkPackage::PACKAGE_CONTENT_CHECKSUM]
                );
 
index f5f7eeba6c347dac3c03bb0e6016e358abb1809b..d84c0afc9ab0dc4a78a77656d01794dc80f0a4d2 100644 (file)
@@ -40,13 +40,14 @@ class UnexpectedPackageStatusException extends FrameworkException {
         */
        public function __construct (array $messageArray, $code) {
                // Construct the message
-               $message = sprintf('[%s:%d] Unexpected package status %s!=%s detected, recipient=%s, sender=%s, hash=%s.',
+               $message = sprintf('[%s:%d] Unexpected package status %s!=%s detected, recipient=%s, sender=%s:%d, hash=%s.',
                        $messageArray[0]->__toString(),
                        $this->getLine(),
                        $messageArray[1][NetworkPackage::PACKAGE_DATA_STATUS],
                        $messageArray[2],
                        $messageArray[1][NetworkPackage::PACKAGE_DATA_RECIPIENT],
-                       $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER],
+                       $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS],
+                       $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER_PORT],
                        $messageArray[1][NetworkPackage::PACKAGE_DATA_HASH]
                );
 
index 47ce2e0ced1593e5a64fdfc61a774e128add1f14..98f25cd431d63361cf1c9bb528f5e5037d19fd37 100644 (file)
@@ -43,13 +43,11 @@ class PeerAlreadyRegisteredException extends FrameworkException {
                $message = sprintf('[%s:%d] Sender %s is already registered.',
                        $messageArray[0]->__toString(),
                        $this->getLine(),
-                       $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER]
+                       $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]
                );
 
                // Call parent exception constructor
                parent::__construct($message, $code);
        }
-}
 
-// [EOF]
-?>
+}