// 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]))
- );
+ // Get current package instance (an array with two elements; see handleIncomingDecodedData() for details)
+ $packageInstance = $this->getStackInstance()->getNamed(self::STACKER_NAME_DECODED_HANDLED);
// Start assembling the raw package data array by chunking it
- $this->getAssemblerInstance()->chunkPackageContent($packageContent);
+ $this->getAssemblerInstance()->chunkPackageContent($packageInstance);
// 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-HANDLER: 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);
+ $this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_CHUNKED, $packageInstance);
}
/**
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\Network\Package\DeliverablePackage;
use Org\Shipsimu\Hub\Handler\Package\NetworkPackageHandler;
use Org\Shipsimu\Hub\Network\Package\Delivery\Fragment\PackageFragmenter;
use Org\Shipsimu\Hub\Network\Receive\Receivable;
/**
* Checks whether given package content is completed (start/end markers are found)
*
- * @param $packageContent An array with two elements: 'raw_data' and 'error_code'
+ * @param $packageInstance An instance of a DeliverablePackage class
* @return $isCompleted Whether the given package content is completed
*/
- private function isPackageContentCompleted (array $packageContent) {
+ private function isPackageContentCompleted (DeliverablePackage $packageInstance) {
// Check both
- $isCompleted = $this->ifStartEndMarkersSet($packageContent[HandleableRawData::PACKAGE_RAW_DATA]);
+ $isCompleted = $this->ifStartEndMarkersSet($packageInstance->getRawData());
// Return status
return $isCompleted;
* chunks and (maybe) re-request some chunks from the sender, this would
* take to much time and therefore slow down this node again.
*
- * @param $packageContent An array with two elements: 'raw_data' and 'error_code'
+ * @param $packageInstance An array with two elements: 'raw_data' and 'error_code'
* @return void
* @throws UnsupportedPackageCodeHandlerException If the package code handler is not implemented
*/
- public function chunkPackageContent (array $packageContent) {
- // Validate the package content array again
- assert(
- (isset($packageContent[HandleableRawData::PACKAGE_RAW_DATA])) &&
- (isset($packageContent[HandleableRawData::PACKAGE_ERROR_CODE]))
- );
-
+ public function chunkPackageInstance (DeliverablePackage $packageInstance) {
// Construct call-back name from package error code
- $this->callbacks[$packageContent[HandleableRawData::PACKAGE_ERROR_CODE]] = 'handlePackageBy' . self::convertToClassName($packageContent[HandleableRawData::PACKAGE_ERROR_CODE]);
+ $this->callbacks[$packageInstance->getErrorCode()] = 'handlePackageBy' . self::convertToClassName($packageInstance->getErrorCode());
// Abort if the call-back method is not there
- if (!method_exists($this, $this->callbacks[$packageContent[HandleableRawData::PACKAGE_ERROR_CODE]])) {
+ if (!method_exists($this, $this->callbacks[$packageInstance->getErrorCode()])) {
// Throw an exception
- throw new UnsupportedPackageCodeHandlerException(array($this, $this->callbacks[$packageContent[HandleableRawData::PACKAGE_ERROR_CODE]], $packageContent), self::EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER);
+ throw new UnsupportedPackageCodeHandlerException(array($this, $this->callbacks[$packageInstance->getErrorCode()], $packageInstance), self::EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER);
} // END - if
// Call it back
- call_user_func(array($this, $this->callbacks[$packageContent[HandleableRawData::PACKAGE_ERROR_CODE]]), $packageContent);
+ call_user_func(array($this, $this->callbacks[$packageInstance->getErrorCode()]), $packageInstance);
}
/**************************************************************************
* class, does some low checks on it and feeds it into another queue for
* verification and re-request for bad chunks.
*
- * @param $packageContent An array with two elements: 'raw_data' and 'error_code'
+ * @param $packageInstance An array with two elements: 'raw_data' and 'error_code'
* @return void
*/
- private function handlePackageByUnhandledPackage (array $packageContent) {
+ private function handlePackageByUnhandledPackage (Deliverable $packageInstance) {
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageContent[' . HandleableRawData::PACKAGE_RAW_DATA . ']=' . $packageContent[HandleableRawData::PACKAGE_RAW_DATA]);
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageInstance->rawData[]=' . $packageInstance->getRawData());
// Check for some conditions
- if ((!$this->ifInputBufferIsEmpty()) || (!$this->isPackageContentCompleted($packageContent))) {
+ if ((!$this->ifInputBufferIsEmpty()) || (!$this->isPackageContentCompleted($packageInstance))) {
// Last chunk is not valid, so wait for more
- $this->pendingData .= $packageContent[HandleableRawData::PACKAGE_RAW_DATA];
+ $this->pendingData .= $packageInstance->getRawData();
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Partial data received. Waiting for more ... ( ' . strlen($packageContent[HandleableRawData::PACKAGE_RAW_DATA]) . ' bytes)');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Partial data received. Waiting for more ... ( ' . strlen($packageInstance->getRawData()) . ' bytes)');
} else {
// Debug message
- //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageContent=' . print_r($packageContent, TRUE) . ',chunks='.print_r($chunks, TRUE));
+ //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: packageContent=' . print_r($packageInstance, TRUE) . ',chunks='.print_r($chunks, TRUE));
}
}
return;
}
- // Init fake array
- $packageContent = array(
- HandleableRawData::PACKAGE_RAW_DATA => $this->getInputStreamInstance()->streamData($this->pendingData),
- HandleableRawData::PACKAGE_ERROR_CODE => StorableSocket::SOCKET_ERROR_UNHANDLED
- );
+ // Init fake package instance
+ $packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class');
+
+ // Set all data
+ $packageInstance->setRawData($this->getInputStreamInstance()->streamData($this->pendingData));
+ $packageInstance->setErrorCode(StorableSocket::SOCKET_ERROR_UNHANDLED);
/*
* Clear pending data as it has been processed and will be handled some
$this->clearPendingData();
// Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Last block of partial data received. A total of ' . strlen($packageContent[HandleableRawData::PACKAGE_RAW_DATA]) . ' bytes has been received.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Last block of partial data received. A total of ' . strlen($packageInstance->getRawData()) . ' bytes has been received.');
// Make sure last CHUNK_SEPARATOR is not there
- if (substr($packageContent[HandleableRawData::PACKAGE_RAW_DATA], -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) {
+ if (substr($packageInstance->getRawData(), -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) {
// Remove it
- $packageContent[HandleableRawData::PACKAGE_RAW_DATA] = substr($packageContent[HandleableRawData::PACKAGE_RAW_DATA], 0, -1);
+ $packageInstance->setRawData(substr($packageInstance->getRawData(), 0, -1));
} // END - if
/*
* array of chunks. These chunks must then be verified by their
* checksums. Also the final chunk must be handled.
*/
- $chunks = explode(PackageFragmenter::CHUNK_SEPARATOR, $packageContent[HandleableRawData::PACKAGE_RAW_DATA]);
+ $chunks = explode(PackageFragmenter::CHUNK_SEPARATOR, $packageInstance->getRawData());
// Add all chunks because the last final chunk is found
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-ASSEMBLER: Going to add ' . count($chunks) . ' to chunk handler ...');
--- /dev/null
+<?php
+// Own namespace
+namespace Org\Shipsimu\Hub\Task;
+
+// Import application-specific stuff
+use Org\Shipsimu\Hub\Node\Node;
+
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Task\BaseTask;
+
+/**
+ * A general hub task
+ *
+ * @author Roland Haeder <webmaster@shipsimu.org>
+ * @version 0.0.0
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 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 <http://www.gnu.org/licenses/>.
+ */
+abstract class BaseHubTask extends BaseTask {
+
+ /**
+ * Node instance
+ */
+ private $nodeInstance = NULL;
+
+ /**
+ * Protected constructor
+ *
+ * @param $className Name of the class
+ * @return void
+ */
+ protected function __construct ($className) {
+ // Call parent constructor
+ parent::__construct($className);
+ }
+
+ /**
+ * Setter for node instance
+ *
+ * @param $nodeInstance A Node instance
+ * @return void
+ */
+ public final function setNodeInstance (Node $nodeInstance) {
+ $this->nodeInstance = $nodeInstance;
+ }
+
+ /**
+ * Getter for node instance
+ *
+ * @return $nodeInstance A Node instance
+ */
+ public function getNodeInstance () {
+ return $this->nodeInstance;
+ }
+
+}