From: Roland Häder Date: Thu, 1 Aug 2019 06:32:34 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=98afdbabc8c70e285b5882d155d5f96e5ed516da;p=hub.git Continued: - more rewrites of $packageData array to PackageData class ($packageInstance) - renamed NetworkPackage to NetworkPackageHandler and moved to proper namespace - updated 'core' Signed-off-by: Roland Häder --- 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/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php new file mode 100644 index 000000000..84aa1d584 --- /dev/null +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -0,0 +1,1579 @@ + + * @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 + * @todo Needs to add functionality for handling the object's type + * + * 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 NetworkPackageHandler extends BaseHubHandler implements Deliverable, Receivable, Registerable, Visitable { + /** + * Package mask for compressing package data: + * 0: Compressor extension + * 1: Raw package data + * 2: Tags, seperated by semicolons, no semicolon is required if only one tag is needed + * 3: Checksum + * 0 1 2 3 + */ + const PACKAGE_MASK = '%s%s%s%s%s%s%s'; + + /** + * Separator for the above mask + */ + const PACKAGE_MASK_SEPARATOR = '^'; + + /** + * Size of an array created by invoking + * explode(NetworkPackageHandler::PACKAGE_MASK_SEPARATOR, $content). + */ + const PACKAGE_CONTENT_ARRAY_SIZE = 4; + + /** + * Separator for checksum + */ + const PACKAGE_CHECKSUM_SEPARATOR = '_'; + + /** + * Array indexes for above mask, start with zero + */ + const INDEX_COMPRESSOR_EXTENSION = 0; + const INDEX_PACKAGE_DATA = 1; + const INDEX_TAGS = 2; + const INDEX_CHECKSUM = 3; + + /** + * Array indexes for raw package array + */ + const INDEX_PACKAGE_SENDER_ADDRESS = 0; + const INDEX_PACKAGE_SENDER_PORT = 1; + const INDEX_PACKAGE_RECIPIENT = 2; + const INDEX_PACKAGE_CONTENT = 3; + const INDEX_PACKAGE_STATUS = 4; + const INDEX_PACKAGE_HASH = 5; + const INDEX_PACKAGE_PRIVATE_KEY_HASH = 6; + + /** + * Size of the decoded data array + */ + const DECODED_DATA_ARRAY_SIZE = 6; + + /** + * Named array elements for decoded package content + */ + const PACKAGE_CONTENT_EXTENSION = 'compressor'; + const PACKAGE_CONTENT_MESSAGE = 'message'; + const PACKAGE_CONTENT_TAGS = 'tags'; + const PACKAGE_CONTENT_CHECKSUM = 'checksum'; + const PACKAGE_CONTENT_SENDER = 'sender'; + const PACKAGE_CONTENT_HASH = 'hash'; + const PACKAGE_CONTENT_PRIVATE_KEY_HASH = 'pkhash'; + + /** + * All package status + */ + const PACKAGE_STATUS_NEW = 'new'; + const PACKAGE_STATUS_FAILED = 'failed'; + const PACKAGE_STATUS_DECODED = 'decoded'; + const PACKAGE_STATUS_FAKED = 'faked'; + + /** + * Constants for message data array + */ + const MESSAGE_ARRAY_DATA = 'message_data'; + const MESSAGE_ARRAY_TYPE = 'message_type'; + const MESSAGE_ARRAY_SENDER = 'message_sender'; + const MESSAGE_ARRAY_HASH = 'message_hash'; + const MESSAGE_ARRAY_TAGS = 'message_tags'; + const MESSAGE_ARRAY_DATA_NODE_ID = 'node-id'; + + /** + * Generic answer status field + */ + + /** + * Tags separator + */ + const PACKAGE_TAGS_SEPARATOR = ';'; + + /** + * Raw package data separator + */ + const PACKAGE_DATA_SEPARATOR = '#'; + + /** + * Separator for more than one recipient + */ + const PACKAGE_RECIPIENT_SEPARATOR = ':'; + + /** + * Network target (alias): 'upper nodes' + */ + const NETWORK_TARGET_UPPER = 'upper'; + + /** + * Network target (alias): 'self' + */ + const NETWORK_TARGET_SELF = 'self'; + + /** + * Network target (alias): 'dht' + */ + const NETWORK_TARGET_DHT = 'dht'; + + /** + * TCP package size in bytes + */ + const TCP_PACKAGE_SIZE = 512; + + /************************************************************************** + * Stacker for out-going packages * + **************************************************************************/ + + /** + * Stacker name for "undeclared" packages + */ + const STACKER_NAME_UNDECLARED = 'package_undeclared'; + + /** + * Stacker name for "declared" packages (which are ready to send out) + */ + const STACKER_NAME_DECLARED = 'package_declared'; + + /** + * Stacker name for "out-going" packages + */ + const STACKER_NAME_OUTGOING = 'package_outgoing'; + + /************************************************************************** + * Stacker for incoming packages * + **************************************************************************/ + + /** + * Stacker name for "incoming" decoded raw data + */ + const STACKER_NAME_DECODED_INCOMING = 'package_decoded_data'; + + /** + * Stacker name for handled decoded raw data + */ + const STACKER_NAME_DECODED_HANDLED = 'package_handled_decoded'; + + /** + * Stacker name for "chunked" decoded raw data + */ + const STACKER_NAME_DECODED_CHUNKED = 'package_chunked_decoded'; + + /************************************************************************** + * Stacker for incoming messages * + **************************************************************************/ + + /** + * Stacker name for new messages + */ + const STACKER_NAME_NEW_MESSAGE = 'package_new_message'; + + /** + * Stacker name for processed messages + */ + const STACKER_NAME_PROCESSED_MESSAGE = 'package_processed_message'; + + /************************************************************************** + * Stacker for raw data handling * + **************************************************************************/ + + /** + * Stacker for outgoing data stream + */ + const STACKER_NAME_OUTGOING_STREAM = 'outgoing_stream'; + + /** + * Array index for final hash + */ + const RAW_INDEX_FINAL_HASH = 'hash'; + + /** + * Array index for encoded data + */ + const RAW_INDEX_ENCODED_DATA = 'data'; + + /** + * Array index for sent bytes + */ + const RAW_INDEX_SENT_BYTES = 'sent'; + + /** + * Array index for socket resource + */ + const RAW_INDEX_SOCKET_INSTANCE = 'socket'; + + /** + * Array index for buffer size + */ + const RAW_INDEX_BUFFER_SIZE = 'buffer'; + + /** + * Array index for diff between buffer and sent bytes + */ + const RAW_INDEX_DIFF = 'diff'; + + /************************************************************************** + * Protocol names * + **************************************************************************/ + const PROTOCOL_TCP = 'TCP'; + const PROTOCOL_UDP = 'UDP'; + + /************************************************************************** + * Exception codes * + **************************************************************************/ + const EXCEPTION_UNEXPECTED_PACKAGE_STATUS = 0xff00; + const EXCEPTION_INVALID_DATA_CHECKSUM = 0xff01; + + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // The socket needs to be put in a special registry that can handle such data + $registryInstance = SocketRegistryFactory::createSocketRegistryInstance(); + + // Set it here + $this->setRegistryInstance($registryInstance); + } + + /** + * Creates an instance of this class + * + * @param $compressorInstance A Compressor instance for compressing the content + * @return $packageInstance An instance of a Deliverable class + */ + public static final function createNetworkPackageHandler (Compressor $compressorInstance) { + // Get new instance + $packageInstance = new NetworkPackageHandler(); + + // Now set the compressor instance + $packageInstance->setCompressorInstance($compressorInstance); + + /* + * We need to initialize a stack here for our packages even for those + * which have no recipient address and stamp... ;-) This stacker will + * also be used for incoming raw data to handle it. + */ + $stackInstance = ObjectFactory::createObjectByConfiguredName('network_package_stacker_class'); + + // At last, set it in this class + $packageInstance->setStackInstance($stackInstance); + + // Init all stacker + $packageInstance->initStacks(); + + // Get a visitor instance for speeding up things and set it + $visitorInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_monitor_visitor_class'); + $packageInstance->setVisitorInstance($visitorInstance); + + // Get crypto instance and set it, too + $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class'); + $packageInstance->setCryptoInstance($cryptoInstance); + + // Get a singleton package assembler instance from factory and set it here, too + $assemblerInstance = PackageAssemblerFactory::createAssemblerInstance($packageInstance); + $packageInstance->setAssemblerInstance($assemblerInstance); + + // Get node instance + $nodeInstance = NodeObjectFactory::createNodeInstance(); + + // Get pool instance from node + $poolInstance = $nodeInstance->getListenerPoolInstance(); + + // And set it here + $packageInstance->setListenerPoolInstance($poolInstance); + + // Return the prepared instance + return $packageInstance; + } + + /** + * Initialize all stackers + * + * @param $forceReInit Whether to force reinitialization of all stacks + * @return void + */ + protected function initStacks ($forceReInit = FALSE) { + // Initialize all + $this->getStackInstance()->initStacks(array( + self::STACKER_NAME_UNDECLARED, + self::STACKER_NAME_DECLARED, + self::STACKER_NAME_OUTGOING, + self::STACKER_NAME_DECODED_INCOMING, + self::STACKER_NAME_DECODED_HANDLED, + self::STACKER_NAME_DECODED_CHUNKED, + self::STACKER_NAME_NEW_MESSAGE, + self::STACKER_NAME_PROCESSED_MESSAGE, + self::STACKER_NAME_OUTGOING_STREAM + ), $forceReInit); + } + + /** + * Determines private key hash from given session id + * + * @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 (DeliverablePackage $packageInstance) { + // Get DHT instance + $dhtInstance = DhtObjectFactory::createDhtInstance('node'); + + // Ask DHT for session id + $senderData = $dhtInstance->findNodeLocalBySessionId($packageInstance->getContentSender()); + + // Debugging: + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: senderData=%s', __METHOD__, __LINE__, print_r($senderData, TRUE))); + + // Is an entry found? + if (count($senderData) > 0) { + // Make sure the element 'private_key_hash' is there + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: senderData=' . print_r($senderData, TRUE)); + assert(isset($senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH])); + + // Don't accept empty hashes + if (empty($senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH])) { + // Don't accept + throw new InvalidPrivateKeyHashException(array($this, $senderData, 'empty hash in senderData'), self::EXCEPTION_INVALID_PRIVATE_KEY_HASH); + } // END - if + + // Return it + return $senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH]; + } // END - if + + // Don't accept empty keys + 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 $packageInstance->getContentPrivateKeyHash(); + } + + /** + * "Getter" for hash from given content + * + * @param $content Raw package content + * @return $hash Hash for given package content + */ + private function getHashFromContent ($content) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); + + // Create the hash + // @TODO md5() is very weak, but it needs to be fast + $hash = md5( + $content . + self::PACKAGE_CHECKSUM_SEPARATOR . + $this->getSessionId() . + self::PACKAGE_CHECKSUM_SEPARATOR . + $this->getCompressorInstance()->getCompressorExtension() + ); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',hash=' . $hash . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); + + // And return it + return $hash; + } + + /** + * Checks whether the checksum (sometimes called "hash") is the same + * + * @param $decodedContent Package raw content + * @param $packageInstance An instance of a DeliverablePackage class + * @return $isChecksumValid Whether the checksum is the same + */ + private function isChecksumValid (array $decodedContent, DeliverablePackage $packageInstance) { + // Get checksum + $checksum = $this->getHashFromContentSessionId($decodedContent, $packageInstance->getSenderAddress()); + + // Is it the same? + $isChecksumValid = ($checksum == $decodedContent[self::PACKAGE_CONTENT_CHECKSUM]); + + // Return it + return $isChecksumValid; + } + + /** + * Change the package with given status in given stack + * + * @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 (DeliverablePackage $packageInstance, $stackerName, $newStatus) { + // Skip this for empty stacks + if ($this->getStackInstance()->isStackEmpty($stackerName)) { + // This avoids an exception after all packages has failed + return; + } // END - if + + // Pop the entry (it should be it) + $nextData = $this->getStackInstance()->popNamed($stackerName); + + // Compare both hashes + assert($nextData->getPackageHash() == $packageInstance->getPackageHash()); + + // Temporary set the new status + $packageInstance->setPackageStatus($newStatus); + + // And push it again + $this->getStackInstance()->pushNamed($stackerName, $packageInstance); + } + + /** + * "Getter" for hash from given content and sender's session id + * + * @param $decodedContent Raw package content + * @param $sessionId Session id of the sender + * @return $hash Hash for given package content + */ + public function getHashFromContentSessionId (array $decodedContent, $sessionId) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($decodedContent[self::PACKAGE_CONTENT_MESSAGE]) . ',sender=' . $sessionId . ',compressor=' . $decodedContent[self::PACKAGE_CONTENT_EXTENSION]); + + // Create the hash + // @TODO md5() is very weak, but it needs to be fast + $hash = md5( + $decodedContent[self::PACKAGE_CONTENT_MESSAGE] . + self::PACKAGE_CHECKSUM_SEPARATOR . + $sessionId . + self::PACKAGE_CHECKSUM_SEPARATOR . + $decodedContent[self::PACKAGE_CONTENT_EXTENSION] + ); + + // And return it + return $hash; + } + + /////////////////////////////////////////////////////////////////////////// + // Delivering packages / raw data + /////////////////////////////////////////////////////////////////////////// + + /** + * Declares the given An instance of a DeliverablePackage class by discovering recipients + * + * @param $packageData An instance of a DeliverablePackage class in an array + * @return void + */ + 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 + * target because it causes an overload on the network and may be + * abused for attacking the network with large packages. + */ + $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance(); + + // Discover all recipients, this may throw an exception + $discoveryInstance->discoverRecipients($packageData); + + // Now get an iterator + $iteratorInstance = $discoveryInstance->getIterator(); + + // Make sure the iterator instance is valid + assert($iteratorInstance instanceof Iterator); + + // Rewind back to the beginning + $iteratorInstance->rewind(); + + // ... and begin iteration + while ($iteratorInstance->valid()) { + // Get current entry + $currentRecipient = $iteratorInstance->current(); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Setting recipient to ' . $currentRecipient . ',previous=' . packageInstance->getRecipient()); + + // Set the recipient + $packageInstance->setRecipient($currentRecipient); + + // Push the declared package to the next stack. + $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Package declared for recipient ' . $currentRecipient); + + // Skip to next entry + $iteratorInstance->next(); + } // END - while + + /* + * The recipient list can be cleaned up here because the package which + * shall be delivered has already been added for all entries from the + * list. + */ + $discoveryInstance->clearRecipients(); + } + + /** + * Delivers raw package data. In short, this will discover the raw socket + * resource through a discovery class (which will analyse the receipient of + * the package), register the socket with the connection (handler/helper?) + * instance and finally push the raw data on our outgoing queue. + * + * @param $packageData An instance of a DeliverablePackage class in an array + * @return void + */ + private function deliverRawPackageData (DeliverablePackage $packageInstance) { + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: packageData()=%d - CALLED!', count($packageData))); + + /* + * This package may become big, depending on the shared object size or + * delivered message size which shouldn't be so long (to save + * bandwidth). Because of the nature of the used protocol (TCP) we need + * to split it up into smaller pieces to fit it into a TCP frame. + * + * So first we need (again) a discovery class but now a protocol + * discovery to choose the right socket resource. The discovery class + * should take a look at the raw package data itself and then decide + * which (configurable!) protocol should be used for that type of + * package. + */ + $discoveryInstance = SocketDiscoveryFactory::createSocketDiscoveryInstance(); + + // Now discover the right socket instance from given package data + $socketInstance = $discoveryInstance->discoverSocket($packageData, StorableSocket::CONNECTION_TYPE_OUTGOING); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after discoverSocket() has been called.'); + + // Get the connection helper from registry + $helperInstance = GenericRegistry::getRegistry()->getInstance('connection'); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: stateInstance=' . $helperInstance->getStateInstance()); + + // And make sure it is valid + assert($helperInstance instanceof ConnectionHelper); + + // Get connection info class + $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($helperInstance->getProtocolName(), 'helper'); + + // Will the info instance with connection helper data + $infoInstance->fillWithConnectionHelperInformation($helperInstance); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.'); + + // Is it not there? + if (($socketInstance->isValidSocket()) && (!$this->getRegistryInstance()->isSocketRegistered($infoInstance, $socketInstance))) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Registering socket ' . $socketInstance . ' ...'); + + // Then register it + $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.'); + + // Shutdown the socket + $socketInstance->shutdownSocket(); + } + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.'); + + // Make sure the connection is up + $helperInstance->getStateInstance()->validatePeerStateConnected(); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.'); + + // Enqueue it again on the out-going queue, the connection is up and working at this point + $this->getStackInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageData); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after pushNamed() has been called.'); + } + + /** + * Sends waiting packages + * + * @param $packageData An instance of a DeliverablePackage class + * @return void + */ + private function sendOutgoingRawPackageData (DeliverablePackage $packageInstance) { + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: packageData()=%d - CALLED!', count($packageData))); + + // Init sent bytes + $sentBytes = 0; + + // Get the right connection instance + $infoInstance = $this->getRegistryInstance()->getInfoInstanceFromPackageInstance($packageData); + + // Test helper instance + assert($infoInstance instanceof ShareableInfo); + + // Get helper instance + $helperInstance = $infoInstance->getHelperInstance(); + + // Some sanity-checks on the object + //* DEBUG-DIE: */ die(': p1=' . $infoInstance->getProtocolName() . ',p2=' . $helperInstance->getProtocolName() . ',infoInstance=' . print_r($infoInstance, TRUE)); + assert($helperInstance instanceof ConnectionHelper); + assert($infoInstance->getProtocolName() == $helperInstance->getProtocolName()); + + // Is this connection still alive? + if ($helperInstance->isShuttedDown()) { + // This connection is shutting down + // @TODO We may want to do somthing more here? + return; + } // END - if + + // Sent out package data + $helperInstance->sendRawPackageData($packageData); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); + } + + /** + * Generates a secure hash for given raw package content and sender id + * + * @param $content An instance of a DeliverablePackage class + * @param $senderId Sender id to generate a hash for + * @return $hash Hash as hex-encoded string + */ + private function generatePackageHash ($content, $senderId) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content()=' . strlen($content) . ',senderId=' . $senderId . ' - CALLED!'); + + // Assert on variables + assert(!empty($content)); + assert(!empty($senderId)); + + // Is the feature enabled? + if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) { + // Feature is not enabled + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Feature "hubcoin_reward" not available, not generating package hash. Returning NULL ...'); + return NULL; + } // END - if + + // Fake array + $data = array( + self::PACKAGE_CONTENT_SENDER => $senderId, + 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))); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: hash=' . $hash . ' - EXIT!'); + + // Return it + return $hash; + } + + /** + * Checks whether the hash of given package data is 'valid', here that + * means it is the same or not. + * + * @param $decodedArray An array with 'decoded' (explode() was mostly called) data + * @return $isHashValid Whether the hash is valid + * @todo Unfinished area, hashes are currently NOT fully supported + */ + private function isPackageHashValid (array $decodedArray) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: decodedArray=' . print_r($decodedArray, TRUE) . ' - CALLED!'); + + // Make sure the required array elements are there + assert(isset($decodedArray[self::PACKAGE_CONTENT_SENDER])); + assert(isset($decodedArray[self::PACKAGE_CONTENT_MESSAGE])); + assert(isset($decodedArray[self::PACKAGE_CONTENT_HASH])); + assert(isset($decodedArray[self::PACKAGE_CONTENT_PRIVATE_KEY_HASH])); + + // Is the feature enabled? + if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) { + // Feature is not enabled, so hashes are always valid + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Feature "hubcoin_reward" not available, not checking hash. Returning TRUE ...'); + return TRUE; + } // END - if + + // Check validity + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: senderId=' . $decodedArray[self::PACKAGE_CONTENT_SENDER] . ',message()=' . strlen($decodedArray[self::PACKAGE_CONTENT_MESSAGE]) . ',hash=' . $decodedArray[self::PACKAGE_CONTENT_HASH]); + //* DEBUG-DIE: */ die(__METHOD__ . ': decodedArray=' . print_r($decodedArray, TRUE)); + $isHashValid = FrameworkFeature::callFeature('hubcoin_reward', 'checkHash', array($decodedArray[self::PACKAGE_CONTENT_SENDER] . ':' . $decodedArray[self::PACKAGE_CONTENT_MESSAGE] . ':' . $this->determineSenderPrivateKeyHash($decodedArray), $decodedArray[self::PACKAGE_CONTENT_HASH])); + + // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: isHashValid=' . intval($isHashValid) . ' - EXIT!'); + return $isHashValid; + } + + /** + * "Enqueues" raw content into this delivery class by reading the raw content + * from given helper's template instance and pushing it on the 'undeclared' + * stack. + * + * @param $helperInstance An instance of a HubHelper class + * @return void + */ + public function enqueueRawDataFromTemplate (HubHelper $helperInstance) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); + + // Get the raw content ... + $content = $helperInstance->getTemplateInstance()->getRawTemplateData(); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('content(' . strlen($content) . ')=' . $content); + + // ... and compress it + $compressed = $this->getCompressorInstance()->compressStream($content); + + // Add magic in front of it and hash behind it, including BASE64 encoding + $packageContent = sprintf(self::PACKAGE_MASK, + // 1.) Compressor's extension + $this->getCompressorInstance()->getCompressorExtension(), + // - separator + self::PACKAGE_MASK_SEPARATOR, + // 2.) Compressed raw package content, encoded with BASE64 + base64_encode($compressed), + // - separator + self::PACKAGE_MASK_SEPARATOR, + // 3.) Tags + implode(self::PACKAGE_TAGS_SEPARATOR, $helperInstance->getPackageTags()), + // - separator + self::PACKAGE_MASK_SEPARATOR, + // 4.) Checksum + $this->getHashFromContent($compressed) + ); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Enqueueing package for recipientType=' . $helperInstance->getRecipientType() . ' ...'); + + // Make sure required data is there + 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, $packageInstance); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); + } + + /** + * Checks whether a package has been enqueued for delivery. + * + * @return $isEnqueued Whether a package is enqueued + */ + public function isPackageEnqueued () { + // Check whether the stacker is not empty + $isEnqueued = (($this->getStackInstance()->isStackInitialized(self::STACKER_NAME_UNDECLARED)) && (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_UNDECLARED))); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: isEnqueued=%d - EXIT!', intval($isEnqueued))); + + // Return the result + return $isEnqueued; + } + + /** + * Checks whether a package has been declared + * + * @return $isDeclared Whether a package is declared + */ + public function isPackageDeclared () { + // Check whether the stacker is not empty + $isDeclared = (($this->getStackInstance()->isStackInitialized(self::STACKER_NAME_DECLARED)) && (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_DECLARED))); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: isDeclared=%d - EXIT!', intval($isDeclared))); + + // Return the result + return $isDeclared; + } + + /** + * Checks whether a package should be sent out + * + * @return $isWaitingDelivery Whether a package is waiting for delivery + */ + public function isPackageWaitingForDelivery () { + // Check whether the stacker is not empty + $isWaitingDelivery = (($this->getStackInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)) && (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING))); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: isWaitingDelivery=%d - EXIT!', intval($isWaitingDelivery))); + + // Return the result + return $isWaitingDelivery; + } + + /** + * Checks whether encoded (raw) data is pending + * + * @return $isPending Whether encoded data is pending + */ + public function isEncodedDataPending () { + // Check whether the stacker is not empty + $isPending = (($this->getStackInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING_STREAM)) && (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING_STREAM))); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: isPending=%d - EXIT!', intval($isPending))); + + // Return the result + return $isPending; + } + + /** + * Delivers an enqueued package to the stated destination. If a non-session + * id is provided, recipient resolver is being asked (and instanced once). + * This allows that a single package is being delivered to multiple targets + * without enqueueing it for every target. If no target is provided or it + * can't be determined a NoTargetException is being thrown. + * + * @return void + * @throws NoTargetException If no target can't be determined + */ + public function declareEnqueuedPackage () { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); + + // Make sure this method isn't working if there is no package enqueued + if (!$this->isPackageEnqueued()) { + // This is not fatal but should be avoided + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: No raw package data waiting declaration, but ' . __METHOD__ . ' has been called!'); + return; + } // END - if + + /* + * Now there are for sure packages to deliver, so start with the first + * one. + */ + $packageData = $this->getStackInstance()->popNamed(self::STACKER_NAME_UNDECLARED); + + // Declare the raw package data for delivery + $this->declareRawPackageData($packageData); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); + } + + /** + * Delivers the next declared package. Only one package per time will be sent + * because this may take time and slows down the whole delivery + * infrastructure. + * + * @return void + */ + public function processDeclaredPackage () { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); + + // Sanity check if we have packages declared + if (!$this->isPackageDeclared()) { + // This is not fatal but should be avoided + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: No package has been declared, but ' . __METHOD__ . ' has been called!'); + return; + } // END - if + + // 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($packageInstance); + + // And remove it finally + $this->getStackInstance()->popNamed(self::STACKER_NAME_DECLARED); + } catch (UnexpectedStateException $e) { + // The state is not excepected (shall be 'connected') + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Caught ' . $e->__toString() . ',message=' . $e->getMessage()); + + // Mark the package with status failed + $this->changePackageStatus($packageInstance, self::STACKER_NAME_DECLARED, self::PACKAGE_STATUS_FAILED); + } + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); + } + + /** + * Sends waiting packages out for delivery + * + * @return void + */ + public function sendWaitingPackage () { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); + + // Sanity check if we have packages waiting for delivery + if (!$this->isPackageWaitingForDelivery()) { + // This is not fatal but should be avoided + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: No package is waiting for delivery, but ' . __METHOD__ . ' was called.'); + return; + } // END - if + + // Get the package + $packageInstance = $this->getStackInstance()->getNamed(self::STACKER_NAME_OUTGOING); + + try { + // Now try to send it + $this->sendOutgoingRawPackageData($packageInstance); + + // And remove it finally + $this->getStackInstance()->popNamed(self::STACKER_NAME_OUTGOING); + } catch (InvalidSocketException $e) { + // Output exception message + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Package was not delivered: ' . $e->getMessage()); + + // Mark package as failed + $this->changePackageStatus($packageInstance, self::STACKER_NAME_OUTGOING, self::PACKAGE_STATUS_FAILED); + } + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); + } + + /** + * Sends out encoded data to a socket + * + * @return void + */ + public function sendEncodedData () { + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); + + // Make sure there is pending encoded data + assert($this->isEncodedDataPending()); + + // Pop current data from stack + $encodedDataArray = $this->getStackInstance()->popNamed(self::STACKER_NAME_OUTGOING_STREAM); + + // Init in this round sent bytes + $sentBytes = 0; + + // Assert on socket instance + assert(isset($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE])); + assert($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE] instanceof StorableSocket); + assert($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]->isValidSocket()); + + // Get socket instance and remove it from array + $socketInstance = $encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]; + $encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE] = NULL; + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: Calling socketInstance->writeBufferToSocketByArray(%d,%d) ...', count($encodedDataArray), $sentBytes)); + + // And deliver it + if (!$socketInstance->writeBufferToSocketByArray($encodedDataArray, $sentBytes)) { + // Something bad happened while writing to socket + $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0')); + } // END - if + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: sentBytes[%s]=%d', gettype($sentBytes), $sentBytes)); + + // If there was an error, don't continue here + if (($sentBytes === 0) && (strlen($encodedDataArray[self::RAW_INDEX_ENCODED_DATA]) > 0)) { + // Nothing sent means all data has been sent + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: All sent! (LINE=' . __LINE__ . ')'); + return; + } elseif (strlen($encodedDataArray[self::RAW_INDEX_ENCODED_DATA]) == 0) { + // Abort here, all sent! + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: All sent! (LINE=' . __LINE__ . ')'); + return; + } + + // Set it back + $encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE] = $socketInstance; + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Pushing raw data back to stacker, as still some data is pending delivery.'); + + // Push array back in stack + $this->getStackInstance()->pushNamed(self::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); + } + + /////////////////////////////////////////////////////////////////////////// + // Receiving packages / raw data + /////////////////////////////////////////////////////////////////////////// + + /** + * Checks whether decoded raw data is pending + * + * @return $isPending Whether decoded raw data is pending + */ + private function isRawDataPending () { + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); + + // Just return whether the stack is not empty + $isPending = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_DECODED_INCOMING)); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: isPending=%d - EXIT!', intval($isPending))); + + // Return the status + return $isPending; + } + + /** + * Checks whether new raw package data has arrived at a socket + * + * @return $hasArrived Whether new raw package data has arrived for processing + */ + public function isNewRawDataPending () { + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: Calling this->getListenerPoolInstance()->accept(%s) ... - CALLED!', $this->getVisitorInstance())); + + // Visit the pool. This monitors the pool for incoming raw data. + $this->getListenerPoolInstance()->accept($this->getVisitorInstance()); + + // Check for new data arrival + $hasArrived = $this->isRawDataPending(); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: hasArrived=%d - EXIT!', intval($hasArrived))); + + // Return the status + return $hasArrived; + } + + /** + * Handles the incoming decoded raw data. This method does not "convert" the + * decoded data back into a package array, it just "handles" it and pushs it + * on the next stack. + * + * @return void + */ + public function handleIncomingDecodedData () { + /* + * This method should only be called if decoded raw data is pending, + * so check it again. + */ + if (!$this->isRawDataPending()) { + // This is not fatal but should be avoided + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: No raw (decoded?) data is pending, but ' . __METHOD__ . ' has been called!'); + return; + } // END - if + + // Very noisy debug message: + //* 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 + $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. + */ + // Remove the last chunk SEPARATOR (because there is no need for it) + //* 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 + $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($packageInstance->getRawData()) . ' bytes to stack ' . self::STACKER_NAME_DECODED_HANDLED . ' ...'); + $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_HANDLED, $packageInstance); + } + + /** + * Adds raw decoded data from the given handler instance to this receiver + * + * @param $handlerInstance An instance of a Networkable class + * @return void + */ + public function addRawDataToIncomingStack (Networkable $handlerInstance) { + /* + * Get the decoded data from the handler, this is an array with + * 'raw_data' and 'error_code' as elements. + */ + $packageInstance = $handlerInstance->getNextRawData(); + + // Very noisy debug message: + //* 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, $packageInstance); + } + + /** + * Checks whether incoming decoded data is handled. + * + * @return $isHandled Whether incoming decoded data is handled + */ + public function isIncomingRawDataHandled () { + // Determine if the stack is not empty + $isHandled = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_DECODED_HANDLED)); + + // Return it + return $isHandled; + } + + /** + * Checks whether the assembler has pending data left + * + * @return $isHandled Whether the assembler has pending data left + */ + public function ifAssemblerHasPendingDataLeft () { + // Determine if the stack is not empty + $isHandled = (!$this->getAssemblerInstance()->isPendingDataEmpty()); + + // Return it + return $isHandled; + } + + /** + * Checks whether the assembler has multiple packages pending + * + * @return $isPending Whether the assembler has multiple packages pending + */ + public function ifMultipleMessagesPending () { + // Determine if the stack is not empty + $isPending = ($this->getAssemblerInstance()->ifMultipleMessagesPending()); + + // Return it + return $isPending; + } + + /** + * Handles the attached assemler's pending data queue to be finally + * assembled to the raw package data back. + * + * @return void + */ + public function handleAssemblerPendingData () { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Calling this->getAssemblerInstance()->handlePendingData() ...'); + + // Handle it + $this->getAssemblerInstance()->handlePendingData(); + } + + /** + * Handles multiple messages. + * + * @return void + */ + public function handleMultipleMessages () { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Calling this->getAssemblerInstance()->handleMultipleMessages() ...'); + + // Handle it + $this->getAssemblerInstance()->handleMultipleMessages(); + } + + /** + * Assembles incoming decoded data so it will become an abstract network + * package again. The assembler does later do it's job by an other task, + * not this one to keep best speed possible. + * + * @return void + */ + public function assembleDecodedDataToPackage () { + // Make sure the raw decoded package data is handled + assert($this->isIncomingRawDataHandled()); + + // Get current package content (an array with two elements; see handleIncomingDecodedData() for details) + $packageContent = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECODED_HANDLED); + + // Assert on some elements + assert( + (is_array($packageContent)) && + (isset($packageContent[HandleableRawData::PACKAGE_RAW_DATA])) && + (isset($packageContent[HandleableRawData::PACKAGE_ERROR_CODE])) + ); + + // Start assembling the raw package data array by chunking it + $this->getAssemblerInstance()->chunkPackageContent($packageContent); + + // Remove the package from 'handled_decoded' stack ... + $this->getStackInstance()->popNamed(self::STACKER_NAME_DECODED_HANDLED); + + // ... and push it on the 'chunked' stacker + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Pushing ' . strlen($packageContent[HandleableRawData::PACKAGE_RAW_DATA]) . ' bytes on stack ' . self::STACKER_NAME_DECODED_CHUNKED . ',packageContent=' . print_r($packageContent, TRUE)); + $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_CHUNKED, $packageContent); + } + + /** + * Accepts the visitor to process the visit "request" + * + * @param $visitorInstance An instance of a Visitor class + * @return void + */ + public function accept (Visitor $visitorInstance) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: ' . $visitorInstance->__toString() . ' has visited - CALLED!'); + + // Visit the package + $visitorInstance->visitNetworkPackageHandler($this); + + // Then visit the assembler to handle multiple packages + $this->getAssemblerInstance()->accept($visitorInstance); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: ' . $visitorInstance->__toString() . ' has visited - EXIT!'); + } + + /** + * Clears all stacks + * + * @return void + */ + public function clearAllStacks () { + // Call the init method to force re-initialization + $this->initStacks(TRUE); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: All stacker have been re-initialized.'); + } + + /** + * Removes the first failed outoging package from the stack to continue + * with next one (it will never work until the issue is fixed by you). + * + * @return void + * @throws UnexpectedPackageStatusException If the package status is not 'failed' + * @todo This may be enchanced for outgoing packages? + */ + public function removeFirstFailedPackage () { + // Get the package again + $packageInstance = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECLARED); + + // Is the package status 'failed'? + if ($packageInstance->getStatus() != self::PACKAGE_STATUS_FAILED) { + // Not failed! + throw new UnexpectedPackageStatusException(array($this, $packageInstance, self::PACKAGE_STATUS_FAILED), self::EXCEPTION_UNEXPECTED_PACKAGE_STATUS); + } // END - if + + // Remove this entry + $this->getStackInstance()->popNamed(self::STACKER_NAME_DECLARED); + } + + /** + * "Decode" the package content into the same array when it was sent. + * + * @param $rawPackageContent The raw package content to be "decoded" + * @return $packageInstance An instance of a DeliverablePackage class + */ + public function decodeRawContent ($rawPackageContent) { + // Use the separator '#' to "decode" it + $decodedArray = explode(self::PACKAGE_DATA_SEPARATOR, $rawPackageContent); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: decodedArray=' . print_r($decodedArray, TRUE)); + + // Assert on count (should be always 3) + assert(count($decodedArray) == self::DECODED_DATA_ARRAY_SIZE); + + // 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 $packageInstance; + } + + /** + * Handles decoded data for this node by "decoding" the 'content' part of + * it. Again this method uses explode() for the "decoding" process. + * + * @param $packageInstance An instance of a DeliverablePackage class + * @return void + * @throws InvalidDataChecksumException If the checksum doesn't match + */ + 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, $packageInstance->getContent()); + + // Assert on array count for a very basic validation + assert(count($decodedContent) == self::PACKAGE_CONTENT_ARRAY_SIZE); + + /* + * Convert the indexed array into an associative array. This is much + * better to remember than plain numbers, isn't it? + */ + $decodedContent = array( + // Compressor's extension used to compress the data + self::PACKAGE_CONTENT_EXTENSION => $decodedContent[self::INDEX_COMPRESSOR_EXTENSION], + // Package data (aka "message") in BASE64-decoded form but still compressed + self::PACKAGE_CONTENT_MESSAGE => base64_decode($decodedContent[self::INDEX_PACKAGE_DATA]), + // Tags as an indexed array for "tagging" the message + self::PACKAGE_CONTENT_TAGS => explode(self::PACKAGE_TAGS_SEPARATOR, $decodedContent[self::INDEX_TAGS]), + // Checksum of the _decoded_ data + self::PACKAGE_CONTENT_CHECKSUM => $decodedContent[self::INDEX_CHECKSUM], + // Sender's id + self::PACKAGE_CONTENT_SENDER => $packageInstance->getSenderAddress(), + // Hash from decoded raw data + self::PACKAGE_CONTENT_HASH => $packageInstance->getHash(), + // Hash of private key + self::PACKAGE_CONTENT_PRIVATE_KEY_HASH => $packageInstance->getPrivateKeyHash() + ); + + // Is the checksum valid? + if (!$this->isChecksumValid($decodedContent, $packageInstance)) { + // Is not the same, so throw an exception here + throw new InvalidDataChecksumException(array($this, $decodedContent, $packageInstance), self::EXCEPTION_INVALID_DATA_CHECKSUM); + } // END - if + + /* + * The checksum is the same, then it can be decompressed safely. The + * original message is at this point fully decoded. + */ + $decodedContent[self::PACKAGE_CONTENT_MESSAGE] = $this->getCompressorInstance()->decompressStream($decodedContent[self::PACKAGE_CONTENT_MESSAGE]); + + // And push it on the next stack + $this->getStackInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $decodedContent); + } + + /** + * Checks whether a new message has arrived + * + * @return $hasArrived Whether a new message has arrived for processing + */ + public function isNewMessageArrived () { + // Determine if the stack is not empty + $hasArrived = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_NEW_MESSAGE)); + + // Return it + return $hasArrived; + } + + /** + * Handles newly arrived messages + * + * @return void + * @todo Implement verification of all sent tags here? + */ + public function handleNewlyArrivedMessage () { + // Make sure there is at least one message + assert($this->isNewMessageArrived()); + + // Get it from the stacker, it is the full array with the decoded message + $decodedContent = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE); + + // Generate the hash of comparing it + if (!$this->isPackageHashValid($decodedContent)) { + // Is not valid, so throw an exception here + exit(__METHOD__ . ':INVALID HASH! UNDER CONSTRUCTION!' . chr(10)); + } // END - if + + // Now get a filter chain back from factory with given tags array + $chainInstance = PackageFilterChainFactory::createChainByTagsArray($decodedContent[self::PACKAGE_CONTENT_TAGS]); + + /* + * Process the message through all filters, note that all other + * elements from $decodedContent are no longer needed. + */ + $chainInstance->processMessage($decodedContent, $this); + + /* + * Post-processing of message data (this won't remove the message from + * the stack). + */ + $chainInstance->postProcessMessage($this); + } + + /** + * Checks whether a processed message is pending for "interpretation" + * + * @return $isPending Whether a processed message is pending + */ + public function isProcessedMessagePending () { + // Check it + $isPending = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_PROCESSED_MESSAGE)); + + // Return it + return $isPending; + } + + /** + * Handle processed messages by "interpreting" the 'message_type' element + * + * @return void + */ + public function handleProcessedMessage () { + // Get it from the stacker, it is the full array with the processed message + $messageArray = $this->getStackInstance()->popNamed(self::STACKER_NAME_PROCESSED_MESSAGE); + + // Add type for later easier handling + $messageArray[self::MESSAGE_ARRAY_DATA][self::MESSAGE_ARRAY_TYPE] = $messageArray[self::MESSAGE_ARRAY_TYPE]; + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: messageArray=' . print_r($messageArray, TRUE)); + + // Create a handler instance from given message type + $handlerInstance = MessageTypeHandlerFactory::createMessageTypeHandlerInstance($messageArray[self::MESSAGE_ARRAY_TYPE]); + + // Handle message data + $handlerInstance->handleMessageData($messageArray[self::MESSAGE_ARRAY_DATA], $this); + + // Post-handling of message data + $handlerInstance->postHandleMessageData($messageArray, $this); + } + + /** + * Feeds the hash and sender (as recipient for the 'sender' reward) to the + * miner's queue, unless the message is not a "reward claim" message as this + * leads to an endless loop. You may wish to run the miner to get some + * reward ("Hubcoins") for "mining" this hash. + * + * @param $messageData Array with message data + * @return void + * @todo ~10% done? + */ + public function feedHashToMiner (array $messageData) { + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: messageData()=%d - CALLED!', count($messageData))); + + // Is the feature enabled? + if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) { + /* + * Feature is not enabled, don't feed the hash to the miner as it + *may be invalid. + */ + return; + } // END - if + + // Make sure the required elements are there + assert(isset($messageData[self::MESSAGE_ARRAY_SENDER])); + assert(isset($messageData[self::MESSAGE_ARRAY_HASH])); + assert(isset($messageData[self::MESSAGE_ARRAY_DATA][self::MESSAGE_ARRAY_DATA_NODE_ID])); + + // Copy node id + $messageData[self::MESSAGE_ARRAY_DATA_NODE_ID] = $messageData[self::MESSAGE_ARRAY_DATA][self::MESSAGE_ARRAY_DATA_NODE_ID]; + + // Let's get rid of some fields that is not needed by the miner: + unset($messageData[self::MESSAGE_ARRAY_TYPE]); + unset($messageData[self::MESSAGE_ARRAY_DATA]); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: messageData=' . print_r($messageData, TRUE)); + + // Resolve session id ('sender' is a session id) into node id + $nodeId = HubTools::resolveNodeIdBySessionId($messageData[self::MESSAGE_ARRAY_SENDER]); + + // Is 'claim_reward' the message type? + if (in_array(Tagable::TAG_CLAIM_MINING_REWARD, $messageData[self::MESSAGE_ARRAY_TAGS])) { + /* + * Then don't feed this message to the miner as this causes an + * endless loop of mining. + */ + return; + } // END - if + + // Also remove tags as the miner don't need this. + unset($messageData[self::MESSAGE_ARRAY_TAGS]); + + // Unfinished stuff: + $this->partialStub(sprintf('[%s:%d]: @TODO nodeId=%s,messageData=%s', __METHOD__, __LINE__, $nodeId, print_r($messageData, TRUE))); + } + +} 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/class_NetworkPackage.php b/application/hub/classes/package/class_NetworkPackage.php deleted file mode 100644 index 23847d706..000000000 --- a/application/hub/classes/package/class_NetworkPackage.php +++ /dev/null @@ -1,1608 +0,0 @@ - - * @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 - * @todo Needs to add functionality for handling the object's type - * - * 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 NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, Registerable, Visitable { - /** - * Package mask for compressing package data: - * 0: Compressor extension - * 1: Raw package data - * 2: Tags, seperated by semicolons, no semicolon is required if only one tag is needed - * 3: Checksum - * 0 1 2 3 - */ - const PACKAGE_MASK = '%s%s%s%s%s%s%s'; - - /** - * Separator for the above mask - */ - const PACKAGE_MASK_SEPARATOR = '^'; - - /** - * Size of an array created by invoking - * explode(NetworkPackage::PACKAGE_MASK_SEPARATOR, $content). - */ - const PACKAGE_CONTENT_ARRAY_SIZE = 4; - - /** - * Separator for checksum - */ - const PACKAGE_CHECKSUM_SEPARATOR = '_'; - - /** - * Array indexes for above mask, start with zero - */ - const INDEX_COMPRESSOR_EXTENSION = 0; - const INDEX_PACKAGE_DATA = 1; - const INDEX_TAGS = 2; - const INDEX_CHECKSUM = 3; - - /** - * Array indexes for raw package array - */ - const INDEX_PACKAGE_SENDER_ADDRESS = 0; - const INDEX_PACKAGE_SENDER_PORT = 1; - const INDEX_PACKAGE_RECIPIENT = 2; - const INDEX_PACKAGE_CONTENT = 3; - const INDEX_PACKAGE_STATUS = 4; - const INDEX_PACKAGE_HASH = 5; - const INDEX_PACKAGE_PRIVATE_KEY_HASH = 6; - - /** - * Size of the decoded data array - */ - const DECODED_DATA_ARRAY_SIZE = 6; - - /** - * Named array elements for decoded package content - */ - const PACKAGE_CONTENT_EXTENSION = 'compressor'; - const PACKAGE_CONTENT_MESSAGE = 'message'; - const PACKAGE_CONTENT_TAGS = 'tags'; - const PACKAGE_CONTENT_CHECKSUM = 'checksum'; - const PACKAGE_CONTENT_SENDER = 'sender'; - 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 - */ - const PACKAGE_STATUS_NEW = 'new'; - const PACKAGE_STATUS_FAILED = 'failed'; - const PACKAGE_STATUS_DECODED = 'decoded'; - const PACKAGE_STATUS_FAKED = 'faked'; - - /** - * Constants for message data array - */ - const MESSAGE_ARRAY_DATA = 'message_data'; - const MESSAGE_ARRAY_TYPE = 'message_type'; - const MESSAGE_ARRAY_SENDER = 'message_sender'; - const MESSAGE_ARRAY_HASH = 'message_hash'; - const MESSAGE_ARRAY_TAGS = 'message_tags'; - const MESSAGE_ARRAY_DATA_NODE_ID = 'node-id'; - - /** - * Generic answer status field - */ - - /** - * Tags separator - */ - const PACKAGE_TAGS_SEPARATOR = ';'; - - /** - * Raw package data separator - */ - const PACKAGE_DATA_SEPARATOR = '#'; - - /** - * Separator for more than one recipient - */ - const PACKAGE_RECIPIENT_SEPARATOR = ':'; - - /** - * Network target (alias): 'upper nodes' - */ - const NETWORK_TARGET_UPPER = 'upper'; - - /** - * Network target (alias): 'self' - */ - const NETWORK_TARGET_SELF = 'self'; - - /** - * Network target (alias): 'dht' - */ - const NETWORK_TARGET_DHT = 'dht'; - - /** - * TCP package size in bytes - */ - const TCP_PACKAGE_SIZE = 512; - - /************************************************************************** - * Stacker for out-going packages * - **************************************************************************/ - - /** - * Stacker name for "undeclared" packages - */ - const STACKER_NAME_UNDECLARED = 'package_undeclared'; - - /** - * Stacker name for "declared" packages (which are ready to send out) - */ - const STACKER_NAME_DECLARED = 'package_declared'; - - /** - * Stacker name for "out-going" packages - */ - const STACKER_NAME_OUTGOING = 'package_outgoing'; - - /************************************************************************** - * Stacker for incoming packages * - **************************************************************************/ - - /** - * Stacker name for "incoming" decoded raw data - */ - const STACKER_NAME_DECODED_INCOMING = 'package_decoded_data'; - - /** - * Stacker name for handled decoded raw data - */ - const STACKER_NAME_DECODED_HANDLED = 'package_handled_decoded'; - - /** - * Stacker name for "chunked" decoded raw data - */ - const STACKER_NAME_DECODED_CHUNKED = 'package_chunked_decoded'; - - /************************************************************************** - * Stacker for incoming messages * - **************************************************************************/ - - /** - * Stacker name for new messages - */ - const STACKER_NAME_NEW_MESSAGE = 'package_new_message'; - - /** - * Stacker name for processed messages - */ - const STACKER_NAME_PROCESSED_MESSAGE = 'package_processed_message'; - - /************************************************************************** - * Stacker for raw data handling * - **************************************************************************/ - - /** - * Stacker for outgoing data stream - */ - const STACKER_NAME_OUTGOING_STREAM = 'outgoing_stream'; - - /** - * Array index for final hash - */ - const RAW_INDEX_FINAL_HASH = 'hash'; - - /** - * Array index for encoded data - */ - const RAW_INDEX_ENCODED_DATA = 'data'; - - /** - * Array index for sent bytes - */ - const RAW_INDEX_SENT_BYTES = 'sent'; - - /** - * Array index for socket resource - */ - const RAW_INDEX_SOCKET_INSTANCE = 'socket'; - - /** - * Array index for buffer size - */ - const RAW_INDEX_BUFFER_SIZE = 'buffer'; - - /** - * Array index for diff between buffer and sent bytes - */ - const RAW_INDEX_DIFF = 'diff'; - - /************************************************************************** - * Protocol names * - **************************************************************************/ - const PROTOCOL_TCP = 'TCP'; - const PROTOCOL_UDP = 'UDP'; - - /************************************************************************** - * Exception codes * - **************************************************************************/ - const EXCEPTION_UNEXPECTED_PACKAGE_STATUS = 0xff00; - const EXCEPTION_INVALID_DATA_CHECKSUM = 0xff01; - - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // The socket needs to be put in a special registry that can handle such data - $registryInstance = SocketRegistryFactory::createSocketRegistryInstance(); - - // Set it here - $this->setRegistryInstance($registryInstance); - } - - /** - * Creates an instance of this class - * - * @param $compressorInstance A Compressor instance for compressing the content - * @return $packageInstance An instance of a Deliverable class - */ - public static final function createNetworkPackage (Compressor $compressorInstance) { - // Get new instance - $packageInstance = new NetworkPackage(); - - // Now set the compressor instance - $packageInstance->setCompressorInstance($compressorInstance); - - /* - * We need to initialize a stack here for our packages even for those - * which have no recipient address and stamp... ;-) This stacker will - * also be used for incoming raw data to handle it. - */ - $stackInstance = ObjectFactory::createObjectByConfiguredName('network_package_stacker_class'); - - // At last, set it in this class - $packageInstance->setStackInstance($stackInstance); - - // Init all stacker - $packageInstance->initStacks(); - - // Get a visitor instance for speeding up things and set it - $visitorInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_monitor_visitor_class'); - $packageInstance->setVisitorInstance($visitorInstance); - - // Get crypto instance and set it, too - $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class'); - $packageInstance->setCryptoInstance($cryptoInstance); - - // Get a singleton package assembler instance from factory and set it here, too - $assemblerInstance = PackageAssemblerFactory::createAssemblerInstance($packageInstance); - $packageInstance->setAssemblerInstance($assemblerInstance); - - // Get node instance - $nodeInstance = NodeObjectFactory::createNodeInstance(); - - // Get pool instance from node - $poolInstance = $nodeInstance->getListenerPoolInstance(); - - // And set it here - $packageInstance->setListenerPoolInstance($poolInstance); - - // Return the prepared instance - return $packageInstance; - } - - /** - * Initialize all stackers - * - * @param $forceReInit Whether to force reinitialization of all stacks - * @return void - */ - protected function initStacks ($forceReInit = FALSE) { - // Initialize all - $this->getStackInstance()->initStacks(array( - self::STACKER_NAME_UNDECLARED, - self::STACKER_NAME_DECLARED, - self::STACKER_NAME_OUTGOING, - self::STACKER_NAME_DECODED_INCOMING, - self::STACKER_NAME_DECODED_HANDLED, - self::STACKER_NAME_DECODED_CHUNKED, - self::STACKER_NAME_NEW_MESSAGE, - self::STACKER_NAME_PROCESSED_MESSAGE, - self::STACKER_NAME_OUTGOING_STREAM - ), $forceReInit); - } - - /** - * Determines private key hash from given session id - * - * @param $decodedData Array with decoded data - * @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])); - - // Get DHT instance - $dhtInstance = DhtObjectFactory::createDhtInstance('node'); - - // Ask DHT for session id - $senderData = $dhtInstance->findNodeLocalBySessionId($decodedData[self::PACKAGE_CONTENT_SENDER]); - - // Debugging: - //* DEBUG-DIE: */ die(sprintf('[%s:%d]: senderData=%s', __METHOD__, __LINE__, print_r($senderData, TRUE))); - - // Is an entry found? - if (count($senderData) > 0) { - // Make sure the element 'private_key_hash' is there - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: senderData=' . print_r($senderData, TRUE)); - assert(isset($senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH])); - - // Don't accept empty hashes - if (empty($senderData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH])) { - // Don't accept - throw new InvalidPrivateKeyHashException(array($this, $senderData, 'empty hash in senderData'), self::EXCEPTION_INVALID_PRIVATE_KEY_HASH); - } // END - if - - // Return it - 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])) { - // 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]; - } - - /** - * "Getter" for hash from given content - * - * @param $content Raw package content - * @return $hash Hash for given package content - */ - private function getHashFromContent ($content) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); - - // Create the hash - // @TODO md5() is very weak, but it needs to be fast - $hash = md5( - $content . - self::PACKAGE_CHECKSUM_SEPARATOR . - $this->getSessionId() . - self::PACKAGE_CHECKSUM_SEPARATOR . - $this->getCompressorInstance()->getCompressorExtension() - ); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($content) . ',sender=' . $this->getSessionId() . ',hash=' . $hash . ',compressor=' . $this->getCompressorInstance()->getCompressorExtension()); - - // And return it - return $hash; - } - - /** - * Checks whether the checksum (sometimes called "hash") is the same - * - * @param $decodedContent Package raw content - * @param $decodedData Whole raw package data array - * @return $isChecksumValid Whether the checksum is the same - */ - private function isChecksumValid (array $decodedContent, array $decodedData) { - // Get checksum - $checksum = $this->getHashFromContentSessionId($decodedContent, $decodedData[self::PACKAGE_DATA_SENDER_ADDRESS]); - - // Is it the same? - $isChecksumValid = ($checksum == $decodedContent[self::PACKAGE_CONTENT_CHECKSUM]); - - // Return it - return $isChecksumValid; - } - - /** - * Change the package with given status in given stack - * - * @param $packageData Raw package data in an array - * @param $stackerName Name of the stacker - * @param $newStatus New status to set - * @return void - */ - private function changePackageStatus (array $packageData, $stackerName, $newStatus) { - // Skip this for empty stacks - if ($this->getStackInstance()->isStackEmpty($stackerName)) { - // This avoids an exception after all packages has failed - return; - } // END - if - - // Pop the entry (it should be it) - $nextData = $this->getStackInstance()->popNamed($stackerName); - - // Compare both hashes - assert($nextData[self::PACKAGE_DATA_HASH] == $packageData[self::PACKAGE_DATA_HASH]); - - // Temporary set the new status - $packageData[self::PACKAGE_DATA_STATUS] = $newStatus; - - // And push it again - $this->getStackInstance()->pushNamed($stackerName, $packageData); - } - - /** - * "Getter" for hash from given content and sender's session id - * - * @param $decodedContent Raw package content - * @param $sessionId Session id of the sender - * @return $hash Hash for given package content - */ - public function getHashFromContentSessionId (array $decodedContent, $sessionId) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content[md5]=' . md5($decodedContent[self::PACKAGE_CONTENT_MESSAGE]) . ',sender=' . $sessionId . ',compressor=' . $decodedContent[self::PACKAGE_CONTENT_EXTENSION]); - - // Create the hash - // @TODO md5() is very weak, but it needs to be fast - $hash = md5( - $decodedContent[self::PACKAGE_CONTENT_MESSAGE] . - self::PACKAGE_CHECKSUM_SEPARATOR . - $sessionId . - self::PACKAGE_CHECKSUM_SEPARATOR . - $decodedContent[self::PACKAGE_CONTENT_EXTENSION] - ); - - // And return it - return $hash; - } - - /////////////////////////////////////////////////////////////////////////// - // Delivering packages / raw data - /////////////////////////////////////////////////////////////////////////// - - /** - * Declares the given raw package data by discovering recipients - * - * @param $packageData Raw package data in an array - * @return void - */ - private function declareRawPackageData (array $packageData) { - // Make sure the required field is there - assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT])); - - /* - * 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 - * target because it causes an overload on the network and may be - * abused for attacking the network with large packages. - */ - $discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance(); - - // Discover all recipients, this may throw an exception - $discoveryInstance->discoverRecipients($packageData); - - // Now get an iterator - $iteratorInstance = $discoveryInstance->getIterator(); - - // Make sure the iterator instance is valid - assert($iteratorInstance instanceof Iterator); - - // Rewind back to the beginning - $iteratorInstance->rewind(); - - // ... and begin iteration - while ($iteratorInstance->valid()) { - // Get current entry - $currentRecipient = $iteratorInstance->current(); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Setting recipient to ' . $currentRecipient . ',previous=' . $packageData[self::PACKAGE_DATA_RECIPIENT]); - - // Set the recipient - $packageData[self::PACKAGE_DATA_RECIPIENT] = $currentRecipient; - - // Push the declared package to the next stack. - $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Package declared for recipient ' . $currentRecipient); - - // Skip to next entry - $iteratorInstance->next(); - } // END - while - - /* - * The recipient list can be cleaned up here because the package which - * shall be delivered has already been added for all entries from the - * list. - */ - $discoveryInstance->clearRecipients(); - } - - /** - * Delivers raw package data. In short, this will discover the raw socket - * resource through a discovery class (which will analyse the receipient of - * 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 - * @return void - */ - private function deliverRawPackageData (array $packageData) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: packageData()=%d - CALLED!', count($packageData))); - - /* - * This package may become big, depending on the shared object size or - * delivered message size which shouldn't be so long (to save - * bandwidth). Because of the nature of the used protocol (TCP) we need - * to split it up into smaller pieces to fit it into a TCP frame. - * - * So first we need (again) a discovery class but now a protocol - * discovery to choose the right socket resource. The discovery class - * should take a look at the raw package data itself and then decide - * which (configurable!) protocol should be used for that type of - * package. - */ - $discoveryInstance = SocketDiscoveryFactory::createSocketDiscoveryInstance(); - - // Now discover the right socket instance from given package data - $socketInstance = $discoveryInstance->discoverSocket($packageData, StorableSocket::CONNECTION_TYPE_OUTGOING); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after discoverSocket() has been called.'); - - // Get the connection helper from registry - $helperInstance = GenericRegistry::getRegistry()->getInstance('connection'); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: stateInstance=' . $helperInstance->getStateInstance()); - - // And make sure it is valid - assert($helperInstance instanceof ConnectionHelper); - - // Get connection info class - $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($helperInstance->getProtocolName(), 'helper'); - - // Will the info instance with connection helper data - $infoInstance->fillWithConnectionHelperInformation($helperInstance); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.'); - - // Is it not there? - if (($socketInstance->isValidSocket()) && (!$this->getRegistryInstance()->isSocketRegistered($infoInstance, $socketInstance))) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Registering socket ' . $socketInstance . ' ...'); - - // Then register it - $this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance, $packageData); - } 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.'); - - // Shutdown the socket - $socketInstance->shutdownSocket(); - } - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.'); - - // Make sure the connection is up - $helperInstance->getStateInstance()->validatePeerStateConnected(); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.'); - - // Enqueue it again on the out-going queue, the connection is up and working at this point - $this->getStackInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageData); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after pushNamed() has been called.'); - } - - /** - * Sends waiting packages - * - * @param $packageData Raw package data - * @return void - */ - private function sendOutgoingRawPackageData (array $packageData) { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: packageData()=%d - CALLED!', count($packageData))); - - // Init sent bytes - $sentBytes = 0; - - // Get the right connection instance - $infoInstance = $this->getRegistryInstance()->getInfoInstanceFromPackageData($packageData); - - // Test helper instance - assert($infoInstance instanceof ShareableInfo); - - // Get helper instance - $helperInstance = $infoInstance->getHelperInstance(); - - // Some sanity-checks on the object - //* DEBUG-DIE: */ die(': p1=' . $infoInstance->getProtocolName() . ',p2=' . $helperInstance->getProtocolName() . ',infoInstance=' . print_r($infoInstance, TRUE)); - assert($helperInstance instanceof ConnectionHelper); - assert($infoInstance->getProtocolName() == $helperInstance->getProtocolName()); - - // Is this connection still alive? - if ($helperInstance->isShuttedDown()) { - // This connection is shutting down - // @TODO We may want to do somthing more here? - return; - } // END - if - - // Sent out package data - $helperInstance->sendRawPackageData($packageData); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); - } - - /** - * Generates a secure hash for given raw package content and sender id - * - * @param $content Raw package data - * @param $senderId Sender id to generate a hash for - * @return $hash Hash as hex-encoded string - */ - private function generatePackageHash ($content, $senderId) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: content()=' . strlen($content) . ',senderId=' . $senderId . ' - CALLED!'); - - // Assert on variables - assert(!empty($content)); - assert(!empty($senderId)); - - // Is the feature enabled? - if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) { - // Feature is not enabled - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Feature "hubcoin_reward" not available, not generating package hash. Returning NULL ...'); - return NULL; - } // END - if - - // Fake array - $data = array( - self::PACKAGE_CONTENT_SENDER => $senderId, - 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))); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: hash=' . $hash . ' - EXIT!'); - - // Return it - return $hash; - } - - /** - * Checks whether the hash of given package data is 'valid', here that - * means it is the same or not. - * - * @param $decodedArray An array with 'decoded' (explode() was mostly called) data - * @return $isHashValid Whether the hash is valid - * @todo Unfinished area, hashes are currently NOT fully supported - */ - private function isPackageHashValid (array $decodedArray) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: decodedArray=' . print_r($decodedArray, TRUE) . ' - CALLED!'); - - // Make sure the required array elements are there - assert(isset($decodedArray[self::PACKAGE_CONTENT_SENDER])); - assert(isset($decodedArray[self::PACKAGE_CONTENT_MESSAGE])); - assert(isset($decodedArray[self::PACKAGE_CONTENT_HASH])); - assert(isset($decodedArray[self::PACKAGE_CONTENT_PRIVATE_KEY_HASH])); - - // Is the feature enabled? - if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) { - // Feature is not enabled, so hashes are always valid - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Feature "hubcoin_reward" not available, not checking hash. Returning TRUE ...'); - return TRUE; - } // END - if - - // Check validity - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: senderId=' . $decodedArray[self::PACKAGE_CONTENT_SENDER] . ',message()=' . strlen($decodedArray[self::PACKAGE_CONTENT_MESSAGE]) . ',hash=' . $decodedArray[self::PACKAGE_CONTENT_HASH]); - //* DEBUG-DIE: */ die(__METHOD__ . ': decodedArray=' . print_r($decodedArray, TRUE)); - $isHashValid = FrameworkFeature::callFeature('hubcoin_reward', 'checkHash', array($decodedArray[self::PACKAGE_CONTENT_SENDER] . ':' . $decodedArray[self::PACKAGE_CONTENT_MESSAGE] . ':' . $this->determineSenderPrivateKeyHash($decodedArray), $decodedArray[self::PACKAGE_CONTENT_HASH])); - - // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: isHashValid=' . intval($isHashValid) . ' - EXIT!'); - return $isHashValid; - } - - /** - * "Enqueues" raw content into this delivery class by reading the raw content - * from given helper's template instance and pushing it on the 'undeclared' - * stack. - * - * @param $helperInstance An instance of a HubHelper class - * @return void - */ - public function enqueueRawDataFromTemplate (HubHelper $helperInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); - - // Get the raw content ... - $content = $helperInstance->getTemplateInstance()->getRawTemplateData(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('content(' . strlen($content) . ')=' . $content); - - // ... and compress it - $compressed = $this->getCompressorInstance()->compressStream($content); - - // Add magic in front of it and hash behind it, including BASE64 encoding - $packageContent = sprintf(self::PACKAGE_MASK, - // 1.) Compressor's extension - $this->getCompressorInstance()->getCompressorExtension(), - // - separator - self::PACKAGE_MASK_SEPARATOR, - // 2.) Compressed raw package content, encoded with BASE64 - base64_encode($compressed), - // - separator - self::PACKAGE_MASK_SEPARATOR, - // 3.) Tags - implode(self::PACKAGE_TAGS_SEPARATOR, $helperInstance->getPackageTags()), - // - separator - self::PACKAGE_MASK_SEPARATOR, - // 4.) Checksum - $this->getHashFromContent($compressed) - ); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Enqueueing package for recipientType=' . $helperInstance->getRecipientType() . ' ...'); - - // Make sure required data is there - assert(!empty($content)); - assert($this->getSessionId() != ''); - - // 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(), - )); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); - } - - /** - * Checks whether a package has been enqueued for delivery. - * - * @return $isEnqueued Whether a package is enqueued - */ - public function isPackageEnqueued () { - // Check whether the stacker is not empty - $isEnqueued = (($this->getStackInstance()->isStackInitialized(self::STACKER_NAME_UNDECLARED)) && (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_UNDECLARED))); - - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: isEnqueued=%d - EXIT!', intval($isEnqueued))); - - // Return the result - return $isEnqueued; - } - - /** - * Checks whether a package has been declared - * - * @return $isDeclared Whether a package is declared - */ - public function isPackageDeclared () { - // Check whether the stacker is not empty - $isDeclared = (($this->getStackInstance()->isStackInitialized(self::STACKER_NAME_DECLARED)) && (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_DECLARED))); - - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: isDeclared=%d - EXIT!', intval($isDeclared))); - - // Return the result - return $isDeclared; - } - - /** - * Checks whether a package should be sent out - * - * @return $isWaitingDelivery Whether a package is waiting for delivery - */ - public function isPackageWaitingForDelivery () { - // Check whether the stacker is not empty - $isWaitingDelivery = (($this->getStackInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)) && (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING))); - - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: isWaitingDelivery=%d - EXIT!', intval($isWaitingDelivery))); - - // Return the result - return $isWaitingDelivery; - } - - /** - * Checks whether encoded (raw) data is pending - * - * @return $isPending Whether encoded data is pending - */ - public function isEncodedDataPending () { - // Check whether the stacker is not empty - $isPending = (($this->getStackInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING_STREAM)) && (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING_STREAM))); - - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: isPending=%d - EXIT!', intval($isPending))); - - // Return the result - return $isPending; - } - - /** - * Delivers an enqueued package to the stated destination. If a non-session - * id is provided, recipient resolver is being asked (and instanced once). - * This allows that a single package is being delivered to multiple targets - * without enqueueing it for every target. If no target is provided or it - * can't be determined a NoTargetException is being thrown. - * - * @return void - * @throws NoTargetException If no target can't be determined - */ - public function declareEnqueuedPackage () { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); - - // Make sure this method isn't working if there is no package enqueued - if (!$this->isPackageEnqueued()) { - // This is not fatal but should be avoided - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: No raw package data waiting declaration, but ' . __METHOD__ . ' has been called!'); - return; - } // END - if - - /* - * Now there are for sure packages to deliver, so start with the first - * one. - */ - $packageData = $this->getStackInstance()->popNamed(self::STACKER_NAME_UNDECLARED); - - // Declare the raw package data for delivery - $this->declareRawPackageData($packageData); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); - } - - /** - * Delivers the next declared package. Only one package per time will be sent - * because this may take time and slows down the whole delivery - * infrastructure. - * - * @return void - */ - public function processDeclaredPackage () { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); - - // Sanity check if we have packages declared - if (!$this->isPackageDeclared()) { - // This is not fatal but should be avoided - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: No package has been declared, but ' . __METHOD__ . ' has been called!'); - return; - } // END - if - - // Get the package - $packageData = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECLARED); - - // Assert on it - assert(isset($packageData[self::PACKAGE_DATA_RECIPIENT])); - - // Try to deliver the package - try { - // And try to send it - $this->deliverRawPackageData($packageData); - - // And remove it finally - $this->getStackInstance()->popNamed(self::STACKER_NAME_DECLARED); - } catch (UnexpectedStateException $e) { - // The state is not excepected (shall be 'connected') - 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); - } - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); - } - - /** - * Sends waiting packages out for delivery - * - * @return void - */ - public function sendWaitingPackage () { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); - - // Sanity check if we have packages waiting for delivery - if (!$this->isPackageWaitingForDelivery()) { - // This is not fatal but should be avoided - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: No package is waiting for delivery, but ' . __METHOD__ . ' was called.'); - return; - } // END - if - - // Get the package - $packageData = $this->getStackInstance()->getNamed(self::STACKER_NAME_OUTGOING); - - try { - // Now try to send it - $this->sendOutgoingRawPackageData($packageData); - - // And remove it finally - $this->getStackInstance()->popNamed(self::STACKER_NAME_OUTGOING); - } catch (InvalidSocketException $e) { - // Output exception message - 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); - } - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); - } - - /** - * Sends out encoded data to a socket - * - * @return void - */ - public function sendEncodedData () { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); - - // Make sure there is pending encoded data - assert($this->isEncodedDataPending()); - - // Pop current data from stack - $encodedDataArray = $this->getStackInstance()->popNamed(self::STACKER_NAME_OUTGOING_STREAM); - - // Init in this round sent bytes - $sentBytes = 0; - - // Assert on socket instance - assert(isset($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE])); - assert($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE] instanceof StorableSocket); - assert($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]->isValidSocket()); - - // Get socket instance and remove it from array - $socketInstance = $encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]; - $encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE] = NULL; - - // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: Calling socketInstance->writeBufferToSocketByArray(%d,%d) ...', count($encodedDataArray), $sentBytes)); - - // And deliver it - if (!$socketInstance->writeBufferToSocketByArray($encodedDataArray, $sentBytes)) { - // Something bad happened while writing to socket - $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0')); - } // END - if - - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: sentBytes[%s]=%d', gettype($sentBytes), $sentBytes)); - - // If there was an error, don't continue here - if (($sentBytes === 0) && (strlen($encodedDataArray[self::RAW_INDEX_ENCODED_DATA]) > 0)) { - // Nothing sent means all data has been sent - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: All sent! (LINE=' . __LINE__ . ')'); - return; - } elseif (strlen($encodedDataArray[self::RAW_INDEX_ENCODED_DATA]) == 0) { - // Abort here, all sent! - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: All sent! (LINE=' . __LINE__ . ')'); - return; - } - - // Set it back - $encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE] = $socketInstance; - - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Pushing raw data back to stacker, as still some data is pending delivery.'); - - // Push array back in stack - $this->getStackInstance()->pushNamed(self::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray); - - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: EXIT!'); - } - - /////////////////////////////////////////////////////////////////////////// - // Receiving packages / raw data - /////////////////////////////////////////////////////////////////////////// - - /** - * Checks whether decoded raw data is pending - * - * @return $isPending Whether decoded raw data is pending - */ - private function isRawDataPending () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: CALLED!'); - - // Just return whether the stack is not empty - $isPending = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_DECODED_INCOMING)); - - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: isPending=%d - EXIT!', intval($isPending))); - - // Return the status - return $isPending; - } - - /** - * Checks whether new raw package data has arrived at a socket - * - * @return $hasArrived Whether new raw package data has arrived for processing - */ - public function isNewRawDataPending () { - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: Calling this->getListenerPoolInstance()->accept(%s) ... - CALLED!', $this->getVisitorInstance())); - - // Visit the pool. This monitors the pool for incoming raw data. - $this->getListenerPoolInstance()->accept($this->getVisitorInstance()); - - // Check for new data arrival - $hasArrived = $this->isRawDataPending(); - - // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: hasArrived=%d - EXIT!', intval($hasArrived))); - - // Return the status - return $hasArrived; - } - - /** - * Handles the incoming decoded raw data. This method does not "convert" the - * decoded data back into a package array, it just "handles" it and pushs it - * on the next stack. - * - * @return void - */ - public function handleIncomingDecodedData () { - /* - * This method should only be called if decoded raw data is pending, - * so check it again. - */ - if (!$this->isRawDataPending()) { - // This is not fatal but should be avoided - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: No raw (decoded?) data is pending, but ' . __METHOD__ . ' has been called!'); - return; - } // END - if - - // Very noisy debug message: - //* 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])) - ); - - /* - * 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) { - // It is there and should be removed - $decodedData[HandleableRawData::PACKAGE_RAW_DATA] = substr($decodedData[HandleableRawData::PACKAGE_RAW_DATA], 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); - } - - /** - * Adds raw decoded data from the given handler instance to this receiver - * - * @param $handlerInstance An instance of a Networkable class - * @return void - */ - public function addRawDataToIncomingStack (Networkable $handlerInstance) { - /* - * Get the decoded data from the handler, this is an array with - * 'raw_data' and 'error_code' as elements. - */ - $decodedData = $handlerInstance->getNextRawData(); - - // Very noisy debug message: - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: decodedData[' . gettype($decodedData) . ']=' . print_r($decodedData, TRUE)); - - // And push it on our stack - $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_INCOMING, $decodedData); - } - - /** - * Checks whether incoming decoded data is handled. - * - * @return $isHandled Whether incoming decoded data is handled - */ - public function isIncomingRawDataHandled () { - // Determine if the stack is not empty - $isHandled = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_DECODED_HANDLED)); - - // Return it - return $isHandled; - } - - /** - * Checks whether the assembler has pending data left - * - * @return $isHandled Whether the assembler has pending data left - */ - public function ifAssemblerHasPendingDataLeft () { - // Determine if the stack is not empty - $isHandled = (!$this->getAssemblerInstance()->isPendingDataEmpty()); - - // Return it - return $isHandled; - } - - /** - * Checks whether the assembler has multiple packages pending - * - * @return $isPending Whether the assembler has multiple packages pending - */ - public function ifMultipleMessagesPending () { - // Determine if the stack is not empty - $isPending = ($this->getAssemblerInstance()->ifMultipleMessagesPending()); - - // Return it - return $isPending; - } - - /** - * Handles the attached assemler's pending data queue to be finally - * assembled to the raw package data back. - * - * @return void - */ - public function handleAssemblerPendingData () { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Calling this->getAssemblerInstance()->handlePendingData() ...'); - - // Handle it - $this->getAssemblerInstance()->handlePendingData(); - } - - /** - * Handles multiple messages. - * - * @return void - */ - public function handleMultipleMessages () { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Calling this->getAssemblerInstance()->handleMultipleMessages() ...'); - - // Handle it - $this->getAssemblerInstance()->handleMultipleMessages(); - } - - /** - * Assembles incoming decoded data so it will become an abstract network - * package again. The assembler does later do it's job by an other task, - * not this one to keep best speed possible. - * - * @return void - */ - public function assembleDecodedDataToPackage () { - // Make sure the raw decoded package data is handled - assert($this->isIncomingRawDataHandled()); - - // Get current package content (an array with two elements; see handleIncomingDecodedData() for details) - $packageContent = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECODED_HANDLED); - - // Assert on some elements - assert( - (is_array($packageContent)) && - (isset($packageContent[HandleableRawData::PACKAGE_RAW_DATA])) && - (isset($packageContent[HandleableRawData::PACKAGE_ERROR_CODE])) - ); - - // Start assembling the raw package data array by chunking it - $this->getAssemblerInstance()->chunkPackageContent($packageContent); - - // Remove the package from 'handled_decoded' stack ... - $this->getStackInstance()->popNamed(self::STACKER_NAME_DECODED_HANDLED); - - // ... and push it on the 'chunked' stacker - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: Pushing ' . strlen($packageContent[HandleableRawData::PACKAGE_RAW_DATA]) . ' bytes on stack ' . self::STACKER_NAME_DECODED_CHUNKED . ',packageContent=' . print_r($packageContent, TRUE)); - $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_CHUNKED, $packageContent); - } - - /** - * Accepts the visitor to process the visit "request" - * - * @param $visitorInstance An instance of a Visitor class - * @return void - */ - public function accept (Visitor $visitorInstance) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: ' . $visitorInstance->__toString() . ' has visited - CALLED!'); - - // Visit the package - $visitorInstance->visitNetworkPackage($this); - - // Then visit the assembler to handle multiple packages - $this->getAssemblerInstance()->accept($visitorInstance); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: ' . $visitorInstance->__toString() . ' has visited - EXIT!'); - } - - /** - * Clears all stacks - * - * @return void - */ - public function clearAllStacks () { - // Call the init method to force re-initialization - $this->initStacks(TRUE); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: All stacker have been re-initialized.'); - } - - /** - * Removes the first failed outoging package from the stack to continue - * with next one (it will never work until the issue is fixed by you). - * - * @return void - * @throws UnexpectedPackageStatusException If the package status is not 'failed' - * @todo This may be enchanced for outgoing packages? - */ - public function removeFirstFailedPackage () { - // Get the package again - $packageData = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECLARED); - - // Is the package status 'failed'? - if ($packageData[self::PACKAGE_DATA_STATUS] != self::PACKAGE_STATUS_FAILED) { - // Not failed! - throw new UnexpectedPackageStatusException(array($this, $packageData, self::PACKAGE_STATUS_FAILED), self::EXCEPTION_UNEXPECTED_PACKAGE_STATUS); - } // END - if - - // Remove this entry - $this->getStackInstance()->popNamed(self::STACKER_NAME_DECLARED); - } - - /** - * "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 - */ - public function decodeRawContent ($rawPackageContent) { - // Use the separator '#' to "decode" it - $decodedArray = explode(self::PACKAGE_DATA_SEPARATOR, $rawPackageContent); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: decodedArray=' . print_r($decodedArray, TRUE)); - - // 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] - ); - - // And return it - return $decodedData; - } - - /** - * 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 - * @return void - * @throws InvalidDataChecksumException If the checksum doesn't match - */ - public function handleRawData (array $decodedData) { - /* - * "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]); - - // Assert on array count for a very basic validation - assert(count($decodedContent) == self::PACKAGE_CONTENT_ARRAY_SIZE); - - /* - * Convert the indexed array into an associative array. This is much - * better to remember than plain numbers, isn't it? - */ - $decodedContent = array( - // Compressor's extension used to compress the data - self::PACKAGE_CONTENT_EXTENSION => $decodedContent[self::INDEX_COMPRESSOR_EXTENSION], - // Package data (aka "message") in BASE64-decoded form but still compressed - self::PACKAGE_CONTENT_MESSAGE => base64_decode($decodedContent[self::INDEX_PACKAGE_DATA]), - // Tags as an indexed array for "tagging" the message - self::PACKAGE_CONTENT_TAGS => explode(self::PACKAGE_TAGS_SEPARATOR, $decodedContent[self::INDEX_TAGS]), - // 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], - // Hash from decoded raw data - self::PACKAGE_CONTENT_HASH => $decodedData[self::PACKAGE_DATA_HASH], - // Hash of private key - self::PACKAGE_CONTENT_PRIVATE_KEY_HASH => $decodedData[self::PACKAGE_DATA_PRIVATE_KEY_HASH] - ); - - // Is the checksum valid? - if (!$this->isChecksumValid($decodedContent, $decodedData)) { - // Is not the same, so throw an exception here - throw new InvalidDataChecksumException(array($this, $decodedContent, $decodedData), self::EXCEPTION_INVALID_DATA_CHECKSUM); - } // END - if - - /* - * The checksum is the same, then it can be decompressed safely. The - * original message is at this point fully decoded. - */ - $decodedContent[self::PACKAGE_CONTENT_MESSAGE] = $this->getCompressorInstance()->decompressStream($decodedContent[self::PACKAGE_CONTENT_MESSAGE]); - - // And push it on the next stack - $this->getStackInstance()->pushNamed(self::STACKER_NAME_NEW_MESSAGE, $decodedContent); - } - - /** - * Checks whether a new message has arrived - * - * @return $hasArrived Whether a new message has arrived for processing - */ - public function isNewMessageArrived () { - // Determine if the stack is not empty - $hasArrived = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_NEW_MESSAGE)); - - // Return it - return $hasArrived; - } - - /** - * Handles newly arrived messages - * - * @return void - * @todo Implement verification of all sent tags here? - */ - public function handleNewlyArrivedMessage () { - // Make sure there is at least one message - assert($this->isNewMessageArrived()); - - // Get it from the stacker, it is the full array with the decoded message - $decodedContent = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE); - - // Generate the hash of comparing it - if (!$this->isPackageHashValid($decodedContent)) { - // Is not valid, so throw an exception here - exit(__METHOD__ . ':INVALID HASH! UNDER CONSTRUCTION!' . chr(10)); - } // END - if - - // Now get a filter chain back from factory with given tags array - $chainInstance = PackageFilterChainFactory::createChainByTagsArray($decodedContent[self::PACKAGE_CONTENT_TAGS]); - - /* - * Process the message through all filters, note that all other - * elements from $decodedContent are no longer needed. - */ - $chainInstance->processMessage($decodedContent, $this); - - /* - * Post-processing of message data (this won't remove the message from - * the stack). - */ - $chainInstance->postProcessMessage($this); - } - - /** - * Checks whether a processed message is pending for "interpretation" - * - * @return $isPending Whether a processed message is pending - */ - public function isProcessedMessagePending () { - // Check it - $isPending = (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_PROCESSED_MESSAGE)); - - // Return it - return $isPending; - } - - /** - * Handle processed messages by "interpreting" the 'message_type' element - * - * @return void - */ - public function handleProcessedMessage () { - // Get it from the stacker, it is the full array with the processed message - $messageArray = $this->getStackInstance()->popNamed(self::STACKER_NAME_PROCESSED_MESSAGE); - - // Add type for later easier handling - $messageArray[self::MESSAGE_ARRAY_DATA][self::MESSAGE_ARRAY_TYPE] = $messageArray[self::MESSAGE_ARRAY_TYPE]; - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: messageArray=' . print_r($messageArray, TRUE)); - - // Create a handler instance from given message type - $handlerInstance = MessageTypeHandlerFactory::createMessageTypeHandlerInstance($messageArray[self::MESSAGE_ARRAY_TYPE]); - - // Handle message data - $handlerInstance->handleMessageData($messageArray[self::MESSAGE_ARRAY_DATA], $this); - - // Post-handling of message data - $handlerInstance->postHandleMessageData($messageArray, $this); - } - - /** - * Feeds the hash and sender (as recipient for the 'sender' reward) to the - * miner's queue, unless the message is not a "reward claim" message as this - * leads to an endless loop. You may wish to run the miner to get some - * reward ("Hubcoins") for "mining" this hash. - * - * @param $messageData Array with message data - * @return void - * @todo ~10% done? - */ - public function feedHashToMiner (array $messageData) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE: messageData()=%d - CALLED!', count($messageData))); - - // Is the feature enabled? - if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) { - /* - * Feature is not enabled, don't feed the hash to the miner as it - *may be invalid. - */ - return; - } // END - if - - // Make sure the required elements are there - assert(isset($messageData[self::MESSAGE_ARRAY_SENDER])); - assert(isset($messageData[self::MESSAGE_ARRAY_HASH])); - assert(isset($messageData[self::MESSAGE_ARRAY_DATA][self::MESSAGE_ARRAY_DATA_NODE_ID])); - - // Copy node id - $messageData[self::MESSAGE_ARRAY_DATA_NODE_ID] = $messageData[self::MESSAGE_ARRAY_DATA][self::MESSAGE_ARRAY_DATA_NODE_ID]; - - // Let's get rid of some fields that is not needed by the miner: - unset($messageData[self::MESSAGE_ARRAY_TYPE]); - unset($messageData[self::MESSAGE_ARRAY_DATA]); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE: messageData=' . print_r($messageData, TRUE)); - - // Resolve session id ('sender' is a session id) into node id - $nodeId = HubTools::resolveNodeIdBySessionId($messageData[self::MESSAGE_ARRAY_SENDER]); - - // Is 'claim_reward' the message type? - if (in_array(Tagable::TAG_CLAIM_MINING_REWARD, $messageData[self::MESSAGE_ARRAY_TAGS])) { - /* - * Then don't feed this message to the miner as this causes an - * endless loop of mining. - */ - return; - } // END - if - - // Also remove tags as the miner don't need this. - unset($messageData[self::MESSAGE_ARRAY_TAGS]); - - // Unfinished stuff: - $this->partialStub(sprintf('[%s:%d]: @TODO nodeId=%s,messageData=%s', __METHOD__, __LINE__, $nodeId, print_r($messageData, TRUE))); - } - -} 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