* @param $version Version number of this apt-proxy
* @return void
*/
- public final function enableIsActive ($isActive = TRUE) {
- $this->isActive = (bool) $isActive;
+ public final function enableIsActive (bool $isActive = TRUE) {
+ $this->isActive = $isActive;
}
/**
* @param $version Version number of this chatter
* @return void
*/
- public final function enableIsActive ($isActive = TRUE) {
- $this->isActive = (bool) $isActive;
+ public final function enableIsActive (bool $isActive = TRUE) {
+ $this->isActive = $isActive;
}
/**
* @param $socketProtocol Socket protocol (TCP, UDP, file)
* @param $packageInstance An instance of a DeliverablePackage class
* @param $socketType Socket type (incoming, outgoing, server, file, ...)
- * @return $socketInstance An instance of this Container class
- * @throws InvalidArgumentException If socket type is not set in $packageData array
+ * @return $socketInstance An instance of a StorableSocket class
+ * @throws InvalidArgumentException If a parameter is not valid
*/
- public static final function createSocketContainer ($socketResource, $socketProtocol, DeliverablePackage $packageInstance, $socketType) {
- // Trace message
+ public static final function createSocketContainer ($socketResource, string $socketProtocol, DeliverablePackage $packageInstance, string $socketType) {
+ // Validate parameter
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketResource[%s]=%s,socketProtocol=%s,packageInstance[]=%s,socketType=%s - CALLED!', gettype($socketResource), $socketResource, $socketProtocol, gettype($packageInstance), $socketType));
- //* DEBUG-PRINT: */ printf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE));
+ if (!is_resource($socketResource)) {
+ // Throw exception
+ throw new InvalidArgumentException(sprintf('socketResource[]=%s is not valid', $socketResource));
+ } elseif (empty($socketProtocol)) {
+ // Throw again
+ throw new InvalidArgumentException('socketProtocol is empty');
+ } elseif (empty($socketType)) {
+ // Throw again
+ throw new InvalidArgumentException('socketType is empty');
+ }
// Get a new instance
$socketInstance = new SocketContainer();
// Set all values
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-CONTAINER: socketResource=%s,socketProtocol=%s', $socketResource, $socketProtocol));
$socketInstance->setSocketProtocol($socketProtocol);
$socketInstance->setSocketResource($socketResource);
$socketInstance->setPackageDataInstance($packageInstance);
*
* @param $unl A Universal Node Locator
* @return $matches Whether $address matches with the one from package data
- */
- public function ifAddressMatches ($unl) {
+ * @throws InvalidArgumentException If a parameter is not valid
+ */
+ public function ifAddressMatches (string $unl) {
+ // Is parameter valid?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unl=%s - CALLED!', strtoupper($this->getSocketProtocol()), $unl));
+ if (empty($unl)) {
+ // Throw again
+ throw new InvalidArgumentException('unl is empty');
+ }
+
// Get current package data
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: unl=%s - CALLED!', strtoupper($this->getSocketProtocol()), $unl));
$packageInstance = $this->getPackageDataInstance();
// So, does both match?
* @return $result Result from asking for peer address
* @throws InvalidSocketException If socket is invalid
*/
- public function determineSocketPeerName (&$peerAddress, &$peerPort) {
+ public function determineSocketPeerName (string &$peerAddress, int &$peerPort) {
// Should be valid socket
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress[%s]=%s,peerPort[%s]=%d - CALLED!', strtoupper($this->getSocketProtocol()), gettype($peerAddress), $peerAddress, gettype($peerPort), $peerPort));
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Init result
$result = FALSE;
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Get package data
$packageInstance = $this->getPackageDataInstance();
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Get recipient
$recipient = $this->getSocketRecipient();
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Get recipient
$recipient = $this->getSocketRecipient();
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Get it from stored socket resource
$socketResource = $this->getSocketResource();
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Get it from stored socket resource
$socketResource = $this->getSocketResource();
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Try to listen on socket
$result = socket_listen($this->getSocketResource());
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Try to set non-blocking I/O
$result = socket_set_nonblock($this->getSocketResource());
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Tries to set option
$result = $this->setSocketOption(SOL_SOCKET, SO_REUSEADDR, 1);
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Get recipient UNL
$unlRecipient = $this->getSocketRecipient();
if (!$this->isValidSocket()) {
// Throw exception
throw new BadMethodCallException(sprintf('[%s:%d]: Shutdown on invalid socket. Maybe called already?', __METHOD__, __LINE__), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Debug message
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Shutting down socket ' . $this->getSocketResource());
if (socket_last_error($this->getSocketResource()) != 107) {
// Something bad happened while we shutdown a socket
throw new SocketShutdownException($this, self::EXCEPTION_INVALID_SOCKET);
- } // END - if
- } // END - if
+ }
+ }
// Try to make blocking I/O for socket_close()
socket_set_block($this->getSocketResource());
*
* @param $connectionType Type of connection, can be 'incoming', 'outgoing' or 'server'
* @return $isValid Whether the provided connection type is valid
+ * @throws InvalidArgumentException If $connectionType is empty
*/
- public function isValidConnectionType ($connectionType) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: connectionType=%d - CALLED!', strtoupper($this->getSocketProtocol()), $connectionType));
+ public function isValidConnectionType (string $connectionType) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: connectionType=%d - CALLED!', strtoupper($this->getSocketProtocol()), $connectionType));
+ if (empty($connectionType)) {
+ // Throw exception
+ throw new InvalidArgumentException('connectionType is empty');
+ }
// Is it valid?
$isValid = in_array($connectionType, $this->connectionTypes, TRUE);
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: isValid=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($isValid)));
-
// Return result
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: isValid=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($isValid)));
return $isValid;
}
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Init all arrays, at least readers
/* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
/*
* Read raw data from socket. If you change PHP_BINARY_READ to
// Init peer address (IP)/port
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
$peerAddress = '0.0.0.0';
- $peerPort = '0';
+ $peerPort = 0;
// Call other method
$result = $this->determineSocketPeerName($peerAddress, $peerPort);
// Set both
$this->setPeerAddress($peerAddress);
$this->setPeerPort($peerPort);
- } // END - if
+ }
// Return result
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), gettype($result), intval($result)));
* @throws InvalidSocketException If the stored socket resource is no socket resource
* @throws NoSocketErrorDetectedException If socket_last_error() gives zero back
*/
- public function handleSocketError ($method, $line) {
+ public function handleSocketError (string $method, int $line) {
// This method handles only socket resources
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: Handling socket errorCode=%d - CALLED!', $this->getLastSocketErrorCode()));
if (!$this->isValidSocket()) {
// No resource, abort here
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Get error code for first validation (0 is not an error)
$errorCode = $this->getLastSocketErrorCode();
if ($errorCode == 0) {
// No error detected (or previously cleared outside this method)
throw new NoSocketErrorDetectedException(array($this), BaseListener::EXCEPTION_NO_SOCKET_ERROR);
- } // END - if
+ }
// Get handler (method) name
$handlerName = $this->getSocketErrorHandlerFromCode($errorCode);
* @return $handlerName Call-back method name for the error handler
* @throws UnsupportedSocketErrorHandlerException If the error handler is not implemented
*/
- protected function getSocketErrorHandlerFromCode ($errorCode) {
+ protected function getSocketErrorHandlerFromCode (int $errorCode) {
// Create a name from translated error code
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $errorCode));
$handlerName = 'handleSocketError' . self::convertToClassName($this->translateSocketErrorCodeToName($errorCode));
if (!method_exists($this, $handlerName)) {
// Please implement this
throw new UnsupportedSocketErrorHandlerException(array($this, $handlerName, $errorCode), BaseConnectionHelper::EXCEPTION_UNSUPPORTED_ERROR_HANDLER);
- } // END - if
+ }
// Return it
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: handlerName=%d - EXIT!', strtoupper($this->getSocketProtocol()), $handlerName));
* @param $errorCode The error code from socket_last_error() to be translated
* @return $errorName The translated name (all lower-case, with underlines)
*/
- private function translateSocketErrorCodeToName ($errorCode) {
+ private function translateSocketErrorCodeToName (int $errorCode) {
// Unknown error code by default
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $errorCode));
$errorName = StorableSocket::SOCKET_ERROR_UNKNOWN;
// Unhandled error code detected, so first debug it because we may want to handle it like the others
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf(strtoupper($this->getSocketProtocol()) . '-SOCKET: Unsupported errorCode=%d,message=%s', $errorCode, socket_strerror($errorCode)));
break;
- } // END - switch
+ }
// Return translated name
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: errorName=%d - EXIT!', strtoupper($this->getSocketProtocol()), $errorName));
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Set it
$result = socket_set_option($this->getSocketResource(), $level, $optionName, $optionValue);
* @param $socketProtocol Socket protocol
* @return void
*/
- private function setSocketProtocol ($socketProtocol) {
+ private function setSocketProtocol (string $socketProtocol) {
$this->socketProtocol = $socketProtocol;
}
* @param $socketType Socket type
* @return void
*/
- protected function setSocketType ($socketType) {
+ protected function setSocketType (string $socketType) {
$this->socketType = $socketType;
}
* @param $peerAddress Peer address
* @return void
*/
- public function setPeerAddress ($peerAddress) {
+ public function setPeerAddress (string $peerAddress) {
$this->peerAddress = $peerAddress;
}
* @param $peerPort Peer port
* @return void
*/
- public function setPeerPort ($peerPort) {
+ public function setPeerPort (int $peerPort) {
$this->peerPort = $peerPort;
}
* @param $senderAddress Sender address
* @return void
*/
- public function setSenderAddress ($senderAddress) {
+ public function setSenderAddress (string $senderAddress) {
$this->senderAddress = $senderAddress;
}
* @param $senderPort Sender port
* @return void
*/
- public function setSenderPort ($senderPort) {
+ public function setSenderPort (int $senderPort) {
$this->senderPort = $senderPort;
}
* @param $version Version number of this crawler
* @return void
*/
- public final function enableIsActive ($isActive = TRUE) {
- $this->isActive = (bool) $isActive;
+ public final function enableIsActive (bool $isActive = TRUE) {
+ $this->isActive = $isActive;
}
/**
* @param $version Version number of this cruncher
* @return void
*/
- public final function enableIsActive ($isActive = TRUE) {
- $this->isActive = (bool) $isActive;
+ public final function enableIsActive (bool $isActive = TRUE) {
+ $this->isActive = $isActive;
}
/**
* @param $key Key to look for
* @param $value Value to compare if key matches
* @return $recipients An indexed array with DHT recipients
+ * @throws InvalidArgumentException If $key is empty
*/
- public function getResultFromKeyValue ($key, $value) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
+ public function getResultFromKeyValue (string $key, $value) {
+ // Is key parameter valid?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: key=%s,value[%s]=%s - CALLED!', $key, gettype($value), $value));
+ if (empty($key)) {
+ // Throw exception
+ throw new InvalidArgumentException('Parameter key is empty');
+ }
// Get max recipients
$maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
// Get a result instance back from DHT database wrapper.
$resultInstance = $this->doSelectByCriteria($searchInstance);
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: EXIT!');
-
// Return result instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: resultInstance=%s - EXIT!', $resultInstance->__toString()));
return $resultInstance;
}
// Init peer address/port
$peerAddress = '0.0.0.0';
- $peerPort = '0';
+ $peerPort = 0;
// Get peer name
if (!$socketInstance->determineSocketPeerName($peerAddress, $peerPort)) {
* @param $key Key to search for
* @param $value Value to check on found key
* @return $recipiens Array with DHT recipients from given key/value pair
+ * @throws InvalidArgumentException If $key is empty
*/
- public function findRecipientsByKey ($key, $value) {
+ public function findRecipientsByKey (string $key, $value) {
+ // Is key parameter valid?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: key=%s,value[%s]=%s - CALLED!', $key, gettype($value), $value));
+ if (empty($key)) {
+ // Throw exception
+ throw new InvalidArgumentException('Parameter key is empty');
+ }
+
// Look for all suitable nodes
$resultInstance = $this->getWrapperInstance()->getResultFromKeyValue($key, $value);
* @throws InvalidArgumentException If one of the parameters are not valid
* @throws LogicException If the discovered listener instance has no pool set
*/
- public function discoverSocket (DeliverablePackage $packageInstance, $connectionType) {
+ public function discoverSocket (DeliverablePackage $packageInstance, string $connectionType) {
// Make sure all parameters are valid
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-SOCKET-DISOVERY: packageInstance=%s,connectionType=%s - CALLED!', $packageInstance->__toString(), $connectionType));
//* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('PACKAGE-SOCKET-DISOVERY: packageInstance=' . print_r($packageInstance, TRUE));
- if (($connectionType != StorableSocket::CONNECTION_TYPE_INCOMING) && ($connectionType != StorableSocket::CONNECTION_TYPE_OUTGOING)) {
+ if (empty($connectionType)) {
+ // Throw exception
+ throw new InvalidArgumentException('Parameter connectionType is empty');
+ } elseif (($connectionType != StorableSocket::CONNECTION_TYPE_INCOMING) && ($connectionType != StorableSocket::CONNECTION_TYPE_OUTGOING)) {
// Abort here
throw new InvalidArgumentException(sprintf('connectionType=%s is whether "%s" nor "%s".',
$connectionType,
use Org\Mxchange\CoreFramework\Socket\InvalidSocketException;
// Import SPL stuff
+use \InvalidArgumentException;
use \LogicException;
use \SplFileInfo;
* @param $socketResource Valid socket resource from calling socket_accept()
* @param $socketProtocol Protcol of socket (e.g. 'tcp', 'udp', see StorableSocket interface for valid values
* @return $socketInstance An instance of a StorableSocket class
+ * @throws InvalidArgumentException If a parameter is not valid
*/
- public static final function createIncomingSocketInstance ($socketResource, $socketProtocol) {
- // Create package instance
+ public static final function createIncomingSocketInstance ($socketResource, string $socketProtocol) {
+ // Validate parameter
/* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketResource[%s]=%s,socketProtocol=%s - CALLED!', gettype($socketResource), $socketResource, $socketProtocol));
+ if (!is_resource($socketResource)) {
+ // Throw exception
+ throw new InvalidArgumentException(sprintf('socketResource[]=%s is not valid', gettype($socketResource)));
+ } elseif (empty($socketProtocol)) {
+ // Throw it again
+ throw new InvalidArgumentException('socketProtocol is empty');
+ }
+
+ // Create package instance
$packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class');
// Create socket instance
use \BadMethodCallException;
use \InvalidArgumentException;
use \Iterator;
+use \UnexpectedValueException;
/**
* A NetworkPackageHandler class. This class implements Deliverable and Receivable
*
* @param $packageInstance An instance of a DeliverablePackage class in an array
* @return $hash Hash for given package content
+ * @throws InvalidArgumentException If $packageInstance has no sessionId set
*/
public function getHashFromPackageSessionId (DeliverablePackage $packageInstance) {
- // Create the hash
+ // Is session id set?
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
+ if (empty($packageInstance->getSessionId())) {
+ // Throw exception
+ throw new InvalidArgumentException('packageInstance has no sessionId set.');
+ }
+
+ // Create the hash
// @TODO md5() is very weak, but it needs to be fast
$hash = md5(sprintf(self::CONTENT_CHECKSUM_MASK,
$packageInstance->getContentMessage(),
* target because it causes an overload on the network and may be
* abused for attacking the network with large packages.
*/
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
$discoveryInstance = PackageDiscoveryFactory::createPackageDiscoveryInstance();
// Discover all recipients, this may throw an exception
*/
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Calling discoveryInstance->clearRecipients() ...');
$discoveryInstance->clearRecipients();
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
}
/**
* which (configurable!) protocol should be used for that type of
* package.
*/
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
$discoveryInstance = SocketDiscoveryFactory::createSocketDiscoveryInstance();
// Now discover the right socket instance from given package data
$socketInstance = $discoveryInstance->discoverSocket($packageInstance, StorableSocket::CONNECTION_TYPE_OUTGOING);
// Get the connection helper from registry
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after discoverSocket() has been called.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after discoverSocket() has been called.');
$helperInstance = GenericRegistry::getRegistry()->getInstance('connection');
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: stateInstance=' . $helperInstance->getStateInstance());
-
- // And make sure it is valid
- assert($helperInstance instanceof ConnectionHelper);
-
// Get connection info class
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: stateInstance=%s', $helperInstance->getStateInstance()));
$infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($helperInstance->getProtocolName(), 'helper');
// Will the info instance with connection helper data
$infoInstance->fillWithConnectionHelperInformation($helperInstance);
// Is it not there?
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.');
if (($socketInstance->isValidSocket()) && (!$this->getRegistryInstance()->isSocketRegistered($infoInstance, $socketInstance))) {
// Then register it
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Registering socket ' . $socketInstance . ' ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Registering socket %s ...', $socketInstance));
$this->getRegistryInstance()->registerSocketInstance($infoInstance, $socketInstance);
} elseif (!$helperInstance->getStateInstance()->isPeerStateConnected()) {
// Is not connected, then we cannot send
}
// Make sure the connection is up
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.');
$helperInstance->getStateInstance()->validatePeerStateConnected();
// Enqueue it again on the out-going queue, the connection is up and working at this point
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.');
$this->getStackInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageInstance);
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after pushNamed() has been called.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Reached line ' . __LINE__ . ' after pushNamed() has been called.');
}
/**
* @return void
*/
private function sendOutgoingRawPackageData (DeliverablePackage $packageInstance) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
-
// Init sent bytes
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
$sentBytes = 0;
// Get the right connection instance
// 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?
+ //* DEBUG-DIE: */ die(': p1=' . $infoInstance->getProtocolName() . ',p2=' . $helperInstance->getProtocolName() . ',infoInstance=' . print_r($infoInstance, TRUE));
if ($helperInstance->isShuttedDown()) {
// This connection is shutting down
// @TODO We may want to do somthing more here?
$helperInstance->sendRawPackageData($packageInstance);
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
}
/**
* @return $hash Hash as hex-encoded string
*/
private function generatePackageHash ($content, $senderId) {
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: content()=' . strlen($content) . ',senderId=' . $senderId . ' - CALLED!');
-
// Assert on variables
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: content()=' . strlen($content) . ',senderId=' . $senderId . ' - CALLED!');
assert(!empty($content));
assert(!empty($senderId));
*
* @param $helperInstance An instance of a HubHelper class
* @return void
+ * @throws UnexpectedValueException If the helper instance contains now raw template data
*/
public function enqueueRawDataFromTemplate (HubHelper $helperInstance) {
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: helperInstance=' . $helperInstance->__toString() . ' - CALLED!');
-
// Get the raw content ...
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: helperInstance=' . $helperInstance->__toString() . ' - CALLED!');
$content = $helperInstance->getTemplateInstance()->getRawTemplateData();
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('content(' . strlen($content) . ')=' . $content);
+
+ // Should not be empty
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: content(%d)=%s', strlen($content), $content));
+ if (empty($content)) {
+ // Abort here
+ throw new UnexpectedValueException('content cannot be empty.');
+ }
// ... and compress it
$compressed = $this->getCompressorInstance()->compressStream($content);
$this->getHashFromContent($compressed)
);
- // Make sure required data is there
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Enqueueing package for recipientType=' . $helperInstance->getRecipientType() . ' ...');
- assert(!empty($content));
- assert($this->getNodeInstance()->getSessionId() != '');
-
// Init package instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: Enqueueing package for recipientType=' . $helperInstance->getRecipientType() . ' ...');
$packageInstance = ObjectFactory::createObjectByConfiguredName('package_data_class');
// Set all data
public function isPackageWaitingForDelivery () {
// Check whether the stacker is not empty
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
- $isWaitingDelivery = (($this->getStackInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)) && (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING)));
+ $isWaitingDelivery = (
+ (
+ $this->getStackInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING)
+ ) && (
+ !$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING)
+ )
+ );
// Return the result
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: isWaitingDelivery=%d - EXIT!', intval($isWaitingDelivery)));
public function isEncodedDataPending () {
// Check whether the stacker is not empty
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
- $isPending = (($this->getStackInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING_STREAM)) && (!$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING_STREAM)));
+ $isPending = (
+ (
+ $this->getStackInstance()->isStackInitialized(self::STACKER_NAME_OUTGOING_STREAM)
+ ) && (
+ !$this->getStackInstance()->isStackEmpty(self::STACKER_NAME_OUTGOING_STREAM)
+ )
+ );
// Return the result
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: isPending=%d - EXIT!', intval($isPending)));
* @throws NoTargetException If no target can't be determined
*/
public function declareEnqueuedPackage () {
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
-
// Make sure this method isn't working if there is no package enqueued
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
if (!$this->isPackageEnqueued()) {
// This is not fatal but should be avoided
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: No raw package data waiting declaration, but ' . __METHOD__ . ' has been called!');
}
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: EXIT!');
}
/**
$packageInstance = $this->getStackInstance()->popNamed(self::STACKER_NAME_NEW_MESSAGE);
// Generate the hash of comparing it
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: packageInstance=%s,senderId=%s,senderPrivateKeyHash=%s', $packageInstance->__toString(), $packageInstance->getSenderId(), $packageInstance->getSenderPrivateKeyHash()));
+ /* DEBUG-DIE: */ die(sprintf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE)));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s,senderId=%s,senderPrivateKeyHash=%s', $packageInstance->__toString(), $packageInstance->getSenderId(), $packageInstance->getSenderPrivateKeyHash()));
if (empty($packageInstance->getSenderId())) {
// Invalid $packageInstance
throw new InvalidArgumentException('packageInstance does not contain senderId');
*/
private function getConnectionClassNameFromSocket () {
// Get recipient address/port
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: CALLED!');
$recipientAddress = $this->getSocketInstance()->getSocketRecipientAddress();
$recipientPort = $this->getSocketInstance()->getSocketRecipientPort();
// Construct it
- $class = sprintf('%s:%d:%s', $recipientAddress, $recipientPort, parent::__toString());
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: recipientAddress=%s,recipientPort=%d', $recipientAddress, $recipientPort));
+ $class = sprintf('%s:%d:%s',
+ $recipientAddress,
+ $recipientPort,
+ parent::__toString()
+ );
// ... and return it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: class=%s - EXIT!', $class));
return $class;
}
*/
private function initState() {
// Get the state factory and create the initial state.
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: CALLED!');
PeerStateFactory::createPeerStateInstanceByName('init', $this);
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: EXIT!');
}
/**
* @return $chunkData Raw data chunk
*/
private function getRawDataFromPackageArray (DeliverablePackage $packageInstance) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: currentFinalHash=' . $this->currentFinalHash);
-
// Make sure the final hash is set
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: currentFinalHash=' . $this->currentFinalHash);
assert((is_string($this->currentFinalHash)) && (!empty($this->currentFinalHash)));
// Get the next raw data chunk from the fragmenter
$rawDataChunk = $this->getFragmenterInstance()->getNextRawDataChunk($this->currentFinalHash);
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: rawDataChunk=' . print_r($rawDataChunk, TRUE));
-
// Get chunk hashes and chunk data
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: rawDataChunk=' . print_r($rawDataChunk, TRUE));
$chunkHashes = array_keys($rawDataChunk);
$chunkData = array_values($rawDataChunk);
$rawData = '';
// Is the required data there?
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: chunkHashes[]=' . count($chunkHashes) . ',chunkData[]=' . count($chunkData));
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('chunkData='.print_r($chunkData, TRUE));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: chunkHashes()=' . count($chunkHashes) . ',chunkData()=' . count($chunkData));
+ //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('chunkData='.print_r($chunkData, TRUE));
if ((isset($chunkHashes[0])) && (isset($chunkData[0]))) {
// Remember this chunk as queued
$this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
// Return the raw data
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: rawData()=' . strlen($chunkData[0]) . ' bytes.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: rawData()=' . strlen($chunkData[0]) . ' bytes.');
$rawData = $chunkData[0];
} // END - if
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: rawData()=%d - EXIT!', strlen($rawData)));
-
// Return raw data
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: rawData(%d)=%s - EXIT!', strlen($rawData), $rawData));
return $rawData;
}
* @param $isInitialized Name of used protocol in this connection
* @return void
*/
- protected final function setIsInitialized ($isInitialized) {
+ protected final function setIsInitialized (bool $isInitialized) {
$this->isInitialized = $isInitialized;
}
*/
public function sendRawPackageData (DeliverablePackage $packageInstance) {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: packageInstance=%s - CALLED!', $packageInstance->__toString()));
// The helper's state must be 'connected'
$this->getStateInstance()->validatePeerStateConnected();
// Implode the package data array and fragement the resulting string, returns the final hash
$finalHash = $this->getFragmenterInstance()->fragmentPackageArray($packageInstance, $this);
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: finalHash[%s]=%s', gettype($finalHash), $finalHash));
-
// Is the final hash set?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: finalHash[%s]=%s', gettype($finalHash), $finalHash));
if ($finalHash !== TRUE) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Setting finalHash=' . $finalHash . ',currentFinalHash[' . gettype($this->currentFinalHash) . ']=' . $this->currentFinalHash);
-
// Set final hash
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: Setting finalHash=%s,currentFinalHash[%s]=%s', $finalHash, gettype($this->currentFinalHash), $this->currentFinalHash));
$this->currentFinalHash = $finalHash;
} // END - if
// Fill sending buffer with data
while (TRUE) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: dataStream()=%d - BEFORE!', strlen($dataStream)));
-
// Convert the package data array to a raw data stream
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: dataStream()=%d - BEFORE!', strlen($dataStream)));
$dataStream = $this->getRawDataFromPackageArray($packageInstance);
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: dataStream()=%d - AFTER!', strlen($dataStream)));
-
// Is it empty?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: dataStream()=%d - AFTER!', strlen($dataStream)));
if (strlen($dataStream) == 0) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: dataStream is now empty, exiting loop ...');
-
// Abort here
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: dataStream is now empty, exiting loop ...');
break;
} // END - if
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
-
// Add raw data
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: Adding %d bytes to the sending buffer ...', strlen($dataStream)));
$rawData .= $dataStream;
} // END - while
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: rawData()=%d - after loop ...', strlen($rawData)));
-
- // Nothing to sent is bad news, so assert on it
- assert(strlen($rawData) > 0);
-
// Calculate buffer size
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: rawData()=%d - after loop ...', strlen($rawData)));
$bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocolName() . '_buffer_length');
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: bufferSize=%d', $bufferSize));
-
// Encode the raw data with our output-stream
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: bufferSize=%d', $bufferSize));
$encodedData = $this->getOutputStreamInstance()->streamData($rawData);
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: socketResource[%s]=%s', gettype($this->getSocketInstance()->getSocketResource()), $this->getSocketInstance()->getSocketResource()));
-
// Init array
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: socketResource[%s]=%s', gettype($this->getSocketInstance()->getSocketResource()), $this->getSocketInstance()->getSocketResource()));
$encodedDataArray = array(
NetworkPackageHandler::RAW_INDEX_FINAL_HASH => $this->currentFinalHash,
NetworkPackageHandler::RAW_INDEX_ENCODED_DATA => $encodedData,
$this->getPackageHandlerInstance()->getStackInstance()->pushNamed(NetworkPackageHandler::STACKER_NAME_OUTGOING_STREAM, $encodedDataArray);
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: EXIT!');
}
/**
use Org\Mxchange\CoreFramework\Registry\Registerable;
use Org\Mxchange\CoreFramework\Socket\InvalidSocketException;
+// Import SPL stuff
+use \InvalidArgumentException;
+
/**
* A Connection information class
*
*
* @param $connectionType Connection type (valid: 'incoming', 'outgoing', 'server')
* @return $infoInstance An instance of a ShareableInfo class
+ * @throws InvalidArgumentException If $connectionType is empty
*/
- public final static function createConnectionInfo ($connectionType) {
- // Get new instance
+ public final static function createConnectionInfo (string $connectionType) {
+ // Validate parameter
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: connectionType=%s - CALLED!', $connectionType));
+ if (empty($connectionType)) {
+ // Throw exception
+ throw new InvalidArgumentException('connectionType is empty');
+ }
+
+ // Get new instance
$infoInstance = new ConnectionInfo();
// Set connection type here
* @param $connectionType Connection type
* @return void
*/
- private function setConnectionType ($connectionType) {
+ private function setConnectionType (string $connectionType) {
$this->connectionType = $connectionType;
}
// Init variables
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO: socketInstance=%s - CALLED!', $socketInstance->__toString()));
$socketAddress = '0.0.0.0';
- $socketPort = '0';
+ $socketPort = 0;
// Get peer name
if (!$socketInstance->determineSocketPeerName($socketAddress, $socketPort)) {
use Org\Mxchange\CoreFramework\Visitor\Visitor;
// Import SPL stuff
+use \InvalidArgumentException;
use \LogicException;
/**
* @param $listenAddress The address this listener should listen on
* @return void
*/
- protected final function setListenAddress ($listenAddress) {
- $this->listenAddress = (string) $listenAddress;
+ protected final function setListenAddress (string $listenAddress) {
+ $this->listenAddress = $listenAddress;
}
/**
* @param $listenPort The port this listener should listen on
* @return void
*/
- protected final function setListenPort ($listenPort) {
- $this->listenPort = (int) $listenPort;
+ protected final function setListenPort (int $listenPort) {
+ $this->listenPort = $listenPort;
}
/**
* @param $configEntry The configuration entry holding our listen address
* @return void
*/
- public final function setListenAddressByConfiguration ($configEntry) {
+ public final function setListenAddressByConfiguration (string $configEntry) {
$this->setListenAddress($this->getConfigInstance()->getConfigEntry($configEntry));
}
* @param $configEntry The configuration entry holding our listen port
* @return void
*/
- public final function setListenPortByConfiguration ($configEntry) {
+ public final function setListenPortByConfiguration (string $configEntry) {
$this->setListenPort($this->getConfigInstance()->getConfigEntry($configEntry));
}
*
* @param $peerSuffix Suffix for peer name (e.g. :0 for TCP(/UDP?) connections)
* @return void
+ * @throws InvalidArgumentException If $peerSuffix is empty
* @throws LogicException If no info instance can be created
*/
- protected function doListenSocketSelect ($peerSuffix) {
- // Check on all instances
+ protected function doListenSocketSelect (string $peerSuffix) {
+ // Validate parameter and socket instance
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: peerSuffix=%s - CALLED!', strtoupper($this->getProtocolName()), $peerSuffix));
- assert($this->getPoolInstance() instanceof Poolable);
- assert($this->getSocketInstance()->isValidSocket());
+ if (empty($peerSuffix)) {
+ // Throw exception
+ throw new InvalidArgumentException('peerSuffix is empty');
+ } elseif(!$this->getSocketInstance()->isValidSocket()) {
+ // Invalid socket
+ throw new LogicException(sprintf('this->socketInstance->socketResource=%s is not valid', $this->getSocketInstance()->getSocketResource()));
+ }
// Get next socket instance from pool over the factory
$socketInstance = SocketFactory::createNextAcceptedSocketFromPool($this->getPoolInstance(), $this->getSocketInstance());
// Init peer address/port
$peerAddress = '0.0.0.0';
- $peerPort = '0';
+ $peerPort = 0;
// Get peer name
if (!$socketInstance->determineSocketPeerName($peerAddress, $peerPort)) {
* @return $accepts Whether this listener does accept
*/
public function ifListenerAcceptsPackageData (DeliverablePackage $packageInstance) {
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: packageInstance=%s - CALLED!', strtoupper($this->getProtocolName()), $packageInstance));
-
// Check if same socket protocol
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: packageInstance=%s - CALLED!', strtoupper($this->getProtocolName()), $packageInstance));
$socketProtocol = $this->getSocketInstance()->getSocketProtocol();
// Get UNL instance
// Get protocol from it
$unlProtocol = $locatorInstance->getUnlProtocol();
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: locatorInstance->unlProtocol=%s,socketProtocol=%s', strtoupper($this->getProtocolName()), $unlProtocol, $socketProtocol));
-
// Is same protocol?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: locatorInstance->unlProtocol=%s,socketProtocol=%s', strtoupper($this->getProtocolName()), $unlProtocol, $socketProtocol));
$accepts = ($unlProtocol == $socketProtocol);
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: accepts=%d - EXIT!', strtoupper($this->getProtocolName()), $accepts));
-
// Return the result
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER: accepts=%d - EXIT!', strtoupper($this->getProtocolName()), $accepts));
return $accepts;
}
* @param $version Version number of this miner
* @return void
*/
- public final function enableIsActive ($isActive = TRUE) {
- $this->isActive = (bool) $isActive;
+ public final function enableIsActive (bool $isActive = TRUE) {
+ $this->isActive = $isActive;
}
/**
* @param $isActive Whether the hub is active
* @return void
*/
- public final function enableIsActive ($isActive = TRUE) {
- $this->isActive = (bool) $isActive;
+ public final function enableIsActive (bool $isActive = TRUE) {
+ $this->isActive = $isActive;
}
/**
* @param $acceptAnnouncements Whether this node accepts announcements (default: TRUE)
* @return void
*/
- protected final function enableAcceptingAnnouncements ($acceptAnnouncements = TRUE) {
+ protected final function enableAcceptingAnnouncements (bool $acceptAnnouncements = TRUE) {
$this->acceptAnnouncements = $acceptAnnouncements;
}
* @param $acceptDhtBootstrap Whether this node accepts DHT bootstrap requests (default: TRUE)
* @return void
*/
- public final function enableAcceptDhtBootstrap ($acceptDhtBootstrap = TRUE) {
+ public final function enableAcceptDhtBootstrap (bool $acceptDhtBootstrap = TRUE) {
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE: Enabling DHT bootstrap requests ...');
$this->acceptDhtBootstrap = $acceptDhtBootstrap;
}
use Org\Mxchange\CoreFramework\Socket\InvalidSocketException;
// Import SPL stuff
+use \InvalidArgumentException;
use \LogicException;
/**
* @param $socketInstance An instance of a StorableSocket class
* @param $connectionType Type of connection, can only be 'incoming', 'outgoing' or 'server'
* @return void
+ * @throws InvalidArgumentException If $connectionType is empty
* @throws InvalidConnectionTypeException If the provided connection type is not valid
*/
- public function addPeer (StorableSocket $socketInstance, $connectionType) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ',connectionType=' . $connectionType . ' - CALLED!');
+ public function addPeer (StorableSocket $socketInstance, string $connectionType) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketResource[%s]=%s,connectionType=%s - CALLED!', gettype($socketInstance->getSocketResource()), $socketInstance->getSocketResource(), $connectionType));
+ if (empty($connectionType)) {
+ // Throw exception
+ throw new InvalidArgumentException('connectionType is empty');
+ }
// Validate the socket
$this->validateSocket($socketInstance);
if (!$socketInstance->isValidConnectionType($connectionType)) {
// Is not a valid connection type!
throw new InvalidConnectionTypeException(array($this, $connectionType), self::EXCEPTION_INVALID_CONNECTION_TYPE);
- } // END - if
+ }
// Default is this peer's IP
$peerAddress = '0.0.0.0';
- $peerPort = '0';
-
- // Trace message
- //* NOSY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEFAULT-PEER-POOL: Calling this->getListenerInstance() ...');
+ $peerPort = 0;
// The socket resource should not match server socket
if (!$this->getListenerInstance()->getSocketInstance()->equals($socketInstance)) {
* @param $connectionType Type of connection, can only be 'incoming', 'outgoing' or 'server'
* @return $sockets An array with sockets of given type
* @throws InvalidConnectionTypeException If the found connection type is not valid
+ * @throws InvalidArgumentException If $connectionType is empty
*/
- public function getSocketsByConnectionType ($connectionType) {
+ public function getSocketsByConnectionType (string $connectionType) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: connectionType=%s - CALLED!', $connectionType));
+ if (empty($connectionType)) {
+ // Throw exception
+ throw new InvalidArgumentException('connectionType is empty');
+ }
+
// Get the array list
$socketArrays = $this->getArrayFromList('pool');
* @throws InvalidConnectionTypeException If the provided connection type is not valid
* @throws LogicException If an expected array element is missing
*/
- public function getSocketFromPackageInstance (DeliverablePackage $packageInstance, $connectionType = NULL) {
+ public function getSocketFromPackageInstance (DeliverablePackage $packageInstance, string $connectionType = NULL) {
// Default is no socket
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL:packageInstance=%s,connectionType[%s]=%s - CALLED!', $packageInstance->__toString(), gettype($connectionType), $connectionType));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: packageInstance=%s,connectionType[%s]=%s - CALLED!', $packageInstance->__toString(), gettype($connectionType), $connectionType));
$socketInstance = NULL;
// Resolve recipient (UNL) into a handler instance
// Init peer address/port
$peerAddress = '0.0.0.0';
- $peerPort = '0';
+ $peerPort = 0;
// Try to get the "peer"'s name
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DEFAULT-PEER-POOL: socketInstance->socketResource=%s,socketInstance->socketProtocol=%s,socketArray[%s]=%s', $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketResource(), $socketArray[Poolable::SOCKET_ARRAY_INSTANCE]->getSocketProtocol(), Poolable::SOCKET_ARRAY_CONN_TYPE, $socketArray[Poolable::SOCKET_ARRAY_CONN_TYPE]));
use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
+// Import SPL stuff
+use \InvalidArgumentException;
+
/**
* A generic XML template engine class
*
* @param $key Key to read from
* @return $value Value from variable
*/
- public function readXmlData ($key) {
+ public function readXmlData (string $key) {
+ // Is key parameter valid?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: key=%s - CALLED!', $key));
+ if (empty($key)) {
+ // Throw exception
+ throw new InvalidArgumentException('Parameter key is empty');
+ }
+
// Read the variable
$value = parent::readVariable($key, 'general');
* @param $version Version number of this apt-proxy
* @return void
*/
- function enableIsActive ($isActive = TRUE);
+ function enableIsActive (bool $isActive = TRUE);
/**
* Determines whether the apt-proxy is active
*/
function isActive ();
}
-
-// [EOF]
-?>
* @param $version Version number of this chatter
* @return void
*/
- function enableIsActive ($isActive = TRUE);
+ function enableIsActive (bool $isActive = TRUE);
/**
* Determines whether the chatter is active
*/
function isActive ();
}
-
-// [EOF]
-?>
* @return $result Result from asking for peer address
* @throws InvalidSocketException If stored socket is invalid
*/
- function determineSocketPeerName (&$peerAddress, &$peerPort);
+ function determineSocketPeerName (string &$peerAddress, int &$peerPort);
/**
* Calls socket_select() on stored socket resource
* @param $unl A Universal Node Locator
* @return $matches Whether $address matches with the one from package data
*/
- function ifAddressMatches ($unl);
+ function ifAddressMatches (string $unl);
/**
* Checks whether the given socket matches with stored
* @throws InvalidSocketException If the stored socket resource is no socket resource
* @throws NoSocketErrorDetectedException If socket_last_error() gives zero back
*/
- function handleSocketError ($method, $line);
+ function handleSocketError (string $method, int $line);
/**
* Do the shutdown sequence for this connection helper
* @param $connectionType Type of connection, can be 'incoming', 'outgoing' or 'server'
* @return $isValid Whether the provided connection type is valid
*/
- function isValidConnectionType ($connectionType);
+ function isValidConnectionType (string $connectionType);
/**
* Clears last socket error
* @param $version Version number of this crawler
* @return void
*/
- function enableIsActive ($isActive = TRUE);
+ function enableIsActive (bool $isActive = TRUE);
/**
* Determines whether the crawler is active
*/
function initCrawler (Stateable $stateInstance);
}
-
-// [EOF]
-?>
* @param $version Version number of this cruncher
* @return void
*/
- function enableIsActive ($isActive = TRUE);
+ function enableIsActive (bool $isActive = TRUE);
/**
* Determines whether the cruncher is active
*/
function initBufferQueues ();
}
-
-// [EOF]
-?>
* @param $value Value to compare if key matches
* @return $recipients An indexed array with DHT recipients
*/
- function getResultFromKeyValue ($key, $value);
+ function getResultFromKeyValue (string $key, $value);
/**
* Enable DHT bootstrap request acceptance for local node
* @throws NoListGroupException If the procol group is not found in peer list
* @throws NullPointerException If listenerInstance is NULL
*/
- function discoverSocket (DeliverablePackage $packageInstance, $connectionType);
+ function discoverSocket (DeliverablePackage $packageInstance, string $connectionType);
/**
* Tries to dicover the right listener instance
* @param $value Value to check on found key
* @return $recipientList Array with DHT recipients from given key/value pair
*/
- function findRecipientsByKey ($key, $value);
+ function findRecipientsByKey (string $key, $value);
/**
* Enable DHT bootstrap request acceptance for local node
* @param $version Version number of this miner
* @return void
*/
- function enableIsActive ($isActive = TRUE);
+ function enableIsActive (bool $isActive = TRUE);
/**
* Determines whether the miner is active
* @return void
* @throws InvalidConnectionTypeException If the provided connection type is not valid
*/
- function addPeer (StorableSocket $socketInstance, $connectionType);
+ function addPeer (StorableSocket $socketInstance, string $connectionType);
/**
* Getter for array of all socket resources
* @return $sockets An array with sockets of given type
* @throws InvalidConnectionTypeException If the found connection type is not valid
*/
- function getSocketsByConnectionType ($connectionType);
+ function getSocketsByConnectionType (string $connectionType);
}
* @return $socketInstance An instance of a StorableSocket class
* @throws InvalidConnectionTypeException If the provided connection type is not valid
*/
- function getSocketFromPackageInstance (DeliverablePackage $packageInstance, $connectionType = NULL);
+ function getSocketFromPackageInstance (DeliverablePackage $packageInstance, string $connectionType = NULL);
}
-Subproject commit 6d6b58a1042e6ad5d6277f8ff5144c95f1abd427
+Subproject commit c15d02389e67a880e7b4c6b19f2b06722efa4e6f