<?php
/**
- * A NetworkPackage class. This class implements the Deliverable class because
- * all network packages should be deliverable to other nodes. It further
- * provides methods for reading raw content from template engines and feeding it
- * to the stacker for undeclared packages.
+ * A NetworkPackage class. This class implements Deliverable and Receivable
+ * because all network packages should be deliverable to other nodes and
+ * receivable from other nodes. It further provides methods for reading raw
+ * content from template engines and feeding it to the stacker for undeclared
+ * packages.
*
* The factory method requires you to provide a compressor class (which must
* implement the Compressor interface). If you don't want any compression (not
* class and encode it with BASE64 for a more error-free transfer over the
* Internet.
*
- * For performance reasons, this class should only be instantiated once and then
+ * For performance reasons, this class should only be instanciated once and then
* used as a "pipe-through" class.
*
* @author Roland Haeder <webmaster@ship-simu.org>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Registerable {
+class NetworkPackage extends BaseFrameworkSystem implements Deliverable, Receivable, Registerable {
/**
* Package mask for compressing package data:
- * 1.) Compressor extension
- * 2.) Raw package data
- * 3.) Tags, seperated by semicolons, no semicolon is required if only one tag is needed
- * 4.) Checksum
+ * 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';
*/
const NETWORK_TARGET_SELF = 'self';
+ /**
+ * TCP package size in bytes
+ */
+ const TCP_PACKAGE_SIZE = 512;
+
/**
* Protected constructor
*
return $hash;
}
+ ///////////////////////////////////////////////////////////////////////////
+ // Delivering packages / raw data
+ ///////////////////////////////////////////////////////////////////////////
+
/**
* Delivers the given raw package data.
*
* @param $packageData Raw package data in an array
* @return void
*/
- private function deliverPackage (array $packageData) {
+ private function declareRawPackageData (array $packageData) {
/*
* 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
}
/**
- * Sends a raw package out
+ * 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 sendRawPackage (array $packageData) {
+ private function deliverRawPackageData (array $packageData) {
/*
* This package may become big, depending on the shared object size or
* delivered message size which shouldn't be so long (to save
* @param $packageData Raw package data
* @return void
*/
- private function sendOutgoingPackage (array $packageData) {
+ private function sendOutgoingRawPackageData (array $packageData) {
// Get the right connection instance
$connectionInstance = SocketRegistry::createSocketRegistry()->getHandlerInstanceFromPackageData($packageData);
return;
} // END - if
- // Sent it away (we catch exceptions one method above
+ // Sent it away (we catch exceptions one method above)
$sentBytes = $connectionInstance->sendRawPackageData($packageData);
// Remember unsent raw bytes in back-buffer, if any
*
* @return $isWaitingDelivery Wether a package is waiting for delivery
*/
- public function isPackageWaitingDelivery () {
+ public function isPackageWaitingForDelivery () {
// Check wether the stacker is not empty
$isWaitingDelivery = (($this->getStackerInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)) && (!$this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING)));
// with the first one.
$packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_UNDECLARED);
- // Finally, deliver the package
- $this->deliverPackage($packageData);
+ // Declare the raw package data for delivery
+ $this->declareRawPackageData($packageData);
// And remove it finally
$this->getStackerInstance()->popNamed(self::STACKER_NAME_UNDECLARED);
$packageData = $this->getStackerInstance()->getNamed(self::STACKER_NAME_DECLARED);
// And send it
- $this->sendRawPackage($packageData);
+ $this->deliverRawPackageData($packageData);
// And remove it finally
$this->getStackerInstance()->popNamed(self::STACKER_NAME_DECLARED);
* @return void
*/
public function sendWaitingPackage () {
- // Sent any waiting bytes in the back-buffer
+ // Send any waiting bytes in the back-buffer before sending a new package
$this->sendBackBufferBytes();
// Sanity check if we have packages waiting for delivery
- if (!$this->isPackageWaitingDelivery()) {
+ if (!$this->isPackageWaitingForDelivery()) {
// This is not fatal but should be avoided
- // @TODO Add some logging here
+ $this->debugOutput('PACKAGE: No package is waiting for delivery, but ' . __FUNCTION__ . ' was called.');
return;
} // END - if
try {
// Now try to send it
- $this->sendOutgoingPackage($packageData);
- die("O!\n");
+ $this->sendOutgoingRawPackageData($packageData);
// And remove it finally
$this->getStackerInstance()->popNamed(self::STACKER_NAME_OUTGOING);
$this->debugOutput('PACKAGE: Package was not delivered: ' . $e->getMessage());
}
}
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Receiving packages / raw data
+ ///////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Checks wether new raw package data has arrived at a socket
+ *
+ * @return $hasArrived Wether new raw package data has arrived for processing
+ */
+ public function isNewRawDataPending () {
+ // @TODO Add some content here
+ }
+
+ /**
+ * Checks wether a new package has arrived
+ *
+ * @return $hasArrived Wether a new package has arrived for processing
+ */
+ public function isNewPackageArrived () {
+ // @TODO Add some content here
+ }
}
// [EOF]