From 28e43f7a80a1135c9bc4d1b1a0d3dd991cc809f6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 26 Oct 2020 01:16:37 +0100 Subject: [PATCH] Continued: - Let's keep nodeInstance as a class field to avoid tons of invocations of the factory method. It is a bit of a trade-off, as the memory foot-print may raise but so does performance and invocations may lower resulting in better performance. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../classes/handler/class_BaseHubHandler.php | 25 +++++++++++++++++++ .../package/class_NetworkPackageHandler.php | 25 ++++++++----------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/application/hub/classes/handler/class_BaseHubHandler.php b/application/hub/classes/handler/class_BaseHubHandler.php index 34f371448..ab424661a 100644 --- a/application/hub/classes/handler/class_BaseHubHandler.php +++ b/application/hub/classes/handler/class_BaseHubHandler.php @@ -9,6 +9,7 @@ use Org\Shipsimu\Hub\Generic\HubInterface; use Org\Shipsimu\Hub\Information\ShareableInfo; use Org\Shipsimu\Hub\Listener\Listenable; use Org\Shipsimu\Hub\Locator\Node\LocateableNode; +use Org\Shipsimu\Hub\Node\Node; use Org\Shipsimu\Hub\Pool\Poolable; // Import framework stuff @@ -54,6 +55,11 @@ abstract class BaseHubHandler extends BaseHandler implements Handleable, HubInte */ private $infoInstance = NULL; + /** + * Node instance + */ + private $nodeInstance = NULL; + /** * A StorableSocket instance */ @@ -255,4 +261,23 @@ abstract class BaseHubHandler extends BaseHandler implements Handleable, HubInte return $this->listenerInstance; } + /** + * Setter for node instance + * + * @param $nodeInstance A Node instance + * @return void + */ + public final function setNodeInstance (Node $nodeInstance) { + $this->nodeInstance = $nodeInstance; + } + + /** + * Getter for node instance + * + * @return $nodeInstance A Node instance + */ + public function getNodeInstance () { + return $this->nodeInstance; + } + } diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php index d1789a9d3..c936ad6ca 100644 --- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -357,6 +357,9 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei // Get node instance $nodeInstance = NodeObjectFactory::createNodeInstance(); + // Set it locally + $packageInstance->setNodeInstance($nodeInstance); + // Get pool instance from node $poolInstance = $nodeInstance->getListenerPoolInstance(); @@ -438,24 +441,21 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * @return $hash Hash for given package content */ private function getHashFromContent ($content) { - // Get node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($content) . ',sender=' . $nodeInstance->getSessionId() . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($content) . ',sender=' . $this->getNodeInstance()->getSessionId() . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); // Create the hash // @TODO md5() is very weak, but it needs to be fast $hash = md5( $content . self::PACKAGE_CHECKSUM_SEPARATOR . - $nodeInstance->getSessionId() . + $this->getNodeInstance()->getSessionId() . self::PACKAGE_CHECKSUM_SEPARATOR . $this->getCompressorInstance()->getCompressorExtension() ); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($content) . ',sender=' . $nodeInstance->getSessionId() . ',hash=' . $hash . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($content) . ',sender=' . $this->getNodeInstance()->getSessionId() . ',hash=' . $hash . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); // And return it return $hash; @@ -741,7 +741,7 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei $data = array( self::PACKAGE_CONTENT_SENDER => $senderId, self::PACKAGE_CONTENT_MESSAGE => $content, - self::PACKAGE_CONTENT_PRIVATE_KEY_HASH => $this->getPrivateKeyHash(), + self::PACKAGE_CONTENT_PRIVATE_KEY_HASH => $this->getNodeInstance()->getPrivateKeyHash(), ); // Hash content and sender id together, use scrypt @@ -832,22 +832,19 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei // Make sure required data is there assert(!empty($content)); - assert($nodeInstance->getSessionId() != ''); + assert($this->getNodeInstance()->getSessionId() != ''); // Init package instance $packageInstance = PackageDataFactory::createPackageDataInstance(); - // Get node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - // Set all data - $packageInstance->setSenderAddress($nodeInstance->getSessionId()); + $packageInstance->setSenderAddress($this->getNodeInstance()->getSessionId()); $packageInstance->setSenderPort('0'); $packageInstance->setRecipientType($helperInstance->getRecipientType()); $packageInstance->setPackageContent($packageContent); $packageInstance->setStatus(self::PACKAGE_STATUS_NEW); - $packageInstance->setContentHash($this->generatePackageHash($content, $nodeInstance->getSessionId())); - $packageInstance->setPrivateKeyHash($this->getPrivateKeyHash()); + $packageInstance->setContentHash($this->generatePackageHash($content, $this->getNodeInstance()->getSessionId())); + $packageInstance->setPrivateKeyHash($this->getNodeInstance()->getPrivateKeyHash()); // Now prepare the temporary array and push it on the 'undeclared' stack $this->getStackInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, $packageInstance); -- 2.39.5