]> git.mxchange.org Git - hub.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Mon, 26 Oct 2020 00:16:37 +0000 (01:16 +0100)
committerRoland Häder <roland@mxchange.org>
Mon, 26 Oct 2020 00:16:37 +0000 (01:16 +0100)
- 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.

Signed-off-by: Roland Häder <roland@mxchange.org>
application/hub/classes/handler/class_BaseHubHandler.php
application/hub/classes/handler/package/class_NetworkPackageHandler.php

index 34f37144800ed79a35deee5be4391f17e8e00245..ab424661a489db33e43bd6f90195939d64e8df28 100644 (file)
@@ -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;
+       }
+
 }
index d1789a9d38b79bff2da5e07f3a0c7d6107dc45fd..c936ad6cad6bcd26590ef72f782b2e14930a255b 100644 (file)
@@ -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);