From dbf4789f1b22bb500c0c9804382b48e16fdc4492 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 27 Oct 2020 13:28:04 +0100 Subject: [PATCH] Continued: - moved socketResource from 'core' project to SocketContainer class - added more debug lines - fixed comments for $packageData (array) to $packageInstance transition - updated core framework MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../classes/client/http/class_HttpClient.php | 8 +- .../container/class_BaseHubContainer.php | 15 +- .../socket/class_SocketContainer.php | 24 +++ .../states/peer/class_PeerStateFactory.php | 14 +- .../package/class_NetworkPackageHandler.php | 152 +++++++++++------- .../raw_data/tcp/class_TcpRawDataHandler.php | 2 +- .../classes/listener/class_BaseListener.php | 18 +-- .../registry/socket/class_SocketRegistry.php | 41 ++--- core | 2 +- 9 files changed, 163 insertions(+), 113 deletions(-) diff --git a/application/hub/classes/client/http/class_HttpClient.php b/application/hub/classes/client/http/class_HttpClient.php index 8a3bf632e..78b4cf7cc 100644 --- a/application/hub/classes/client/http/class_HttpClient.php +++ b/application/hub/classes/client/http/class_HttpClient.php @@ -64,7 +64,13 @@ class HttpClient extends BaseClient implements Client { // Do we have cache? if (!isset($GLOBALS[__METHOD__])) { // Determine it - $GLOBALS[__METHOD__] = (($this->getConfigInstance()->getConfigEntry('proxy_host') != '') && ($this->getConfigInstance()->getConfigEntry('proxy_port') > 0)); + $GLOBALS[__METHOD__] = ( + ( + $this->getConfigInstance()->getConfigEntry('proxy_host') != '' + ) && ( + $this->getConfigInstance()->getConfigEntry('proxy_port') > 0 + ) + ); } // END - if // Return cache diff --git a/application/hub/classes/container/class_BaseHubContainer.php b/application/hub/classes/container/class_BaseHubContainer.php index 53fa4b479..2a33da9dc 100644 --- a/application/hub/classes/container/class_BaseHubContainer.php +++ b/application/hub/classes/container/class_BaseHubContainer.php @@ -70,7 +70,7 @@ abstract class BaseHubContainer extends BaseContainer implements HubInterface { */ protected function __construct ($className) { // Call parent constructor - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-CONTAINER: className=%s - CONSTRUCTED!', $className)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HUB-CONTAINER: className=%s - CONSTRUCTED!', $className)); parent::__construct($className); } @@ -81,13 +81,18 @@ abstract class BaseHubContainer extends BaseContainer implements HubInterface { * @return $isset Whether start/end marker are set */ public final function ifStartEndMarkersSet ($data) { - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-CONTAINER: data()=%d - CALLED!', strlen($data))); - // Determine it - $isset = ((substr($data, 0, strlen(HandleableRawData::STREAM_START_MARKER)) == HandleableRawData::STREAM_START_MARKER) && (substr($data, -1 * strlen(HandleableRawData::STREAM_END_MARKER), strlen(HandleableRawData::STREAM_END_MARKER)) == HandleableRawData::STREAM_END_MARKER)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HUB-CONTAINER: data()=%d - CALLED!', strlen($data))); + $isset = ( + ( + substr($data, 0, strlen(HandleableRawData::STREAM_START_MARKER)) == HandleableRawData::STREAM_START_MARKER + ) && ( + substr($data, -1 * strlen(HandleableRawData::STREAM_END_MARKER), strlen(HandleableRawData::STREAM_END_MARKER)) == HandleableRawData::STREAM_END_MARKER + ) + ); // ... and return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HUB-CONTAINER: isset=%d - EXIT!', intval($isset))); return $isset; } diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index d0ab636ce..91e7742cd 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -108,6 +108,11 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita */ private $senderPort; + /** + * Socket resource + */ + private $socketResource = false; + /** * Protected constructor * @@ -1643,4 +1648,23 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $this->senderPort = $senderPort; } + /** + * Setter for socket resource + * + * @param $socketResource A valid socket resource + * @return void + */ + public final function setSocketResource ($socketResource) { + $this->socketResource = $socketResource; + } + + /** + * Getter for socket resource + * + * @return $socketResource A valid socket resource + */ + public final function getSocketResource () { + return $this->socketResource; + } + } diff --git a/application/hub/classes/factories/states/peer/class_PeerStateFactory.php b/application/hub/classes/factories/states/peer/class_PeerStateFactory.php index f2df80ef1..f7ad94ea2 100644 --- a/application/hub/classes/factories/states/peer/class_PeerStateFactory.php +++ b/application/hub/classes/factories/states/peer/class_PeerStateFactory.php @@ -11,6 +11,9 @@ use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Socket\InvalidSocketException; +// Import SPL stuff +use \InvalidArgumentException; + /** * A factory class for peer states * @@ -72,12 +75,19 @@ class PeerStateFactory extends ObjectFactory { * from current state. * * @param $helperInstance An instance of a ConnectionHelper class - * @param $packageInstance An instance of a DeliverablePackage class - * @param $socketInstance A valid socket resource + * @param $packageInstance An instance of a DeliverablePackage class + * @param $socketInstance An instance of a StorableSocket class * @param $errorCode The last error code * @return $stateInstance A Stateable class instance */ public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance, StorableSocket $socketInstance, $errorCode) { + // Error code and helper's state name should not be the same + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PEER-STATE-FACTORY: helperInstance=%s,packageInstance=%s,socketInstance=%s,errorCode=%s - CALLED!', $helperInstance->__toString(), $packageInstance->__toString(), $socketInstance->__to_string(), $errorCode)); + if ($errorCode == $helperInstance->getPrintableState()) { + // Not valid! + throw new InvalidArgumentException(sprintf('Current state and errorCode=%s are the same.', $errorCode)); + } + // Init state instance, this is better coding practice $stateInstance = NULL; diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php index 788ba76f3..82e777adb 100644 --- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -25,6 +25,7 @@ use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Network\Package\Delivery\Fragment\PackageFragmenter; use Org\Shipsimu\Hub\Network\Receive\Receivable; use Org\Shipsimu\Hub\Node\Generic\InvalidPrivateKeyHashException; +use Org\Shipsimu\Hub\RawData\InvalidDataChecksumException; use Org\Shipsimu\Hub\Tag\Tagable; use Org\Shipsimu\Hub\Tools\HubTools; @@ -40,6 +41,7 @@ use Org\Mxchange\CoreFramework\Visitor\Visitable; use Org\Mxchange\CoreFramework\Visitor\Visitor; // Import SPL stuff +use \InvalidArgumentException; use \Iterator; /** @@ -100,9 +102,9 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei const PACKAGE_CONTENT_ARRAY_SIZE = 4; /** - * Separator for checksum + * Mask for content's checksum generation */ - const PACKAGE_CHECKSUM_SEPARATOR = '_'; + const CONTENT_CHECKSUM_MASK = '%s_%s_%s'; /** * Array indexes for above mask, start with zero @@ -446,23 +448,17 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * @return $hash Hash for given package content */ private function getHashFromContent ($content) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: content[md5]=' . md5($content) . ',sender=' . $this->getNodeInstance()->getSessionId() . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); - // Create the hash + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: content[md5]=%s - CALLED!', md5($content))); // @TODO md5() is very weak, but it needs to be fast - $hash = md5( - $content . - self::PACKAGE_CHECKSUM_SEPARATOR . - $this->getNodeInstance()->getSessionId() . - self::PACKAGE_CHECKSUM_SEPARATOR . + $hash = md5(sprintf(self::CONTENT_CHECKSUM_MASK, + $content, + $this->getNodeInstance()->getSessionId(), $this->getCompressorInstance()->getCompressorExtension() - ); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: content[md5]=' . md5($content) . ',sender=' . $this->getNodeInstance()->getSessionId() . ',hash=' . $hash . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); + )); // And return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: content[md5]=%s,sender=%s,hash=%s,compressor=%s - EXIT!', md5($content), $this->getNodeInstance()->getSessionId(), $hash, $this->getCompressorInstance()->getCompressorExtension())); return $hash; } @@ -475,12 +471,14 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei */ private function isChecksumValid (array $decodedContent, DeliverablePackage $packageInstance) { // Get checksum + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: decodedContent()=%d,packageInstance=%s - CALLED!', count($decodedContent), $packageInstance->__toString())); $checksum = $this->getHashFromContentSessionId($decodedContent, $packageInstance->getSenderAddress()); // Is it the same? $isChecksumValid = ($checksum == $decodedContent[self::PACKAGE_CONTENT_CHECKSUM]); // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: isChecksumValid=%d - EXIT!', intval($isChecksumValid))); return $isChecksumValid; } @@ -491,25 +489,37 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * @param $stackerName Name of the stacker * @param $newStatus New status to set * @return void + * @throws InvalidArgumentException If hashes are mismatching */ private function changePackageStatus (DeliverablePackage $packageInstance, $stackerName, $newStatus) { // Skip this for empty stacks + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s,stackerName=%s,newStatus=%s - CALLED!', $packageInstance->__toString(), $stackerName, $newStatus)); if ($this->getStackInstance()->isStackEmpty($stackerName)) { // This avoids an exception after all packages has failed + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: stackerName=%s is empty - EXIT!', $stackerName)); return; } // END - if // Pop the entry (it should be it) - $nextData = $this->getStackInstance()->popNamed($stackerName); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Getting next stack from stackerName=%s ...', $stackerName)); + $nextPackageInstance = $this->getStackInstance()->popNamed($stackerName); // Compare both hashes - assert($nextData->getPackageHash() == $packageInstance->getPackageHash()); + if ($nextPackageInstance->getPackageHash() != $packageInstance->getPackageHash()) { + // Hash is not matching + throw new InvalidArgumentException(sprintf('Hashes "%s" and "%s" are mismatching.', $packageInstance->getPackageHash(), $nextPackageInstance->getPackageHash())); + } // Temporary set the new status + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Setting newStatus=%s ....', $newStatus)); $packageInstance->setPackageStatus($newStatus); // And push it again + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Adding packageInstance=%s to stackerName=%s ...', $packageInstance->__toString(), $stackerName)); $this->getStackInstance()->pushNamed($stackerName, $packageInstance); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!'); } /** @@ -520,20 +530,18 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * @return $hash Hash for given package content */ public function getHashFromContentSessionId (array $decodedContent, $sessionId) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: content[md5]=' . md5($decodedContent[self::PACKAGE_CONTENT_MESSAGE]) . ',sender=' . $sessionId . ',compressor=' . $decodedContent[self::PACKAGE_CONTENT_COMPRESSOR]); - // Create the hash + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: decodedContent()=%d,sessionId=%s - CALLED!', count($decodedContent), $sessionId)); // @TODO md5() is very weak, but it needs to be fast - $hash = md5( - $decodedContent[self::PACKAGE_CONTENT_MESSAGE] . - self::PACKAGE_CHECKSUM_SEPARATOR . - $sessionId . - self::PACKAGE_CHECKSUM_SEPARATOR . + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: content[md5]=' . md5($decodedContent[self::PACKAGE_CONTENT_MESSAGE]) . ',sender=' . $sessionId . ',compressor=' . $decodedContent[self::PACKAGE_CONTENT_COMPRESSOR]); + $hash = md5(sprintf(self::CONTENT_CHECKSUM_MASK, + $decodedContent[self::PACKAGE_CONTENT_MESSAGE], + $sessionId, $decodedContent[self::PACKAGE_CONTENT_COMPRESSOR] - ); + )); // And return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: hash=%s - EXIT!', $hash)); return $hash; } @@ -554,6 +562,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * target because it causes an overload on the network and may be * abused for attacking the network with large packages. */ + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString())); $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance(); // Discover all recipients, this may throw an exception @@ -579,9 +588,9 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei // Push the declared package to the next stack. $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageInstance); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Package declared for recipient ' . $currentRecipient); // Skip to next entry + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Package declared for recipient ' . $currentRecipient); $iteratorInstance->next(); } // END - while @@ -590,6 +599,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * shall be delivered has already been added for all entries from the * list. */ + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Calling discoveryInstance->clearRecipients() ...'); $discoveryInstance->clearRecipients(); } @@ -603,9 +613,6 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * @return void */ private function deliverRawPackageData (DeliverablePackage $packageInstance) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString())); - /* * This package may become big, depending on the shared object size or * delivered message size which shouldn't be so long (to save @@ -618,15 +625,14 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * which (configurable!) protocol should be used for that type of * package. */ + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString())); $discoveryInstance = SocketDiscoveryFactory::createSocketDiscoveryInstance(); // Now discover the right socket instance from given package data $socketInstance = $discoveryInstance->discoverSocket($packageInstance, StorableSocket::CONNECTION_TYPE_OUTGOING); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after discoverSocket() has been called.'); - // Get the connection helper from registry + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after discoverSocket() has been called.'); $helperInstance = GenericRegistry::getRegistry()->getInstance('connection'); // Debug message @@ -641,15 +647,11 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei // Will the info instance with connection helper data $infoInstance->fillWithConnectionHelperInformation($helperInstance); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.'); - // Is it not there? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.'); if (($socketInstance->isValidSocket()) && (!$this->getRegistryInstance()->isSocketRegistered($infoInstance, $socketInstance))) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Registering socket ' . $socketInstance . ' ...'); - // Then register it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Registering socket ' . $socketInstance . ' ...'); $this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance); } elseif (!$helperInstance->getStateInstance()->isPeerStateConnected()) { // Is not connected, then we cannot send @@ -659,19 +661,15 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei $socketInstance->shutdownSocket(); } - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.'); - // Make sure the connection is up + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.'); $helperInstance->getStateInstance()->validatePeerStateConnected(); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.'); - // Enqueue it again on the out-going queue, the connection is up and working at this point + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.'); $this->getStackInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageInstance); - // Debug message + // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after pushNamed() has been called.'); } @@ -1361,15 +1359,30 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * * @param $rawPackageContent The raw package content to be "decoded" * @return $packageInstance An instance of a DeliverablePackage class + * @throws InvalidArgumentException If $rawPackageContent contains double seperators */ public function decodeRawContent ($rawPackageContent) { - // Use the separator '#' to "decode" it + // Check if no double seperator is found /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: rawPackageContent(%d)=%s - CALLED!', strlen($rawPackageContent), $rawPackageContent)); + if (strpos($rawPackageContent, self::PACKAGE_DATA_SEPARATOR . self::PACKAGE_DATA_SEPARATOR) !== FALSE) { + // Opps, should never happend + throw new InvalidArgumentException(sprintf('rawPackageContent=%s has 2x or more "%s" in it.', $rawPackageContent, self::PACKAGE_DATA_SEPARATOR)); + } + + // Use the separator '#' to "decode" it $decodedArray = explode(self::PACKAGE_DATA_SEPARATOR, $rawPackageContent); - // Assert on count (should be always 3) - assert(count($decodedArray) == self::DECODED_DATA_ARRAY_SIZE); - //* DEBUG-DIE: */ die(sprintf('[%s:%d] decodedArray=%s', __METHOD__, __LINE__, print_r($decodedArray, TRUE))); + // Assert on count (should be always 6) + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: decodedArray()=%d', count($decodedArray))); + /* DEBUG-DIE: */ die(sprintf('[%s:%d] decodedArray=%s', __METHOD__, __LINE__, print_r($decodedArray, TRUE))); + if (count($decodedArray) != self::DECODED_DATA_ARRAY_SIZE) { + // Count of array elements not expected + throw new InvalidArgumentException(sprintf('decodedArray()=%d does not match expected size %d. rawPackageContent=%s', + count($decodedArray), + self::DECODED_DATA_ARRAY_SIZE, + $rawPackageContent + )); + } // Create package instance $packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class'); @@ -1395,9 +1408,20 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * * @param $packageInstance An instance of a DeliverablePackage class * @return void + * @throws InvalidArgumentException If the package content does not have expected amount of entries * @throws InvalidDataChecksumException If the checksum doesn't match */ public function handleReceivedPackageInstance (DeliverablePackage $packageInstance) { + // Is the package content set? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString())); + if ($packageInstance->getPackageContent() == '') { + // Package content is not set + throw new InvalidArgumentException(sprintf('Package with receipientId=%s,recipientType=%s has empty packageContent', + $packageInstance->getRecipientId(), + $packageInstance->getRecipientType() + )); + } + /* * "Decode" the package's content by a simple explode() call, for * details of the array elements, see comments for constant @@ -1406,13 +1430,21 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei $decodedContent = explode(self::PACKAGE_MASK_SEPARATOR, $packageInstance->getPackageContent()); // Assert on array count for a very basic validation - assert(count($decodedContent) == self::PACKAGE_CONTENT_ARRAY_SIZE); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: decodedContent()=%d', count($decodedContent))); + if (count($decodedContent) != self::PACKAGE_CONTENT_ARRAY_SIZE) { + // Not valid decoded content + throw new InvalidArgumentException(sprintf('decodedContent()=%d does not match expected %d. packageContent=%s', + count($decodedContent), + self::PACKAGE_CONTENT_ARRAY_SIZE, + $packageInstance->getPackageContent() + )); + } /* * Convert the indexed array into an associative array. This is much * better to remember than plain numbers, isn't it? */ - $decodedContent = array( + $decodedMessage = array( // Compressor's extension used to compress the data self::PACKAGE_CONTENT_COMPRESSOR => $decodedContent[self::INDEX_COMPRESSOR_EXTENSION], // Package data (aka "message") in BASE64-decoded form but still compressed @@ -1430,19 +1462,19 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei ); // Is the checksum valid? - if (!$this->isChecksumValid($decodedContent, $packageInstance)) { + if (!$this->isChecksumValid($decodedMessage, $packageInstance)) { // Is not the same, so throw an exception here - throw new InvalidDataChecksumException(array($this, $decodedContent, $packageInstance), self::EXCEPTION_INVALID_DATA_CHECKSUM); + throw new InvalidDataChecksumException(array($this, $decodedMessage, $packageInstance), self::EXCEPTION_INVALID_DATA_CHECKSUM); } // END - if /* * The checksum is the same, then it can be decompressed safely. The * original message is at this point fully decoded. */ - $decodedContent[self::PACKAGE_CONTENT_MESSAGE] = $this->getCompressorInstance()->decompressStream($decodedContent[self::PACKAGE_CONTENT_MESSAGE]); + $decodedMessage[self::PACKAGE_CONTENT_MESSAGE] = $this->getCompressorInstance()->decompressStream($decodedMessage[self::PACKAGE_CONTENT_MESSAGE]); // And push it on the next stack - $this->getStackInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $decodedContent); + $this->getStackInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $decodedMessage); } /** @@ -1469,22 +1501,22 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei assert($this->isNewMessageArrived()); // Get it from the stacker, it is the full array with the decoded message - $decodedContent = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE); + $decodedMessage = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE); // Generate the hash of comparing it - if (!$this->isPackageHashValid($decodedContent)) { + if (!$this->isPackageHashValid($decodedMessage)) { // Is not valid, so throw an exception here exit(__METHOD__ . ':INVALID HASH! UNDER CONSTRUCTION!' . chr(10)); } // END - if // Now get a filter chain back from factory with given tags array - $chainInstance = PackageFilterChainFactory::createChainByTagsArray($decodedContent[self::PACKAGE_CONTENT_TAGS]); + $chainInstance = PackageFilterChainFactory::createChainByTagsArray($decodedMessage[self::PACKAGE_CONTENT_TAGS]); /* * Process the message through all filters, note that all other - * elements from $decodedContent are no longer needed. + * elements from $decodedMessage are no longer needed. */ - $chainInstance->processMessage($decodedContent, $this); + $chainInstance->processMessage($decodedMessage, $this); /* * Post-processing of message data (this won't remove the message from diff --git a/application/hub/classes/handler/raw_data/tcp/class_TcpRawDataHandler.php b/application/hub/classes/handler/raw_data/tcp/class_TcpRawDataHandler.php index e5eca72aa..ba38d5d01 100644 --- a/application/hub/classes/handler/raw_data/tcp/class_TcpRawDataHandler.php +++ b/application/hub/classes/handler/raw_data/tcp/class_TcpRawDataHandler.php @@ -72,7 +72,7 @@ class TcpRawDataHandler extends BaseRawDataHandler implements Networkable { * Processes raw data from given socket instance. This is mostly useful for * TCP package handling and is implemented in the TcpListener class. * - * @param $resource A valid socket resource array + * @param $socketInstance An instance of a StorableSocket class * @return void */ public function processRawDataFromSocketInstance (StorableSocket $socketInstance) { diff --git a/application/hub/classes/listener/class_BaseListener.php b/application/hub/classes/listener/class_BaseListener.php index ec5c439ee..03f153f7f 100644 --- a/application/hub/classes/listener/class_BaseListener.php +++ b/application/hub/classes/listener/class_BaseListener.php @@ -222,34 +222,26 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { * @throws LogicException If no info instance can be created */ protected function isServerSocketRegistered (StorableSocket $socketInstance) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=%s - CALLED!', strtoupper($this->getProtocolName()), $socketInstance->__toString())); - // Get a connection info instance + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=%s - CALLED!', strtoupper($this->getProtocolName()), $socketInstance->__toString())); $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), StorableSocket::CONNECTION_TYPE_SERVER); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: infoInstance[]=%s', strtoupper($this->getProtocolName()), gettype($infoInstance))); - // Is the instance set? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: infoInstance[]=%s', strtoupper($this->getProtocolName()), gettype($infoInstance))); 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); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Checking socketInstance ... (socketResource=%s)', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource())); - // Check it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Checking socketInstance ... (socketResource=%s)', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource())); $isRegistered = $this->getRegistryInstance()->isSocketRegistered($infoInstance, $socketInstance); - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: isRegistered=%d - EXIT!', strtoupper($this->getProtocolName()), intval($isRegistered))); - // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: isRegistered=%d - EXIT!', strtoupper($this->getProtocolName()), intval($isRegistered))); return $isRegistered; } diff --git a/application/hub/classes/registry/socket/class_SocketRegistry.php b/application/hub/classes/registry/socket/class_SocketRegistry.php index adec18383..645b8aad4 100644 --- a/application/hub/classes/registry/socket/class_SocketRegistry.php +++ b/application/hub/classes/registry/socket/class_SocketRegistry.php @@ -162,10 +162,8 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke * @return $isRegistered Whether the given socket resource is registered */ public function isSocketRegistered (ShareableInfo $infoInstance, StorableSocket $socketInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ' - CALLED!'); - // Default is not registered + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ' - CALLED!'); $isRegistered = FALSE; // First, check for the instance, there can be only once @@ -201,10 +199,8 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke } // END - if } // END - if - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!'); - // Return the result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!'); return $isRegistered; } @@ -260,10 +256,8 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke * @throws NoSocketRegisteredException If the requested socket is not registered */ public function getRegisteredSocketResource (Listenable $listenerInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY:listener=' . $listenerInstance->getProtocolName() . ' - CALLED!'); - // The socket must be registered before we can return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY:listener=' . $listenerInstance->getProtocolName() . ' - CALLED!'); if (!$this->isInfoRegistered($listenerInstance)) { // Throw the exception throw new NoSocketRegisteredException ($listenerInstance, self::EXCEPTION_SOCKET_NOT_REGISTERED); @@ -281,10 +275,8 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke // And the final socket resource $socketInstance = $registryInstance->getInstance($socketKey); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: listener=' . $listenerInstance->getProtocolName() . ',socketInstance[]=' . gettype($socketInstance) . ' - EXIT!'); - // Return the resource + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: listener=' . $listenerInstance->getProtocolName() . ',socketInstance[]=' . gettype($socketInstance) . ' - EXIT!'); return $socketInstance; } @@ -296,35 +288,26 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke */ public function getInfoInstanceFromPackageInstance (DeliverablePackage $packageInstance) { // Init info instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: packageInstance=%s - CALLED!', $packageInstance)); $infoInstance = NULL; //* DEBUG-DIE: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE)); // Get all keys and check them foreach ($this->getInstanceRegistry() as $key => $registryInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',registryInstance=' . $registryInstance->__toString()); - // This is always a SubRegistry instance + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',registryInstance=' . $registryInstance->__toString()); foreach ($registryInstance->getInstanceRegistry() as $subKey => $socketInstance) { - // Debug message - //* 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? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',subKey=' . $subKey . ',socketInstance=' . $socketInstance->__toString()); if (($socketInstance instanceof StorableSocket) && ($socketInstance->ifAddressMatches($packageInstance->getRecipientUnl()))) { - // Debug die - //* 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 + //* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: Calling socketInstance->getListenerInstance() ...'); + //* DEBUG-DIE: */ die(__METHOD__ . ': socketInstance=' . print_r($socketInstance, TRUE)); $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)); - // Is a listener or helper set? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',subKey=' . $subKey . ',listenerInstance[]=' . gettype($listenerInstance) . ',helperInstance[]=' . gettype($helperInstance)); if ($listenerInstance instanceof Listenable) { // Found a listener, so get the info instance first $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($listenerInstance->getProtocolName(), 'helper'); @@ -355,10 +338,8 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke } // END - if } // END - foreach - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: infoInstance[]=' . gettype($infoInstance) . ' - EXIT!'); - // Return the info instance + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: infoInstance[]=' . gettype($infoInstance) . ' - EXIT!'); return $infoInstance; } diff --git a/core b/core index b4d42594f..22758e48b 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit b4d42594f7eb8c7f254a14901743ffdef168e84f +Subproject commit 22758e48b312f672167569bbe674476fc19769b2 -- 2.39.5