use Org\Mxchange\CoreFramework\Visitor\Visitor;
// Import SPL stuff
+use \BadMethodCallException;
use \InvalidArgumentException;
use \Iterator;
* on the next stack.
*
* @return void
+ * @throws BadMethodCallException If no incoming decoded data was on stack
*/
public function handleIncomingDecodedData () {
/*
* This method should only be called if decoded raw data is pending,
* so check it again.
*/
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
if (!$this->isRawDataPending()) {
- // This is not fatal but should be avoided
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: No raw (decoded?) data is pending, but ' . __METHOD__ . ' has been called!');
- return;
+ // Should not be invoked anymore
+ throw new BadMethodCallException('No incoming decoded data on stack but method was called.');
} // END - if
- // Very noisy debug message:
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Stacker size is ' . $this->getStackInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING) . ' entries.');
-
// "Pop" the next entry (the same array again) from the stack
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Stacker size is %d entries.', $this->getStackInstance()->getStackCount(self::STACKER_NAME_DECODED_INCOMING)));
$packageInstance = $this->getStackInstance()->popNamed(self::STACKER_NAME_DECODED_INCOMING);
/*
* 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-HANDLER: errorCode=' . $packageInstance->getErrorCode() . '(' . StorableSocket::SOCKET_ERROR_UNHANDLED . ')');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: errorCode=%s/%s', $packageInstance->getErrorCode(), StorableSocket::SOCKET_ERROR_UNHANDLED));
if (substr($packageInstance->getRawData(), -1, 1) == PackageFragmenter::CHUNK_SEPARATOR) {
// It is there and should be removed
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Raw data contains CHUNK_SEPARATOR=%s - Removing ...', PackageFragmenter::CHUNK_SEPARATOR));
$packageInstance->setRawData(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-HANDLER: Pushing ' . strlen($packageInstance->getRawData()) . ' bytes to stack ' . self::STACKER_NAME_DECODED_HANDLED . ' ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Pushing %d(%s) bytes to stack %s ...', strlen($packageInstance->getRawData()), $packageInstance->getRawData(), self::STACKER_NAME_DECODED_HANDLED));
$this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_HANDLED, $packageInstance);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
}
/**
* Get the decoded data from the handler, this is an array with
* 'raw_data' and 'error_code' as elements.
*/
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: handlerInstance=%s - CALLED!', $handlerInstance->__toString()));
$packageInstance = $handlerInstance->getNextPackageInstance();
- // Very noisy debug message:
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: decodedData[' . gettype($packageInstance) . ']=' . print_r($packageInstance, TRUE));
-
// And push it on our stack
$this->getStackInstance()->pushNamed(self::STACKER_NAME_DECODED_INCOMING, $packageInstance);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
}
/**
* @return void
*/
public function handleAssemblerPendingData () {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Calling this->getAssemblerInstance()->handlePendingData() ...');
-
// Handle it
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Calling this->getAssemblerInstance()->handlePendingData() ...');
$this->getAssemblerInstance()->handlePendingData();
}
* @return void
*/
public function handleMultipleMessages () {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Calling this->getAssemblerInstance()->handleMultipleMessages() ...');
-
// Handle it
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Calling this->getAssemblerInstance()->handleMultipleMessages() ...');
$this->getAssemblerInstance()->handleMultipleMessages();
}
* @return void
*/
public function accept (Visitor $visitorInstance) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: ' . $visitorInstance->__toString() . ' has visited - CALLED!');
-
// Visit the package
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: ' . $visitorInstance->__toString() . ' has visited - CALLED!');
$visitorInstance->visitNetworkPackageHandler($this);
// Then visit the assembler to handle multiple packages
$this->setRegistryInstance($registryInstance);
}
+ /**
+ * Handles incoming socket instance
+ *
+ * @param $socketInstance An instance of a StorableSocket class
+ * @return void
+ */
+ private function handleIncomingSocket (StorableSocket $socketInstance) {
+ // Handle it here, if not main server socket
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=%s - CALLED!', strtoupper($this->getProtocolName()), $socketInstance->__toString()));
+ $this->getHandlerInstance()->processRawDataFromSocketInstance($socketInstance);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName())));
+ }
+
/**
* Setter for listen address
*
// Is NULL returned?
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance[]=%s', strtoupper($this->getProtocolName()), gettype($socketInstance)));
- if (is_null($socketInstance)) {
+ if (!($socketInstance instanceof StorableSocket)) {
// Then abort here
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance=NULL - EXIT!', strtoupper($this->getProtocolName())));
return;
return;
}
- // @TODO Unfinished:
- /* DEBUG-DIE: */ die(sprintf('[%s:%d]: socketInstance=%s', __METHOD__, __LINE__, print_r($socketInstance, TRUE)));
+ // Invoke private method
+ $this->handleIncomingSocket($socketInstance);
// Advance to next
+ // @TODO $infoInstance is unused
$iteratorInstance->next();
// Trace message
return;
} // END - if
- // Init peer address/port
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: socketInstance->socketResource=%s accepted.', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource()));
- $peerAddress = '0.0.0.0';
- $peerPort = '0';
-
- // Get peer name
- if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) {
- // Handle this socket error
- $socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
-
- // Get node instance
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Creating node instance ...', strtoupper($this->getProtocolName())));
- $nodeInstance = NodeObjectFactory::createNodeInstance();
-
// Get a connection info instance
$infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), StorableSocket::CONNECTION_TYPE_INCOMING);
// Will the info instance with listener data
$infoInstance->fillWithSocketPeerInformation($socketInstance);
+ // Init peer address/port
+ $peerAddress = '0.0.0.0';
+ $peerPort = '0';
+
+ // Get peer name
+ if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) {
+ // Handle this socket error
+ $socketInstance->handleSocketError(__METHOD__, __LINE__);
+ } // END - if
+
// Set all required data
//* DEBUG-DIE: */ $infoInstance->debugInstance();
$socketInstance->setSenderAddress($peerAddress . $peerSuffix);
// Register the socket with the registry and with the faked array
$this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance);
- // Handle it here, if not main server socket
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: Calling this.handlerInstance->processRawDataFromSocketInstance(resource=%s) ...', strtoupper($this->getProtocolName()), $socketInstance->getSocketResource()));
- $this->getHandlerInstance()->processRawDataFromSocketInstance($socketInstance);
+ // Invoke private method
+ $this->handleIncomingSocket($socketInstance);
// Trace message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: EXIT!', strtoupper($this->getProtocolName())));