From 704767e83379523e958e5cea47f687e335519841 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 30 May 2017 22:12:23 +0200 Subject: [PATCH] Continued a bit: - splitted sender address and port to have both being copyable (needed later on) to ShareableInfo class - used some lesser socket_strerr() but socketInstance calls MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../socket/class_SocketContainer.php | 2 +- ...odeDistributedHashTableDatabaseWrapper.php | 4 ++-- .../class_PeerStateLookupDatabaseWrapper.php | 23 ++++++++----------- .../decoder/package/class_PackageDecoder.php | 3 ++- .../classes/listener/class_BaseListener.php | 23 +++++++++++++++---- .../classes/package/class_NetworkPackage.php | 13 +++++++---- .../fragmenter/class_PackageFragmenter.php | 6 ++--- .../class_InvalidDataChecksumException.php | 2 +- ...class_UnexpectedPackageStatusException.php | 5 ++-- .../class_PeerAlreadyRegisteredException.php | 6 ++--- 10 files changed, 51 insertions(+), 36 deletions(-) diff --git a/application/hub/classes/container/socket/class_SocketContainer.php b/application/hub/classes/container/socket/class_SocketContainer.php index 5eabaaaad..851041b06 100644 --- a/application/hub/classes/container/socket/class_SocketContainer.php +++ b/application/hub/classes/container/socket/class_SocketContainer.php @@ -454,7 +454,7 @@ class SocketContainer extends BaseHubContainer implements StorableSocket, Visita $socketResource = $this->getSocketResource(); // Get error code and message - $errorMessage = socket_strerror(socket_last_error($socketResource)); + $errorMessage = socket_strerror($this->getLastSocketErrorCode()); // Return it return $errorMessage; diff --git a/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php b/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php index b92b6f2bf..1b90bde43 100644 --- a/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/node/class_NodeDistributedHashTableDatabaseWrapper.php @@ -620,7 +620,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper imp /* 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'); @@ -629,7 +629,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseHubDatabaseWrapper imp $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); diff --git a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php index 07a9a5000..3591a81f3 100644 --- a/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php +++ b/application/hub/classes/database/frontend/states/class_PeerStateLookupDatabaseWrapper.php @@ -88,11 +88,11 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L */ 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 @@ -111,7 +111,7 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L /* 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 @@ -159,7 +159,7 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L $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'; @@ -167,12 +167,9 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L // 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 @@ -183,7 +180,7 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L $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.'); } /** @@ -203,11 +200,11 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L $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')); @@ -226,7 +223,7 @@ class PeerStateLookupDatabaseWrapper extends BaseHubDatabaseWrapper implements L } // 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()); } /** diff --git a/application/hub/classes/decoder/package/class_PackageDecoder.php b/application/hub/classes/decoder/package/class_PackageDecoder.php index 69004a39d..32e0ee894 100644 --- a/application/hub/classes/decoder/package/class_PackageDecoder.php +++ b/application/hub/classes/decoder/package/class_PackageDecoder.php @@ -113,7 +113,8 @@ class PackageDecoder extends BaseDecoder implements Decodeable, Registerable { // 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])) diff --git a/application/hub/classes/listener/class_BaseListener.php b/application/hub/classes/listener/class_BaseListener.php index a557a59cc..d983ad5b9 100644 --- a/application/hub/classes/listener/class_BaseListener.php +++ b/application/hub/classes/listener/class_BaseListener.php @@ -170,6 +170,7 @@ class BaseListener extends BaseHubSystem implements Visitable { * @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 @@ -187,6 +188,12 @@ class BaseListener extends BaseHubSystem implements Visitable { // 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); @@ -284,6 +291,7 @@ class BaseListener extends BaseHubSystem implements Visitable { * * @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 @@ -326,16 +334,23 @@ class BaseListener extends BaseHubSystem implements Visitable { // 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(); diff --git a/application/hub/classes/package/class_NetworkPackage.php b/application/hub/classes/package/class_NetworkPackage.php index 2ffd30771..9f70d1ab0 100644 --- a/application/hub/classes/package/class_NetworkPackage.php +++ b/application/hub/classes/package/class_NetworkPackage.php @@ -137,7 +137,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R /** * 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'; @@ -466,7 +467,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R */ 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]); @@ -824,7 +825,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R // 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, @@ -1342,7 +1344,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R * 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, @@ -1387,7 +1390,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R // 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 diff --git a/application/hub/classes/package/fragmenter/class_PackageFragmenter.php b/application/hub/classes/package/fragmenter/class_PackageFragmenter.php index e9848fc28..ab7d86789 100644 --- a/application/hub/classes/package/fragmenter/class_PackageFragmenter.php +++ b/application/hub/classes/package/fragmenter/class_PackageFragmenter.php @@ -170,9 +170,9 @@ class PackageFragmenter extends BaseHubSystem implements Fragmentable, Registera */ 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 ); } diff --git a/application/hub/exceptions/package/class_InvalidDataChecksumException.php b/application/hub/exceptions/package/class_InvalidDataChecksumException.php index 4eab57e8f..fac075078 100644 --- a/application/hub/exceptions/package/class_InvalidDataChecksumException.php +++ b/application/hub/exceptions/package/class_InvalidDataChecksumException.php @@ -44,7 +44,7 @@ class InvalidDataChecksumException extends FrameworkException { $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] ); diff --git a/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php b/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php index f5f7eeba6..d84c0afc9 100644 --- a/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php +++ b/application/hub/exceptions/package/class_UnexpectedPackageStatusException.php @@ -40,13 +40,14 @@ class UnexpectedPackageStatusException extends FrameworkException { */ 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] ); diff --git a/application/hub/exceptions/peer/class_PeerAlreadyRegisteredException.php b/application/hub/exceptions/peer/class_PeerAlreadyRegisteredException.php index 47ce2e0ce..98f25cd43 100644 --- a/application/hub/exceptions/peer/class_PeerAlreadyRegisteredException.php +++ b/application/hub/exceptions/peer/class_PeerAlreadyRegisteredException.php @@ -43,13 +43,11 @@ class PeerAlreadyRegisteredException extends FrameworkException { $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] -?> +} -- 2.39.5