namespace Hub\Generic;
// Import application-specific stuff
+use Hub\Container\Socket\StorableSocket;
use Hub\Handler\Protocol\HandleableProtocol;
use Hub\Handler\Network\RawData\BaseRawDataHandler;
use Hub\Information\ShareableInfo;
*/
private $protocolInstance = NULL;
+ /**
+ * A StorableSocket instance
+ */
+ private $socketInstance = NULL;
+
/**
* Name of used protocol
*/
$this->protocolName = $protocolName;
}
+ /**
+ * 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;
+ }
+
}
// Import application-specific stuff
use Hub\Handler\Network\RawData\BaseRawDataHandler;
+use Hub\Factory\Network\Locator\UniversalNodeLocatorFactory;
use Hub\Factory\Socket\SocketFactory;
use Hub\Helper\Connection\BaseConnectionHelper;
use Hub\Information\ShareableInfo;
// Import SPL stuff
use \BadMethodCallException;
+use \LogicException;
/**
* A Socket Container class
return $this->socketProtocol;
}
+ /**
+ * Getter for socket recipient array element
+ *
+ * @return $recipient Recipient array element
+ * @throws LogicException If 'recipient' array element is not found
+ */
+ public function getSocketRecipient () {
+ // Get package data
+ $packageData = $this->getPackageData();
+
+ // Is the element there?
+ if (!isset($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) {
+ // Abort here
+ throw new LogicException(sprintf('packageData[%s] is not set.', NetworkPackage::PACKAGE_DATA_RECIPIENT));
+ } // END - if
+
+ // Return it
+ return $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT];
+ }
+
/**
* Validates stored stocket
*
return $result;
}
+ /**
+ * Connects to socket recipient (supplied in package data array
+ *
+ * @return $result Result from calling socket_connect()
+ * @throws InvalidSocketException If stored socket is invalid
+ */
+ public function connectToSocketRecipient () {
+ // Should be valid socket
+ if (!$this->isValidSocket()) {
+ // Throw exception
+ throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
+ } // END - if
+
+ // Get recipient UNL
+ $unlRecipient = $this->getSocketRecipient();
+
+ // Create UNL instance from it. This will validate the connection
+ $unlInstance = UniversalNodeLocatorFactory::createUnlInstanceFromString($unlRecipient);
+
+ // Try to connect to it
+ $result = socket_connect($this->getSocketResource(), $unlInstance->getUnlAddress(), $unlInstance->getUnlPort());
+
+ // Return result
+ return $result;
+ }
+
/**
* Do the shutdown sequence for this connection helper
*
* @return void
* @throws SocketShutdownException If the current socket could not be shut down
+ * @throws BaseMethodCallException If this method is possibly called twice
* @todo We may want to implement a filter for ease notification of other objects like our pool
* @todo rewrite this!
*/
public function doShutdown () {
+ // Should be valid socket
+ if (!$this->isValidSocket()) {
+ // Throw exception
+ throw new BaseMethodCallException(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());
$this->partialStub('Please rewrite this method.');
* @return $result Whether OOB has been enabled
*/
public function enableSocketOutOfBandData () {
+ $this->partialStub('Please implement this method.');
+ }
+
+ /**
+ * Clears last socket error
+ *
+ * @return void
+ * @throws InvalidSocketException If stored socket is invalid
+ * @throws BadMethodCallException If no socket error was reported but method called
+ */
+ public function clearLastSocketError () {
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Clearing socket error - CALLED!');
+
+ // Should be valid socket
+ if (!$this->isValidSocket()) {
+ // Throw exception
+ throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
+ } elseif ($this->getLastSocketErrorCode() === 0) {
+ // Nothing to clear
+ throw new BadMethodCallException(sprintf('Socket "%s" has no error reported, but method is called.', $this->getSocketResource()));
+ }
+
+ // Clear last error
+ socket_clear_error($this->getSocketResource());
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!');
}
/**
/**
* Handles socket error 'operation already in progress' which happens in
- * method connectToPeerByUnlInstance() on timed out connection
- * attempts.
+ * method connectToPeerBySocketRecipient() on timed-out connection attempts.
*
* @param $socketData Valid socket data array
* @return void
return $result;
}
- /**
- * Clears last socket error
- *
- * @return void
- * @throws InvalidSocketException If stored socket is invalid
- * @throws BadMethodCallException If no socket error was reported but method called
- */
- private function clearLastSocketError () {
- // Trace message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Clearing socket error - CALLED!');
-
- // Should be valid socket
- if (!$this->isValidSocket()) {
- // Throw exception
- throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
- } elseif ($this->getLastSocketErrorCode() === 0) {
- // Nothing to clear
- throw new BadMethodCallException(sprintf('Socket "%s" has no error reported, but method is called.', $this->getSocketResource()));
- }
-
- // Clear last error
- socket_clear_error($this->getSocketResource());
-
- // Trace message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!');
- }
-
}
// Import application-specific stuff
use Hub\Factory\Fragmenter\FragmenterFactory;
use Hub\Factory\Network\NetworkPackageFactory;
+use Hub\Factory\State\Peer\PeerStateFactory;
use Hub\Helper\BaseHubSystemHelper;
use Hub\Network\Package\NetworkPackage;
// Exception codes
const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x9100;
- /**
- * (IP) Adress used
- */
- private $address = 0;
-
/**
* Sent data in bytes
*/
*/
public final function __toString () {
// Class name representation
- $class = self::getConnectionClassName($this->getAddress(), $this->getConnectionPort(), parent::__toString());
+ $class = self::getConnectionClassName($this->getSocketInstance(), parent::__toString());
// Return it
return $class;
}
- /**
- * Getter for IP address
- *
- * @return $address The IP address
- */
- public final function getAddress () {
- return $this->address;
- }
-
- /**
- * Setter for IP address
- *
- * @param $address The IP address
- * @return void
- */
- protected final function setAddress ($address) {
- $this->address = $address;
- }
-
/**
* Setter for isInitialized
*
namespace Hub\Helper\Connection\Network\IpV4;
// Import application-specific stuff
+use Hub\Factory\State\Peer\PeerStateFactory;
use Hub\Helper\Connection\BaseConnectionHelper;
use Hub\Locator\Node\LocateableNode;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class BaseIpV4ConnectionHelper extends BaseConnectionHelper {
- /**
- * Port number used
- */
- private $connectionPort = 0;
-
/**
* Protected constructor
*
parent::__construct($className);
}
- /**
- * Getter for port number to
- *
- * @return $connectionPort The port number
- */
- public final function getConnectionPort () {
- return $this->connectionPort;
- }
-
- /**
- * Setter for port number
- *
- * @param $connectionPort The port number
- * @return void
- */
- protected final function setConnectionPort ($connectionPort) {
- $this->connectionPort = $connectionPort;
- }
-
/**
* Attempts to connect to a peer by given IP number and port from an UNL
* instance with currently configured timeout.
*
- * @param $unlInstance An instance of a LocateableNode class
* @return $isConnected Whether the connection went fine
* @see Please see http://de.php.net/manual/en/function.socket-connect.php#84465 for original code
* @todo Rewrite the while() loop to a iterator to not let the software stay very long here
*/
- protected function connectToPeerByUnlInstance (LocateableNode $unlInstance) {
+ protected function connectToPeerBySocketRecipient () {
+ //* DEBUG-DIE: */ die(__METHOD__.':socketInstance='.print_r($this->getSocketInstance(), TRUE));
+
// Only call this if the connection is fully initialized
assert($this->isInitialized());
- // Is the UNL data complete?
- assert($unlInstance->isValidUnl());
-
// "Cache" socket resource and timeout config
$timeout = $this->getConfigInstance()->getConfigEntry('socket_timeout_seconds');
// Debug output
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Trying to connect to ' . $unlInstance->getUnlAddress() . ':' . $unlInstance->getUnlPort() . ' with socketResource[' . gettype($this->getSocketInstance()->getSocketResource()) . ']=' . $this->getSocketInstance()->getSocketResource() . ' ...');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-HELPER: Trying to connect to %s with socketResource[%s]=%s ...', $this->getSocketInstance()->getSocketRecipient(), gettype($this->getSocketInstance()->getSocketResource()), $this->getSocketInstance()->getSocketResource()));
// Get current time
$hasTimedOut = FALSE;
$time = time();
- $this->partialStub('Please rewrite this part.');
- return;
// Try to connect until it is connected
- while ($isConnected = !@socket_connect($this->getSocketInstance(), $unlData[LocateableNode::UNL_PART_ADDRESS], $unlData[LocateableNode::UNL_PART_PORT])) {
+ while ($isConnected = !$this->getSocketInstance()->connectToSocketRecipient()) {
// Get last socket error
- $socketError = socket_last_error($this->getSocketInstance());
+ $socketError = $this->getSocketInstance()->getLastSocketErrorCode();
// Log error code and status
/* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: socketError=' . $socketError . ',isConnected=' . intval($isConnected));
$isConnected = TRUE;
// Clear error
- socket_clear_error($this->getSocketInstance());
+ $this->getSocketInstance()->clearLastSocketError();
} // END - if
// Is the peer connected?
}
}
- // Set address and maybe port
- $helperInstance->setAddress($unlInstance->getUnlAddress());
- $helperInstance->setConnectionPort($unlInstance->getUnlPort());
-
// Now connect to it
- if (!$helperInstance->connectToPeerByUnlInstance($unlData)) {
+ if (!$helperInstance->connectToPeerBySocketRecipient()) {
// Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HELPER: helperInstance=' . $helperInstance->__toString() . ',unlData=' . print_r($unlData, TRUE));
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TCP-CONNECTION-HELPER: helperInstance=%s,unlInstance.unlData=%s', $helperInstance->__toString(), print_r($unlInstance->getUnlData(), TRUE)));
// Handle socket error
- $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, $unlData);
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, $unlInstance->getUnlData());
} // END - if
// Okay, that should be it. Return it...
- return $socketResource;
+ return $socketInstance;
}
/**
*/
public function doShutdown () {
// Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HELPER: Shutting down connection ... - CALLED!');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-CONNECTION-HELPER: Shutting down connection ... - CALLED!');
$this->partialStub('Please implement this method.');
}
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(': protocolName=' . $helperInstance->getProtocolName() . ',helperInstance=' . $helperInstance->__toString() . ',socketResource=' . $helperInstance->getSocketResource() . ' - CALLED!');
// Fill the generic array with several data from the listener:
- $this->setProtocolName($helperInstance->getProtocolName());
- $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $helperInstance->getAddress());
- $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $helperInstance->getConnectionPort());
+ $this->setProtocolName($helperInstance->getSocketInstance()->getSocketProtocol());
+ $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_ADDRESS , $helperInstance->getSocketInstance()->getRecipientAddress());
+ $this->setGenericArrayElement('connection', 'dummy', 'dummy', LocateableNode::UNL_PART_PORT , $helperInstance->getSocketInstance()->getRecipientPort());
// Set helper here
$this->setHelperInstance($helperInstance);
*/
private $poolInstance = NULL;
- /**
- * A StorableSocket instance
- */
- private $socketInstance = NULL;
-
/**
* Protected constructor
*
$this->getIteratorInstance()->next();
}
- /**
- * 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;
- }
-
}
return $unlData[NodeInformationDatabaseWrapper::DB_COLUMN_INTERNAL_UNL];
}
+ /**
+ * Some "getter" for UNL address part.
+ *
+ * @return $unlAddress UNL address part
+ */
+ public function getUnlAddress () {
+ // Get UNL data
+ $unlData = $this->getUnlData();
+
+ // Default value is NULL
+ $unlPort = NULL;
+
+ // Is the element there?
+ if (isset($unlData[LocateableNode::UNL_PART_ADDRESS])) {
+ // Use it
+ $unlPort = $unlData[LocateableNode::UNL_PART_ADDRESS];
+ } // END - if
+
+ // Return it
+ return $unlPort;
+ }
+
/**
* Some "getter" for UNL port (if supported). If not supported, NULL is
* being returned.
*
- * @return $unlPort Port number of UNL
+ * @return $unlPort Port number of UNL or NULL
*/
public function getUnlPort () {
// Get UNL data
$this->parseUniversalNodeLocator($unl);
}
+ /**
+ * Getter for UNL data array
+ *
+ * @return $unlData An array with UNL data
+ */
+ public final function getUnlData () {
+ return $this->unlData;
+ }
+
/**
* Parses the given UNL by splitting it up in its components. The UNL ...
*
} // END - foreach
}
- /**
- * Getter for UNL data array
- *
- * @return $unlData An array with UNL data
- */
- private function getUnlData () {
- return $this->unlData;
- }
-
/**
* Validates given UNL very basicly by given regular expression. You
* normally don't need/want to overwrite this method as this is a very basic
// Import framework stuff
use CoreFramework\Factory\ObjectFactory;
+use CoreFramework\Resolver\BaseResolver;
/**
* A generic state resolver class
// Import application-specific stuff
use Hub\Container\Socket\StorableSocket;
+use Hub\Factory\State\Peer\PeerStateFactory;
use Hub\Helper\Connection\ConnectionHelper;
// Import framework stuff
<?php
// Own namespace
-namespace Hub\Peer\State;
+namespace Hub\State\Peer;
// Import framework stuff
use CoreFramework\State\BaseState;
// Import framework stuff
use CoreFramework\Stream\BaseStream;
+use CoreFramework\Stream\Output\OutputStream;
/**
* A RawDataOutputStream class
///////////////////////////////////////////////////////////////////////////////
// CFG: PEER-INIT-STATE-CLASS
-$cfg->setConfigEntry('peer_init_state_class', 'InitPeerState');
+$cfg->setConfigEntry('peer_init_state_class', 'Hub\State\Peer\InitPeerState');
// CFG: PEER-CONNECTED-STATE-CLASS
-$cfg->setConfigEntry('peer_connected_state_class', 'ConnectedPeerState');
+$cfg->setConfigEntry('peer_connected_state_class', 'Hub\State\Peer\ConnectedPeerState');
// CFG: PEER-PROBLEM-STATE-CLASS
-$cfg->setConfigEntry('peer_problem_state_class', 'ProblemPeerState');
+$cfg->setConfigEntry('peer_problem_state_class', 'Hub\State\Peer;\ProblemPeerState');
// CFG: PEER-CONNECTION-REFUSED-STATE-CLASS
-$cfg->setConfigEntry('peer_connection_refused_state_class', 'ConnectionRefusedPeerState');
+$cfg->setConfigEntry('peer_connection_refused_state_class', 'Hub\State\Peer\ConnectionRefusedPeerState');
// CFG: PEER-CONNECTION-TIMED-OUT-STATE-CLASS
-$cfg->setConfigEntry('peer_connection_timed_out_state_class', 'ConnectionTimedOutPeerState');
+$cfg->setConfigEntry('peer_connection_timed_out_state_class', 'Hub\State\Peer\ConnectionTimedOutPeerState');
// CFG: PEER-TRANSPORT-ENDPOINT-STATE-CLASS
-$cfg->setConfigEntry('peer_transport_endpoint_state_class', 'TransportEndpointGonePeerState');
+$cfg->setConfigEntry('peer_transport_endpoint_state_class', 'Hub\State\Peer\TransportEndpointGonePeerState');
// CFG: PEER-OPERATION-ALREADY-PROGRESS-STATE-CLASS
-$cfg->setConfigEntry('peer_operation_already_progress_state_class', 'OperationAlreadyProgressPeerState');
+$cfg->setConfigEntry('peer_operation_already_progress_state_class', 'Hub\State\Peer\OperationAlreadyProgressPeerState');
// CFG: PEER-NO-ROUTE-TO-HOST-STATE-CLASS
-$cfg->setConfigEntry('peer_no_route_to_host_state_class', 'NoRouteToHostPeerState');
+$cfg->setConfigEntry('peer_no_route_to_host_state_class', 'Hub\State\Peer\NoRouteToHostPeerState');
///////////////////////////////////////////////////////////////////////////////
// DHT states
namespace Hub\Generic;
// Import application-specific stuff
+use Hub\Container\Socket\StorableSocket;
// Inport frameworks stuff
use CoreFramework\Generic\FrameworkInterface;
*/
function getSessionId ();
+ /**
+ * Setter for socket instance
+ *
+ * @param $socketInstance A StorableSocket instance
+ * @return void
+ */
+ function setSocketInstance (StorableSocket $socketInstance);
+
+ /**
+ * Getter for socket instance
+ *
+ * @return $socketInstance An instance of a StorableSocket class
+ */
+ function getSocketInstance ();
+
}
*/
function enableSocketOutOfBandData ();
+ /**
+ * Connects to socket recipient (supplied in package data array
+ *
+ * @return $result Result from calling socket_connect()
+ * @throws InvalidSocketException If stored socket is invalid
+ */
+ function connectToSocketRecipient ();
+
/**
* Getter for socket procotol field
*
*/
function getSocketProtocol ();
+ /**
+ * Getter for socket recipient array element
+ *
+ * @return $recipient Recipient array element
+ * @throws LogicException If 'recipient' array element is not found
+ */
+ function getSocketRecipient ();
+
/**
* Checks whether the given Universal Node Locator matches with the one from package data
*
*
* @return void
* @throws SocketShutdownException If the current socket could not be shut down
+ * @throws BaseMethodCallException If this method is possibly called twice
*/
function doShutdown ();
*/
function isValidConnectionType ($connectionType);
+ /**
+ * Clears last socket error
+ *
+ * @return void
+ * @throws InvalidSocketException If stored socket is invalid
+ * @throws BadMethodCallException If no socket error was reported but method called
+ */
+ function clearLastSocketError ();
+
}
*/
function doShutdown ();
- /**
- * Getter for port number
- *
- * @return $port The port number
- */
- function getConnectionPort ();
-
- /**
- * Getter for IP address
- *
- * @return $address The IP address
- */
- function getAddress ();
-
/**
* Static "getter" for this connection class' name
*
namespace Hub\Listener;
// Import application-specific stuff
-use Hub\Container\Socket\StorableSocket;
use Hub\Generic\HubInterface;
/**
*/
function getPoolInstance ();
- /**
- * Setter for socket instance
- *
- * @param $socketInstance A StorableSocket instance
- * @return void
- */
- function setSocketInstance (StorableSocket $socketInstance);
-
- /**
- * Getter for socket instance
- *
- * @return $socketInstance An instance of a StorableSocket class
- */
- function getSocketInstance ();
-
}
*/
function getInternalUnl ();
+ /**
+ * Some "getter" for UNL address.
+ *
+ * @return $unlAddress Address part of UNL
+ */
+ function getUnlAddress ();
+
/**
* Some "getter" for UNL port (if supported). If not supported, NULL is
* being returned.
*
- * @return $unlPort Port number of UNL
+ * @return $unlPort Port number of UNL or NULL
*/
function getUnlPort ();
+ /**
+ * Getter for UNL data array
+ *
+ * @return $unlData An array with UNL data
+ */
+ function getUnlData ();
+
/**
* Parses given UNL string as UNL array
*
-Subproject commit fd5598626e163040b19bf8e153d4898a49038b23
+Subproject commit e9e5242797adec674d25da4e641965f8d6dcbecd