- splitted sender address and port to have both being copyable (needed later on)
to ShareableInfo class
- used some lesser socket_strerr() but socketInstance calls
Signed-off-by: Roland Häder <roland@mxchange.org>
$socketResource = $this->getSocketResource();
// Get error code and message
- $errorMessage = socket_strerror(socket_last_error($socketResource));
+ $errorMessage = socket_strerror($this->getLastSocketErrorCode());
// Return it
return $errorMessage;
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DHT-WRAPPER: CALLED!');
// Assert on required array field
- assert(isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER]));
+ assert(isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]));
// Get max recipients
$maxRecipients = $this->getConfigInstance()->getConfigEntry('max_dht_recipients');
$searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
// Then exclude 'sender' field as the sender is the current (*this*) node
- $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
+ $searchInstance->addExcludeCriteria(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]);
// Set limit to maximum DHT recipients
$searchInstance->setLimit($maxRecipients);
*/
private function getSenderSearchInstanceFromPackageData (array $packageData) {
// Get the instance
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Searching for sender ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Searching for sender ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]);
$searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
// Add 'sender' as the peer's IP address
- $searchInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
+ $searchInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]);
$searchInstance->setLimit(1);
// Return the instance
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: ' . __FUNCTION__ . ' called with packageData()=' . count($packageData) . ' - CALLED!');
// Is the package valid?
- if (!isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER])) {
+ if (!isset($packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS])) {
// Invalid package found, please report this
$this->debugBackTrace(': packageData=' . print_r($packageData, TRUE));
} // END - if
$dataSetInstance->setUniqueKey(self::DB_COLUMN_PEER_SESSION_ID);
// Add session id
- $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
+ $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]);
// Init peer address/port
$peerAddress = '0.0.0.0';
// Get peer name
if (!$socketInstance->getSocketPeerName($peerAddress, $peerPort)) {
- // Get last error
- $lastError = $socketInstance->getLastSocketErrorCode();
-
// ... and cleartext message from it and put both into criteria
- $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_CODE, $lastError);
- $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_MSG , socket_strerror($lastError));
+ $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_CODE, $socketInstance->getLastSocketErrorCode());
+ $dataSetInstance->addCriteria(self::DB_COLUMN_SOCKET_ERROR_MSG , $socketInstance->getLastSocketErrorMessage());
} // END - if
// Add ip address and port
$this->queryInsertDataSet($dataSetInstance, array(self::DB_COLUMN_PEER_SESSION_ID => TRUE));
// Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER] . ' has been registered.');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS] . ' has been registered.');
}
/**
$dataSetInstance->setUniqueKey(self::DB_COLUMN_PEER_SESSION_ID);
// Add session id
- $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER]);
+ $dataSetInstance->addCriteria(self::DB_COLUMN_PEER_SESSION_ID, $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]);
$dataSetInstance->addCriteria(self::DB_COLUMN_PEER_STATE , $stateInstance->getStateName());
// Try to resolve sender's session id
- $senderData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_SENDER]));
+ $senderData = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]));
// Just make sure that 'invalid:invalid' is not being processed
assert(($senderData[0] != 'invalid') && ($senderData[1] != 'invalid') && ($senderData[2] != 'invalid'));
}
// Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER] . ' has been registered/updated with state ' . $stateInstance->getStateName());
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE-WRAPPER: Peer ' . $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS] . ' has been registered/updated with state ' . $stateInstance->getStateName());
}
/**
// Some checks
assert(
(is_array($decodedData)) &&
- (isset($decodedData[NetworkPackage::PACKAGE_DATA_SENDER])) &&
+ (isset($decodedData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS])) &&
+ (isset($decodedData[NetworkPackage::PACKAGE_DATA_SENDER_PORT])) &&
(isset($decodedData[NetworkPackage::PACKAGE_DATA_RECIPIENT])) &&
(isset($decodedData[NetworkPackage::PACKAGE_DATA_CONTENT])) &&
(isset($decodedData[NetworkPackage::PACKAGE_DATA_STATUS]))
* @param $socketInstance An instance of a StorableSocket class
* @return void
* @throws SocketAlreadyRegisteredException If the given resource is already registered
+ * @throws LogicException If no info instance can be created
*/
protected function registerServerSocketInstance (StorableSocket $socketInstance) {
// Trace message
// Get a connection info instance
$infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener');
+ // Is the instance set?
+ if (!($infoInstance instanceof ShareableInfo)) {
+ // Should not happen
+ throw new LogicException(sprintf('infoInstance[]=%s does not implement ShareableInfo', gettype($infoInstance)));
+ } // END - if
+
// Will the info instance with listener data
$infoInstance->fillWithListenerInformation($this);
*
* @param $peerSuffix Suffix for peer name (e.g. :0 for TCP(/UDP?) connections)
* @return void
+ * @throws LogicException If no info instance can be created
*/
protected function doListenSocketSelect ($peerSuffix) {
// Trace message
// Create a faked package data array
$packageData = array(
- NetworkPackage::PACKAGE_DATA_SENDER => $peerAddress . $peerSuffix,
- NetworkPackage::PACKAGE_DATA_RECIPIENT => $nodeInstance->getSessionId(),
- NetworkPackage::PACKAGE_DATA_STATUS => NetworkPackage::PACKAGE_STATUS_FAKED
+ NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS => $peerAddress . $peerSuffix,
+ NetworkPackage::PACKAGE_DATA_SENDER_PORT => $peerPort,
+ NetworkPackage::PACKAGE_DATA_RECIPIENT => $nodeInstance->getSessionId(),
+ NetworkPackage::PACKAGE_DATA_STATUS => NetworkPackage::PACKAGE_STATUS_FAKED
);
// Get a connection info instance
$infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener');
+ // Is the instance set?
+ if (!($infoInstance instanceof ShareableInfo)) {
+ // Should not happen
+ throw new LogicException(sprintf('infoInstance[]=%s does not implement ShareableInfo', gettype($infoInstance)));
+ } // END - if
+
// Will the info instance with listener data
- $infoInstance->fillWithListenerInformation($this);
+ $infoInstance->fillWithSocketPeerInformation($socketInstance);
// Get a socket registry
$registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
/**
* Named array elements for package data
*/
- const PACKAGE_DATA_SENDER = 'sender';
+ const PACKAGE_DATA_SENDER_ADDRESS = 'sender_address';
+ const PACKAGE_DATA_SENDER_PORT = 'sender_port';
const PACKAGE_DATA_RECIPIENT = 'recipient';
const PACKAGE_DATA_CONTENT = 'content';
const PACKAGE_DATA_STATUS = 'status';
*/
private function isChecksumValid (array $decodedContent, array $decodedData) {
// Get checksum
- $checksum = $this->getHashFromContentSessionId($decodedContent, $decodedData[self::PACKAGE_DATA_SENDER]);
+ $checksum = $this->getHashFromContentSessionId($decodedContent, $decodedData[self::PACKAGE_DATA_SENDER_ADDRESS]);
// Is it the same?
$isChecksumValid = ($checksum == $decodedContent[self::PACKAGE_CONTENT_CHECKSUM]);
// Now prepare the temporary array and push it on the 'undeclared' stack
$this->getStackInstance()->pushNamed(self::STACKER_NAME_UNDECLARED, array(
- self::PACKAGE_DATA_SENDER => $this->getSessionId(),
+ self::PACKAGE_DATA_SENDER_ADDRESS => $this->getSessionId(),
+ self::PACKAGE_DATA_SENDER_PORT => '0',
self::PACKAGE_DATA_RECIPIENT => $helperInstance->getRecipientType(),
self::PACKAGE_DATA_CONTENT => $packageContent,
self::PACKAGE_DATA_STATUS => self::PACKAGE_STATUS_NEW,
* Create 'decodedData' array with all assoziative array elements.
*/
$decodedData = array(
- self::PACKAGE_DATA_SENDER => $decodedArray[self::INDEX_PACKAGE_SENDER],
+ self::PACKAGE_DATA_SENDER_ADDRESS => $decodedArray[self::INDEX_PACKAGE_SENDER],
+ self::PACKAGE_DATA_SENDER_PORT => '0',
self::PACKAGE_DATA_RECIPIENT => $decodedArray[self::INDEX_PACKAGE_RECIPIENT],
self::PACKAGE_DATA_CONTENT => $decodedArray[self::INDEX_PACKAGE_CONTENT],
self::PACKAGE_DATA_STATUS => self::PACKAGE_STATUS_DECODED,
// Checksum of the _decoded_ data
self::PACKAGE_CONTENT_CHECKSUM => $decodedContent[self::INDEX_CHECKSUM],
// Sender's id
- self::PACKAGE_CONTENT_SENDER => $decodedData[self::PACKAGE_DATA_SENDER],
+ self::PACKAGE_CONTENT_SENDER => $decodedData[self::PACKAGE_DATA_SENDER_ADDRESS],
// Hash from decoded raw data
self::PACKAGE_CONTENT_HASH => $decodedData[self::PACKAGE_DATA_HASH],
// Hash of private key
*/
private function getProcessedPackagesIndex (array $packageData) {
return (
- $packageData[NetworkPackage::PACKAGE_DATA_SENDER] . NetworkPackage::PACKAGE_DATA_SEPARATOR .
- $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . NetworkPackage::PACKAGE_DATA_SEPARATOR .
- $packageData[NetworkPackage::PACKAGE_DATA_CONTENT] . NetworkPackage::PACKAGE_DATA_SEPARATOR
+ $packageData[NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS] . NetworkPackage::PACKAGE_DATA_SEPARATOR .
+ $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT] . NetworkPackage::PACKAGE_DATA_SEPARATOR .
+ $packageData[NetworkPackage::PACKAGE_DATA_CONTENT] . NetworkPackage::PACKAGE_DATA_SEPARATOR
);
}
$message = sprintf('[%s:%d] The checksum %s doesnt match the checksum of the content: %s',
$messageArray[0]->__toString(),
$this->getLine(),
- $messageArray[0]->getHashFromContentSessionId($messageArray[1], $messageArray[2][NetworkPackage::PACKAGE_DATA_SENDER]),
+ $messageArray[0]->getHashFromContentSessionId($messageArray[1], $messageArray[2][NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]),
$messageArray[1][NetworkPackage::PACKAGE_CONTENT_CHECKSUM]
);
*/
public function __construct (array $messageArray, $code) {
// Construct the message
- $message = sprintf('[%s:%d] Unexpected package status %s!=%s detected, recipient=%s, sender=%s, hash=%s.',
+ $message = sprintf('[%s:%d] Unexpected package status %s!=%s detected, recipient=%s, sender=%s:%d, hash=%s.',
$messageArray[0]->__toString(),
$this->getLine(),
$messageArray[1][NetworkPackage::PACKAGE_DATA_STATUS],
$messageArray[2],
$messageArray[1][NetworkPackage::PACKAGE_DATA_RECIPIENT],
- $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER],
+ $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS],
+ $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER_PORT],
$messageArray[1][NetworkPackage::PACKAGE_DATA_HASH]
);
$message = sprintf('[%s:%d] Sender %s is already registered.',
$messageArray[0]->__toString(),
$this->getLine(),
- $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER]
+ $messageArray[1][NetworkPackage::PACKAGE_DATA_SENDER_ADDRESS]
);
// Call parent exception constructor
parent::__construct($message, $code);
}
-}
-// [EOF]
-?>
+}