From 98afdbabc8c70e285b5882d155d5f96e5ed516da Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 1 Aug 2019 08:32:34 +0200 Subject: [PATCH] Continued: - more rewrites of $packageData array to PackageData class ($packageInstance) - renamed NetworkPackage to NetworkPackageHandler and moved to proper namespace - updated 'core' MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../hub/classes/class_BaseHubSystem.php | 47 +++- .../socket/class_SocketContainer.php | 101 ++++----- ...odeDistributedHashTableDatabaseWrapper.php | 12 +- .../class_PeerStateLookupDatabaseWrapper.php | 47 ++-- .../decoder/package/class_PackageDecoder.php | 40 +--- .../classes/dht/node/class_NodeDhtFacade.php | 5 +- .../protocol/class_ProtocolDiscovery.php | 13 +- .../dht/class_DhtRecipientDiscovery.php | 5 +- .../class_PackageRecipientDiscovery.php | 34 ++- .../socket/class_PackageSocketDiscovery.php | 26 +-- .../class_NetworkPackageHandlerFactory.php | 79 +++++++ .../handler/class_ProtocolHandlerFactory.php | 3 +- .../package/class_NetworkPackageFactory.php | 2 +- .../classes/factories/package/data/.htaccess | 1 + .../package/data/class_PackageDataFactory.php | 69 ++++++ .../factories/socket/class_SocketFactory.php | 44 ++-- .../states/peer/class_PeerStateFactory.php | 5 +- .../classes/filter/class_BaseHubFilter.php | 24 +- .../handler/chunks/class_ChunkHandler.php | 2 +- .../hub/classes/handler/package/.htaccess | 1 + .../package/class_NetworkPackageHandler.php} | 205 ++++++++---------- .../ipv4/class_BaseIpV4ProtocolHandler.php | 16 +- .../raw_data/class_BaseRawDataHandler.php | 9 +- .../connection/class_BaseConnectionHelper.php | 31 +-- .../ipv4/tcp/class_TcpConnectionHelper.php | 23 +- .../ipv4/udp/class_UdpConnectionHelper.php | 5 +- .../helper/dht/class_DhtBootstrapHelper.php | 6 +- .../dht/class_DhtPublishEntryHelper.php | 6 +- .../class_NodeAnnouncementHelper.php | 6 +- ...ss_NodeAnnouncementMessageAnswerHelper.php | 2 +- ...ss_NodeDhtBootstrapMessageAnswerHelper.php | 2 +- ...NodeRequestNodeListMessageAnswerHelper.php | 2 +- .../class_NodeSelfConnectHelper.php | 6 +- .../class_NodeRequestNodeListHelper.php | 6 +- .../classes/listener/class_BaseListener.php | 27 ++- .../listener/class_BaseListenerDecorator.php | 2 +- .../socket/class_SocketFileListener.php | 5 +- .../class_SocketFileListenerDecorator.php | 5 +- .../class_ClientTcpListenerDecorator.php | 5 +- .../class_HubTcpListenerDecorator.php | 5 +- .../listener/udp/class_UdpListener.php | 5 +- .../class_ClientUdpListenerDecorator.php | 5 +- .../class_HubUdpListenerDecorator.php | 5 +- .../hub/classes/nodes/class_BaseHubNode.php | 4 +- .../assembler/class_PackageAssembler.php | 11 +- .../hub/classes/package/deliverable/.htaccess | 1 + .../package/deliverable/class_PackageData.php | 57 +++++ .../fragmenter/class_PackageFragmenter.php | 39 ++-- .../pools/peer/class_DefaultPeerPool.php | 11 +- .../recipient/dht/class_DhtRecipient.php | 9 +- .../direct/class_DirectRecipient.php | 9 +- .../recipient/self/class_SelfRecipient.php | 9 +- .../recipient/upper/class_UpperRecipient.php | 9 +- .../registry/socket/class_SocketRegistry.php | 16 +- .../state/peer/class_PeerStateResolver.php | 5 +- .../tags/package/class_PackageTags.php | 29 +-- .../class_NetworkPackageReaderTask.php | 2 +- .../class_NetworkPackageWriterTask.php | 2 +- .../class_HalfShutdownSocketVisitor.php | 6 +- .../socket/class_ShutdownSocketVisitor.php | 4 +- application/hub/config.php | 9 +- .../class_InvalidDataChecksumException.php | 6 +- ...class_UnexpectedPackageStatusException.php | 13 +- .../class_PeerAlreadyRegisteredException.php | 6 +- .../unl/class_InvalidUnlException.php | 4 +- .../container/socket/class_StorableSocket.php | 2 +- .../frontend/class_NodeDhtWrapper.php | 7 +- .../dht/class_DiscoverableDhtRecipient.php | 5 +- .../node/class_DiscoverableNodeRecipient.php | 9 +- .../socket/class_DiscoverableSocket.php | 9 +- .../distributable/class_Distributable.php | 9 +- .../handler/network/class_Networkable.php | 2 +- .../protocol/class_HandleableProtocol.php | 5 +- .../connections/class_ConnectionHelper.php | 9 +- .../interfaces/listener/class_Listenable.php | 7 +- .../peer_states/class_LookupablePeerState.php | 23 +- .../package/assembler/class_Assembler.php | 4 +- .../interfaces/package/class_Receivable.php | 6 +- .../package/fragmenter/class_Fragmentable.php | 11 +- .../hub/interfaces/package/package/.htaccess | 1 + .../package/class_DeliverablePackage.php | 32 +++ .../interfaces/recipient/class_Recipient.php | 12 +- .../socket/class_RegisterableSocket.php | 5 +- .../resolver/state/class_StateResolver.php | 5 +- .../hub/interfaces/socket/class_SocketTag.php | 5 +- .../hub/interfaces/tags/class_Tagable.php | 11 +- core | 2 +- 87 files changed, 809 insertions(+), 582 deletions(-) create mode 100644 application/hub/classes/factories/handler/class_NetworkPackageHandlerFactory.php create mode 100644 application/hub/classes/factories/package/data/.htaccess create mode 100644 application/hub/classes/factories/package/data/class_PackageDataFactory.php create mode 100644 application/hub/classes/handler/package/.htaccess rename application/hub/classes/{package/class_NetworkPackage.php => handler/package/class_NetworkPackageHandler.php} (89%) create mode 100644 application/hub/classes/package/deliverable/.htaccess create mode 100644 application/hub/classes/package/deliverable/class_PackageData.php create mode 100644 application/hub/interfaces/package/package/.htaccess create mode 100644 application/hub/interfaces/package/package/class_DeliverablePackage.php diff --git a/application/hub/classes/class_BaseHubSystem.php b/application/hub/classes/class_BaseHubSystem.php index ca22256a2..89c498073 100644 --- a/application/hub/classes/class_BaseHubSystem.php +++ b/application/hub/classes/class_BaseHubSystem.php @@ -14,6 +14,7 @@ use Org\Shipsimu\Hub\Listener\Listenable; use Org\Shipsimu\Hub\Locator\Node\LocateableNode; use Org\Shipsimu\Hub\Network\Deliver\Deliverable; use Org\Shipsimu\Hub\Network\Package\Assembler\Assembler; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Network\Package\Fragment\Fragmentable; use Org\Shipsimu\Hub\Network\Receive\Receivable; use Org\Shipsimu\Hub\Pool\Poolable; @@ -74,7 +75,7 @@ abstract class BaseHubSystem extends BaseFrameworkSystem implements HubInterface /** * A network package handler instance */ - private $packageInstance = NULL; + private $packageHandlerInstance = NULL; /** * A Receivable instance @@ -131,6 +132,11 @@ abstract class BaseHubSystem extends BaseFrameworkSystem implements HubInterface */ private $sourceInstance = NULL; + /** + * Package instance + */ + private $packageInstance = NULL; + /** * Name of used protocol */ @@ -158,7 +164,13 @@ abstract class BaseHubSystem extends BaseFrameworkSystem implements HubInterface /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: 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)); + $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 return $isset; @@ -167,20 +179,20 @@ abstract class BaseHubSystem extends BaseFrameworkSystem implements HubInterface /** * Setter for network package handler instance * - * @param $packageInstance The network package instance we shall set + * @param $packageHandlerInstance The network package instance we shall set * @return void */ - protected final function setPackageInstance (Deliverable $packageInstance) { - $this->packageInstance = $packageInstance; + protected final function setPackageHandlerInstance (Deliverable $packageHandlerInstance) { + $this->packageHandlerInstance = $packageHandlerInstance; } /** * Getter for network package handler instance * - * @return $packageInstance The network package handler instance we shall set + * @return $packageHandlerInstance The network package handler instance we shall set */ - protected final function getPackageInstance () { - return $this->packageInstance; + protected final function getPackageHandlerInstance () { + return $this->packageHandlerInstance; } /** @@ -495,4 +507,23 @@ abstract class BaseHubSystem extends BaseFrameworkSystem implements HubInterface return $this->sourceInstance; } + /** + * Setter for package instance + * + * @param $packageInstance An instance of a DeliverablePackage instance + * @return void + */ + public final function setPackageInstance (DeliverablePackage $packageInstance) { + $this->packageInstance = $packageInstance; + } + + /** + * Getter for package instance + * + * @return $packageInstance An instance of a DeliverablePackage instance + */ + public function getPackageInstance () { + return $this->packageInstance; + } + } diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 80a6cd8a8..fc57d7338 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -11,7 +11,8 @@ use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper; use Org\Shipsimu\Hub\Information\ShareableInfo; use Org\Shipsimu\Hub\Listener\BaseListener; use Org\Shipsimu\Hub\Listener\Listenable; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; +use Org\Shipsimu\Hub\Network\Package\NetworkPackageHandler; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\ObjectFactory; @@ -56,7 +57,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita /** * Socket protocol: * - 'tcp' for TCP/IPv4 connections - * - 'file' for Unix* file-based sockets + * - 'file' for Uni* file-based sockets */ private $socketProtocol = StorableSocket::SOCKET_PROTOCOL_INVALID; @@ -94,40 +95,34 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita * Creates an instance of this Container class and prepares it for usage * * @param $socketResource A valid socket resource - * @param $socketProtocol Socket protocol - * @param $packageData Raw package data + * @param $socketProtocol Socket protocol (TCP, UDP, file) + * @param $packageInstance An instance of a DeliverablePackage class * @return $socketInstance An instance of this Container class * @throws InvalidArgumentException If socket type is not set in $packageData array */ - public static final function createSocketContainer ($socketResource, $socketProtocol, array $packageData) { + public static final function createSocketContainer ($socketResource, $socketProtocol, DeliverablePackage $packageInstance) { // Trace message - /* 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)); - - // Socket type must always be there - if (!isset($packageData[StorableSocket::SOCKET_ARRAY_INDEX_TYPE])) { - // Abort here - throw new InvalidArgumentException(sprintf('Array packageData does not contain "%s".', StorableSocket::SOCKET_ARRAY_INDEX_TYPE)); - } // END - if + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET: socketResource[%s]=%s,socketProtocol=%s,packageInstance[]=%s - CALLED!', gettype($socketResource), $socketResource, $socketProtocol, gettype($packageInstance))); + //* DEBUG-PRINT: */ printf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE)); // Get a new instance $socketInstance = new SocketContainer(); // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET: socketResource=%s,socketProtocol=%s,packageData[%s]=%s', $socketResource, $socketProtocol, StorableSocket::SOCKET_ARRAY_INDEX_TYPE, $packageData[StorableSocket::SOCKET_ARRAY_INDEX_TYPE])); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET: socketResource=%s,socketProtocol=%s,packageInstance->socketType=%s', $socketResource, $socketProtocol, $packageInstance->getSocketType())); // Set protocol $socketInstance->setSocketProtocol($socketProtocol); - // Remove unneeded entries - unset($packageData[NetworkPackage::PACKAGE_DATA_CONTENT]); - unset($packageData[NetworkPackage::PACKAGE_DATA_HASH]); + // Set content/hash to null + $packageInstance->setContent(NULL); + $packageInstance->setHash(NULL); // Set the resource ... $socketInstance->setSocketResource($socketResource); - // ... and package data - $socketInstance->setPackageData($packageData); + // ... and package instance + $socketInstance->setPackageDataInstance($packageInstance); // Return the prepared instance return $socketInstance; @@ -144,13 +139,13 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unl=%s - CALLED!', strtoupper($this->getSocketProtocol()), $unl)); // Get current package data - $packageData = $this->getPackageData(); + $packageInstance = $this->getPackageDataInstance(); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: unl=' . $unl . ',packageData=' . print_r($packageData, true)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: unl=' . $unl . ',packageInstance=' . print_r($packageInstance, true)); // So, does both match? - $matches = ((isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) && ($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] === $unl)); + $matches = (($packageInstance->getRecipient() !== '') && ($packageInstance->getRecipient() === $unl)); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: matches=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($matches))); @@ -311,19 +306,13 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita } // END - if // Get package data - $packageData = $this->getPackageData(); - - // Is the element there? - if (!isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) { - // Abort here - throw new LogicException(sprintf('packageData[%s] is not set.', NetworkPackage::PACKAGE_DATA_RECIPIENT)); - } // END - if + $packageInstance = $this->getPackageDataInstance(); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: packageData[%s]=%s - EXIT!', strtoupper($this->getSocketProtocol()), NetworkPackage::PACKAGE_DATA_RECIPIENT, $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: packageInstance.recipient=%s - EXIT!', strtoupper($this->getSocketProtocol()), $packageInstance->getRecipient())); // Return it - return $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]; + return $packageInstance->getRecipient(); } /** @@ -832,15 +821,9 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Valid? if ($result === TRUE) { - // Get package data - $packageData = $this->getPackageData(); - // Set both - $packageData[StorableSocket::SOCKET_ARRAY_INDEX_ADDRESS] = $peerAddress; - $packageData[StorableSocket::SOCKET_ARRAY_INDEX_PORT] = $peerPort; - - // Set it back - $this->setPackageData($packageData); + $this->getPackageDataInstance()->setAddress($peerAddress); + $this->getPackageDataInstance()->setPort($peerPort); } // END - if // Trace message @@ -983,36 +966,36 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita if (!$this->isValidSocket()) { // Throw exception throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET); - } elseif (!isset($socketBuffer[NetworkPackage::RAW_INDEX_DIFF])) { + } elseif (!isset($socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF])) { // Opps, should not happen - throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackage::RAW_INDEX_DIFF)); - } elseif (!isset($socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA])) { + throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackageHandler::RAW_INDEX_DIFF)); + } elseif (!isset($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA])) { // Opps, should not happen - throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackage::RAW_INDEX_ENCODED_DATA)); - } elseif (!isset($socketBuffer[NetworkPackage::RAW_INDEX_BUFFER_SIZE])) { + throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackageHandler::RAW_INDEX_ENCODED_DATA)); + } elseif (!isset($socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE])) { // Opps, should not happen - throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackage::RAW_INDEX_BUFFER_SIZE)); - } elseif (!isset($socketBuffer[NetworkPackage::RAW_INDEX_SENT_BYTES])) { + throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE)); + } elseif (!isset($socketBuffer[NetworkPackageHandler::RAW_INDEX_SENT_BYTES])) { // Opps, should not happen - throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackage::RAW_INDEX_SENT_BYTES)); + throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackageHandler::RAW_INDEX_SENT_BYTES)); } // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Sending out %d bytes,rawBufferSize=%d,diff=%d to socketResource=%s', strtoupper($this->getSocketProtocol()), strlen($socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA]), $socketBuffer[NetworkPackage::RAW_INDEX_BUFFER_SIZE], $socketBuffer[NetworkPackage::RAW_INDEX_DIFF], $this->getSocketResource())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Sending out %d bytes,rawBufferSize=%d,diff=%d to socketResource=%s', strtoupper($this->getSocketProtocol()), strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]), $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE], $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF], $this->getSocketResource())); // Is some data still pending or sent all out? - if ($socketBuffer[NetworkPackage::RAW_INDEX_DIFF] >= 0) { + if ($socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF] >= 0) { // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: MD5=%s', strtoupper($this->getSocketProtocol()), md5(substr($socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA], 0, ($socketBuffer[NetworkPackage::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackage::RAW_INDEX_DIFF]))))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: MD5=%s', strtoupper($this->getSocketProtocol()), md5(substr($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], 0, ($socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF]))))); // Send all out (encodedData is smaller than or equal buffer size) - $sentBytes = socket_write($this->getSocketResource(), $socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA], ($socketBuffer[NetworkPackage::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackage::RAW_INDEX_DIFF])); + $sentBytes = socket_write($this->getSocketResource(), $socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], ($socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF])); } else { // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: MD5=%s', strtoupper($this->getSocketProtocol()), md5(substr($socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA], 0, $socketBuffer[NetworkPackage::RAW_INDEX_BUFFER_SIZE])))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: MD5=%s', strtoupper($this->getSocketProtocol()), md5(substr($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], 0, $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE])))); // Send buffer size out - $sentBytes = socket_write($this->getSocketResource(), $socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA], $socketBuffer[NetworkPackage::RAW_INDEX_BUFFER_SIZE]); + $sentBytes = socket_write($this->getSocketResource(), $socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE]); } // Check if the operation was okay @@ -1029,7 +1012,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita // Failed delivery! return FALSE; - } elseif (($sentBytes === 0) && (strlen($socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA]) > 0)) { + } elseif (($sentBytes === 0) && (strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]) > 0)) { // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: All sent, returning TRUE ... - EXIT!', strtoupper($this->getSocketProtocol()))); @@ -1037,19 +1020,19 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita return TRUE; } else { // The difference between sent bytes and length of raw data should not go below zero - assert((strlen($socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA]) - $sentBytes) >= 0); + assert((strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]) - $sentBytes) >= 0); // Add total sent bytes - $socketBuffer[NetworkPackage::RAW_INDEX_SENT_BYTES] += $sentBytes; + $socketBuffer[NetworkPackageHandler::RAW_INDEX_SENT_BYTES] += $sentBytes; // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Sent out %d of %d bytes ...', strtoupper($this->getSocketProtocol()), $sentBytes, strlen($socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA]))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Sent out %d of %d bytes ...', strtoupper($this->getSocketProtocol()), $sentBytes, strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]))); // Cut out the last unsent bytes - $socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA] = substr($socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA], $sentBytes); + $socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA] = substr($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA], $sentBytes); // Calculate difference again - $socketBuffer[NetworkPackage::RAW_INDEX_DIFF] = $socketBuffer[NetworkPackage::RAW_INDEX_BUFFER_SIZE] - strlen($socketBuffer[NetworkPackage::RAW_INDEX_ENCODED_DATA]); + $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF] = $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - strlen($socketBuffer[NetworkPackageHandler::RAW_INDEX_ENCODED_DATA]); } // Trace message diff --git a/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php b/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php index fe27d14af..8322f9ac7 100644 --- a/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php @@ -5,7 +5,8 @@ namespace Org\Shipsimu\Hub\Database\Frontend\Node\Dht; // Import application-specific stuff use Org\Shipsimu\Hub\Database\Frontend\BaseHubDatabaseWrapper; use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Node\BaseHubNode; // Import framework stuff @@ -612,16 +613,13 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper imp /** * Find recipients for given package data and exclude the sender * - * @param $packageData An array of valid package data + * @param $packageData An instance of a DeliverablePackage class * @return $recipients An indexed array with DHT recipients */ - public function getResultFromExcludedSender (array $packageData) { + public function getResultFromExcludedSender (DeliverablePackage $packageInstance) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!'); - // Assert on required array field - assert(isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS])); - // Get max recipients $maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients'); @@ -629,7 +627,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_ADDRESS]); + $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID, $packageInstance->getSenderAddress()); // Set limit to maximum DHT recipients $searchInstance->setLimit($maxRecipients); diff --git a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php index 114858855..47852d431 100644 --- a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php @@ -5,8 +5,9 @@ namespace Org\Shipsimu\Hub\Database\Frontend\Node\PeerState; // Import application-specific sutff use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Database\Frontend\BaseHubDatabaseWrapper; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\LookupTable\Lookupable; use Org\Shipsimu\Hub\State\Peer\Lookup\LookupablePeerState; use Org\Shipsimu\Hub\State\Peer\PeerStateable; @@ -86,13 +87,13 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L * @param $packageData Valid raw package data array * @return $searchInstance An instance of a LocalSearchCriteria class */ - private function getSenderSearchInstanceFromPackageData (array $packageData) { + private function getSenderSearchInstanceFromPackageInstance (DeliverablePackage $packageInstance) { // Get the instance - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-DATABASE-WRAPPER: Searching for sender ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-DATABASE-WRAPPER: Searching for sender ' . $packageInstance->getSenderAddress()); $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_ADDRESS]); + $searchInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageInstance->getSenderAddress()); $searchInstance->setLimit(1); // Return the instance @@ -102,22 +103,16 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L /** * Checks whether given 'sender' is a new peer * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @param $dataSetInstance An optional instance of a StoreableCriteria class * @return $isNewPeer Whether 'sender' is a new peer to this peer */ - public function isSenderNewPeer (array $packageData, StoreableCriteria $dataSetInstance = NULL) { + public function isSenderNewPeer (DeliverablePackage $packageInstance, StoreableCriteria $dataSetInstance = NULL) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-DATABASE-WRAPPER: packageData()=' . count($packageData) . ' - CALLED!'); - // Is the package valid? - if (!isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS])) { - // Invalid package found, please report this - $this->debugBackTrace(': packageData=' . print_r($packageData, TRUE)); - } // END - if - // Get a search criteria instance from package data - $searchInstance = $this->getSenderSearchInstanceFromPackageData($packageData); + $searchInstance = $this->getSenderSearchInstanceFromPackageInstance($packageData); // Is the dataset instance set? if ($dataSetInstance instanceof StoreableCriteria) { @@ -140,12 +135,12 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L /** * Registers a new peer with given package data. We use the session id from it. * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @param $socketResource An instance of a StorableSocket class * @return void * @throws PeerAlreadyRegisteredException If a peer is already registered */ - public function registerPeerByPackageData (array $packageData, StorableSocket $socketInstance) { + public function registerPeerByPackageData (DeliverablePackage $packageInstance, StorableSocket $socketInstance) { // Make sure only new peers can be registered with package data if (!$this->isSenderNewPeer($packageData)) { // Throw an exception because this should normally not happen @@ -159,7 +154,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_ADDRESS]); + $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageInstance->getSenderAddress()); // Init peer address/port $peerAddress = '0.0.0.0'; @@ -180,19 +175,19 @@ 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('PEER-STATE-DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS] . ' has been registered.'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-DATABASE-WRAPPER: Peer ' . $packageInstance->getSenderAddress() . ' has been registered.'); } /** - * Registers the given peer state and raw package data + * Registers the given peer state and An instance of a DeliverablePackage class * * @param $stateInstance A PeerStateable class instance - * @param $packageData Valid package data array + * @param $packageData An instance of a DeliverablePackage class * @return void * @throws PeerAlreadyRegisteredException If a peer is already registered * @todo Unfinished area */ - public function registerPeerState (PeerStateable $stateInstance, array $packageData) { + public function registerPeerState (PeerStateable $stateInstance, DeliverablePackage $packageInstance) { // Generate a dataset instance $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_PEER_LOOKUP)); @@ -200,11 +195,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_ADDRESS]); + $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageInstance->getSenderAddress()); $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_ADDRESS])); + $senderData = explode(':', HubTools::resolveSessionId($packageInstance->getSenderAddress())); // Just make sure that 'invalid:invalid' is not being processed assert(($senderData[0] != 'invalid') && ($senderData[1] != 'invalid') && ($senderData[2] != 'invalid')); @@ -223,7 +218,7 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L } // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS] . ' has been registered/updated with state ' . $stateInstance->getStateName()); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-DATABASE-WRAPPER: Peer ' . $packageInstance->getSenderAddress() . ' has been registered/updated with state ' . $stateInstance->getStateName()); } /** @@ -242,15 +237,15 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L * in database compared with the one from $helperInstance. * * @param $helperInstance An instance of a ConnectionHelper class - * @param $packageData Valid package data array + * @param $packageData An instance of a DeliverablePackage class * @return $isSamePeerState Whether the peer's state is the same */ - public function isSamePeerState (ConnectionHelper $helperInstance, array $packageData) { + public function isSamePeerState (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PEER-STATE-DATABASE-WRAPPER: State ' . $helperInstance->getPrintableState() . ' needs to be checked it has changed ...'); // Now get the search instance from given package data - $searchInstance = $this->getSenderSearchInstanceFromPackageData($packageData); + $searchInstance = $this->getSenderSearchInstanceFromPackageInstance($packageData); // With this search instance query the database for the peer and get a result instance $resultInstance = $this->doSelectByCriteria($searchInstance); diff --git a/application/hub/classes/decoder/package/class_PackageDecoder.php b/application/hub/classes/decoder/package/class_PackageDecoder.php index 99e09d0d7..5eddd4ed7 100644 --- a/application/hub/classes/decoder/package/class_PackageDecoder.php +++ b/application/hub/classes/decoder/package/class_PackageDecoder.php @@ -9,7 +9,7 @@ use Org\Shipsimu\Hub\Factory\Discovery\Package\PackageDiscoveryFactory; use Org\Shipsimu\Hub\Factory\Network\NetworkPackageFactory; use Org\Shipsimu\Hub\Handler\Network\RawData\Chunks\ChunkHandler; use Org\Shipsimu\Hub\Handler\Network\RawData\Chunks\HandleableChunks; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; // Import framework stuff use Org\Mxchange\CoreFramework\Registry\Registerable; @@ -69,10 +69,10 @@ class PackageDecoder extends BaseDecoder implements Decodeable, Registerable { $decoderInstance->setHandlerInstance($handlerInstance); // Get a singleton network package instance - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // And set it in this decoder - $decoderInstance->setPackageInstance($packageInstance); + $decoderInstance->setPackageHandlerInstance($packageInstance); // Return the prepared instance return $decoderInstance; @@ -107,39 +107,23 @@ class PackageDecoder extends BaseDecoder implements Decodeable, Registerable { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-DECODER: Got ' . strlen($rawPackageContent) . ' bytes from stack ' . ChunkHandler::STACKER_NAME_ASSEMBLED_RAW_DATA . ', decoding it ...'); - // "Decode" the raw package content by using the NetworkPackage instance - $decodedData = $this->getPackageInstance()->decodeRawContent($rawPackageContent); - - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-DECODER: decodedData=' . print_r($decodedData, TRUE)); - - // Some checks - assert( - (is_array($decodedData)) && - (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])) - ); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-DECODER: decodedData(' . count($decodedData) . ')=' . print_r($decodedData, TRUE)); + // "Decode" the raw package content by using the NetworkPackageHandler instance + $packageInstance = $this->getPackageHandlerInstance()->decodeRawContent($rawPackageContent); // Next get a recipient-discovery instance $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance(); // ... then disover all recipient (might be only one), this package may shall be forwarded - $discoveryInstance->discoverRawRecipients($decodedData); + $discoveryInstance->discoverRawRecipients($packageInstance); // Check for 'recipient' field (the 'sender' field and others are ignored here) if ($discoveryInstance->isRecipientListEmpty()) { // The recipient is this node so next stack it on 'decoded_package' - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-DECODER: Pushing ' . strlen($decodedData) . ' bytes to stack ' . self::STACKER_NAME_DECODED_PACKAGE . ' ...'); - $this->getHandlerInstance()->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_PACKAGE, $decodedData); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-DECODER: Pushing ' . strlen($packageInstance) . ' bytes to stack ' . self::STACKER_NAME_DECODED_PACKAGE . ' ...'); + $this->getHandlerInstance()->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_PACKAGE, $packageInstance); } else { // Forward the package to the next node - $this->getPackageInstance()->forwardRawPackage($decodedData); + $this->getPackageHandlerInstance()->forwardRawPackage($packageInstance); } } @@ -166,11 +150,11 @@ class PackageDecoder extends BaseDecoder implements Decodeable, Registerable { assert($this->ifDeocedPackagesLeft()); // Get the next entry (assoziative array) - $decodedData = $this->getHandlerInstance()->getStackInstance()->popNamed(self::STACKER_NAME_DECODED_PACKAGE); + $packageInstance = $this->getHandlerInstance()->getStackInstance()->popNamed(self::STACKER_NAME_DECODED_PACKAGE); // Handle it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-DECODER: decodedData(' . count($decodedData) . ')=' . print_r($decodedData, TRUE)); - $this->getPackageInstance()->handleRawData($decodedData); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-DECODER: decodedData(' . count($packageInstance) . ')=' . print_r($packageInstance, TRUE)); + $this->getPackageHandlerInstance()->handleRawData($packageInstance); } } diff --git a/application/hub/classes/dht/node/class_NodeDhtFacade.php b/application/hub/classes/dht/node/class_NodeDhtFacade.php index aca68aa34..236933dd9 100644 --- a/application/hub/classes/dht/node/class_NodeDhtFacade.php +++ b/application/hub/classes/dht/node/class_NodeDhtFacade.php @@ -6,6 +6,7 @@ namespace Org\Shipsimu\Hub\Dht\Node; use Org\Shipsimu\Hub\Dht\BaseDht; use Org\Shipsimu\Hub\Dht\Distributable; use Org\Shipsimu\Hub\Generic\BaseHubSystem; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\Database\Wrapper\DatabaseWrapperFactory; @@ -332,10 +333,10 @@ class NodeDhtFacade extends BaseDht implements DistributableNode, Registerable { /** * Find recipients for given package data * - * @param $packageData An array of valid package data + * @param $packageData An instance of a DeliverablePackage class * @return $recipients An indexed array with DHT recipients */ - public function findRecipientsByPackageData (array $packageData) { + public function findRecipientsByPackageData (DeliverablePackage $packageInstance) { // Query get a result instance back from DHT database wrapper. $resultInstance = $this->getWrapperInstance()->getResultFromExcludedSender($packageData); diff --git a/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php b/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php index ca9ae636f..348956f30 100644 --- a/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php +++ b/application/hub/classes/discovery/protocol/class_ProtocolDiscovery.php @@ -5,7 +5,8 @@ namespace Org\Shipsimu\Hub\Discovery\Protocol; // Import application-specific stuff use Org\Shipsimu\Hub\Discovery\Node\BaseNodeDiscovery; use Org\Shipsimu\Hub\Factory\Tag\Package\PackageTagsFactory; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Registry\Registerable; @@ -62,7 +63,7 @@ class ProtocolDiscovery extends BaseNodeDiscovery implements DiscoverableProtoco * @param $packageData Valid package data * @return $protocolInstance An instance of a HandleableProtocol class */ - public static final function determineProtocolByPackageData (array $packageData) { + public static final function determineProtocolByPackageData (DeliverablePackage $packageInstance) { // First we need a tags instance $tagsInstance = PackageTagsFactory::createPackageTagsInstance(); @@ -73,7 +74,7 @@ class ProtocolDiscovery extends BaseNodeDiscovery implements DiscoverableProtoco * and if so we can re-use it. If there is no socket registered, we try * to make a new connection to the given Universal Node Locator. */ - $protocolInstance = $tagsInstance->chooseProtocolFromPackageData($packageData); + $protocolInstance = $tagsInstance->chooseProtocolFromPackageInstance($packageData); // Return it return $protocolInstance; @@ -83,17 +84,17 @@ class ProtocolDiscovery extends BaseNodeDiscovery implements DiscoverableProtoco * "Discovers" the protocol type from given raw package data. This is done * by looking at the 'recipient' field and extract the first part from it. * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $protocolType Type of protocol, e.g. 'tcp' for TCP/IPv4 connections */ - public static final function discoverProtocolByPackageData (array $packageData) { + public static final function discoverProtocolByPackageData (DeliverablePackage $packageInstance) { //* DEBUG: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE)); /* * "Explode" the 'recipient' array element into a new one, giving at * least two entries: protocol://address */ - $recipient = explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + $recipient = explode(':', $packageInstance->getRecipient()); // At least 2 entries must be found assert(count($recipient) >= 2); diff --git a/application/hub/classes/discovery/recipient/dht/class_DhtRecipientDiscovery.php b/application/hub/classes/discovery/recipient/dht/class_DhtRecipientDiscovery.php index 825bbb510..54dfe716b 100644 --- a/application/hub/classes/discovery/recipient/dht/class_DhtRecipientDiscovery.php +++ b/application/hub/classes/discovery/recipient/dht/class_DhtRecipientDiscovery.php @@ -5,6 +5,7 @@ namespace Org\Shipsimu\Hub\Discovery\Recipient\Dht; // Import application-specific stuff use Org\Shipsimu\Hub\Discovery\Recipient\BaseRecipientDiscovery; use Org\Shipsimu\Hub\Factory\Dht\DhtObjectFactory; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Registry\Registerable; @@ -58,10 +59,10 @@ class DhtRecipientDiscovery extends BaseRecipientDiscovery implements Discoverab /** * Resolves one or more recipients for a DHT transfer by given package data. * - * @param $packageData Valid package data array + * @param $packageData An instance of a DeliverablePackage class * @return $recipients An indexed array with DHT recipients */ - public function resolveRecipientsByPackageData (array $packageData) { + public function resolveRecipientsByPackageData (DeliverablePackage $packageInstance) { // Use facade to get recipients back $recipients = DhtObjectFactory::createDhtInstance('node')->findRecipientsByPackageData($packageData); diff --git a/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php b/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php index 8310587ff..8213e6193 100644 --- a/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php +++ b/application/hub/classes/discovery/recipient/package/class_PackageRecipientDiscovery.php @@ -6,8 +6,9 @@ namespace Org\Shipsimu\Hub\Discovery\Network\Recipient; use Org\Shipsimu\Hub\Discovery\Recipient\BaseRecipientDiscovery; use Org\Shipsimu\Hub\Discovery\Recipient\Node\DiscoverableNodeRecipient; use Org\Shipsimu\Hub\Factory\Handler\Protocol\ProtocolHandlerFactory; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Locator\Node\Tools\NodeLocatorUtils; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Node\Data\InvalidSessionIdException; use Org\Shipsimu\Hub\Tools\HubTools; @@ -65,14 +66,10 @@ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements Discov /** * Tries to discover all recipients for given package data * - * @param $packageData Raw package data array + * @param $packageData An instance of a DeliverablePackage class array * @return void */ - public function discoverRecipients (array $packageData) { - // This must be available - //* DEBUG: */ print $this->__toString() . ': packageData=' . print_r($packageData, TRUE); - assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); - + public function discoverRecipients (DeliverablePackage $packageInstance) { // Init instance $recipientInstance = NULL; @@ -83,16 +80,16 @@ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements Discov * recipient must be direct recipient then, otherwise a "virtual" * recipient. */ - if (NodeLocatorUtils::isValidUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) { + if (NodeLocatorUtils::isValidUniversalNodeLocator($packageInstance->getRecipient())) { // Get instance (should not break) $recipientInstance = ObjectFactory::createObjectByConfiguredName('direct_recipient_class'); } else { // Try to find the right class - $recipientInstance = ObjectFactory::createObjectByConfiguredName(strtolower($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]) . '_recipient_class'); + $recipientInstance = ObjectFactory::createObjectByConfiguredName(strtolower($packageInstance->getRecipient()) . '_recipient_class'); } // Try to solve it - $recipientInstance->resolveRecipient($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], $this->getListInstance(), $packageData); + $recipientInstance->resolveRecipient($packageInstance->getRecipient(), $this->getListInstance(), $packageData); } catch (FrameworkException $e) { // Could not find class, what ever failed $this->debugInstance(sprintf('[%s:%d]: Exception: %s,message=%s', __METHOD__, __LINE__, $e->__toString(), $e->getMessage())); @@ -102,23 +99,20 @@ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements Discov /** * Tries to discover all recipients by given decoded package data. * - * @param $decodedData Raw raw package data array + * @param $packageInstance An instance of a DeliverablePackage class * @return void * @todo Add some validation of recipient field, e.g. an Universal Node Locator is found * @todo Enrich both messages with recipient data */ - public function discoverRawRecipients (array $decodedData) { - // This must be available - assert(isset($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); - + public function discoverRawRecipients (DeliverablePackage $packageInstance) { // First clear all recipients $this->clearRecipients(); // Get a protocol handler back from decoded data - $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($decodedData); + $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageInstance($packageInstance); // Is the 'recipient' field same as this peer's IP? - if ($handlerInstance->isOwnAddress($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) { + if ($handlerInstance->isOwnAddress($packageInstance->getRecipient())) { /* * Is same as own external address + TCP/UDP listen port or * internal address, don't do anything here so other classes found @@ -127,13 +121,13 @@ class PackageRecipientDiscovery extends BaseRecipientDiscovery implements Discov */ // Debug output (may flood) - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RECIPIENT-DISCOVERY: Recipient ' . $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ' matches own ip (external=' . HubTools::determineOwnExternalAddress() . ' or internal=' . HubTools::determineOwnInternalAddress() . ')'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RECIPIENT-DISCOVERY: Recipient ' . $packageInstance->getRecipient() . ' matches own ip (external=' . HubTools::determineOwnExternalAddress() . ' or internal=' . HubTools::determineOwnInternalAddress() . ')'); } else { // Debug output (may flood) - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RECIPIENT-DISCOVERY: Recipient ' . $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ' is different than own external address (' . HubTools::determineOwnExternalAddress() . ') nor internal address (' . HubTools::determineOwnInternalAddress() . '), need to forward (not yet implemented)!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RECIPIENT-DISCOVERY: Recipient ' . $packageInstance->getRecipient() . ' is different than own external address (' . HubTools::determineOwnExternalAddress() . ') nor internal address (' . HubTools::determineOwnInternalAddress() . '), need to forward (not yet implemented)!'); // This package is to be delivered to someone else, so add it - // @TODO Unfinished: $this->getListInstance()->addEntry('unl', $decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + // @TODO Unfinished: $this->getListInstance()->addEntry('unl', $packageInstance->getRecipient()); } } diff --git a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php index 12c64e5c7..bf2fac542 100644 --- a/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php +++ b/application/hub/classes/discovery/recipient/socket/class_PackageSocketDiscovery.php @@ -10,9 +10,10 @@ use Org\Shipsimu\Hub\Discovery\Socket\DiscoverableSocket; use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory; use Org\Shipsimu\Hub\Factory\Socket\SocketFactory; use Org\Shipsimu\Hub\Generic\BaseHubSystem; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Handler\Protocol\HandleableProtocol; use Org\Shipsimu\Hub\Listener\Listenable; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Resolver\State\Peer\PeerStateResolver; // Import framework stuff @@ -77,10 +78,10 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera * Tries to dicover the right listener instance * * @param $protocolInstance An instance of a HandleableProtocol class - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $listenerInstance An instance of a Listenable instance or null */ - public function discoverListenerInstance (HandleableProtocol $protocolInstance, array $packageData) { + public function discoverListenerInstance (HandleableProtocol $protocolInstance, DeliverablePackage $packageInstance) { // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: protocolInstance=' . $protocolInstance->__toString() . ' - CALLED!'); @@ -140,7 +141,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera * Tries to discover the right socket for given package data and returns a * matching socket resource for that protocol. * - * @param $packageData Raw package data array + * @param $packageData An instance of a DeliverablePackage class array * @param $connectionType Type of connection, can be either 'incoming' or 'outgoing', but *NEVER* 'server'! * @return $socketResource A valid socket resource or FALSE if an error occured * @throws NoListGroupException If the procol group is not found in peer list @@ -149,19 +150,16 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera * @throws InvalidArgumentException If one of the parameters are not valid * @throws LogicException If the discovered listener instance has no pool set */ - public function discoverSocket (array $packageData, $connectionType) { + public function discoverSocket (DeliverablePackage $packageInstance, $connectionType) { // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISOVERY: packageData()=' . count($packageData) . ',connectionType=' . $connectionType . ' - CALLED!'); /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISOVERY: packageData=' . print_r($packageData, TRUE)); // Make sure all parameters are valid - if (!isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) { - // Abort here - throw new InvalidArgumentException(sprintf('Array packageData(%d) does not contain field "%s".', count($packageData), NetworkPackage::PACKAGE_DATA_RECIPIENT)); - } elseif (($connectionType != StorableSocket::CONNECTION_TYPE_INCOMING) && ($connectionType != StorableSocket::CONNECTION_TYPE_OUTGOING)) { + if (($connectionType != StorableSocket::CONNECTION_TYPE_INCOMING) && ($connectionType != StorableSocket::CONNECTION_TYPE_OUTGOING)) { // Abort here throw new InvalidArgumentException(sprintf('connectionType=%s is whether "%s" nor "%s".', $connectionType, StorableSocket::CONNECTION_TYPE_INCOMING, StorableSocket::CONNECTION_TYPE_OUTGOING)); - } + } // END - if // Determine protocol instance $protocolInstance = ProtocolDiscovery::determineProtocolByPackageData($packageData); @@ -199,7 +197,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera * instance and pass over the whole package data to get the right * socket. */ - $socketInstance = $listenerInstance->getPoolInstance()->getSocketFromPackageData($packageData, $connectionType); + $socketInstance = $listenerInstance->getPoolInstance()->getSocketFromPackageInstance($packageData, $connectionType); // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISCOVERY: socketInstance[]=%s', gettype($socketInstance))); @@ -209,10 +207,10 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera // Try to create a new socket resource try { // Possibly noisy debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Trying to establish a ' . strtoupper($listenerInstance->getProtocolName()) . ' connection to ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ' ...'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Trying to establish a ' . strtoupper($listenerInstance->getProtocolName()) . ' connection to ' . $packageInstance->getRecipient() . ' ...'); // Get a socket resource from our factory (if succeeded) - $socketInstance = SocketFactory::createSocketFromPackageData($packageData, $protocolInstance); + $socketInstance = SocketFactory::createSocketFromPackageInstance($packageData, $protocolInstance); } catch (SocketConnectionException $e) { // The connection fails of being established, so log it away self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Caught ' . $e->__toString() . ',message=' . $e->getMessage()); @@ -231,7 +229,7 @@ class PackageSocketDiscovery extends BaseRecipientDiscovery implements Discovera PeerStateResolver::resolveStateByPackage($helperInstance, $packageData, $socketInstance); } catch (InvalidSocketException $e) { // This cannot be fixed, so log it away - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Cannot discover socket resource for recipient ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ': ' . $e->getMessage()); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISCOVERY: Cannot discover socket resource for recipient ' . $packageInstance->getRecipient() . ': ' . $e->getMessage()); // Make any failed attempts to 'FALSE' $socketInstance = NULL; diff --git a/application/hub/classes/factories/handler/class_NetworkPackageHandlerFactory.php b/application/hub/classes/factories/handler/class_NetworkPackageHandlerFactory.php new file mode 100644 index 000000000..aa7421a70 --- /dev/null +++ b/application/hub/classes/factories/handler/class_NetworkPackageHandlerFactory.php @@ -0,0 +1,79 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub 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 . + */ +class NetworkPackageHandlerFactory extends ObjectFactory { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Returns a singleton network package handler instance. If an instance is + * found in the registry it will be returned, else a new instance is created + * and stored in the same registry entry. + * + * @return $handlerInstance A network package handler instance + */ + public static final function createNetworkPackageHandlerInstance () { + // Do we have an instance in the registry? + if (GenericRegistry::getRegistry()->instanceExists('network_package_handler')) { + // Then use this instance + $handlerInstance = GenericRegistry::getRegistry()->getInstance('network_package_handler'); + } else { + /* + * Prepare the compressor for our package, ZLIB should be fine but we + * keep it open here so you can experiment with the settings and don't + * need to touch this code. + */ + $compressorInstance = self::createObjectByConfiguredName('raw_package_compressor_class'); + + // Prepare the decorator compressor (for later flawless and easy updates) + $compressorInstance = self::createObjectByConfiguredName('deco_package_compressor_class', array($compressorInstance)); + + /* + * Now prepare the network package for delivery so only need to do this + * once just before the "big announcement loop". + */ + $handlerInstance = self::createObjectByConfiguredName('network_package_handler_class', array($compressorInstance)); + + // Set the instance in registry for further use + GenericRegistry::getRegistry()->addInstance('network_package_handler', $handlerInstance); + } + + // Return the instance + return $handlerInstance; + } + +} diff --git a/application/hub/classes/factories/handler/class_ProtocolHandlerFactory.php b/application/hub/classes/factories/handler/class_ProtocolHandlerFactory.php index 1fce8ba0d..01cc53722 100644 --- a/application/hub/classes/factories/handler/class_ProtocolHandlerFactory.php +++ b/application/hub/classes/factories/handler/class_ProtocolHandlerFactory.php @@ -5,6 +5,7 @@ namespace Org\Shipsimu\Hub\Factory\Handler\Protocol; // Import application-specific stuff use Org\Shipsimu\Hub\Discovery\Protocol\ProtocolDiscovery; use Org\Shipsimu\Hub\Helper\HubHelper; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\ObjectFactory; @@ -89,7 +90,7 @@ class ProtocolHandlerFactory extends ObjectFactory { * @param $packageData An array with raw package data * @return $handlerInstance A protocol handler instance */ - public static final function createProtocolHandlerFromPackageData (array $packageData) { + public static final function createProtocolHandlerFromPackageInstance (DeliverablePackage $packageInstance) { // "Discover" the protocol type $protocolType = ProtocolDiscovery::discoverProtocolByPackageData($packageData); diff --git a/application/hub/classes/factories/package/class_NetworkPackageFactory.php b/application/hub/classes/factories/package/class_NetworkPackageFactory.php index 61c256813..767e275bb 100644 --- a/application/hub/classes/factories/package/class_NetworkPackageFactory.php +++ b/application/hub/classes/factories/package/class_NetworkPackageFactory.php @@ -46,7 +46,7 @@ class NetworkPackageFactory extends ObjectFactory { * * @return $packageInstance A network package instance */ - public static final function createNetworkPackageInstance () { + public static final function createNetworkPackageHandlerInstance () { // Do we have an instance in the registry? if (GenericRegistry::getRegistry()->instanceExists('network_package')) { // Then use this instance diff --git a/application/hub/classes/factories/package/data/.htaccess b/application/hub/classes/factories/package/data/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/factories/package/data/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/factories/package/data/class_PackageDataFactory.php b/application/hub/classes/factories/package/data/class_PackageDataFactory.php new file mode 100644 index 000000000..9fbbb64ff --- /dev/null +++ b/application/hub/classes/factories/package/data/class_PackageDataFactory.php @@ -0,0 +1,69 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub 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 . + */ +class PackageDataFactory extends ObjectFactory { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Returns a singleton DeliverablePackage instance. If an instance is found + * in the registry it will be returned, else a new instance is created and + * stored in the same registry entry. + * + * @return $packageInstance An instance of a DeliverablePackage class + */ + public static final function createPackageDataInstance () { + // Do we have an instance in the registry? + if (GenericRegistry::getRegistry()->instanceExists('package_data')) { + // Then use this instance + $packageInstance = GenericRegistry::getRegistry()->getInstance('package_data'); + } else { + /* + * Now prepare the network package for delivery so only need to do this + * once just before the "big announcement loop". + */ + $packageInstance = self::createObjectByConfiguredName('package_data_class'); + + // Set the instance in registry for further use + GenericRegistry::getRegistry()->addInstance('package_data', $packageInstance); + } + + // Return the instance + return $packageInstance; + } + +} diff --git a/application/hub/classes/factories/socket/class_SocketFactory.php b/application/hub/classes/factories/socket/class_SocketFactory.php index 104240b97..ac6a9d1c2 100644 --- a/application/hub/classes/factories/socket/class_SocketFactory.php +++ b/application/hub/classes/factories/socket/class_SocketFactory.php @@ -4,10 +4,12 @@ namespace Org\Shipsimu\Hub\Factory\Socket; // Import application-specific stuff use Org\Shipsimu\Hub\Container\Socket\StorableSocket; +use Org\Shipsimu\Hub\Factory\Network\PackageDataFactory; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Handler\Protocol\HandleableProtocol; use Org\Shipsimu\Hub\Listener\BaseListener; use Org\Shipsimu\Hub\Listener\Listenable; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Pool\Poolable; // Import framework stuff @@ -57,16 +59,16 @@ class SocketFactory extends ObjectFactory { /** * Creates a valid socket resource from given packae data and protocol * - * @param $packageData Raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @param $protocolInstance An instance of a HandleableProtocol class * @return $socketInstance An instance of a StorableSocket class */ - public static function createSocketFromPackageData (array $packageData, HandleableProtocol $protocolInstance) { + public static function createSocketFromPackageInstance (DeliverablePackage $packageInstance, HandleableProtocol $protocolInstance) { // Init instance $socketInstance = NULL; // Construct registry key - $registryKey = 'socket_' . $protocolInstance->getProtocolName() . '_' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]; + $registryKey = 'socket_' . $protocolInstance->getProtocolName() . '_' . $packageInstance->getRecipient(); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Trying to find a socket with registryKey=' . $registryKey); @@ -86,7 +88,7 @@ class SocketFactory extends ObjectFactory { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Going to use class ' . $className . ' for creating a socket resource ...'); // And call the static method - $socketInstance = call_user_func($className . '::createConnectionFromPackageData', $packageData); + $socketInstance = call_user_func($className . '::createConnectionFromPackageInstance', $packageInstance); // Register it with the registry GenericRegistry::getRegistry()->addInstance($registryKey, $socketInstance); @@ -122,18 +124,18 @@ class SocketFactory extends ObjectFactory { // File name must not be empty assert(!empty($socketFile)); - // Init package data - $packageData = array( - StorableSocket::SOCKET_ARRAY_INDEX_FILE => $socketFile, - '__fake_port' => '0', - StorableSocket::SOCKET_ARRAY_INDEX_TYPE => StorableSocket::CONNECTION_TYPE_SERVER, - ); + // Init package instance + $packageInstance = PackageDataFactory::createPackageDataInstance(); + + // Set file socket data + $packageInstance->setSocketFile($socketFile); + $packageInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER); // Init main socket $socketResource = socket_create(AF_UNIX, SOCK_STREAM, 0); // Get container from it - $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_FILE, $packageData)); + $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_FILE, $packageInstance)); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance[]=%s', gettype($socketInstance))); @@ -147,13 +149,13 @@ class SocketFactory extends ObjectFactory { // Check if there was an error else if ($socketInstance->getLastSocketErrorCode() > 0) { // Handle this socket error with a faked recipientData array - $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData)); + $socketInstance->handleSocketError(__METHOD__, __LINE__, $packageInstance->getAllValuesAsArray()); } // END - if // Is the file there? - if ((FrameworkBootstrap::isReachableFilePath($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE])) && (file_exists($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE]))) { + if ((FrameworkBootstrap::isReachableFilePath($packageInstance->getSocketFile())) && (file_exists($packageInstance->getSocketFile()))) { // Old socket found - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: WARNING: Old socket at ' . $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE] . ' found. Will not start.'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: WARNING: Old socket at ' . $packageInstance->getSocketFile() . ' found. Will not start.'); // Shutdown this socket $socketInstance->shutdownSocket(); @@ -163,10 +165,10 @@ class SocketFactory extends ObjectFactory { } // END - if // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE] . ' ...'); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $packageInstance->getSocketFile() . ' ...'); // Try to bind to it - if (!$socketInstance->bindSocketTo($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE])) { + if (!$socketInstance->bindSocketTo($packageInstance->getSocketFile())) { // Handle error here $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData)); } // END - if @@ -196,14 +198,14 @@ class SocketFactory extends ObjectFactory { * Creates a socket instance from given package data. The socket then * can be used to make outgoing TCP/IPv4/6 connections. * - * @param $packageData Valid package data + * @param $packageInstance An instance of a DeliverablePackge class * @return $socketInstance An instance of a StorableSocket class * @throws SocketCreationException If the socket cannot be created * @todo Rewrite this to also handle IPv6 addresses and sockets */ - public static final function createTcpOutgoingSocketFromPackageData (array $packageData) { + public static final function createTcpOutgoingSocketFromPackageInstance (DeliverablePackage $packageInstance) { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: packageData=' . print_r($packageData, TRUE) . ' - CALLED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s - CALLED!', $packageInstance->__toString())); // Create a socket instance $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); @@ -344,7 +346,7 @@ class SocketFactory extends ObjectFactory { // Trace message //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString())); - // Create a streaming socket, of type TCP/IP + // Create a streaming socket, of type UDP $socketResource = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // Init fake package data with address/port from listener diff --git a/application/hub/classes/factories/states/peer/class_PeerStateFactory.php b/application/hub/classes/factories/states/peer/class_PeerStateFactory.php index c100c479c..f96bcf451 100644 --- a/application/hub/classes/factories/states/peer/class_PeerStateFactory.php +++ b/application/hub/classes/factories/states/peer/class_PeerStateFactory.php @@ -5,6 +5,7 @@ namespace Org\Shipsimu\Hub\Factory\State\Peer; // Import application-specific stuff use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\ObjectFactory; @@ -71,12 +72,12 @@ class PeerStateFactory extends ObjectFactory { * from current state. * * @param $helperInstance An instance of a ConnectionHelper class - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @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, StorableSocket $socketInstance, $errorCode) { + public static final function createPeerStateInstanceBySocketStatusCode (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance, StorableSocket $socketInstance, $errorCode) { // Init state instance, this is better coding practice $stateInstance = NULL; diff --git a/application/hub/classes/filter/class_BaseHubFilter.php b/application/hub/classes/filter/class_BaseHubFilter.php index 9b8276c84..dccea157d 100644 --- a/application/hub/classes/filter/class_BaseHubFilter.php +++ b/application/hub/classes/filter/class_BaseHubFilter.php @@ -3,7 +3,7 @@ namespace Org\Shipsimu\Hub\Filter; // Import application-specific stuff -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Network\Receive\Receivable; // Import framework stuff @@ -68,16 +68,16 @@ abstract class BaseHubFilter extends BaseFilter { */ protected function genericProcessMessage ($messageType, array $messageData, Receivable $packageInstance) { // Make sure the wanted element is there - assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_MESSAGE])); - assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_SENDER])); - assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_HASH])); - assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_TAGS])); + assert(isset($messageData[NetworkPackageHandler::PACKAGE_CONTENT_MESSAGE])); + assert(isset($messageData[NetworkPackageHandler::PACKAGE_CONTENT_SENDER])); + assert(isset($messageData[NetworkPackageHandler::PACKAGE_CONTENT_HASH])); + assert(isset($messageData[NetworkPackageHandler::PACKAGE_CONTENT_TAGS])); // Get a template instance from the factory $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_' . $messageType . '_template_class'); // Get message content - $messageContent = $messageData[NetworkPackage::PACKAGE_CONTENT_MESSAGE]; + $messageContent = $messageData[NetworkPackageHandler::PACKAGE_CONTENT_MESSAGE]; // And render the XML content (aka message) $templateInstance->renderXmlContent($messageContent); @@ -117,19 +117,19 @@ abstract class BaseHubFilter extends BaseFilter { // Construct an array for pushing it on next stack $messageArray = array( // Message data itself - NetworkPackage::MESSAGE_ARRAY_DATA => $this->dataXmlNodes, + NetworkPackageHandler::MESSAGE_ARRAY_DATA => $this->dataXmlNodes, // Message type (which is $messageType) - NetworkPackage::MESSAGE_ARRAY_TYPE => $messageType, + NetworkPackageHandler::MESSAGE_ARRAY_TYPE => $messageType, // Message sender - NetworkPackage::MESSAGE_ARRAY_SENDER => $messageData[NetworkPackage::PACKAGE_CONTENT_SENDER], + NetworkPackageHandler::MESSAGE_ARRAY_SENDER => $messageData[NetworkPackageHandler::PACKAGE_CONTENT_SENDER], // Package hash - NetworkPackage::MESSAGE_ARRAY_HASH => $messageData[NetworkPackage::PACKAGE_CONTENT_HASH], + NetworkPackageHandler::MESSAGE_ARRAY_HASH => $messageData[NetworkPackageHandler::PACKAGE_CONTENT_HASH], // Package tags - NetworkPackage::MESSAGE_ARRAY_TAGS => $messageData[NetworkPackage::PACKAGE_CONTENT_TAGS], + NetworkPackageHandler::MESSAGE_ARRAY_TAGS => $messageData[NetworkPackageHandler::PACKAGE_CONTENT_TAGS], ); // Push the processed message back on stack - $packageInstance->getStackInstance()->pushNamed(NetworkPackage::STACKER_NAME_PROCESSED_MESSAGE, $messageArray); + $packageInstance->getStackInstance()->pushNamed(NetworkPackageHandler::STACKER_NAME_PROCESSED_MESSAGE, $messageArray); } } diff --git a/application/hub/classes/handler/chunks/class_ChunkHandler.php b/application/hub/classes/handler/chunks/class_ChunkHandler.php index 7c48c2d20..3a5eb5fde 100644 --- a/application/hub/classes/handler/chunks/class_ChunkHandler.php +++ b/application/hub/classes/handler/chunks/class_ChunkHandler.php @@ -74,7 +74,7 @@ class ChunkHandler extends BaseHubHandler implements HandleableChunks, Registera private $eopChunk = array(); /** - * Raw package data + * An instance of raw package data */ private $rawPackageData = ''; diff --git a/application/hub/classes/handler/package/.htaccess b/application/hub/classes/handler/package/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/handler/package/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/package/class_NetworkPackage.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php similarity index 89% rename from application/hub/classes/package/class_NetworkPackage.php rename to application/hub/classes/handler/package/class_NetworkPackageHandler.php index 23847d706..84aa1d584 100644 --- a/application/hub/classes/package/class_NetworkPackage.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -1,6 +1,6 @@ . */ -class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, Registerable, Visitable { +class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Receivable, Registerable, Visitable { /** * Package mask for compressing package data: * 0: Compressor extension @@ -94,7 +95,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R /** * Size of an array created by invoking - * explode(NetworkPackage::PACKAGE_MASK_SEPARATOR, $content). + * explode(NetworkPackageHandler::PACKAGE_MASK_SEPARATOR, $content). */ const PACKAGE_CONTENT_ARRAY_SIZE = 4; @@ -138,17 +139,6 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R const PACKAGE_CONTENT_HASH = 'hash'; const PACKAGE_CONTENT_PRIVATE_KEY_HASH = 'pkhash'; - /** - * Named array elements for package data - */ - 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'; - const PACKAGE_DATA_HASH = 'hash'; - const PACKAGE_DATA_PRIVATE_KEY_HASH = 'pkhash'; - /** * All package status */ @@ -331,9 +321,9 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R * @param $compressorInstance A Compressor instance for compressing the content * @return $packageInstance An instance of a Deliverable class */ - public static final function createNetworkPackage (Compressor $compressorInstance) { + public static final function createNetworkPackageHandler (Compressor $compressorInstance) { // Get new instance - $packageInstance = new NetworkPackage(); + $packageInstance = new NetworkPackageHandler(); // Now set the compressor instance $packageInstance->setCompressorInstance($compressorInstance); @@ -400,19 +390,16 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R /** * Determines private key hash from given session id * - * @param $decodedData Array with decoded data + * @param $packageInstance An instance of a DeliverablePackage class * @return $hash Private key's hash * @throws InvalidPrivateKeyHashException If the private key's hash is not valid */ - private function determineSenderPrivateKeyHash (array $decodedData) { - // Assert on required fields - assert(!empty($decodedData[self::PACKAGE_CONTENT_SENDER])); - + private function determineSenderPrivateKeyHash (DeliverablePackage $packageInstance) { // Get DHT instance $dhtInstance = DhtObjectFactory::createDhtInstance('node'); // Ask DHT for session id - $senderData = $dhtInstance->findNodeLocalBySessionId($decodedData[self::PACKAGE_CONTENT_SENDER]); + $senderData = $dhtInstance->findNodeLocalBySessionId($packageInstance->getContentSender()); // Debugging: //* DEBUG-DIE: */ die(sprintf('[%s:%d]: senderData=%s', __METHOD__, __LINE__, print_r($senderData, TRUE))); @@ -433,18 +420,14 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R return $senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH]; } // END - if - // Make sure the requested element is there - //* DEBUG-DIE */ die('decodedData=' . print_r($decodedData, TRUE)); - assert(isset($decodedData[self::PACKAGE_CONTENT_PRIVATE_KEY_HASH])); - // Don't accept empty keys - if (empty($decodedData[self::PACKAGE_CONTENT_PRIVATE_KEY_HASH])) { + if (empty($packageInstance->getContentPrivateKeyHash())) { // This needs fixing throw new InvalidPrivateKeyHashException(array($this, $senderData, 'empty hash in decodedData'), self::EXCEPTION_INVALID_PRIVATE_KEY_HASH); } // END - if // There is no DHT entry so, accept the hash from decoded data - return $decodedData[self::PACKAGE_CONTENT_PRIVATE_KEY_HASH]; + return $packageInstance->getContentPrivateKeyHash(); } /** @@ -478,12 +461,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R * Checks whether the checksum (sometimes called "hash") is the same * * @param $decodedContent Package raw content - * @param $decodedData Whole raw package data array + * @param $packageInstance An instance of a DeliverablePackage class * @return $isChecksumValid Whether the checksum is the same */ - private function isChecksumValid (array $decodedContent, array $decodedData) { + private function isChecksumValid (array $decodedContent, DeliverablePackage $packageInstance) { // Get checksum - $checksum = $this->getHashFromContentSessionId($decodedContent, $decodedData[self::PACKAGE_DATA_SENDER_ADDRESS]); + $checksum = $this->getHashFromContentSessionId($decodedContent, $packageInstance->getSenderAddress()); // Is it the same? $isChecksumValid = ($checksum == $decodedContent[self::PACKAGE_CONTENT_CHECKSUM]); @@ -495,12 +478,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R /** * Change the package with given status in given stack * - * @param $packageData Raw package data in an array + * @param $packageInstance An instance of a DeliverablePackage class in an array * @param $stackerName Name of the stacker * @param $newStatus New status to set * @return void */ - private function changePackageStatus (array $packageData, $stackerName, $newStatus) { + private function changePackageStatus (DeliverablePackage $packageInstance, $stackerName, $newStatus) { // Skip this for empty stacks if ($this->getStackInstance()->isStackEmpty($stackerName)) { // This avoids an exception after all packages has failed @@ -511,13 +494,13 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R $nextData = $this->getStackInstance()->popNamed($stackerName); // Compare both hashes - assert($nextData[self::PACKAGE_DATA_HASH] == $packageData[self::PACKAGE_DATA_HASH]); + assert($nextData->getPackageHash() == $packageInstance->getPackageHash()); // Temporary set the new status - $packageData[self::PACKAGE_DATA_STATUS] = $newStatus; + $packageInstance->setPackageStatus($newStatus); // And push it again - $this->getStackInstance()->pushNamed($stackerName, $packageData); + $this->getStackInstance()->pushNamed($stackerName, $packageInstance); } /** @@ -550,15 +533,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R /////////////////////////////////////////////////////////////////////////// /** - * Declares the given raw package data by discovering recipients + * Declares the given An instance of a DeliverablePackage class by discovering recipients * - * @param $packageData Raw package data in an array + * @param $packageData An instance of a DeliverablePackage class in an array * @return void */ - private function declareRawPackageData (array $packageData) { - // Make sure the required field is there - assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT])); - + private function declareRawPackageData (DeliverablePackage $packageInstance) { /* * We need to disover every recipient, just in case we have a * multi-recipient entry like 'upper' is. 'all' may be a not so good @@ -585,10 +565,10 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R $currentRecipient = $iteratorInstance->current(); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Setting recipient to ' . $currentRecipient . ',previous=' . $packageData[self::PACKAGE_DATA_RECIPIENT]); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Setting recipient to ' . $currentRecipient . ',previous=' . packageInstance->getRecipient()); // Set the recipient - $packageData[self::PACKAGE_DATA_RECIPIENT] = $currentRecipient; + $packageInstance->setRecipient($currentRecipient); // Push the declared package to the next stack. $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData); @@ -614,10 +594,10 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R * the package), register the socket with the connection (handler/helper?) * instance and finally push the raw data on our outgoing queue. * - * @param $packageData Raw package data in an array + * @param $packageData An instance of a DeliverablePackage class in an array * @return void */ - private function deliverRawPackageData (array $packageData) { + private function deliverRawPackageData (DeliverablePackage $packageInstance) { // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: packageData()=%d - CALLED!', count($packageData))); @@ -665,7 +645,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Registering socket ' . $socketInstance . ' ...'); // Then register it - $this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance, $packageData); + $this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance); } elseif (!$helperInstance->getStateInstance()->isPeerStateConnected()) { // Is not connected, then we cannot send self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Unexpected peer state ' . $helperInstance->getStateInstance()->__toString() . ' detected.'); @@ -693,10 +673,10 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R /** * Sends waiting packages * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return void */ - private function sendOutgoingRawPackageData (array $packageData) { + private function sendOutgoingRawPackageData (DeliverablePackage $packageInstance) { // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: packageData()=%d - CALLED!', count($packageData))); @@ -704,7 +684,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R $sentBytes = 0; // Get the right connection instance - $infoInstance = $this->getRegistryInstance()->getInfoInstanceFromPackageData($packageData); + $infoInstance = $this->getRegistryInstance()->getInfoInstanceFromPackageInstance($packageData); // Test helper instance assert($infoInstance instanceof ShareableInfo); @@ -734,7 +714,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R /** * Generates a secure hash for given raw package content and sender id * - * @param $content Raw package data + * @param $content An instance of a DeliverablePackage class * @param $senderId Sender id to generate a hash for * @return $hash Hash as hex-encoded string */ @@ -759,7 +739,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R self::PACKAGE_CONTENT_MESSAGE => $content, self::PACKAGE_CONTENT_PRIVATE_KEY_HASH => $this->getPrivateKeyHash(), ); - + // Hash content and sender id together, use scrypt //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: senderId=' . $senderId . ',content()=' . strlen($content)); $hash = FrameworkFeature::callFeature('hubcoin_reward', 'generateHash', array($senderId . ':' . $content . ':' . $this->determineSenderPrivateKeyHash($data))); @@ -850,16 +830,20 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R assert(!empty($content)); assert($this->getSessionId() != ''); + // Init package instance + $packageInstance = PackageDataFactory::createDeliverPackageInstance(); + + // Set all data + $packageInstance->setSenderAddress($this->getSessionId()); + $packageInstance->setSenderPort('0'); + $packageInstance->setRecipientType($helperInstance->getRecipientType()); + $packageInstance->setContent($packageContent); + $packageInstance->setStatus(self::PACKAGE_STATUS_NEW); + $packageInstance->setHash($this->generatePackageHash($content, $this->getSessionId())); + $packageInstance->setPrivateKeyHash($this->getPrivateKeyHash()); + // Now prepare the temporary array and push it on the 'undeclared' stack - $this->getStackInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array( - 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, - self::PACKAGE_DATA_HASH => $this->generatePackageHash($content, $this->getSessionId()), - self::PACKAGE_DATA_PRIVATE_KEY_HASH => $this->getPrivateKeyHash(), - )); + $this->getStackInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, $packageInstance); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); @@ -981,16 +965,13 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R return; } // END - if - // Get the package - $packageData = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECLARED); - - // Assert on it - assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT])); + // Get the package instance + $packageInstance = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECLARED); // Try to deliver the package try { // And try to send it - $this->deliverRawPackageData($packageData); + $this->deliverRawPackageData($packageInstance); // And remove it finally $this->getStackInstance()->popNamed(self::STACKER_NAME_DECLARED); @@ -999,7 +980,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Caught ' . $e->__toString() . ',message=' . $e->getMessage()); // Mark the package with status failed - $this->changePackageStatus($packageData, self::STACKER_NAME_DECLARED, self::PACKAGE_STATUS_FAILED); + $this->changePackageStatus($packageInstance, self::STACKER_NAME_DECLARED, self::PACKAGE_STATUS_FAILED); } // Debug message @@ -1023,11 +1004,11 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R } // END - if // Get the package - $packageData = $this->getStackInstance()->getNamed(self::STACKER_NAME_OUTGOING); + $packageInstance = $this->getStackInstance()->getNamed(self::STACKER_NAME_OUTGOING); try { // Now try to send it - $this->sendOutgoingRawPackageData($packageData); + $this->sendOutgoingRawPackageData($packageInstance); // And remove it finally $this->getStackInstance()->popNamed(self::STACKER_NAME_OUTGOING); @@ -1036,7 +1017,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Package was not delivered: ' . $e->getMessage()); // Mark package as failed - $this->changePackageStatus($packageData, self::STACKER_NAME_OUTGOING, self::PACKAGE_STATUS_FAILED); + $this->changePackageStatus($packageInstance, self::STACKER_NAME_OUTGOING, self::PACKAGE_STATUS_FAILED); } // Debug message @@ -1173,31 +1154,22 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Stacker size is ' . $this->getStackInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING) . ' entries.'); // "Pop" the next entry (the same array again) from the stack - $decodedData = $this->getStackInstance()->popNamed(self::STACKER_NAME_DECODED_INCOMING); - - // Make sure both array elements are there - assert( - (is_array($decodedData)) && - (isset($decodedData[HandleableRawData::PACKAGE_RAW_DATA])) && - (isset($decodedData[HandleableRawData::PACKAGE_ERROR_CODE])) - ); + $packageInstance = $this->getStackInstance()->popNamed(self::STACKER_NAME_DECODED_INCOMING); /* * Also make sure the error code is SOCKET_ERROR_UNHANDLED because we * only want to handle unhandled packages here. */ - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: errorCode=' . $decodedData[HandleableRawData::PACKAGE_ERROR_CODE] . '(' . StorableSocket::SOCKET_ERROR_UNHANDLED . ')'); - assert($decodedData[HandleableRawData::PACKAGE_ERROR_CODE] == StorableSocket::SOCKET_ERROR_UNHANDLED); - // Remove the last chunk SEPARATOR (because there is no need for it) - if (substr($decodedData[HandleableRawData::PACKAGE_RAW_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: errorCode=' . $packageInstance->getErrorCode() . '(' . StorableSocket::SOCKET_ERROR_UNHANDLED . ')'); + if (substr($packageInstance->getRawData(), -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) { // It is there and should be removed - $decodedData[HandleableRawData::PACKAGE_RAW_DATA] = substr($decodedData[HandleableRawData::PACKAGE_RAW_DATA], 0, -1); + $packageInstance->getRawData() = substr($packageInstance->getRawData(), 0, -1); } // END - if // This package is "handled" and can be pushed on the next stack - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Pushing ' . strlen($decodedData[HandleableRawData::PACKAGE_RAW_DATA]) . ' bytes to stack ' . self::STACKER_NAME_DECODED_HANDLED . ' ...'); - $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_HANDLED, $decodedData); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Pushing ' . strlen($packageInstance->getRawData()) . ' bytes to stack ' . self::STACKER_NAME_DECODED_HANDLED . ' ...'); + $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_HANDLED, $packageInstance); } /** @@ -1211,13 +1183,13 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R * Get the decoded data from the handler, this is an array with * 'raw_data' and 'error_code' as elements. */ - $decodedData = $handlerInstance->getNextRawData(); + $packageInstance = $handlerInstance->getNextRawData(); // Very noisy debug message: - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: decodedData[' . gettype($decodedData) . ']=' . print_r($decodedData, TRUE)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: decodedData[' . gettype($packageInstance) . ']=' . print_r($packageInstance, TRUE)); // And push it on our stack - $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_INCOMING, $decodedData); + $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_INCOMING, $packageInstance); } /** @@ -1329,7 +1301,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: ' . $visitorInstance->__toString() . ' has visited - CALLED!'); // Visit the package - $visitorInstance->visitNetworkPackage($this); + $visitorInstance->visitNetworkPackageHandler($this); // Then visit the assembler to handle multiple packages $this->getAssemblerInstance()->accept($visitorInstance); @@ -1361,12 +1333,12 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R */ public function removeFirstFailedPackage () { // Get the package again - $packageData = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECLARED); + $packageInstance = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECLARED); // Is the package status 'failed'? - if ($packageData[self::PACKAGE_DATA_STATUS] != self::PACKAGE_STATUS_FAILED) { + if ($packageInstance->getStatus() != self::PACKAGE_STATUS_FAILED) { // Not failed! - throw new UnexpectedPackageStatusException(array($this, $packageData, self::PACKAGE_STATUS_FAILED), self::EXCEPTION_UNEXPECTED_PACKAGE_STATUS); + throw new UnexpectedPackageStatusException(array($this, $packageInstance, self::PACKAGE_STATUS_FAILED), self::EXCEPTION_UNEXPECTED_PACKAGE_STATUS); } // END - if // Remove this entry @@ -1377,7 +1349,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R * "Decode" the package content into the same array when it was sent. * * @param $rawPackageContent The raw package content to be "decoded" - * @return $decodedData An array with 'sender', 'recipient', 'content' and 'status' elements + * @return $packageInstance An instance of a DeliverablePackage class */ public function decodeRawContent ($rawPackageContent) { // Use the separator '#' to "decode" it @@ -1389,38 +1361,37 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R // Assert on count (should be always 3) assert(count($decodedArray) == self::DECODED_DATA_ARRAY_SIZE); - /* - * Create 'decodedData' array with all assoziative array elements. - */ - $decodedData = array( - self::PACKAGE_DATA_SENDER_ADDRESS => $decodedArray[self::INDEX_PACKAGE_SENDER_ADDRESS], - self::PACKAGE_DATA_SENDER_PORT => $decodedArray[self::INDEX_PACKAGE_SENDER_PORT], - 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, - self::PACKAGE_DATA_HASH => $decodedArray[self::INDEX_PACKAGE_HASH], - self::PACKAGE_DATA_PRIVATE_KEY_HASH => $decodedArray[self::INDEX_PACKAGE_PRIVATE_KEY_HASH] - ); + // Create package instance + $packageInstance = PackageDataFactory::createPackageDataInstance(); + + // Set all data + $packageInstance->setSenderAddress($decodedArray[self::INDEX_PACKAGE_SENDER_ADDRESS]); + $packageInstance->setSenderPort($decodedArray[self::INDEX_PACKAGE_SENDER_PORT]); + $packageInstance->setRecipient($decodedArray[self::INDEX_PACKAGE_RECIPIENT]); + $packageInstance->setContent($decodedArray[self::INDEX_PACKAGE_CONTENT]); + $packageInstance->setStatus(self::PACKAGE_STATUS_DECODED); + $packageInstance->setHash($decodedArray[self::INDEX_PACKAGE_HASH]); + $packageInstance->setPrivateKeyHash($decodedArray[self::INDEX_PACKAGE_PRIVATE_KEY_HASH]); // And return it - return $decodedData; + return $packageInstance; } /** * Handles decoded data for this node by "decoding" the 'content' part of * it. Again this method uses explode() for the "decoding" process. * - * @param $decodedData An array with decoded raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @return void * @throws InvalidDataChecksumException If the checksum doesn't match */ - public function handleRawData (array $decodedData) { + public function handleRawData (DeliverablePackage $packageInstance) { /* * "Decode" the package's content by a simple explode() call, for * details of the array elements, see comments for constant * PACKAGE_MASK. */ - $decodedContent = explode(self::PACKAGE_MASK_SEPARATOR, $decodedData[self::PACKAGE_DATA_CONTENT]); + $decodedContent = explode(self::PACKAGE_MASK_SEPARATOR, $packageInstance->getContent()); // Assert on array count for a very basic validation assert(count($decodedContent) == self::PACKAGE_CONTENT_ARRAY_SIZE); @@ -1439,17 +1410,17 @@ 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_ADDRESS], + self::PACKAGE_CONTENT_SENDER => $packageInstance->getSenderAddress(), // Hash from decoded raw data - self::PACKAGE_CONTENT_HASH => $decodedData[self::PACKAGE_DATA_HASH], + self::PACKAGE_CONTENT_HASH => $packageInstance->getHash(), // Hash of private key - self::PACKAGE_CONTENT_PRIVATE_KEY_HASH => $decodedData[self::PACKAGE_DATA_PRIVATE_KEY_HASH] + self::PACKAGE_CONTENT_PRIVATE_KEY_HASH => $packageInstance->getPrivateKeyHash() ); // Is the checksum valid? - if (!$this->isChecksumValid($decodedContent, $decodedData)) { + if (!$this->isChecksumValid($decodedContent, $packageInstance)) { // Is not the same, so throw an exception here - throw new InvalidDataChecksumException(array($this, $decodedContent, $decodedData), self::EXCEPTION_INVALID_DATA_CHECKSUM); + throw new InvalidDataChecksumException(array($this, $decodedContent, $packageInstance), self::EXCEPTION_INVALID_DATA_CHECKSUM); } // END - if /* diff --git a/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php b/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php index da8c00133..918a40b01 100644 --- a/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php +++ b/application/hub/classes/handler/protocol/ipv4/class_BaseIpV4ProtocolHandler.php @@ -4,10 +4,11 @@ namespace Org\Shipsimu\Hub\Handler\Protocol\Network\IpV4; // Import application-specific stuff use Org\Shipsimu\Hub\Factory\Network\Locator\UniversalNodeLocatorFactory; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Handler\Protocol\BaseProtocolHandler; use Org\Shipsimu\Hub\Locator\Node\LocateableNode; use Org\Shipsimu\Hub\Locator\Node\Tools\NodeLocatorUtils; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; /** * A general handler for IPv4 protocols such as TCP, UDP and others. @@ -76,26 +77,23 @@ abstract class BaseIpV4ProtocolHandler extends BaseProtocolHandler { * @param $packageData Valid raw package data * @return $isValid Whether the UNL can be validated */ - public function isValidUniversalNodeLocatorByPackageData (array $packageData) { + public function isValidUniversalNodeLocatorByPackageData (DeliverablePackage $packageInstance) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('IPV4-PROTOCOL-HANDLER: CALLED!'); - // Is 'recipient' there? - assert(isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); - // Is the correct handler choosen? - assert(substr($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], 0, strlen($this->getHandlerName())) != $this->getHandlerName()); + assert(substr($packageInstance->getRecipient(), 0, strlen($this->getHandlerName())) != $this->getHandlerName()); // Default is from generic validation - $isValid = NodeLocatorUtils::isValidUniversalNodeLocator($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + $isValid = NodeLocatorUtils::isValidUniversalNodeLocator($packageInstance->getRecipient()); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('IPV4-PROTOCOL-HANDLER: PACKAGE_DATA_RECIPIENT=' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ',isValid[' . gettype($isValid) . ']=' . intval($isValid)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('IPV4-PROTOCOL-HANDLER: packageInstance.recipient=' . $packageInstance->getRecipient() . ',isValid[' . gettype($isValid) . ']=' . intval($isValid)); // If this doesn't fail, continue validating the IP:port combination if ($isValid === TRUE) { // ... and validate IP:port, first "parse" the UNL - $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipient()); // Set whole UNL instance // @TODO don't do this in a isValidFoo() method diff --git a/application/hub/classes/handler/raw_data/class_BaseRawDataHandler.php b/application/hub/classes/handler/raw_data/class_BaseRawDataHandler.php index bd95799ef..0ac476c8f 100644 --- a/application/hub/classes/handler/raw_data/class_BaseRawDataHandler.php +++ b/application/hub/classes/handler/raw_data/class_BaseRawDataHandler.php @@ -7,7 +7,8 @@ use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory; use Org\Shipsimu\Hub\Generic\HubInterface; use Org\Shipsimu\Hub\Handler\BaseHubHandler; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\ObjectFactory; @@ -139,11 +140,11 @@ abstract class BaseRawDataHandler extends BaseHubHandler implements HandleableRa * Checks whether the 'recipient' field matches our own an universal node * locator. * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $matches Whether it matches * @todo This method will be moved to a better place */ - protected function ifRecipientMatchesOwnUniversalNodeLocator (array $packageData) { + protected function ifRecipientMatchesOwnUniversalNodeLocator (DeliverablePackage $packageInstance) { // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-DATA-HANDLER: packageData()=%d - CALLED!', strlen($packageData))); @@ -151,7 +152,7 @@ abstract class BaseRawDataHandler extends BaseHubHandler implements HandleableRa $ownAddress = NodeObjectFactory::createNodeInstance()->determineUniversalNodeLocator(); // Does it match? - $matches = ($ownAddress === $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + $matches = ($ownAddress === $packageInstance->getRecipient()); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-DATA-HANDLER: matches=%d - EXIT!', intval($matches))); diff --git a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php index a8ad13c48..5d4caf469 100644 --- a/application/hub/classes/helper/connection/class_BaseConnectionHelper.php +++ b/application/hub/classes/helper/connection/class_BaseConnectionHelper.php @@ -7,8 +7,9 @@ use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Fragmenter\FragmenterFactory; use Org\Shipsimu\Hub\Factory\Network\NetworkPackageFactory; use Org\Shipsimu\Hub\Factory\State\Peer\PeerStateFactory; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Helper\BaseHubSystemHelper; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\ObjectFactory; @@ -89,10 +90,10 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit $this->setOutputStreamInstance($streamInstance); // Get package instance from factory - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // ... and set it here - $this->setPackageInstance($packageInstance); + $this->setPackageHandlerInstance($packageInstance); // Register this connection helper GenericRegistry::getRegistry()->addInstance('connection', $this); @@ -175,10 +176,10 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit * 1) Request a chunk from set fragmenter instance * 2) Finally return the chunk (array) to the caller * - * @param $packageData Raw package data array + * @param $packageData An instance of a DeliverablePackage class array * @return $chunkData Raw data chunk */ - private function getRawDataFromPackageArray (array $packageData) { + private function getRawDataFromPackageArray (DeliverablePackage $packageInstance) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: currentFinalHash=' . $this->currentFinalHash); @@ -235,12 +236,12 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit } /** - * Sends raw package data to the recipient + * Sends An instance of a DeliverablePackage class to the recipient * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return void */ - public function sendRawPackageData (array $packageData) { + public function sendRawPackageData (DeliverablePackage $packageInstance) { // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: packageData()=%d - CALLED!', count($packageData))); @@ -316,16 +317,16 @@ abstract class BaseConnectionHelper extends BaseHubSystemHelper implements Visit // Init array $encodedDataArray = array( - NetworkPackage::RAW_INDEX_FINAL_HASH => $this->currentFinalHash, - NetworkPackage::RAW_INDEX_ENCODED_DATA => $encodedData, - NetworkPackage::RAW_INDEX_SENT_BYTES => 0, - NetworkPackage::RAW_INDEX_SOCKET_INSTANCE => $this->getSocketInstance(), - NetworkPackage::RAW_INDEX_BUFFER_SIZE => $bufferSize, - NetworkPackage::RAW_INDEX_DIFF => ($bufferSize - strlen($encodedData)), + NetworkPackageHandler::RAW_INDEX_FINAL_HASH => $this->currentFinalHash, + NetworkPackageHandler::RAW_INDEX_ENCODED_DATA => $encodedData, + NetworkPackageHandler::RAW_INDEX_SENT_BYTES => 0, + NetworkPackageHandler::RAW_INDEX_SOCKET_INSTANCE => $this->getSocketInstance(), + NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE => $bufferSize, + NetworkPackageHandler::RAW_INDEX_DIFF => ($bufferSize - strlen($encodedData)), ); // Push raw data to the package's outgoing stack - $this->getPackageInstance()->getStackInstance()->pushNamed(NetworkPackage::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray); + $this->getPackageHandlerInstance()->getStackInstance()->pushNamed(NetworkPackageHandler::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: EXIT!'); diff --git a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php index abb03dbaf..334dace84 100644 --- a/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/tcp/class_TcpConnectionHelper.php @@ -6,10 +6,11 @@ namespace Org\Shipsimu\Hub\Helper\Connection\Network\IpV4\Tcp; use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Handler\Protocol\ProtocolHandlerFactory; use Org\Shipsimu\Hub\Factory\Socket\SocketFactory; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper; use Org\Shipsimu\Hub\Helper\Connection\Network\IpV4\BaseIpV4ConnectionHelper; use Org\Shipsimu\Hub\Locator\Node\LocateableNode; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Tools\HubTools; // Import framework stuff @@ -55,15 +56,15 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection * recipient in package data. After you called this method you still need to * connect to the other node. * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $socketInstance An instance of a StorableSocket class */ - public static function createConnectionFromPackageData (array $packageData) { + public static function createConnectionFromPackageInstance (DeliverablePackage $packageInstance) { // Create an instance $helperInstance = new TcpConnectionHelper(); // Construct container class, this won't be reached if an exception is thrown - $socketInstance = SocketFactory::createTcpOutgoingSocketFromPackageData($packageData); + $socketInstance = SocketFactory::createTcpOutgoingSocketFromPackageInstance($packageData); // Set the resource $helperInstance->setSocketInstance($socketInstance); @@ -75,7 +76,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection // Try to solve the recipient try { // Get protocol handler back from package data - $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($packageData); + $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageInstance($packageData); // Get UNL instance $unlInstance = $handlerInstance->getUniversalNodeLocatorInstance(); @@ -84,23 +85,23 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection $helperInstance->setHandlerInstance($handlerInstance); } catch (NoValidHostnameException $e) { // Debug message - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Failed to resolve ' . $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . ':' . $e->getMessage()); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Failed to resolve ' . $packageInstance->getRecipient() . ':' . $e->getMessage()); // Is the recipient equal as configured UNL? - if (substr($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], 0, strlen($helperInstance->getConfigInstance()->getConfigEntry('external_address'))) == $helperInstance->getConfigInstance()->getConfigEntry('external_address')) { + if (substr($packageInstance->getRecipient(), 0, strlen($helperInstance->getConfigInstance()->getConfigEntry('external_address'))) == $helperInstance->getConfigInstance()->getConfigEntry('external_address')) { // This may connect to shipsimu.org and requests 'ip.php' which will return our external IP address $unlAddress = HubTools::determineExternalAddress(); // Do we have ip:port match? // @TODO Rewrite this test for UNLs - if (strpos($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], ':') === FALSE) { + if (strpos($packageInstance->getRecipient(), ':') === FALSE) { // No ip:port! - $helperInstance->debugInstance(sprintf('[%s:%d]: recipient=%s does not contain ":". Please fix this.', __METHOD__, __LINE__, $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); + $helperInstance->debugInstance(sprintf('[%s:%d]: recipient=%s does not contain ":". Please fix this.', __METHOD__, __LINE__, $packageInstance->getRecipient())); } // END - if // "explode" the ip:port, so index 1 will be the port number // @TODO Rewrite this test for UNLs - $recipientArray = explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + $recipientArray = explode(':', $packageInstance->getRecipient()); // Add the port $unlPort = $recipientArray[LocateableNode::UNL_PART_PORT]; @@ -110,7 +111,7 @@ class TcpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection //$unlInstance =; } else { // It doesn't match, we need to take care of this later - $helperInstance->debugInstance(sprintf('[%s:%d]: recipient=%s!=%s', __METHOD__, __LINE__, $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT], $helperInstance->getConfigInstance()->getConfigEntry('external_address'))); + $helperInstance->debugInstance(sprintf('[%s:%d]: recipient=%s!=%s', __METHOD__, __LINE__, $packageInstance->getRecipient(), $helperInstance->getConfigInstance()->getConfigEntry('external_address'))); } } diff --git a/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php b/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php index c729b3303..c541b4744 100644 --- a/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php +++ b/application/hub/classes/helper/connection/ipv4/udp/class_UdpConnectionHelper.php @@ -6,6 +6,7 @@ namespace Org\Shipsimu\Hub\Helper\Connection\Network\IpV4\Udp; use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper; use Org\Shipsimu\Hub\Helper\Connection\Network\IpV4\BaseIpV4ConnectionHelper; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; /** * A UDP connection helper class @@ -49,10 +50,10 @@ class UdpConnectionHelper extends BaseIpV4ConnectionHelper implements Connection * recipient in package data. After you called this method you still need to * connect to the other node. * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $socketInstance An instance of a StorableSocket class */ - public static function createConnectionFromPackageData (array $packageData) { + public static function createConnectionFromPackageInstance (DeliverablePackage $packageInstance) { $this->debugBackTrace(': Unfinished method, packageData[]=' . count($packageData)); } diff --git a/application/hub/classes/helper/dht/class_DhtBootstrapHelper.php b/application/hub/classes/helper/dht/class_DhtBootstrapHelper.php index df727830b..17f98319d 100644 --- a/application/hub/classes/helper/dht/class_DhtBootstrapHelper.php +++ b/application/hub/classes/helper/dht/class_DhtBootstrapHelper.php @@ -6,9 +6,9 @@ namespace Org\Shipsimu\Hub\Helper\Dht\Bootstrap; use Org\Shipsimu\Hub\Dht\Distributable; use Org\Shipsimu\Hub\Factory\Network\NetworkPackageFactory; use Org\Shipsimu\Hub\Generic\BaseHubSystem; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Helper\BaseHubSystemHelper; use Org\Shipsimu\Hub\Helper\Dht\HelpableDht; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\Template\XmlTemplateEngineFactory; @@ -47,7 +47,7 @@ class DhtBootstrapHelper extends BaseHubSystemHelper implements HelpableDht { parent::__construct(__CLASS__); // Set recipient type - $this->setRecipientType(NetworkPackage::NETWORK_TARGET_DHT); + $this->setRecipientType(NetworkPackageHandler::NETWORK_TARGET_DHT); // Set package tags $this->setPackageTags(array('dht_bootstrap')); @@ -109,7 +109,7 @@ class DhtBootstrapHelper extends BaseHubSystemHelper implements HelpableDht { $this->getTemplateInstance()->compileTemplate(); // Get a singleton network package instance - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // Next, feed the content in. The network package class is a pipe-through class. //* DEBUG: */ die(__METHOD__ . ':dhtInstance=' . print_r($dhtInstance, TRUE)); diff --git a/application/hub/classes/helper/dht/class_DhtPublishEntryHelper.php b/application/hub/classes/helper/dht/class_DhtPublishEntryHelper.php index b08f166ef..0af95d918 100644 --- a/application/hub/classes/helper/dht/class_DhtPublishEntryHelper.php +++ b/application/hub/classes/helper/dht/class_DhtPublishEntryHelper.php @@ -6,9 +6,9 @@ namespace Org\Shipsimu\Hub\Helper\Dht\Publish; use Org\Shipsimu\Hub\Dht\Distributable; use Org\Shipsimu\Hub\Factory\Network\NetworkPackageFactory; use Org\Shipsimu\Hub\Generic\BaseHubSystem; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Helper\BaseHubSystemHelper; use Org\Shipsimu\Hub\Helper\Dht\HelpableDht; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\Template\XmlTemplateEngineFactory; @@ -47,7 +47,7 @@ class DhtPublishEntryHelper extends BaseHubSystemHelper implements HelpableDht { parent::__construct(__CLASS__); // Set recipient type - $this->setRecipientType(NetworkPackage::NETWORK_TARGET_DHT); + $this->setRecipientType(NetworkPackageHandler::NETWORK_TARGET_DHT); // Set package tags $this->setPackageTags(array('dht_publish_entry')); @@ -102,7 +102,7 @@ class DhtPublishEntryHelper extends BaseHubSystemHelper implements HelpableDht { $this->getTemplateInstance()->compileTemplate(); // Get a singleton network package instance - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // Next, feed the content in. The network package class is a pipe-through class. $packageInstance->enqueueRawDataFromTemplate($this); diff --git a/application/hub/classes/helper/node/announcement/class_NodeAnnouncementHelper.php b/application/hub/classes/helper/node/announcement/class_NodeAnnouncementHelper.php index 41f5f12e2..b3eca5b58 100644 --- a/application/hub/classes/helper/node/announcement/class_NodeAnnouncementHelper.php +++ b/application/hub/classes/helper/node/announcement/class_NodeAnnouncementHelper.php @@ -4,9 +4,9 @@ namespace Org\Shipsimu\Hub\Node\Helper\Announcement; // Import application-specific stuff use Org\Shipsimu\Hub\Factory\Network\NetworkPackageFactory; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Helper\Node\BaseNodeHelper; use Org\Shipsimu\Hub\Helper\Node\HelpableNode; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; use Org\Shipsimu\Hub\Node\Node; // Import framework stuff @@ -46,7 +46,7 @@ class NodeAnnouncementHelper extends BaseNodeHelper implements HelpableNode { parent::__construct(__CLASS__); // Set recipient type - $this->setRecipientType(NetworkPackage::NETWORK_TARGET_UPPER); + $this->setRecipientType(NetworkPackageHandler::NETWORK_TARGET_UPPER); // Set package tags $this->setPackageTags(array('announcement')); @@ -106,7 +106,7 @@ class NodeAnnouncementHelper extends BaseNodeHelper implements HelpableNode { $this->getTemplateInstance()->compileTemplate(); // Get a singleton network package instance - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // Next, feed the content in. The network package class is a pipe-through class. $packageInstance->enqueueRawDataFromTemplate($this); diff --git a/application/hub/classes/helper/node/answer/announcement/class_NodeAnnouncementMessageAnswerHelper.php b/application/hub/classes/helper/node/answer/announcement/class_NodeAnnouncementMessageAnswerHelper.php index 174a37c73..fb8f34ef1 100644 --- a/application/hub/classes/helper/node/answer/announcement/class_NodeAnnouncementMessageAnswerHelper.php +++ b/application/hub/classes/helper/node/answer/announcement/class_NodeAnnouncementMessageAnswerHelper.php @@ -105,7 +105,7 @@ class NodeAnnouncementMessageAnswerHelper extends BaseHubAnswerHelper implements $this->getTemplateInstance()->compileTemplate(); // Get a singleton network package instance - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // Next, feed the content in. The network package class is a pipe-through class. $packageInstance->enqueueRawDataFromTemplate($this); diff --git a/application/hub/classes/helper/node/answer/dht/class_NodeDhtBootstrapMessageAnswerHelper.php b/application/hub/classes/helper/node/answer/dht/class_NodeDhtBootstrapMessageAnswerHelper.php index 1f2549170..a9022252f 100644 --- a/application/hub/classes/helper/node/answer/dht/class_NodeDhtBootstrapMessageAnswerHelper.php +++ b/application/hub/classes/helper/node/answer/dht/class_NodeDhtBootstrapMessageAnswerHelper.php @@ -105,7 +105,7 @@ class NodeDhtBootstrapMessageAnswerHelper extends BaseHubAnswerHelper implements $this->getTemplateInstance()->compileTemplate(); // Get a singleton network package instance - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // Next, feed the content in. The network package class is a pipe-through class. $packageInstance->enqueueRawDataFromTemplate($this); diff --git a/application/hub/classes/helper/node/answer/requests/class_NodeRequestNodeListMessageAnswerHelper.php b/application/hub/classes/helper/node/answer/requests/class_NodeRequestNodeListMessageAnswerHelper.php index 0b1b7d678..e0bc1890b 100644 --- a/application/hub/classes/helper/node/answer/requests/class_NodeRequestNodeListMessageAnswerHelper.php +++ b/application/hub/classes/helper/node/answer/requests/class_NodeRequestNodeListMessageAnswerHelper.php @@ -111,7 +111,7 @@ class NodeRequestNodeListMessageAnswerHelper extends BaseHubAnswerHelper impleme $this->getTemplateInstance()->compileTemplate(); // Get a singleton network package instance - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // Next, feed the content in. The network package class is a pipe-through class. $packageInstance->enqueueRawDataFromTemplate($this); diff --git a/application/hub/classes/helper/node/connection/class_NodeSelfConnectHelper.php b/application/hub/classes/helper/node/connection/class_NodeSelfConnectHelper.php index e936985e0..6e5e85212 100644 --- a/application/hub/classes/helper/node/connection/class_NodeSelfConnectHelper.php +++ b/application/hub/classes/helper/node/connection/class_NodeSelfConnectHelper.php @@ -4,9 +4,9 @@ namespace Org\Shipsimu\Hub\Node\Helper\SelfConnect; // Import application-specific stuff use Org\Shipsimu\Hub\Factory\Network\NetworkPackageFactory; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Helper\Node\BaseNodeHelper; use Org\Shipsimu\Hub\Helper\Node\HelpableNode; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; use Org\Shipsimu\Hub\Node\Node; use Org\Shipsimu\Hub\Tag\Tagable; @@ -47,7 +47,7 @@ class NodeSelfConnectHelper extends BaseNodeHelper implements HelpableNode { parent::__construct(__CLASS__); // Set recipient type - $this->setRecipientType(NetworkPackage::NETWORK_TARGET_SELF); + $this->setRecipientType(NetworkPackageHandler::NETWORK_TARGET_SELF); // Set package tags $this->setPackageTags(array(Tagable::TAG_SELF_CONNECT)); @@ -103,7 +103,7 @@ class NodeSelfConnectHelper extends BaseNodeHelper implements HelpableNode { $this->getTemplateInstance()->compileTemplate(); // Get a singleton network package instance - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // Next, feed the content in. The network package class is a pipe-through class. $packageInstance->enqueueRawDataFromTemplate($this); diff --git a/application/hub/classes/helper/node/requests/class_NodeRequestNodeListHelper.php b/application/hub/classes/helper/node/requests/class_NodeRequestNodeListHelper.php index a69885602..77e13a54e 100644 --- a/application/hub/classes/helper/node/requests/class_NodeRequestNodeListHelper.php +++ b/application/hub/classes/helper/node/requests/class_NodeRequestNodeListHelper.php @@ -4,9 +4,9 @@ namespace Org\Shipsimu\Hub\Node\Helper\Request\NodeList; // Import application-specific stuff use Org\Shipsimu\Hub\Factory\Network\NetworkPackageFactory; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Helper\Node\BaseNodeHelper; use Org\Shipsimu\Hub\Helper\Node\HelpableNode; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; use Org\Shipsimu\Hub\Node\Node; // Import framework stuff @@ -46,7 +46,7 @@ class NodeRequestNodeListHelper extends BaseNodeHelper implements HelpableNode { parent::__construct(__CLASS__); // Set recipient type - $this->setRecipientType(NetworkPackage::NETWORK_TARGET_UPPER); + $this->setRecipientType(NetworkPackageHandler::NETWORK_TARGET_UPPER); // Set package tags $this->setPackageTags(array('request_node_list')); @@ -102,7 +102,7 @@ class NodeRequestNodeListHelper extends BaseNodeHelper implements HelpableNode { $this->getTemplateInstance()->compileTemplate(); // Get a singleton network package instance - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // Next, feed the content in. The network package class is a pipe-through class. $packageInstance->enqueueRawDataFromTemplate($this); diff --git a/application/hub/classes/listener/class_BaseListener.php b/application/hub/classes/listener/class_BaseListener.php index e907d051e..82380b976 100644 --- a/application/hub/classes/listener/class_BaseListener.php +++ b/application/hub/classes/listener/class_BaseListener.php @@ -9,8 +9,9 @@ use Org\Shipsimu\Hub\Factory\Network\Locator\UniversalNodeLocatorFactory; use Org\Shipsimu\Hub\Factory\Node\NodeObjectFactory; use Org\Shipsimu\Hub\Factory\Socket\SocketFactory; use Org\Shipsimu\Hub\Generic\BaseHubSystem; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Information\ShareableInfo; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Pool\Peer\PoolablePeer; use Org\Shipsimu\Hub\Pool\Poolable; @@ -401,16 +402,18 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { // Debugging: //* DEBUG-DIE: */ $infoInstance->debugInstance(); - // Create a faked package data array - $packageData = array( - 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 - ); + // Create a faked package data instance + $packageInstance = DeliverPackageFactory::createDeliverPackageInstance(); + + // Set all required data + $packageInstance->setSenderAddres($peerAddress . $peerSuffix); + $packageInstance->setSenderPort($peerPort); + $packageInstance->setRecipient($nodeInstance->getSessionId()); + $packageInstance->setStatus(NetworkPackageHandler::PACKAGE_STATUS_FAKED); + die(__METHOD__.': UNFINISHED!'); // Register the socket with the registry and with the faked array - $this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance, $packageData); + $this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Calling this.handlerInstance.processRawDataFromSocketInstance(resource=%s) ...', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource())); @@ -425,10 +428,10 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { /** * Checks whether the listener would accept the given package data array * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $accepts Whether this listener does accept */ - public function ifListenerAcceptsPackageData (array $packageData) { + public function ifListenerAcceptsPackageData (DeliverablePackage $packageInstance) { // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: packageData()=%d - CALLED!', strtoupper($this->getProtocolName()), count($packageData))); @@ -436,7 +439,7 @@ abstract class BaseListener extends BaseHubSystem implements Visitable { $socketProtocol = $this->getSocketInstance()->getSocketProtocol(); // Get UNL instance - $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]); + $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($packageInstance->getRecipient()); // Get protocol from it $unlProtocol = $unlInstance->getUnlProtocol(); diff --git a/application/hub/classes/listener/class_BaseListenerDecorator.php b/application/hub/classes/listener/class_BaseListenerDecorator.php index 293eba4eb..3aa9a45c0 100644 --- a/application/hub/classes/listener/class_BaseListenerDecorator.php +++ b/application/hub/classes/listener/class_BaseListenerDecorator.php @@ -203,7 +203,7 @@ abstract class BaseListenerDecorator extends BaseDecorator implements Visitable } // END - if // Get receiver (network package) instance - $receiverInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $receiverInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); /* * There is some decoded data waiting. The receiver instance is an diff --git a/application/hub/classes/listener/socket/class_SocketFileListener.php b/application/hub/classes/listener/socket/class_SocketFileListener.php index 2dd172ef7..a72c6a7b5 100644 --- a/application/hub/classes/listener/socket/class_SocketFileListener.php +++ b/application/hub/classes/listener/socket/class_SocketFileListener.php @@ -8,6 +8,7 @@ use Org\Shipsimu\Hub\Factory\Socket\SocketFactory; use Org\Shipsimu\Hub\Factory\Tag\Package\PackageTagsFactory; use Org\Shipsimu\Hub\Listener\BaseListener; use Org\Shipsimu\Hub\Listener\Listenable; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; @@ -119,10 +120,10 @@ class SocketFileListener extends BaseListener implements Listenable { /** * Checks whether the listener would accept the given package data array * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $accepts Whether this listener does accept */ - public function ifListenerAcceptsPackageData (array $packageData) { + public function ifListenerAcceptsPackageData (DeliverablePackage $packageInstance) { // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FILE-LISTENER: packageData()=%d - CALLED!', count($packageData))); diff --git a/application/hub/classes/listener/socket/decorator/class_SocketFileListenerDecorator.php b/application/hub/classes/listener/socket/decorator/class_SocketFileListenerDecorator.php index a78e03e9c..6da509810 100644 --- a/application/hub/classes/listener/socket/decorator/class_SocketFileListenerDecorator.php +++ b/application/hub/classes/listener/socket/decorator/class_SocketFileListenerDecorator.php @@ -7,6 +7,7 @@ use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Tag\Package\PackageTagsFactory; use Org\Shipsimu\Hub\Listener\BaseListenerDecorator; use Org\Shipsimu\Hub\Listener\Listenable; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff @@ -92,10 +93,10 @@ class SocketFileListenerDecorator extends BaseListenerDecorator implements Liste /** * Checks whether the listener would accept the given package data array * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $accepts Whether this listener does accept */ - public function ifListenerAcceptsPackageData (array $packageData) { + public function ifListenerAcceptsPackageData (DeliverablePackage $packageInstance) { // Debug message /* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Calling this->getListenerInstance()->ifListenerAcceptsPackageData() ...'); diff --git a/application/hub/classes/listener/tcp/decorators/class_ClientTcpListenerDecorator.php b/application/hub/classes/listener/tcp/decorators/class_ClientTcpListenerDecorator.php index da4b3a984..a1b3ad23b 100644 --- a/application/hub/classes/listener/tcp/decorators/class_ClientTcpListenerDecorator.php +++ b/application/hub/classes/listener/tcp/decorators/class_ClientTcpListenerDecorator.php @@ -7,6 +7,7 @@ use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Tag\Package\PackageTagsFactory; use Org\Shipsimu\Hub\Listener\BaseListenerDecorator; use Org\Shipsimu\Hub\Listener\Listenable; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff @@ -92,10 +93,10 @@ class ClientTcpListenerDecorator extends BaseListenerDecorator implements Listen /** * Checks whether the listener would accept the given package data array * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $accepts Whether this listener does accept */ - public function ifListenerAcceptsPackageData (array $packageData) { + public function ifListenerAcceptsPackageData (DeliverablePackage $packageInstance) { // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CLIENT-TCP-LISTENER: packageData()=%d - CALLED!', count($packageData))); diff --git a/application/hub/classes/listener/tcp/decorators/class_HubTcpListenerDecorator.php b/application/hub/classes/listener/tcp/decorators/class_HubTcpListenerDecorator.php index 5b78a4e93..7f83f22d1 100644 --- a/application/hub/classes/listener/tcp/decorators/class_HubTcpListenerDecorator.php +++ b/application/hub/classes/listener/tcp/decorators/class_HubTcpListenerDecorator.php @@ -7,6 +7,7 @@ use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Tag\Package\PackageTagsFactory; use Org\Shipsimu\Hub\Listener\BaseListenerDecorator; use Org\Shipsimu\Hub\Listener\Listenable; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff @@ -92,10 +93,10 @@ class HubTcpListenerDecorator extends BaseListenerDecorator implements Listenabl /** * Checks whether the listener would accept the given package data array * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $accepts Whether this listener does accept */ - public function ifListenerAcceptsPackageData (array $packageData) { + public function ifListenerAcceptsPackageData (DeliverablePackage $packageInstance) { // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TCP-LISTENER: packageData()=%d - CALLED!', count($packageData))); diff --git a/application/hub/classes/listener/udp/class_UdpListener.php b/application/hub/classes/listener/udp/class_UdpListener.php index 092612b8c..08f55d407 100644 --- a/application/hub/classes/listener/udp/class_UdpListener.php +++ b/application/hub/classes/listener/udp/class_UdpListener.php @@ -7,6 +7,7 @@ use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Socket\SocketFactory; use Org\Shipsimu\Hub\Listener\BaseListener; use Org\Shipsimu\Hub\Listener\Listenable; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\ObjectFactory; @@ -118,11 +119,11 @@ class UdpListener extends BaseListener implements Listenable { /** * Checks whether the listener would accept the given package data array * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $accepts Whether this listener does accept * @throws UnsupportedOperationException If this method is called */ - public function ifListenerAcceptsPackageData (array $packageData) { + public function ifListenerAcceptsPackageData (DeliverablePackage $packageInstance) { throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } diff --git a/application/hub/classes/listener/udp/decorators/class_ClientUdpListenerDecorator.php b/application/hub/classes/listener/udp/decorators/class_ClientUdpListenerDecorator.php index 0699cb224..209c538db 100644 --- a/application/hub/classes/listener/udp/decorators/class_ClientUdpListenerDecorator.php +++ b/application/hub/classes/listener/udp/decorators/class_ClientUdpListenerDecorator.php @@ -7,6 +7,7 @@ use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Tag\Package\PackageTagsFactory; use Org\Shipsimu\Hub\Listener\BaseListenerDecorator; use Org\Shipsimu\Hub\Listener\Listenable; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff @@ -92,10 +93,10 @@ class ClientUdpListenerDecorator extends BaseListenerDecorator implements Listen /** * Checks whether the listener would accept the given package data array * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $accepts Whether this listener does accept */ - function ifListenerAcceptsPackageData (array $packageData) { + function ifListenerAcceptsPackageData (DeliverablePackage $packageInstance) { // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CLIENT-UDP-LISTENER: packageData()=%d - CALLED!', count($packageData))); diff --git a/application/hub/classes/listener/udp/decorators/class_HubUdpListenerDecorator.php b/application/hub/classes/listener/udp/decorators/class_HubUdpListenerDecorator.php index a21291965..9baa90d0c 100644 --- a/application/hub/classes/listener/udp/decorators/class_HubUdpListenerDecorator.php +++ b/application/hub/classes/listener/udp/decorators/class_HubUdpListenerDecorator.php @@ -7,6 +7,7 @@ use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Tag\Package\PackageTagsFactory; use Org\Shipsimu\Hub\Listener\BaseListenerDecorator; use Org\Shipsimu\Hub\Listener\Listenable; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff @@ -92,10 +93,10 @@ class HubUdpListenerDecorator extends BaseListenerDecorator implements Listenabl /** * Checks whether the listener would accept the given package data array * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $accepts Whether this listener does accept */ - public function ifListenerAcceptsPackageData (array $packageData) { + public function ifListenerAcceptsPackageData (DeliverablePackage $packageInstance) { // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-UDP-LISTENER: packageData()=%d - CALLED!', count($packageData))); diff --git a/application/hub/classes/nodes/class_BaseHubNode.php b/application/hub/classes/nodes/class_BaseHubNode.php index 41dbe5a4b..c97cf0222 100644 --- a/application/hub/classes/nodes/class_BaseHubNode.php +++ b/application/hub/classes/nodes/class_BaseHubNode.php @@ -9,7 +9,7 @@ use Org\Shipsimu\Hub\Factory\Registry\Object\ObjectTypeRegistryFactory; use Org\Shipsimu\Hub\Factory\State\Node\NodeStateFactory; use Org\Shipsimu\Hub\Locator\Node\LocateableNode; use Org\Shipsimu\Hub\Generic\BaseHubSystem; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Network\Receive\Receivable; use Org\Shipsimu\Hub\Template\Engine\Xml\Network\Answer\BaseXmlAnswerTemplateEngine; use Org\Shipsimu\Hub\Template\Engine\Xml\ObjectRegistry\XmlObjectRegistryTemplateEngine; @@ -807,7 +807,7 @@ abstract class BaseHubNode extends BaseHubSystem implements Updateable, AddableC assert(!empty($messageData[BaseXmlAnswerTemplateEngine::ANSWER_STATUS])); // Construct configuration entry for handling class' name - $classConfigEntry = strtolower($messageData[NetworkPackage::MESSAGE_ARRAY_TYPE] . '_status_' . $messageData[BaseXmlAnswerTemplateEngine::ANSWER_STATUS]) . '_handler_class'; + $classConfigEntry = strtolower($messageData[NetworkPackageHandler::MESSAGE_ARRAY_TYPE] . '_status_' . $messageData[BaseXmlAnswerTemplateEngine::ANSWER_STATUS]) . '_handler_class'; // Try to get a class $handlerInstance = ObjectFactory::createObjectByConfiguredName($classConfigEntry); diff --git a/application/hub/classes/package/assembler/class_PackageAssembler.php b/application/hub/classes/package/assembler/class_PackageAssembler.php index b791e7fd0..836ac7605 100644 --- a/application/hub/classes/package/assembler/class_PackageAssembler.php +++ b/application/hub/classes/package/assembler/class_PackageAssembler.php @@ -7,8 +7,8 @@ use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Handler\Chunk\ChunkHandlerFactory; use Org\Shipsimu\Hub\Generic\BaseHubSystem; use Org\Shipsimu\Hub\Handler\Network\RawData\HandleableRawData; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Network\Package\Fragment\PackageFragmenter; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; use Org\Shipsimu\Hub\Network\Receive\Receivable; // Import framework stuff @@ -80,7 +80,7 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, $assemblerInstance = new PackageAssembler(); // Set package instance here - $assemblerInstance->setPackageInstance($packageInstance); + $assemblerInstance->setPackageHandlerInstance($packageInstance); // Create an instance of a raw data input stream $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_input_stream_class'); @@ -114,7 +114,7 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, */ private function ifInputBufferIsEmpty () { // Check it - $isInputBufferEmpty = $this->getPackageInstance()->getStackInstance()->isStackEmpty(NetworkPackage::STACKER_NAME_DECODED_HANDLED); + $isInputBufferEmpty = $this->getPackageHandlerInstance()->getStackInstance()->isStackEmpty(NetworkPackageHandler::STACKER_NAME_DECODED_HANDLED); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: isInputBufferEmpty=' . intval($isInputBufferEmpty)); @@ -186,7 +186,7 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, */ private function handlePackageByUnhandledPackage (array $packageContent) { // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageData[' . HandleableRawData::PACKAGE_RAW_DATA . ']=' . $packageContent[HandleableRawData::PACKAGE_RAW_DATA]); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageContent[' . HandleableRawData::PACKAGE_RAW_DATA . ']=' . $packageContent[HandleableRawData::PACKAGE_RAW_DATA]); // Check for some conditions if ((!$this->ifInputBufferIsEmpty()) || (!$this->isPackageContentCompleted($packageContent))) { @@ -348,6 +348,3 @@ class PackageAssembler extends BaseHubSystem implements Assembler, Registerable, $this->pendingData = ''; } } - -// [EOF] -?> diff --git a/application/hub/classes/package/deliverable/.htaccess b/application/hub/classes/package/deliverable/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/classes/package/deliverable/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/classes/package/deliverable/class_PackageData.php b/application/hub/classes/package/deliverable/class_PackageData.php new file mode 100644 index 000000000..449cf6a77 --- /dev/null +++ b/application/hub/classes/package/deliverable/class_PackageData.php @@ -0,0 +1,57 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub 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 . + */ +class PackageData extends BaseHubSystem implements DeliverablePackage, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this class + * + * @return $packageInstance An instance of a PackageData class + */ + public final static function createPackageData () { + // Get new instance + $packageInstance = new PackageData(); + + // Return the prepared instance + return $packageInstance; + } + +} diff --git a/application/hub/classes/package/fragmenter/class_PackageFragmenter.php b/application/hub/classes/package/fragmenter/class_PackageFragmenter.php index 28adaac88..7869833a9 100644 --- a/application/hub/classes/package/fragmenter/class_PackageFragmenter.php +++ b/application/hub/classes/package/fragmenter/class_PackageFragmenter.php @@ -4,8 +4,9 @@ namespace Org\Shipsimu\Hub\Network\Package\Fragment; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\BaseHubSystem; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Assertion\AssertionException; @@ -166,24 +167,24 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera /** * "Getter" for processedPackages array index * - * @param $packageData Raw package data array + * @param $packageInstance An instance of a DeliverablePackage class array * @return $index Array index for processedPackages */ - private function getProcessedPackagesIndex (array $packageData) { + private function getProcessedPackagesIndex (DeliverablePackage $packageInstance) { return ( - $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 + $packageInstance->getSenderAddress() . NetworkPackageHandler::PACKAGE_DATA_SEPARATOR . + $packageInstance->getRecipient() . NetworkPackageHandler::PACKAGE_DATA_SEPARATOR . + $packageInstance->getContent() . NetworkPackageHandler::PACKAGE_DATA_SEPARATOR ); } /** * Checks whether the given package data is already processed by this fragmenter * - * @param $packageData Raw package data array + * @param $packageData An instance of a DeliverablePackage class array * @return $isProcessed Whether the package has been fragmented */ - private function isPackageProcessed (array $packageData) { + private function isPackageProcessed (DeliverablePackage $packageInstance) { // Get array index $index = $this->getProcessedPackagesIndex($packageData); @@ -201,10 +202,10 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera /** * Marks the given package data as processed by this fragmenter * - * @param $packageData Raw package data array + * @param $packageData An instance of a DeliverablePackage class array * @return void */ - private function markPackageDataProcessed (array $packageData) { + private function markPackageDataProcessed (DeliverablePackage $packageInstance) { // Remember it (until we may remove it) $this->processedPackages[$this->getProcessedPackagesIndex($packageData)] = TRUE; } @@ -212,10 +213,10 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera /** * Getter for final hash from given package data * - * @param $packageData Raw package data array + * @param $packageData An instance of a DeliverablePackage class array * @return $finalHash Final hash for package data */ - private function getFinalHashFromPackageData (array $packageData) { + private function getFinalHashFromPackageInstance (DeliverablePackage $packageInstance) { // Make sure it is there assert(isset($this->processedPackages[$this->getProcessedPackagesIndex($packageData)])); @@ -379,8 +380,8 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera ); // Make sure the chunk is not larger than a TCP package can hold - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-FRAGMENTER: assert: ' . strlen($rawData) . '/' . NetworkPackage::TCP_PACKAGE_SIZE . ' ...'); - // @TODO This assert broke packages where the hash chunk was very large: assert(strlen($rawData) <= NetworkPackage::TCP_PACKAGE_SIZE); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-FRAGMENTER: assert: ' . strlen($rawData) . '/' . NetworkPackageHandler::TCP_PACKAGE_SIZE . ' ...'); + // @TODO This assert broke packages where the hash chunk was very large: assert(strlen($rawData) <= NetworkPackageHandler::TCP_PACKAGE_SIZE); // Add it to the array if ($prepend === TRUE) { @@ -448,16 +449,16 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera * to all chunks and prepends a chunk with all hashes only in it. It will * return the final hash for faster processing of packages. * - * @param $packageData Raw package data array + * @param $packageData An instance of a DeliverablePackage class array * @param $helperInstance An instance of a ConnectionHelper class * @return $finalHash Final hash for faster processing * @todo $helperInstance is unused */ - public function fragmentPackageArray (array $packageData, ConnectionHelper $helperInstance) { + public function fragmentPackageArray (DeliverablePackage $packageInstance, ConnectionHelper $helperInstance) { // Is this package already fragmented? if (!$this->isPackageProcessed($packageData)) { // First we need to "implode" the array - $rawData = implode(NetworkPackage::PACKAGE_DATA_SEPARATOR, $packageData); + $rawData = implode(NetworkPackageHandler::PACKAGE_DATA_SEPARATOR, $packageInstance->getAllAsArray()); // Generate the final hash from the raw data (not encoded!) $finalHash = $this->generateHashFromRawData($rawData); @@ -479,7 +480,7 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera $this->markPackageDataProcessed($packageData); } else { // Get the final hash from the package data - $finalHash = $this->getFinalHashFromPackageData($packageData); + $finalHash = $this->getFinalHashFromPackageInstance($packageData); } // Return final hash @@ -493,7 +494,7 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera * the key is the chunk hash and value the raw chunk data. * * @param $finalHash Final hash for faster lookup - * @return $rawDataChunk Raw package data chunk + * @return $rawDataChunk An instance of a DeliverablePackage class chunk * @throws AssertionException If $finalHash was not 'TRUE' */ public function getNextRawDataChunk ($finalHash) { diff --git a/application/hub/classes/pools/peer/class_DefaultPeerPool.php b/application/hub/classes/pools/peer/class_DefaultPeerPool.php index 6585192d3..d7361ecb4 100644 --- a/application/hub/classes/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/classes/pools/peer/class_DefaultPeerPool.php @@ -6,9 +6,10 @@ namespace Org\Shipsimu\Hub\Pool\Peer; use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Connection\InvalidConnectionTypeException; use Org\Shipsimu\Hub\Factory\Handler\Protocol\ProtocolHandlerFactory; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Listener\Listenable; use Org\Shipsimu\Hub\Locator\Node\LocateableNode; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Pool\BasePool; use Org\Shipsimu\Hub\Pool\Poolable; use Org\Shipsimu\Hub\Pool\Peer\PoolablePeer; @@ -220,13 +221,13 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { /** * "Getter" for a valid socket resource from given packae data. * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @param $connectionType Type of connection, can be 'incoming', 'outgoing', 'server' or default * @return $socketInstance An instance of a StorableSocket class * @throws InvalidConnectionTypeException If the provided connection type is not valid * @throws LogicException If an expected array element is missing */ - public function getSocketFromPackageData (array $packageData, $connectionType = NULL) { + public function getSocketFromPackageInstance (DeliverablePackage $packageInstance, $connectionType = NULL) { // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL:packageData()=%d,connectionType[%s]=%s - CALLED!', count($packageData), gettype($connectionType), $connectionType)); @@ -234,7 +235,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { $socketInstance = NULL; // Resolve recipient (UNL) into a handler instance - $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($packageData); + $handlerInstance = ProtocolHandlerFactory::createProtocolHandlerFromPackageInstance($packageData); // Get UNL data $unlInstance = $handlerInstance->getUniversalNodeLocatorInstance(); @@ -277,7 +278,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { // Try to get the "peer"'s name if (!$socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketPeerName($peerAddress, $peerPort)) { // Handle the socket error with given package data - $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->handleSocketError(__METHOD__, __LINE__, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])); + $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->handleSocketError(__METHOD__, __LINE__, explode(':', $packageInstance->getRecipient())); } // END - if // Debug message diff --git a/application/hub/classes/recipient/dht/class_DhtRecipient.php b/application/hub/classes/recipient/dht/class_DhtRecipient.php index 63caa0a5c..2b2ce8e40 100644 --- a/application/hub/classes/recipient/dht/class_DhtRecipient.php +++ b/application/hub/classes/recipient/dht/class_DhtRecipient.php @@ -4,7 +4,8 @@ namespace Org\Shipsimu\Hub\Network\Recipient\Dht; // Import application-specific stuff use Org\Shipsimu\Hub\Database\Frontend\Node\Dht\NodeDistributedHashTableDatabaseWrapper; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Network\Recipient\BaseRecipient; use Org\Shipsimu\Hub\Network\Recipient\Recipient; @@ -66,16 +67,16 @@ class DhtRecipient extends BaseRecipient implements Recipient { * * @param $recipient Recipient to resolve (e.g. could be a virtual recipient or direct session id) * @param $listInstance An instance of a Listable class - * @param $packageData Valid package data array + * @param $packageData An instance of a DeliverablePackage class * @return void * @throws FrameworkException Could throw different exceptions depending on implementation */ - public function resolveRecipient ($recipient, Listable $listInstance, array $packageData) { + public function resolveRecipient ($recipient, Listable $listInstance, DeliverablePackage $packageInstance) { // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-RECIPIENT: recipient=' . $recipient . ' - CALLED!'); // Make sure the recipient is valid - assert($recipient == NetworkPackage::NETWORK_TARGET_DHT); + assert($recipient == NetworkPackageHandler::NETWORK_TARGET_DHT); // Get recipient discovery instance $discoverInstance = ObjectFactory::createObjectByConfiguredName('dht_recipient_discovery_class'); diff --git a/application/hub/classes/recipient/direct/class_DirectRecipient.php b/application/hub/classes/recipient/direct/class_DirectRecipient.php index ebfd2a73d..306672a98 100644 --- a/application/hub/classes/recipient/direct/class_DirectRecipient.php +++ b/application/hub/classes/recipient/direct/class_DirectRecipient.php @@ -3,7 +3,8 @@ namespace Org\Shipsimu\Hub\Network\Recipient\Direct; // Import application-specific sutff -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Network\Recipient\BaseRecipient; use Org\Shipsimu\Hub\Network\Recipient\Recipient; use Org\Shipsimu\Hub\Tools\HubTools; @@ -63,16 +64,16 @@ class DirectRecipient extends BaseRecipient implements Recipient { * * @param $recipient Recipient to resolve (e.g. could be a virtual recipient or direct session id) * @param $listInstance An instance of a Listable class - * @param $packageData Valid package data array + * @param $packageData An instance of a DeliverablePackage class * @return $resolved Resolved recipient or VOID if only the set list has been filled * @throws FrameworkException Could throw different exceptions depending on implementation */ - public function resolveRecipient ($recipient, Listable $listInstance, array $packageData) { + public function resolveRecipient ($recipient, Listable $listInstance, DeliverablePackage $packageInstance) { // Debug message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECT-RECIPIENT: recipient=' . $recipient); // "Explode" all recipients - $recipients = explode(NetworkPackage::PACKAGE_RECIPIENT_SEPARATOR, $recipient); + $recipients = explode(NetworkPackageHandler::PACKAGE_RECIPIENT_SEPARATOR, $recipient); // Is maximum reached? assert(count($recipients) <= $this->getConfigInstance()->getConfigEntry('package_recipient_max_count')); diff --git a/application/hub/classes/recipient/self/class_SelfRecipient.php b/application/hub/classes/recipient/self/class_SelfRecipient.php index b2afc8ccd..2978c7e94 100644 --- a/application/hub/classes/recipient/self/class_SelfRecipient.php +++ b/application/hub/classes/recipient/self/class_SelfRecipient.php @@ -3,7 +3,8 @@ namespace Org\Shipsimu\Hub\Network\Recipient\Loopback; // Import application-specific sutff -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Network\Recipient\BaseRecipient; use Org\Shipsimu\Hub\Network\Recipient\Recipient; use Org\Shipsimu\Hub\Tools\HubTools; @@ -63,15 +64,15 @@ class SelfRecipient extends BaseRecipient implements Recipient { * * @param $recipient Recipient to resolve (e.g. could be a virtual recipient or direct session id) * @param $listInstance An instance of a Listable class - * @param $packageData Valid package data array + * @param $packageData An instance of a DeliverablePackage class * @return $resolved Resolved recipient or VOID if only the set list has been filled * @throws FrameworkException Could throw different exceptions depending on implementation */ - public function resolveRecipient ($recipient, Listable $listInstance, array $packageData) { + public function resolveRecipient ($recipient, Listable $listInstance, DeliverablePackage $packageInstance) { // Make sure the recipient is valid /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SELF-RECIPIENT: recipient=' . $recipient); // @TODO Add more checks on data - assert($recipient == NetworkPackage::NETWORK_TARGET_SELF); + assert($recipient == NetworkPackageHandler::NETWORK_TARGET_SELF); // Determine IP or 'external_address' if set $unl = HubTools::determineOwnExternalAddress(); diff --git a/application/hub/classes/recipient/upper/class_UpperRecipient.php b/application/hub/classes/recipient/upper/class_UpperRecipient.php index 1442acc7e..2900af802 100644 --- a/application/hub/classes/recipient/upper/class_UpperRecipient.php +++ b/application/hub/classes/recipient/upper/class_UpperRecipient.php @@ -4,7 +4,8 @@ namespace Org\Shipsimu\Hub\Network\Recipient\Upper; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\BaseHubSystem; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Network\Recipient\BaseRecipient; use Org\Shipsimu\Hub\Network\Recipient\Recipient; @@ -63,14 +64,14 @@ class UpperRecipient extends BaseRecipient implements Recipient { * * @param $recipient Recipient to resolve (e.g. could be a virtual recipient or direct session id) * @param $listInstance An instance of a Listable class - * @param $packageData Valid package data array + * @param $packageData An instance of a DeliverablePackage class * @return $resolved Resolved recipient or VOID if only the set list has been filled * @throws FrameworkException Could throw different exceptions depending on implementation */ - public function resolveRecipient ($recipient, Listable $listInstance, array $packageData) { + public function resolveRecipient ($recipient, Listable $listInstance, DeliverablePackage $packageInstance) { // Make sure the recipient is valid /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UPPER-RECIPIENT: recipient=' . $recipient); - assert($recipient == NetworkPackage::NETWORK_TARGET_UPPER); + assert($recipient == NetworkPackageHandler::NETWORK_TARGET_UPPER); // Get all bootstrap nodes foreach (explode(BaseHubSystem::BOOTSTRAP_NODES_SEPARATOR, $this->getConfigInstance()->getConfigEntry('hub_bootstrap_nodes')) as $unlData) { diff --git a/application/hub/classes/registry/socket/class_SocketRegistry.php b/application/hub/classes/registry/socket/class_SocketRegistry.php index f94bbd553..6d5a42d1c 100644 --- a/application/hub/classes/registry/socket/class_SocketRegistry.php +++ b/application/hub/classes/registry/socket/class_SocketRegistry.php @@ -5,11 +5,12 @@ namespace Org\Shipsimu\Hub\Registry\Socket; // Import application-specific stuff use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\Information\Connection\ConnectionInfoFactory; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper; use Org\Shipsimu\Hub\Information\ShareableInfo; use Org\Shipsimu\Hub\Listener\BaseListener; use Org\Shipsimu\Hub\Listener\Listenable; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Registry\Socket\RegisterableSocket; // Import framework stuff @@ -213,11 +214,10 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke * * @param $infoInstance An instance of a ShareableInfo class * @param $socketInstance An instance of a StorableSocket class - * @param $packageData Optional raw package data * @throws SocketAlreadyRegisteredException If the given socket is already registered * @return void */ - public function registerSocketInstance (ShareableInfo $infoInstance, StorableSocket $socketInstance, array $packageData = array()) { + public function registerSocketInstance (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!'); @@ -292,10 +292,10 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke /** * "Getter" for info instance from given package data * - * @param $packageData Raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @return $infoInstance An instance of a ShareableInfo class */ - public function getInfoInstanceFromPackageData (array $packageData) { + public function getInfoInstanceFromPackageInstance (DeliverablePackage $packageInstance) { // Init info instance $infoInstance = NULL; //* DEBUG-DIE: */ die(__METHOD__ . ':packageData=' . print_r($packageData, TRUE)); @@ -311,7 +311,7 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke //* 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 (($socketInstance instanceof StorableSocket) && ($socketInstance->ifAddressMatches($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]))) { + if (($socketInstance instanceof StorableSocket) && ($socketInstance->ifAddressMatches($packageInstance->getRecipient()))) { // Debug die //* DEBUG-DIE: */ die(__METHOD__ . ': socketInstance=' . print_r($socketInstance, TRUE)); @@ -371,13 +371,13 @@ class SocketRegistry extends BaseRegistry implements Register, RegisterableSocke */ public function isIncomingSocketRegistered () { // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: CALLED!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: CALLED!'); // Default is not found $isRegistered = FALSE; // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: isRegistered=%d - EXIT!', intval($isRegistered))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-REGISTRY: isRegistered=%d - EXIT!', intval($isRegistered))); // Return status return $isRegistered; diff --git a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php index 4ecf6dcf7..30584132f 100644 --- a/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php +++ b/application/hub/classes/resolver/state/peer/class_PeerStateResolver.php @@ -6,6 +6,7 @@ namespace Org\Shipsimu\Hub\Resolver\State\Peer; use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Factory\State\Peer\PeerStateFactory; use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Resolver\State\BaseStateResolver; use Org\Shipsimu\Hub\Resolver\State\StateResolver; @@ -65,13 +66,13 @@ class PeerStateResolver extends BaseStateResolver implements StateResolver { * Returns an state instance for a given raw package data and socket resource * * @param $helperInstance An instance of a ConnectionHelper class - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @param $socketInstance An instance of a StorableSocket class * @return $stateInstance An instance of the resolved state * @throws InvalidSocketException If socketResource, even from getSocketResource() is no valid resource * @todo ~30% done */ - public static function resolveStateByPackage (ConnectionHelper $helperInstance, array $packageData, StorableSocket $socketInstance) { + public static function resolveStateByPackage (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance, StorableSocket $socketInstance) { // Get temporary resolver instance $resolverInstance = self::createPeerStateResolver(); diff --git a/application/hub/classes/tags/package/class_PackageTags.php b/application/hub/classes/tags/package/class_PackageTags.php index 61b86b713..d25d7ad8b 100644 --- a/application/hub/classes/tags/package/class_PackageTags.php +++ b/application/hub/classes/tags/package/class_PackageTags.php @@ -5,8 +5,9 @@ namespace Org\Shipsimu\Hub\Tag\Package; // Import application-specific stuff use Org\Shipsimu\Hub\Factory\Handler\Protocol\ProtocolHandlerFactory; use Org\Shipsimu\Hub\Factory\Registry\Object\ObjectTypeRegistryFactory; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; use Org\Shipsimu\Hub\Listener\Listenable; -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\Tag\BaseTag; use Org\Shipsimu\Hub\Tag\Tagable; use Org\Shipsimu\Hub\Template\Engine\Xml\ObjectRegistry\XmlObjectRegistryTemplateEngine; @@ -109,10 +110,10 @@ class PackageTags extends BaseTag implements Tagable { /** * Extracts the tags from given package data * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return void */ - private function extractTagsFromPackageData (array $packageData) { + private function extractTagsFromPackageInstance (DeliverablePackage $packageInstance) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TAGS: packageData=' . print_r($packageData, TRUE)); @@ -120,10 +121,10 @@ class PackageTags extends BaseTag implements Tagable { * We take a look at the tags (in most cases only one is needed) so * first we need the content data splitted up into all it's parts. */ - $contentData = explode(NetworkPackage::PACKAGE_MASK_SEPARATOR, $packageData[NetworkPackage::PACKAGE_DATA_CONTENT]); + $contentData = explode(NetworkPackageHandler::PACKAGE_MASK_SEPARATOR, $packageInstance->getContent()); // Get the tags and store them locally - $this->setTags(explode(NetworkPackage::PACKAGE_TAGS_SEPARATOR, $contentData[NetworkPackage::INDEX_TAGS])); + $this->setTags(explode(NetworkPackageHandler::PACKAGE_TAGS_SEPARATOR, $contentData[NetworkPackageHandler::INDEX_TAGS])); } /** @@ -132,11 +133,11 @@ class PackageTags extends BaseTag implements Tagable { * shared over the whole hub-network. So if the "tag" (let's better say * object type) isn't found in that XML the package won't be distributed. * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return void * @throws InvalidTagException If a provided tag from the package data is invalid */ - private function verifyAllTags (array $packageData) { + private function verifyAllTags (DeliverablePackage $packageInstance) { // Get the registry $objectRegistryInstance = ObjectTypeRegistryFactory::createObjectTypeRegistryInstance(); @@ -158,7 +159,7 @@ class PackageTags extends BaseTag implements Tagable { } // END - if // Now save the last discovered protocol/recipient type - $this->lastProtocol = ProtocolHandlerFactory::createProtocolHandlerFromPackageData($packageData); + $this->lastProtocol = ProtocolHandlerFactory::createProtocolHandlerFromPackageInstance($packageData); $this->lastRecipientType = $entry[XmlObjectRegistryTemplateEngine::OBJECT_TYPE_DATA_RECIPIENT_TYPE]; } // END - foreach } @@ -166,12 +167,12 @@ class PackageTags extends BaseTag implements Tagable { /** * Chooses the right protocol from given package data * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @return $lastProtocol An instance of the last used HandleableProtocol class */ - public function chooseProtocolFromPackageData (array $packageData) { + public function chooseProtocolFromPackageInstance (DeliverablePackage $packageInstance) { // Extract the tags - $this->extractTagsFromPackageData($packageData); + $this->extractTagsFromPackageInstance($packageData); // Now we need to verify every single tag $this->verifyAllTags($packageData); @@ -183,13 +184,13 @@ class PackageTags extends BaseTag implements Tagable { /** * Checks whether the given package data is accepted by the listener * - * @param $packageData Raw package data + * @param $packageData An instance of a DeliverablePackage class * @param $listenerInstance A Listenable instance * @return $accepts Whether it is accepted */ - public function ifPackageDataIsAcceptedByListener (array $packageData, Listenable $listenerInstance) { + public function ifPackageDataIsAcceptedByListener (DeliverablePackage $packageInstance, Listenable $listenerInstance) { // Extract the tags - $this->extractTagsFromPackageData($packageData); + $this->extractTagsFromPackageInstance($packageData); // Now every single tag needs to be validated $this->verifyAllTags($packageData); diff --git a/application/hub/classes/tasks/network/class_NetworkPackageReaderTask.php b/application/hub/classes/tasks/network/class_NetworkPackageReaderTask.php index ffb878a5d..f3c0df456 100644 --- a/application/hub/classes/tasks/network/class_NetworkPackageReaderTask.php +++ b/application/hub/classes/tasks/network/class_NetworkPackageReaderTask.php @@ -85,7 +85,7 @@ class NetworkPackageReaderTask extends BaseTask implements Taskable, Visitable { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-READER-TASK: CALLED!'); // Get package instance - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-READER-TASK: packageInstance=%s', $packageInstance->__toString())); diff --git a/application/hub/classes/tasks/network/class_NetworkPackageWriterTask.php b/application/hub/classes/tasks/network/class_NetworkPackageWriterTask.php index a9b8eee72..a76a98758 100644 --- a/application/hub/classes/tasks/network/class_NetworkPackageWriterTask.php +++ b/application/hub/classes/tasks/network/class_NetworkPackageWriterTask.php @@ -85,7 +85,7 @@ class NetworkPackageWriterTask extends BaseTask implements Taskable, Visitable { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-WRITER-TASK: CALLED!'); // "Cache" package instance - $packageInstance = NetworkPackageFactory::createNetworkPackageInstance(); + $packageInstance = NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance(); // Do we have something to deliver? if ($packageInstance->isEncodedDataPending()) { diff --git a/application/hub/classes/visitor/socket/class_HalfShutdownSocketVisitor.php b/application/hub/classes/visitor/socket/class_HalfShutdownSocketVisitor.php index 9a1ab9f02..3996d44c3 100644 --- a/application/hub/classes/visitor/socket/class_HalfShutdownSocketVisitor.php +++ b/application/hub/classes/visitor/socket/class_HalfShutdownSocketVisitor.php @@ -71,9 +71,9 @@ class HalfShutdownSocketVisitor extends BaseVisitor implements Visitor { // Do we have reached the retry count? if (ConnectionStatisticsHelper::isConnectRetryExhausted($helperInstance)) { // Also visit the network package to clear any out-going packages - NetworkPackageFactory::createNetworkPackageInstance()->accept($this); + NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance()->accept($this); - // Shutdown the connection helper (look in visitNetworkPackage() for details) + // Shutdown the connection helper (look in visitNetworkPackageHandler() for details) $helperInstance->doShutdown(); } else { // We can still move on and retry the connection attempt @@ -89,7 +89,7 @@ class HalfShutdownSocketVisitor extends BaseVisitor implements Visitor { * @param $packageInstance An instance of a Deliverable class * @return void */ - public function visitNetworkPackage (Deliverable $packageInstance) { + public function visitNetworkPackageHandler (Deliverable $packageInstance) { // Just call it back $packageInstance->removeFirstFailedPackage(); } diff --git a/application/hub/classes/visitor/socket/class_ShutdownSocketVisitor.php b/application/hub/classes/visitor/socket/class_ShutdownSocketVisitor.php index f4e052fda..e2e6510fd 100644 --- a/application/hub/classes/visitor/socket/class_ShutdownSocketVisitor.php +++ b/application/hub/classes/visitor/socket/class_ShutdownSocketVisitor.php @@ -72,7 +72,7 @@ class ShutdownSocketVisitor extends BaseVisitor implements Visitor { // Do we have reached the retry count? if (ConnectionStatisticsHelper::isConnectRetryExhausted($helperInstance)) { // Also visit the network package to clear any out-going packages - NetworkPackageFactory::createNetworkPackageInstance()->accept($this); + NetworkPackageHandlerFactory::createNetworkPackageHandlerInstance()->accept($this); // Shutdown the connection $helperInstance->doShutdown(); @@ -89,7 +89,7 @@ class ShutdownSocketVisitor extends BaseVisitor implements Visitor { * @param $packageInstance An instance of a Deliverable class * @return void */ - public function visitNetworkPackage (Deliverable $packageInstance) { + public function visitNetworkPackageHandler (Deliverable $packageInstance) { // Just call it back $packageInstance->clearAllStacks(); } diff --git a/application/hub/config.php b/application/hub/config.php index a6db49c7b..2cadaed33 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -709,10 +709,13 @@ $cfg->setConfigEntry('query_iterator_class', 'Org\Mxchange\CoreFramework\Iterato $cfg->setConfigEntry('node_ping_iterator_class', 'Org\Shipsimu\Hub\Iterator\Node\Ping\NodePingIterator'); // CFG: WEB-TEMPLATE-CLASS @OVERRIDE -$cfg->setConfigEntry('web_template_class', 'ConsoleTemplateEngine'); +$cfg->setConfigEntry('web_template_class', 'Org\Mxchange\CoreFramework\Template\Engine\ConsoleTemplateEngine'); -// CFG: NETWORK-PACKAGE-CLASS -$cfg->setConfigEntry('network_package_class', 'Org\Shipsimu\Hub\Network\Package\NetworkPackage'); +// CFG: PACKAGE-DATA-CLASS +$cfg->setConfigEntry('package_data_class', 'Org\Shipsimu\Hub\Network\Package\PackageData'); + +// CFG: NETWORK-PACKAGE-HANDLER-CLASS +$cfg->setConfigEntry('network_package_handler_class', 'Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler'); // CFG: PACKAGE-COMPRESSOR-CLASS $cfg->setConfigEntry('deco_package_compressor_class', 'Org\Shipsimu\Hub\Network\Compressor\Package\NetworkPackageCompressorDecorator'); diff --git a/application/hub/exceptions/package/class_InvalidDataChecksumException.php b/application/hub/exceptions/package/class_InvalidDataChecksumException.php index d7c5da83b..c1874b130 100644 --- a/application/hub/exceptions/package/class_InvalidDataChecksumException.php +++ b/application/hub/exceptions/package/class_InvalidDataChecksumException.php @@ -3,7 +3,7 @@ namespace Org\Shipsimu\Hub\RawData; // Import application-specific stuff -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; // Import framework stuff use Org\Mxchange\CoreFramework\Generic\FrameworkException; @@ -44,8 +44,8 @@ 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_ADDRESS]), - $messageArray[1][NetworkPackage::PACKAGE_CONTENT_CHECKSUM] + $messageArray[0]->getHashFromContentSessionId($messageArray[1], $messageArray[2]->getSenderAddress()), + $messageArray[1][NetworkPackageHandler::PACKAGE_CONTENT_CHECKSUM] ); // Call parent exception constructor diff --git a/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php b/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php index 1fb018317..1b40bd057 100644 --- a/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php +++ b/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php @@ -2,9 +2,6 @@ // Own namespace namespace Org\Shipsimu\Hub\Status\Package; -// Import application-specific stuff -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; - // Import framework stuff use Org\Mxchange\CoreFramework\Generic\FrameworkException; @@ -43,12 +40,12 @@ class UnexpectedPackageStatusException extends FrameworkException { $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[1]->getPackageStatus(), $messageArray[2], - $messageArray[1][NetworkPackage::PACKAGE_DATA_RECIPIENT], - $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS], - $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER_PORT], - $messageArray[1][NetworkPackage::PACKAGE_DATA_HASH] + $messageArray[1]->getRecipient(), + $messageArray[1]->getSenderAddress(), + $messageArray[1]->getSenderPort(), + $messageArray[1]->getHash() ); // Call parent exception constructor diff --git a/application/hub/exceptions/peer/class_PeerAlreadyRegisteredException.php b/application/hub/exceptions/peer/class_PeerAlreadyRegisteredException.php index 9f1bf9fbb..4b2cf7dfc 100644 --- a/application/hub/exceptions/peer/class_PeerAlreadyRegisteredException.php +++ b/application/hub/exceptions/peer/class_PeerAlreadyRegisteredException.php @@ -3,7 +3,7 @@ namespace Org\Shipsimu\Hub\Peer; // Import application-specific stuff -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; // Import framework stuff use Org\Mxchange\CoreFramework\Generic\FrameworkException; @@ -40,10 +40,10 @@ class PeerAlreadyRegisteredException extends FrameworkException { */ public function __construct (array $messageArray, $code) { // Construct the message - $message = sprintf('[%s:%d] Sender %s is already registered.', + $message = sprintf('[%s:%d] Sender "%s" is already registered.', $messageArray[0]->__toString(), $this->getLine(), - $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS] + $messageArray[1]->getSenderAddress() ); // Call parent exception constructor diff --git a/application/hub/exceptions/unl/class_InvalidUnlException.php b/application/hub/exceptions/unl/class_InvalidUnlException.php index f80c644ee..d8b525806 100644 --- a/application/hub/exceptions/unl/class_InvalidUnlException.php +++ b/application/hub/exceptions/unl/class_InvalidUnlException.php @@ -3,7 +3,7 @@ namespace Org\Shipsimu\Hub\Unl; // Import application-specific stuff -use Org\Shipsimu\Hub\Network\Package\NetworkPackage; +use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler; // Import framework stuff use Org\Mxchange\CoreFramework\Generic\FrameworkException; @@ -44,7 +44,7 @@ class InvalidUnlException extends FrameworkException { $messageArray[0]->__toString(), $this->getLine(), $messageArray[1]->__toString(), - $messageArray[2][NetworkPackage::PACKAGE_DATA_RECIPIENT] + $messageArray[2]->getRecipient() ); // Call parent exception constructor diff --git a/application/hub/interfaces/container/socket/class_StorableSocket.php b/application/hub/interfaces/container/socket/class_StorableSocket.php index 6daf041cc..1f234ddf2 100644 --- a/application/hub/interfaces/container/socket/class_StorableSocket.php +++ b/application/hub/interfaces/container/socket/class_StorableSocket.php @@ -51,7 +51,7 @@ interface StorableSocket extends FrameworkInterface { const SOCKET_CONNECTED = 'connected'; // Nothing errorous happens, socket is connected // Socket data array indexes - const SOCKET_ARRAY_INDEX_FILE = 'socket_file'; + // @deprecated Use a class instead of asociative array const SOCKET_ARRAY_INDEX_ADDRESS = 'socket_address'; const SOCKET_ARRAY_INDEX_PORT = 'socket_port'; const SOCKET_ARRAY_INDEX_TYPE = 'socket_type'; diff --git a/application/hub/interfaces/database/frontend/class_NodeDhtWrapper.php b/application/hub/interfaces/database/frontend/class_NodeDhtWrapper.php index a14c7fc4e..f7c74349e 100644 --- a/application/hub/interfaces/database/frontend/class_NodeDhtWrapper.php +++ b/application/hub/interfaces/database/frontend/class_NodeDhtWrapper.php @@ -2,6 +2,9 @@ // Own namespace namespace Org\Shipsimu\Hub\Database\Frontend\Node\Dht; +// Import application-specific stuff +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; + // Import framework stuff use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria; use Org\Mxchange\CoreFramework\Database\Frontend\DatabaseWrapper; @@ -152,10 +155,10 @@ interface NodeDhtWrapper extends DatabaseWrapper { /** * Find recipients for given package data and returns it as a result instance * - * @param $packageData An array of valid package data + * @param $packageInstance An instance of a DeliverablePackage class * @return $recipients An indexed array with DHT recipients */ - function getResultFromExcludedSender (array $packageData); + function getResultFromExcludedSender (DeliverablePackage $packageInstance); /** * Find recopients by given key/value pair. First look for the key and if it diff --git a/application/hub/interfaces/discovery/recipient/dht/class_DiscoverableDhtRecipient.php b/application/hub/interfaces/discovery/recipient/dht/class_DiscoverableDhtRecipient.php index 468531e05..95ebd9d06 100644 --- a/application/hub/interfaces/discovery/recipient/dht/class_DiscoverableDhtRecipient.php +++ b/application/hub/interfaces/discovery/recipient/dht/class_DiscoverableDhtRecipient.php @@ -4,6 +4,7 @@ namespace Org\Shipsimu\Hub\Discovery\Recipient\Dht; // Import application-specific stuff use Org\Shipsimu\Hub\Discovery\Recipient\DiscoverableRecipient; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; /** * An interface for DHT recipient discovery @@ -31,9 +32,9 @@ interface DiscoverableDhtRecipient extends DiscoverableRecipient { /** * Resolves one or more recipients for a DHT transfer by given package data. * - * @param $packageData Valid package data array + * @param $packageInstance An instance of a DeliverablePackage class * @return $recipients An indexed array with DHT recipients */ - function resolveRecipientsByPackageData (array $packageData); + function resolveRecipientsByPackageData (DeliverablePackage $packageInstance); } diff --git a/application/hub/interfaces/discovery/recipient/node/class_DiscoverableNodeRecipient.php b/application/hub/interfaces/discovery/recipient/node/class_DiscoverableNodeRecipient.php index 43f635718..98175d3f6 100644 --- a/application/hub/interfaces/discovery/recipient/node/class_DiscoverableNodeRecipient.php +++ b/application/hub/interfaces/discovery/recipient/node/class_DiscoverableNodeRecipient.php @@ -4,6 +4,7 @@ namespace Org\Shipsimu\Hub\Discovery\Recipient\Node; // Import application-specific stuff use Org\Shipsimu\Hub\Discovery\Recipient\DiscoverableRecipient; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; /** * An interface for node recipient discovery @@ -31,17 +32,17 @@ interface DiscoverableNodeRecipient extends DiscoverableRecipient { /** * Tries to discover all recipients for given package data * - * @param $packageData Raw package data array + * @param $packageInstance An instance of a DeliverablePackage class array * @return void */ - function discoverRecipients (array $packageData); + function discoverRecipients (DeliverablePackage $packageInstance); /** * Tries to discover all recipients by given decoded package data. * - * @param $decodedData Raw raw package data array + * @param $packageInstance An instance of a DeliverablePackage class * @return void */ - function discoverRawRecipients (array $decodedData); + function discoverRawRecipients (DeliverablePackage $packageInstance); } diff --git a/application/hub/interfaces/discovery/recipient/socket/class_DiscoverableSocket.php b/application/hub/interfaces/discovery/recipient/socket/class_DiscoverableSocket.php index a1bf746b2..d1acb3b18 100644 --- a/application/hub/interfaces/discovery/recipient/socket/class_DiscoverableSocket.php +++ b/application/hub/interfaces/discovery/recipient/socket/class_DiscoverableSocket.php @@ -5,6 +5,7 @@ namespace Org\Shipsimu\Hub\Discovery\Socket; // Import application-specific stuff use Org\Shipsimu\Hub\Discovery\Recipient\DiscoverableRecipient; use Org\Shipsimu\Hub\Handler\Protocol\HandleableProtocol; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; /** * An interface for socket discovery @@ -33,21 +34,21 @@ interface DiscoverableSocket extends DiscoverableRecipient { * Tries to discover the right socket for given package data and returns a * matching socket resource. * - * @param $packageData Raw package data array + * @param $packageInstance An instance of a DeliverablePackage class array * @param $connectionType Type of connection, can be either 'incoming' or 'outgoing', but *NEVER* 'server'! * @return $socketInstance An instance of a StorableSocket class * @throws NoListGroupException If the procol group is not found in peer list * @throws NullPointerException If listenerInstance is NULL */ - function discoverSocket (array $packageData, $connectionType); + function discoverSocket (DeliverablePackage $packageInstance, $connectionType); /** * Tries to dicover the right listener instance * * @param $protocolInstance An instance of a HandleableProtocol class - * @param $packageData Raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @return $listenerInstance An instance of a Listenable instance or null */ - function discoverListenerInstance (HandleableProtocol $protocolInstance, array $packageData); + function discoverListenerInstance (HandleableProtocol $protocolInstance, DeliverablePackage $packageInstance); } diff --git a/application/hub/interfaces/distributable/class_Distributable.php b/application/hub/interfaces/distributable/class_Distributable.php index d5c9e30da..d37f20f2e 100644 --- a/application/hub/interfaces/distributable/class_Distributable.php +++ b/application/hub/interfaces/distributable/class_Distributable.php @@ -4,6 +4,7 @@ namespace Org\Shipsimu\Hub\Dht; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; /** * An interface for DHTs @@ -85,10 +86,10 @@ interface Distributable extends HubInterface { /** * Find recipients for given package data * - * @param $packageData An array of valid package data + * @param $packageInstance An instance of a DeliverablePackage class * @return $recipients An indexed array with DHT recipients */ - function findRecipientsByPackageData (array $packageData); + function findRecipientsByPackageData (DeliverablePackage $packageInstance); /** * Whether the DHT has fully bootstrapped (after state 'booting') @@ -119,7 +120,5 @@ interface Distributable extends HubInterface { * @return void */ function enableAcceptDhtBootstrap (); -} -// [EOF] -?> +} diff --git a/application/hub/interfaces/handler/network/class_Networkable.php b/application/hub/interfaces/handler/network/class_Networkable.php index e613fe880..8f221b08c 100644 --- a/application/hub/interfaces/handler/network/class_Networkable.php +++ b/application/hub/interfaces/handler/network/class_Networkable.php @@ -52,7 +52,7 @@ interface Networkable extends HandleableDataSet, HandleableRawData { /** * "Getter" for next decoded data from the stacker * - * @return $decodedData Raw data from the stacker + * @return $packageInstance An instance of a DeliverablePackage class */ function getNextRawData (); diff --git a/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php b/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php index ae647db2c..4a17eee0b 100644 --- a/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php +++ b/application/hub/interfaces/handler/protocol/class_HandleableProtocol.php @@ -4,6 +4,7 @@ namespace Org\Shipsimu\Hub\Handler\Protocol; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Handler\Handleable; @@ -57,10 +58,10 @@ interface HandleableProtocol extends Handleable, HubInterface { * Validates given 'recipient' if it is a valid UNL. This means that the UNL * can be parsed by the protocol handler. * - * @param $packageData Valid raw package data + * @param $packageInstance Valid raw package data * @return $isValid Whether the UNL can be validated */ - function isValidUniversalNodeLocatorByPackageData (array $packageData); + function isValidUniversalNodeLocatorByPackageData (DeliverablePackage $packageInstance); /** * If the found UNL (address) matches own external or internal address diff --git a/application/hub/interfaces/helper/connections/class_ConnectionHelper.php b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php index 3913fc4dc..58f0ea6db 100644 --- a/application/hub/interfaces/helper/connections/class_ConnectionHelper.php +++ b/application/hub/interfaces/helper/connections/class_ConnectionHelper.php @@ -5,6 +5,7 @@ namespace Org\Shipsimu\Hub\Helper\Connection; // Import application-specific stuff use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Helper\HubHelper; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; /** * An interface for connection helper classes @@ -33,11 +34,11 @@ interface ConnectionHelper extends HubHelper { /** * Sends raw package data to the recipient * - * @param $packageData Raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @return void * @throws InvalidSocketException If we got a problem with this socket */ - function sendRawPackageData (array $packageData); + function sendRawPackageData (DeliverablePackage $packageInstance); /** * Do the shutdown sequence for this connection helper @@ -52,8 +53,8 @@ interface ConnectionHelper extends HubHelper { * Static "getter" for this connection class' name * * @param $socketInstance An instance of a StorableSocket class - * @param $className Name of calling class - * @return $class Expanded class name + * @param $className Name of calling class + * @return $class Expanded class name */ static function getConnectionClassNameFromSocket (StorableSocket $socketInstance, $className); diff --git a/application/hub/interfaces/listener/class_Listenable.php b/application/hub/interfaces/listener/class_Listenable.php index 35669a594..7ec1368ca 100644 --- a/application/hub/interfaces/listener/class_Listenable.php +++ b/application/hub/interfaces/listener/class_Listenable.php @@ -4,6 +4,7 @@ namespace Org\Shipsimu\Hub\Listener; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; /** * An interface for listeners @@ -45,10 +46,10 @@ interface Listenable extends HubInterface { /** * Checks whether the listener would accept the given package data array * - * @param $packageData Raw package data - * @return $accepts Whether this listener does accept + * @param $packageInstance An instance of a DeliverablePackage class + * @return $accepts Whether this listener does accept */ - function ifListenerAcceptsPackageData (array $packageData); + function ifListenerAcceptsPackageData (DeliverablePackage $packageInstance); /** * Monitors incoming raw data from the handler and transfers it to the diff --git a/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php b/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php index f0382f25a..b7a24c7d2 100644 --- a/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php +++ b/application/hub/interfaces/lookup/peer_states/class_LookupablePeerState.php @@ -6,6 +6,7 @@ namespace Org\Shipsimu\Hub\State\Peer\Lookup; use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper; use Org\Shipsimu\Hub\LookupTable\Lookupable; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; use Org\Shipsimu\Hub\State\Peer\PeerStateable; // Import framework stuff @@ -37,28 +38,28 @@ interface LookupablePeerState extends Lookupable { /** * Checks whether given 'sender' is a new peer * - * @param $packageData Raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @return $isNewPeer Whether 'sender' is a new peer to this node */ - function isSenderNewPeer (array $packageData); + function isSenderNewPeer (DeliverablePackage $packageInstance); /** * Registers a peer with given package data. We use the session id from it * - * @param $packageData Valid raw package data + * @param $packageInstance Valid An instance of a DeliverablePackage class * @param $socketInstance An instance of a StorableSocket class * @return void */ - function registerPeerByPackageData (array $packageData, StorableSocket $socketInstance); + function registerPeerByPackageData (DeliverablePackage $packageInstance, StorableSocket $socketInstance); /** - * Registers the given peer state and raw package data + * Registers the given peer state and An instance of a DeliverablePackage class * * @param $stateInstance A PeerStateable class instance - * @param $packageData Valid package data array + * @param $packageInstance An instance of a DeliverablePackage class * @return void */ - function registerPeerState (PeerStateable $stateInstance, array $packageData); + function registerPeerState (PeerStateable $stateInstance, DeliverablePackage $packageInstance); /** * Purges old entries of given socket resource. We use the IP address from that resource. @@ -72,10 +73,10 @@ interface LookupablePeerState extends Lookupable { * Checks whether a given peer state (in helper instance) is same as stored * in database compared with the one from $helperInstance. * - * @param $helperInstance An instance of a ConnectionHelper class - * @param $packageData Valid package data array - * @return $isSamePeerState Whether the peer's state is the same + * @param $helperInstance An instance of a ConnectionHelper class + * @param $packageInstance An instance of a DeliverablePackage class + * @return $isSamePeerState Whether the peer's state is the same */ - function isSamePeerState (ConnectionHelper $helperInstance, array $packageData); + function isSamePeerState (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance); } diff --git a/application/hub/interfaces/package/assembler/class_Assembler.php b/application/hub/interfaces/package/assembler/class_Assembler.php index 97dd6b699..b22f4b92d 100644 --- a/application/hub/interfaces/package/assembler/class_Assembler.php +++ b/application/hub/interfaces/package/assembler/class_Assembler.php @@ -65,7 +65,5 @@ interface Assembler extends HubInterface { * @return void */ function handleMultipleMessages (); -} -// [EOF] -?> +} diff --git a/application/hub/interfaces/package/class_Receivable.php b/application/hub/interfaces/package/class_Receivable.php index dae766703..7b0281080 100644 --- a/application/hub/interfaces/package/class_Receivable.php +++ b/application/hub/interfaces/package/class_Receivable.php @@ -103,7 +103,7 @@ interface Receivable extends HubInterface { * against the real raw package data (that what the sender has sent). * * @param $rawPackageContent The raw package content to be "decoded" - * @return $decodedData The real package data that the sender has sent + * @return $packageInstance An instance of a DeliverablePackage class */ function decodeRawContent ($rawPackageContent); @@ -125,10 +125,10 @@ interface Receivable extends HubInterface { /** * Handles decoded data for this node * - * @param $decodedData An array with decoded raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @return void */ - function handleRawData (array $decodedData); + function handleRawData (DeliverablePackage $packageInstance); /** * Checks whether the assembler has multiple messages pending diff --git a/application/hub/interfaces/package/fragmenter/class_Fragmentable.php b/application/hub/interfaces/package/fragmenter/class_Fragmentable.php index ff749e615..6ed4880a1 100644 --- a/application/hub/interfaces/package/fragmenter/class_Fragmentable.php +++ b/application/hub/interfaces/package/fragmenter/class_Fragmentable.php @@ -7,6 +7,7 @@ use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; /** * An interface for a package fragmenter @@ -37,11 +38,11 @@ interface Fragmentable extends HubInterface { * to all chunks and prepends a final hashsum chunk. It will return the * final hash for faster processing of packages. * - * @param $packageData Raw package data array + * @param $packageInstance An instance of a DeliverablePackage instance * @param $helperInstance An instance of a ConnectionHelper class * @return $finalHash Final hash for faster processing */ - function fragmentPackageArray (array $packageData, ConnectionHelper $helperInstance); + function fragmentPackageArray (DeliverablePackage $packageInstance, ConnectionHelper $helperInstance); /** * This method gets the next chunk from the internal FIFO which should be @@ -49,10 +50,8 @@ interface Fragmentable extends HubInterface { * the key is the chunk hash and value the raw chunk data. * * @param $finalHash Final hash for faster lookup - * @return $rawDataChunk Raw package data chunk + * @return $rawDataChunk An instance of a DeliverablePackage class chunk */ function getNextRawDataChunk ($finalHash); -} -// [EOF] -?> +} diff --git a/application/hub/interfaces/package/package/.htaccess b/application/hub/interfaces/package/package/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/interfaces/package/package/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/interfaces/package/package/class_DeliverablePackage.php b/application/hub/interfaces/package/package/class_DeliverablePackage.php new file mode 100644 index 000000000..16df801be --- /dev/null +++ b/application/hub/interfaces/package/package/class_DeliverablePackage.php @@ -0,0 +1,32 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub 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 . + */ +interface DeliverablePackage extends HubInterface { + +} diff --git a/application/hub/interfaces/recipient/class_Recipient.php b/application/hub/interfaces/recipient/class_Recipient.php index a666838f6..3cdc5a60e 100644 --- a/application/hub/interfaces/recipient/class_Recipient.php +++ b/application/hub/interfaces/recipient/class_Recipient.php @@ -4,6 +4,7 @@ namespace Org\Shipsimu\Hub\Network\Recipient; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Lists\Listable; @@ -35,14 +36,11 @@ interface Recipient extends HubInterface { * Tries to resolve given recipient into session ids or Universal Node Locator * depending on implementation (hint: Template Method Pattern) * - * @param $recipient Recipient to resolve (e.g. could be a virtual recipient or direct session id) + * @param $recipient Recipient to resolve (e.g. could be a virtual recipient or direct session id) * @param $listInstance An instance of a Listable class - * @param $packageData Valid package data array - * @return $resolved Resolved recipient or VOID if only the set list has been filled + * @param $packageInstance An instance of a DeliverablePackage class + * @return $resolved Resolved recipient or VOID if only the set list has been filled * @throws FrameworkException Could throw different exceptions depending on implementation */ - function resolveRecipient ($recipient, Listable $listInstance, array $packageData); + function resolveRecipient ($recipient, Listable $listInstance, DeliverablePackage $packageInstance); } - -// [EOF] -?> diff --git a/application/hub/interfaces/registry/socket/class_RegisterableSocket.php b/application/hub/interfaces/registry/socket/class_RegisterableSocket.php index 233433b95..73707b9db 100644 --- a/application/hub/interfaces/registry/socket/class_RegisterableSocket.php +++ b/application/hub/interfaces/registry/socket/class_RegisterableSocket.php @@ -6,6 +6,7 @@ namespace Org\Shipsimu\Hub\Registry\Socket; use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Information\ShareableInfo; use Org\Shipsimu\Hub\Listener\Listenable; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Registry\Registerable; @@ -65,10 +66,10 @@ interface RegisterableSocket extends Registerable { /** * "Getter" for info instance from given package data * - * @param $packageData Raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @return $infoInstance An instance of a ShareableInfo class */ - function getInfoInstanceFromPackageData (array $packageData); + function getInfoInstanceFromPackageInstance (DeliverablePackage $packageInstance); /** * Checks whether a socket of a connection type 'incoming' is registered. diff --git a/application/hub/interfaces/resolver/state/class_StateResolver.php b/application/hub/interfaces/resolver/state/class_StateResolver.php index ade54cb06..c361fdfc0 100644 --- a/application/hub/interfaces/resolver/state/class_StateResolver.php +++ b/application/hub/interfaces/resolver/state/class_StateResolver.php @@ -5,6 +5,7 @@ namespace Org\Shipsimu\Hub\Resolver\State; // Import application-specific stuff use Org\Shipsimu\Hub\Container\Socket\StorableSocket; use Org\Shipsimu\Hub\Helper\Connection\ConnectionHelper; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff use Org\Mxchange\CoreFramework\Resolver\Resolver; @@ -36,11 +37,11 @@ interface StateResolver extends Resolver { * Returns an state instance for a given package raw data and socket resource * * @param $helperInstance An instance of a ConnectionHelper class - * @param $packageData Raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @param $socketInstance An instance of a StorableSocket class * @return $stateInstance An instance of the resolved state */ - static function resolveStateByPackage (ConnectionHelper $helperInstance, array $packageData, StorableSocket $socketInstance); + static function resolveStateByPackage (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance, StorableSocket $socketInstance); /** * Checks whether the given state is valid diff --git a/application/hub/interfaces/socket/class_SocketTag.php b/application/hub/interfaces/socket/class_SocketTag.php index 719ecc596..1f955dc46 100644 --- a/application/hub/interfaces/socket/class_SocketTag.php +++ b/application/hub/interfaces/socket/class_SocketTag.php @@ -4,6 +4,7 @@ namespace Org\Shipsimu\Hub\Tag\Socket; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; /** * An interface/tag for socket classes @@ -31,11 +32,11 @@ interface SocketTag extends HubInterface { /** * "Getter" for a valid socket resource from given packae data. * - * @param $packageData Raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @param $connectionType Type of connection, can be 'incoming', 'outgoing', 'server' or default * @return $socketInstance An instance of a StorableSocket class * @throws InvalidConnectionTypeException If the provided connection type is not valid */ - function getSocketFromPackageData (array $packageData, $connectionType = NULL); + function getSocketFromPackageInstance (DeliverablePackage $packageInstance, $connectionType = NULL); } diff --git a/application/hub/interfaces/tags/class_Tagable.php b/application/hub/interfaces/tags/class_Tagable.php index f54abf8e7..b05d8e526 100644 --- a/application/hub/interfaces/tags/class_Tagable.php +++ b/application/hub/interfaces/tags/class_Tagable.php @@ -5,6 +5,7 @@ namespace Org\Shipsimu\Hub\Tag; // Import application-specific stuff use Org\Shipsimu\Hub\Generic\HubInterface; use Org\Shipsimu\Hub\Listener\Listenable; +use Org\Shipsimu\Hub\Network\Package\DeliverablePackage; // Import framework stuff @@ -38,18 +39,18 @@ interface Tagable extends HubInterface { /** * Chooses the right protocol from given package data * - * @param $packageData Raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @return $protocolInstance An instance of a HandleableProtocol class */ - function chooseProtocolFromPackageData (array $packageData); + function chooseProtocolFromPackageInstance (DeliverablePackage $packageInstance); /** * Checks whether the given package data is accepted by the listener * - * @param $packageData Raw package data + * @param $packageInstance An instance of a DeliverablePackage class * @param $listenerInstance A Listenable instance - * @return $accepts Whether it is accepted + * @return $accepts Whether it is accepted */ - function ifPackageDataIsAcceptedByListener (array $packageData, Listenable $listenerInstance); + function ifPackageDataIsAcceptedByListener (DeliverablePackage $packageInstance, Listenable $listenerInstance); } diff --git a/core b/core index 1956ac6a5..93b7ac061 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 1956ac6a5f89588c5cf1e10c0cbd0a14915c4646 +Subproject commit 93b7ac0613b9702427ec8314a6ef45dbfc3ea224 -- 2.39.5