// Import application-specific stuff
use Org\Shipsimu\Hub\Generic\HubInterface;
-use Org\Shipsimu\Hub\Listener\Listenable;
use Org\Shipsimu\Hub\Pool\Poolable;
+use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait;
// Import framework stuff
use Org\Mxchange\CoreFramework\Container\BaseContainer;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class BaseHubContainer extends BaseContainer implements HubInterface {
- /**
- * Listener instance
- */
- private $listenerInstance = NULL;
+ // Load traits
+ use ListenableTrait;
/**
* Listener pool instance
return $this->listenerPoolInstance;
}
- /**
- * Setter for listener instance
- *
- * @param $listenerInstance A Listenable instance
- * @return void
- */
- public final function setListenerInstance (Listenable $listenerInstance) {
- $this->listenerInstance = $listenerInstance;
- }
-
- /**
- * Getter for listener instance
- *
- * @return $listenerInstance A Listenable instance
- */
- public final function getListenerInstance () {
- return $this->listenerInstance;
- }
-
}
// Import application-specific stuff
use Org\Shipsimu\Hub\Generic\HubInterface;
-use Org\Shipsimu\Hub\Node\Node;
use Org\Shipsimu\Hub\Pool\Poolable;
+use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait;
+use Org\Shipsimu\Hub\Traits\Node\NodeTrait;
// Import framework stuff
use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseFrontend;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class BaseHubDatabaseFrontend extends BaseDatabaseFrontend implements HubInterface {
- /**
- * Listener instance
- */
- private $listenerInstance = NULL;
+ // Load traits
+ use ListenableTrait;
+ use NodeTrait;
/**
* Listener pool instance
*/
private $listenerPoolInstance = NULL;
- /**
- * Node instance
- */
- private $nodeInstance = NULL;
-
/**
* Protected constructor
*
return $this->listenerPoolInstance;
}
- /**
- * Setter for listener instance
- *
- * @param $listenerInstance A Listenable instance
- * @return void
- */
- public final function setListenerInstance (Listenable $listenerInstance) {
- $this->listenerInstance = $listenerInstance;
- }
-
- /**
- * Getter for listener instance
- *
- * @return $listenerInstance A Listenable instance
- */
- public final function getListenerInstance () {
- return $this->listenerInstance;
- }
-
- /**
- * 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;
- }
-
}
* @return $resultInstance An instance of a SearchableResult class
* @throws InvalidArgumentException If parameter $sessionId is not valid
*/
- public function findNodeLocalBySessionId ($sessionId) {
+ public function findNodeLocalBySessionId (string $sessionId) {
// Validate parameter
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DHT-WRAPPER: sessionId=%s - CALLED!', $sessionId));
if (empty($sessionId)) {
use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+// Import SPL stuff
+use \UnexpectedValueException;
+
/**
* A database frontend for peer state lookups
*
* @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
+ * @throws UnexpectedValueException If $resultInstance->next() returns FALSE
*/
public function isSamePeerState (ConnectionHelper $helperInstance, DeliverablePackage $packageInstance) {
// Debug message
$resultInstance = $this->doSelectByCriteria($searchInstance);
// Do we have an entry? This should always the case
- assert($resultInstance->next());
+ if (!$resultInstance->next()) {
+ // No next record!
+ throw new UnexpectedValueException(sprintf('resultInstance=%s,count()=%d has no next entry', $resultInstance->__toString(), $resultInstance->count()));
+ }
// Yes, so get the current (=first) entry from it
$rowData = $resultInstance->current();
use Org\Mxchange\CoreFramework\Traits\Stacker\StackableTrait;
use Org\Mxchange\CoreFramework\Traits\State\StateableTrait;
+// Import SPL stuff
+use \BadMethodCallException;
+use \UnexpectedValueException;
+
/**
* A general DHT class
*
* them by uploading to other (recently appeared) DHT members.
*
* @return void
+ * @throws UnexpectedValueException If $resultInstance is not valid
*/
public function initEntryPublication () {
// Call method on database frontend
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: CALLED!');
$this->getFrontendInstance()->initEntryPublication();
// Get result instance
$resultInstance = $this->getFrontendInstance()->getUnpublishedEntriesInstance();
// Make sure the result instance is valid
- assert($resultInstance instanceof SearchableResult);
- assert($resultInstance->valid());
+ if (!($resultInstance instanceof SearchableResult)) {
+ // Not valid
+ throw new UnexpectedValueException(sprintf('resultInstance[]=%s does not implement SearchableResult', gettype($resultInstance)));
+ } elseif (!$resultInstance->valid()) {
+ // Not valid
+ throw new UnexpectedValueException(sprintf('resultInstance=%s is not valid', $resultInstance->__toString()));
+ }
// "Walk" through all entries
while ($resultInstance->next()) {
assert(is_array($current));
// ... and push it to the next stack
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: Pushing entry with ' . count($current) . ' elements to stack ' . self::STACKER_NAME_PENDING_PUBLISHING . ' ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: Pushing entry with ' . count($current) . ' elements to stack ' . self::STACKER_NAME_PENDING_PUBLISHING . ' ...');
$this->getStackInstance()->pushNamed(self::STACKER_NAME_PENDING_PUBLISHING, $current);
- } // END - while
+ }
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: EXIT!');
}
/**
*/
public function hasEntriesPendingPublication () {
// Determine it if it is not empty
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: CALLED!');
$isPending = ($this->getStackInstance()->isStackEmpty(self::STACKER_NAME_PENDING_PUBLISHING) === FALSE);
// Return status
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: isPending=' . intval($isPending));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DHT: isPending=%d - EXIT!', intval($isPending)));
return $isPending;
}
*/
public function ifDhtIsBooting () {
// Call state instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: CALLED!');
$isBooting = $this->getStateInstance()->ifDhtIsBooting();
// Return status
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: isBooting=' . intval($isBooting));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-DHT: isBooting=%d - EXIT!', intval($isBooting)));
return $isBooting;
}
*
* @return void
* @todo Find out if loadDescriptorXml() can be called only once to avoid a lot methods working.
+ * @throws BadMethodCallException If this method was called but no pending entries publication
*/
public function publishEntry () {
// This test must not fail
- assert($this->hasEntriesPendingPublication());
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: CALLED!');
+ if (!$this->hasEntriesPendingPublication()) {
+ // Bad invocation
+ throw new BadMethodCallException('Has no entries pending publication but method was called.');
+ }
// Is there an instance?
if (!$this->publishHelperInstance instanceof HelpableDht) {
assert(is_array($entry));
// Remove any non-public data the database layer desires
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: Calling this->getFrontendInstance()->removeNonPublicDataFromArray(data) ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: Calling this->getFrontendInstance()->removeNonPublicDataFromArray(data) ...');
$entry = $this->getFrontendInstance()->removeNonPublicDataFromArray($entry);
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: entry[]=' . gettype($entry));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: entry[]=' . gettype($entry));
// Some sanity-checks again
assert(is_array($entry));
$this->publishHelperInstance->getTemplateInstance()->assignMultipleVariables($entry);
// "Publish" the descriptor by sending it to the bootstrap/list nodes
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT: Calling this->publishHelperInstance->sendPackage(' . $this->__toString() . ') ...');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: Calling this->publishHelperInstance->sendPackage(' . $this->__toString() . ') ...');
$this->publishHelperInstance->sendPackage($this);
}
*/
public function hasFullyBootstrapped () {
// Get state and check it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-DHT: CALLED!');
$this->partialStub('Please implement this method.');
}
* @param $sessionId Session id to lookup
* @return $nodeData Node-data array
*/
- public function findNodeLocalBySessionId ($sessionId) {
+ public function findNodeLocalBySessionId (string $sessionId) {
// Validate parameter
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: sessionId=%s - CALLED!', $sessionId));
if (empty($sessionId)) {
* @return void
* @throws NodeSessionIdVerficationException If the node was not found and update is forced
*/
- public function registerNodeByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, $forceUpdate = FALSE) {
+ public function registerNodeByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, bool $forceUpdate = FALSE) {
// Get a search criteria class
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: state=%s,messageInstance=%s,handlerInstance=%s,forceUpdate=%d - CALLED', $this->getPrintableState(), $messageInstance->__toString(), $handlerInstance->__toString(), intval($forceUpdate)));
$searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
* @param $separator Sepator char (1st parameter for explode() call)
* @return $nodeList An array with all found nodes
*/
- public function queryLocalNodeListExceptByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, $excludeKey, $andKey, $separator) {
+ public function queryLocalNodeListExceptByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, string $excludeKey, string $andKey, string $separator) {
// Get a search criteria class
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: messageInstance=%s,handlerInstance=%s,excludeKey=%s,andKey=%s,separator=%s - CALLED!', $messageInstance->__toString(), $handlerInstance->__toString(), $excludeKey, $andKey, $separator));
/* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: state=%s,messageInstance=%s', $this->getPrintableState(), print_r($messageInstance, TRUE)));
*/
public function findRecipientsByPackageInstance (DeliverablePackage $packageInstance) {
// Query get a result instance back from DHT database frontend
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: packageInstance=%s - CALLED!', $packageInstance->__toString()));
$resultInstance = $this->getFrontendInstance()->getResultFromExcludedSender($packageInstance);
// Make sure the result instance is valid
}
// Return filled array
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-DHT-FACADE: recipients()=%d - EXIT!', count($recipients)));
return $recipients;
}
*/
public static final function createListenFileSocket (Listenable $listenerInstance) {
// Create SplFileInfo
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - EXIT!', $listenerInstance->__toString()));
$fileInfo = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('ipc_socket_file_name'));
// Create file name
if (!$socketInstance->isValidSocket()) {
// Something bad happened
throw new InvalidSocketException(array($listenerInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Check if there was an error else
if ($socketInstance->getLastSocketErrorCode() > 0) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Is the file there?
if ((FrameworkBootstrap::isReachableFilePath($socketInstance->getSocketFile())) && (file_exists($socketInstance->getSocketFile()))) {
// Quit here
exit;
- } // END - if
+ }
// Debug message
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $socketInstance->getSocketFile() . ' ...');
if (!$socketInstance->bindSocketToFile()) {
// Handle error here
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Start listen for connections
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.');
if (!$socketInstance->listenToSocket()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Allow non-blocking I/O
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
-
- // Trace message
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
+ }
// Return socket instance
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}
* @todo Rewrite this to also handle IPv6 addresses and sockets
*/
public static final function createTcpOutgoingSocketFromPackageInstance (DeliverablePackage $packageInstance) {
- // Trace message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s - CALLED!', $packageInstance->__toString()));
-
// Create a socket instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: packageInstance=%s - CALLED!', $packageInstance->__toString()));
$socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Debug message
* InvalidSocketException back.
*/
throw new SocketCreationException(array($factoryInstance, $socketInstance->getSocketResource()), StorableSocket::EXCEPTION_SOCKET_CREATION_FAILED);
- } // END - if
+ }
// Check if there was an error else
if ($socketInstance->getLastSocketErrorCode() > 0) {
// Handle this socket error
$helperInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Set the option to reuse the port
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...');
if (!$socketInstance->enableSocketReuseAddress()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
/*
* Set socket to non-blocking mode before trying to establish a link to
if (!$socketInstance->enableSocketNonBlocking()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
-
- // Trace message
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
+ }
// Return it
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}
* @throws InvalidSocketException Thrown if the socket could not be initialized
*/
public static function createListenTcpSocket (Listenable $listenerInstance) {
- // 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
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: listenerInstance=%s - CALLED!', $listenerInstance->__toString()));
$socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Init fake "package" instance, the SocketContainer class requires this
if (!$socketInstance->isValidSocket()) {
// Something bad happened
throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Check if there was an error else
if ($socketInstance->getLastSocketErrorCode() > 0) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Set the option to reuse the port
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...');
if (!$socketInstance->enableSocketReuseAddress()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
/*
* "Bind" the socket to the given address, on given port so this means
if (!$socketInstance->bindSocketToListener()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Start listen for connections
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Listening for connections.');
if (!$socketInstance->listenToSocket()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Now, we want non-blocking mode
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
-
- // Trace message
- //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
+ }
// Return prepepared socket
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}
if (!$socketInstance->isValidSocket()) {
// Something bad happened
throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Check if there was an error else
if ($socketInstance->getLastSocketErrorCode() > 0) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Set the option to reuse the port
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Enabling re-use address ...');
if (!$socketInstance->enableSocketReuseAddress()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
/*
* "Bind" the socket to the given address, on given port so this means
if (!$socketInstance->bindSocketToListener()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Now, we want non-blocking mode
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Trace message
//* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
if (!$iteratorInstance->valid()) {
// Try to rewind it
$iteratorInstance->rewind();
- } // END - if
+ }
// Get current socket instance
$current = $iteratorInstance->current();
if (!$socketInstance->isValidSocket()) {
// Something bad happened
throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ }
// Try to identify socket peer
if (!$socketInstance->identifySocketPeer()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Set timeout to configured seconds
if (!$socketInstance->setSocketTimeoutOptions()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Enable SO_OOBINLINE
if (!$socketInstance->enableSocketOutOfBandData()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Set non-blocking
if (!$socketInstance->enableSocketNonBlocking()) {
// Handle this socket error
$socketInstance->handleSocketError(__METHOD__, __LINE__);
- } // END - if
+ }
// Return found socket instance
/* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance=%s - EXIT!', $socketInstance->__toString()));
// Import application-specific stuff
use Org\Shipsimu\Hub\Generic\HubInterface;
-use Org\Shipsimu\Hub\Listener\Listenable;
use Org\Shipsimu\Hub\Locator\Node\LocateableNode;
use Org\Shipsimu\Hub\Network\Package\Receiver\Assembler\Assembler;
use Org\Shipsimu\Hub\Node\Node;
use Org\Shipsimu\Hub\Pool\Poolable;
+use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait;
+use Org\Shipsimu\Hub\Traits\Node\NodeTrait;
// Import framework stuff
use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
*/
abstract class BaseHubHandler extends BaseHandler implements Handleable, HubInterface {
// Load traits
+ use ListenableTrait;
+ use NodeTrait;
use StackableTrait;
- /**
- * Listener instance
- */
- private $listenerInstance = NULL;
-
/**
* Listener pool instance
*/
private $listenerPoolInstance = NULL;
- /**
- * Node instance
- */
- private $nodeInstance = NULL;
-
/**
* An instance of a LocateableNode class
*/
return $this->universalNodeLocatorInstance;
}
- /**
- * Setter for listener instance
- *
- * @param $listenerInstance A Listenable instance
- * @return void
- */
- public final function setListenerInstance (Listenable $listenerInstance) {
- $this->listenerInstance = $listenerInstance;
- }
-
- /**
- * Getter for listener instance
- *
- * @return $listenerInstance A Listenable instance
- */
- public function getListenerInstance () {
- return $this->listenerInstance;
- }
-
- /**
- * 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;
- }
-
/**
* Setter for assembler instance
*
} // END - if
// Okay, that should be it. Return it...
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-CONNECTION-HELPER: socketInstance=%s - EXIT!', $socketInstance->__toString()));
return $socketInstance;
}
use Org\Shipsimu\Hub\Listener\Listenable;
use Org\Shipsimu\Hub\Locator\Node\LocateableNode;
use Org\Shipsimu\Hub\Traits\Container\Socket\StorableSocketTrait;
+use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait;
// Import framework stuff
use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
*/
class ConnectionInfo extends BaseInfo implements ShareableInfo, Registerable {
// Load traits
+ use ListenableTrait;
use StorableSocketTrait;
- /**
- * Listener instance
- */
- private $listenerInstance = NULL;
-
/**
* Connection type: 'incoming', 'outgoing', 'server'
*/
$this->protocolName = $protocolName;
}
- /**
- * Setter for listener instance
- *
- * @param $listenerInstance A Listenable instance
- * @return void
- */
- public final function setListenerInstance (Listenable $listenerInstance) {
- $this->listenerInstance = $listenerInstance;
- }
-
- /**
- * Getter for listener instance
- *
- * @return $listenerInstance A Listenable instance
- */
- public final function getListenerInstance () {
- return $this->listenerInstance;
- }
-
/**
* Getter for address
*
use Org\Shipsimu\Hub\Container\Socket\StorableSocket;
use Org\Shipsimu\Hub\Factory\Handler\Network\NetworkPackageHandlerFactory;
use Org\Shipsimu\Hub\Factory\Network\NetworkPackageFactory;
-use Org\Shipsimu\Hub\Listener\Listenable;
use Org\Shipsimu\Hub\Network\Networkable;
+use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait;
+use Org\Shipsimu\Hub\Traits\Container\Socket\StorableSocketTrait;
// Import framework stuff
use Org\Mxchange\CoreFramework\Generic\BaseDecorator;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class BaseListenerDecorator extends BaseDecorator implements Visitable {
+ // Load traits
+ use ListenableTrait;
+ use StorableSocketTrait;
+
/**
* Listener type
*/
*/
private $protocolName = 'invalid';
- /**
- * The decorated listener instance
- */
- private $listenerInstance = NULL;
-
/**
* Protected constructor
*
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-LISTENER-DECORATOR: EXIT!', strtoupper($this->getProtocolName())));
}
- /**
- * Setter for socket instance
- *
- * @param $socketInstance A StorableSocket instance
- * @return void
- */
- public final function setSocketInstance (StorableSocket $socketInstance) {
- $this->socketInstance = $socketInstance;
- }
-
- /**
- * Getter for socket instance
- *
- * @return $socketInstance An instance of a StorableSocket class
- */
- public final function getSocketInstance () {
- return $this->socketInstance;
- }
-
- /**
- * Setter for listener instance
- *
- * @param $listenerInstance A Listenable instance
- * @return void
- */
- public final function setListenerInstance (Listenable $listenerInstance) {
- $this->listenerInstance = $listenerInstance;
- }
-
- /**
- * Getter for listener instance
- *
- * @return $listenerInstance A Listenable instance
- */
- public final function getListenerInstance () {
- return $this->listenerInstance;
- }
-
/**
* Getter for protocol name
*
*/
public final static function createUniversalNodeLocator (array $current = []) {
// Get new instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: current()=%d - CALLED!', count($current)));
$locatorInstance = new UniversalNodeLocator();
// Init instance
$locatorInstance->initUniversalNodeLocator($current);
// Return the prepared instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: locatorInstance=%s - EXIT!', $locatorInstance->__toString()));
return $locatorInstance;
}
* @param $unl Universal Node Locator (UNL) to "parse"
* @throws InvalidArgumentException If given UNL is not valid
*/
- private function parseUniversalNodeLocator ($unl) {
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: unl=[%s]=%s - CALLED!', gettype($unl), $unl));
-
+ private function parseUniversalNodeLocator (string $unl) {
// Make sure the UNL is valid
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: unl=[%s]=%s - CALLED!', gettype($unl), $unl));
if (!NodeLocatorUtils::isValidUniversalNodeLocator($unl)) {
// UNL is not valid
throw new InvalidArgumentException(sprintf('unl[%s]=%s is not valid.', gettype($unl), $unl));
- } // END - if
+ }
/*
* "Parse" the UNL "generically", sadly this cannot be done by using preg_match() :-(
$unlParts[1] = explode(':', $unlParts[1]);
// Now there is an almost useable array which then can be copied to the "real" array.
- $unlData = array(
+ $unlData = [
LocateableNode::UNL_PART_PROTOCOL => $unlParts[0],
LocateableNode::UNL_PART_ADDRESS => $unlParts[1][0],
LocateableNode::UNL_PART_EXTRA => $unlParts[1][1],
- );
-
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: unlData=' . print_r($unlData, TRUE) . ' - EXIT!');
+ ];
// Call init method
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: unlData=' . print_r($unlData, TRUE) . ' - EXIT!');
$this->initUniversalNodeLocator($unlData);
+
+ // Trace message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: EXIT!');
}
/**
*/
private function initUniversalNodeLocator (array $unlData = []) {
// Init UNL array
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: CALLED!');
$this->unlData = [];
// Copy all found entries
NodeInformationDatabaseFrontend::DB_COLUMN_INTERNAL_UNL,
NodeInformationDatabaseFrontend::DB_COLUMN_EXTERNAL_UNL) as $key) {
// Init entry
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: key=%s', $key));
$this->unlData[$key] = NULL;
// Is the key found?
if (isset($unlData[$key])) {
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: Copying unlData[' . $key . ']=' . $unlData[$key] . ' ...');
-
// The copy the entry
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('UNIVERSAL-NODE-LOCATOR: Copying unlData[%s]=%s ...', $key, $unlData[$key]));
$this->unlData[$key] = $unlData[$key];
- } // END - if
- } // END - foreach
+ }
+ }
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: EXIT!');
}
/**
if (isset($unlData[LocateableNode::UNL_PART_PROTOCOL])) {
// Use it
$unkProtocol = $unlData[LocateableNode::UNL_PART_PROTOCOL];
- } // END - if
+ }
// Return it
return $unkProtocol;
if (isset($unlData[LocateableNode::UNL_PART_ADDRESS])) {
// Use it
$unkAddress = $unlData[LocateableNode::UNL_PART_ADDRESS];
- } // END - if
+ }
// Return it
return $unkAddress;
if (isset($unlData[LocateableNode::UNL_PART_EXTRA])) {
// Use it
$unlPort = $unlData[LocateableNode::UNL_PART_EXTRA];
- } // END - if
+ }
// Return it
return $unlPort;
if (!is_string($unl)) {
// Abort here
throw new InvalidArgumentException(sprintf('unl[]=%s is not string.', gettype($unl)));
- } // END - if
+ }
// Parse it
$this->parseUniversalNodeLocator($unl);
// Import application-specificl stuff
use Org\Shipsimu\Hub\Generic\BaseHubSystem;
-use Org\Shipsimu\Hub\Listener\Listenable;
use Org\Shipsimu\Hub\Pool\Poolable;
+use Org\Shipsimu\Hub\Traits\Listener\ListenableTrait;
// Import framework stuff
use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class BasePool extends BaseHubSystem implements Poolable, Visitable {
- /**
- * Listener instance
- */
- private $listenerInstance = NULL;
+ // Load traits
+ use ListenableTrait;
/**
* A list of pool entries
return $this->poolEntriesInstance;
}
- /**
- * Setter for listener instance
- *
- * @param $listenerInstance A Listenable instance
- * @return void
- */
- public final function setListenerInstance (Listenable $listenerInstance) {
- $this->listenerInstance = $listenerInstance;
- }
-
- /**
- * Getter for listener instance
- *
- * @return $listenerInstance A Listenable instance
- */
- public final function getListenerInstance () {
- return $this->listenerInstance;
- }
-
/**
* Accepts the visitor to process the visit "request"
*
if (is_null(self::$registryInstance)) {
// Not yet, so create one
self::$registryInstance = new SocketRegistry();
- } // END - if
+ }
// Return the instance
return self::$registryInstance;
// Debug message
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: Final result: isRegistered(' . $socketInstance->getSocketResource() . ')=' . intval($isRegistered));
- } // END - if
- } // END - if
+ }
+ }
// Return the result
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: protocolName=' . $infoInstance->getProtocolName() . ',socketResource[' . gettype($socketInstance->getSocketResource()) . ']=' . $socketInstance->getSocketResource() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
if ($this->isSocketRegistered($infoInstance, $socketInstance)) {
// Throw the exception
throw new SocketAlreadyRegisteredException(array($infoInstance, $socketInstance->getSocketResource()), BaseListener::EXCEPTION_SOCKET_ALREADY_REGISTERED);
- } // END - if
+ }
// Does the instance exist?
if (!$this->isInfoRegistered($infoInstance)) {
if (!$this->isInfoRegistered($listenerInstance)) {
// Throw the exception
throw new NoSocketRegisteredException ($listenerInstance, self::EXCEPTION_SOCKET_NOT_REGISTERED);
- } // END - if
+ }
// Now get the key from the listener
$key = $this->getRegistryKeyFromInfo($listenerInstance);
// Debug message
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: key=' . $key . ',subKey=' . $subKey . ',infoInstance[' . gettype($infoInstance) . ']=' . $infoInstance->__toString() . ' with protocol ' . $infoInstance->getProtocolName() . ' - FOUND!');
break;
- } // END - if
- } // END - foreach
+ }
+ }
// Is no longer NULL set?
if (!is_null($infoInstance)) {
// Then skip here, too
break;
- } // END - if
- } // END - foreach
+ }
+ }
// Return the info instance
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-REGISTRY: infoInstance[]=' . gettype($infoInstance) . ' - EXIT!');
namespace Org\Shipsimu\Hub\Task;
// Import application-specific stuff
-use Org\Shipsimu\Hub\Node\Node;
+use Org\Shipsimu\Hub\Traits\Node\NodeTrait;
// Import framework stuff
use Org\Mxchange\CoreFramework\Task\BaseTask;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class BaseHubTask extends BaseTask {
-
- /**
- * Node instance
- */
- private $nodeInstance = NULL;
+ // Load traits
+ use NodeTrait;
/**
* Protected 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;
- }
-
}
* @return $recipientUniversalNodeLocator Recipient as Universal Node Locator
* @throws InvalidArgumentException If $sessionId is not valid
*/
- protected function resolveUniversalNodeLocatorBySessionId ($sessionId) {
+ protected function resolveUniversalNodeLocatorBySessionId (string $sessionId) {
// Validate parameter
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: sessionId=%s - CALLED!', $sessionId));
if (empty($sessionId)) {
* @return $nodeId Node id
* @throws InvalidArgumentException If $sessionId is not valid
*/
- public static function resolveNodeIdBySessionId ($sessionId) {
+ public static function resolveNodeIdBySessionId (string $sessionId) {
// Validate parameter
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: sessionId=%s - CALLED!', $sessionId));
if (empty($sessionId)) {
* @return $resultInstance An instance of a SearchableResult class
* @throws InvalidArgumentException If parameter $sessionId is not valid
*/
- function findNodeLocalBySessionId ($sessionId);
+ function findNodeLocalBySessionId (string $sessionId);
/**
* Finds a node locally by given UNL instance
* @return $nodeData Node-data array
* @throws InvalidArgumentException If parameter $sessionId is not valid
*/
- function findNodeLocalBySessionId ($sessionId);
+ function findNodeLocalBySessionId (string $sessionId);
/**
* Finds a node locally by given UNL instance
* - external-address (hostname or IP number)
* - listen-port (TCP/UDP listen port for inbound connections)
*
- * @param $messageData An instance of a DeliverableMessage class
+ * @param $messageInstance An instance of a DeliverableMessage class
* @param $handlerInstance An instance of a HandleableDataSet class
* @param $forceUpdate Optionally force update, don't register (default: register if not found)
* @return void
*/
- function registerNodeByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, $forceUpdate = FALSE);
+ function registerNodeByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, bool $forceUpdate = FALSE);
/**
* Queries the local DHT data(base) for a node list with all supported
* object types except the node by given session id.
*
- * @param $messageData An instance of a DeliverableMessage class
+ * @param $messageInstance An instance of a DeliverableMessage class
* @param $handlerInstance An instance of a HandleableDataSet class
* @param $excludeKey Array key which should be excluded
* @param $andKey Array of $separator-separated list of elements which all must match
* @param $separator Sepator char (1st parameter for explode() call)
* @return $nodeList An array with all found nodes
*/
- function queryLocalNodeListExceptByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, $excludeKey, $andKey, $separator);
+ function queryLocalNodeListExceptByMessageInstance (DeliverableMessage $messageInstance, HandleableDataSet $handlerInstance, string $excludeKey, string $andKey, string $separator);
/**
* Inserts given node list array (from earlier database result produced by
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
interface PoolableListener extends Poolable {
-
/**
* Adds a listener instance to this pool
*
--- /dev/null
+<?php
+// Own namespace
+namespace Org\Shipsimu\Hub\Traits\Listener;
+
+// Import application-specific stuff
+use Org\Shipsimu\Hub\Listener\Listenable;
+
+/**
+ * A trait for listener
+ *
+ * @author Roland Haeder <webmaster@shipsimu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+trait ListenableTrait {
+ /**
+ * The decorated listener instance
+ */
+ private $listenerInstance = NULL;
+
+ /**
+ * Setter for listener instance
+ *
+ * @param $listenerInstance A Listenable instance
+ * @return void
+ */
+ public final function setListenerInstance (Listenable $listenerInstance) {
+ $this->listenerInstance = $listenerInstance;
+ }
+
+ /**
+ * Getter for listener instance
+ *
+ * @return $listenerInstance A Listenable instance
+ */
+ public final function getListenerInstance () {
+ return $this->listenerInstance;
+ }
+
+}
--- /dev/null
+<?php
+// Own namespace
+namespace Org\Shipsimu\Hub\Traits\Node;
+
+// Import application-specific stuff
+use Org\Shipsimu\Hub\Node\Node;
+
+/**
+ * A trait for nodes
+ *
+ * @author Roland Haeder <webmaster@shipsimu.org>
+ * @version 0.0.0
+ * @copyright Copyright (c) 2011 - 2014 - 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 <http://www.gnu.org/licenses/>.
+ */
+trait NodeTrait {
+ /**
+ * Node instance
+ */
+ private $nodeInstance = NULL;
+
+ /**
+ * 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;
+ }
+
+}