From 3e71fa3c83c14d20d3a564c2a0d074b5ce272aa0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 27 Apr 2012 16:47:27 +0000 Subject: [PATCH] 'hub' project continued: - discoverSocket() does now throw a NPE if listenerInstance is NULL - Missing rewind() call in BaseList added - Some debug messages added/improved - TODOs.txt updated --- .../socket/class_PackageSocketDiscovery.php | 18 +++++++++++++-- .../tcp/class_TcpConnectionHelper.php | 4 +++- .../udp/class_UdpConnectionHelper.php | 4 +++- application/hub/main/lists/class_BaseList.php | 6 +++++ .../hub/main/package/class_NetworkPackage.php | 22 +++++++++++-------- docs/TODOs.txt | 20 ++++++++--------- 6 files changed, 50 insertions(+), 24 deletions(-) diff --git a/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php b/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php index 1816b51c8..1fea2706d 100644 --- a/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php +++ b/application/hub/main/discovery/socket/class_PackageSocketDiscovery.php @@ -56,6 +56,9 @@ class PackageSocketDiscovery extends BaseHubDiscovery implements DiscoverableSoc * @return $listenerInstance An instance of a Listenable instance or null */ public function discoverListenerInstance ($protocolName, array $packageData) { + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput('SOCKET-DISCOVERY: protocolName=' . $protocolName . ' - ENTERED!'); + /* * Get the listener pool instance, we need to lookup the matching * listener->poolInstance chain there. @@ -71,11 +74,18 @@ class PackageSocketDiscovery extends BaseHubDiscovery implements DiscoverableSoc * pool. */ foreach ($poolInstance->getPoolEntriesInstance()->getArrayFromGroup($protocolName) as $listenerInstance) { + // Debug output + //* NOISY-DEBUG: */ $this->debugOutput('SOCKET-DISCOVERY: protocolName=' . $protocolName . ',listenerInstance=' . $listenerInstance->__toString()); + // Does the listener want that package? if ($listenerInstance->ifListenerAcceptsPackageData($packageData)) { // This listener likes our package data, so abort here + //* NOISY-DEBUG: */ $this->debugOutput('SOCKET-DISCOVERY: Listener is accepting package data.'); break; } // END - if + + // Debug output + //* NOISY-DEBUG: */ $this->debugOutput('SOCKET-DISCOVERY: Listener is NOT accepting package data.'); } // END - foreach // Return it @@ -89,6 +99,7 @@ class PackageSocketDiscovery extends BaseHubDiscovery implements DiscoverableSoc * @param $packageData Raw package data array * @return $socketResource A valid socket resource or FALSE if an error occured * @throws NoListGroupException If the procol group is not found in peer list + * @throws NullPointerException If listenerInstance is NULL */ public function discoverSocket (array $packageData) { // Determine protocol name @@ -99,9 +110,8 @@ class PackageSocketDiscovery extends BaseHubDiscovery implements DiscoverableSoc // If there is no listener who wants to have that package, we simply drop it here if (is_null($listenerInstance)) { - // @TODO We may need some logging here // Abort with no resource - return false; + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); } // END - if /* @@ -112,6 +122,9 @@ class PackageSocketDiscovery extends BaseHubDiscovery implements DiscoverableSoc */ $socketResource = $listenerInstance->getPoolInstance()->getSocketFromPackageData($packageData); + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput('socketResource=' . $socketResource . ',packageData='.print_r($packageData,true)); + // Is it false, the recipient isn't known to us and we have no connection to it if (($socketResource === false) || (!is_resource($socketResource)) || (socket_last_error($socketResource) > 0)) { // Try to create a new socket resource @@ -146,6 +159,7 @@ class PackageSocketDiscovery extends BaseHubDiscovery implements DiscoverableSoc } // And return it + $this->debugOutput('SOCKET-DISCOVERY: socketResource=' . $socketResource . ',packageData=' . print_r($packageData, true)); return $socketResource; } } diff --git a/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php b/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php index d0bec3e01..d10b80f88 100644 --- a/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php +++ b/application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php @@ -37,7 +37,9 @@ class TcpConnectionHelper extends BaseConnectionHelper implements ConnectionHelp } /** - * Creates a socket resource ("connection") for given recipient in package data + * Creates a half-connected socket resource ("connection") for given + * recipient in package data. After you called this method you still need to + * connect to the other node. * * @param $packageData Raw package data * @return $socketResource Socket resource diff --git a/application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php b/application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php index 1343c5588..26c345843 100644 --- a/application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php +++ b/application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php @@ -37,7 +37,9 @@ class UdpConnectionHelper extends BaseConnectionHelper implements ConnectionHelp } /** - * Creates a socket resource ("connection") for given recipient in package data + * Creates a half-connected socket resource ("connection") for given + * recipient in package data. After you called this method you still need to + * connect to the other node. * * @param $packageData Raw package data * @return $socketResource Socket resource diff --git a/application/hub/main/lists/class_BaseList.php b/application/hub/main/lists/class_BaseList.php index 0fd7a8a80..563317ba6 100644 --- a/application/hub/main/lists/class_BaseList.php +++ b/application/hub/main/lists/class_BaseList.php @@ -400,6 +400,9 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable { // Get an iterator $iteratorInstance = $this->listGroups[$groupName]->getIterator(); + // Rewind the iterator for this round + $iteratorInstance->rewind(); + // Go through all entries while ($iteratorInstance->valid()) { // Get key @@ -408,6 +411,9 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable { // ... and the final entry which is the stored instance $entry = $this->getEntry($entryIndex); + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput('LIST: Adding entry ' . $entry . ' ...'); + // Add it to the list $entries[$iteratorInstance->current()] = $entry; diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index 4e480d3e4..06869fcd1 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -311,20 +311,23 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R // Now get an iterator $iteratorInstance = $discoveryInstance->getIterator(); + // Rewind back to the beginning + $iteratorInstance->rewind(); + // ... and begin iteration while ($iteratorInstance->valid()) { // Get current entry $currentRecipient = $iteratorInstance->current(); - // Debug message - $this->debugOutput('PACKAGE: Package declared for recipient ' . $currentRecipient); - // Set the recipient $packageData[self::PACKAGE_DATA_RECIPIENT] = $currentRecipient; // And enqueue it to the writer class $this->getStackerInstance()->pushNamed(self::STACKER_NAME_DECLARED, $packageData); + // Debug message + $this->debugOutput('PACKAGE: Package declared for recipient ' . $currentRecipient); + // Skip to next entry $iteratorInstance->next(); } // END - while @@ -365,7 +368,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R $socketResource = $discoveryInstance->discoverSocket($packageData); // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after discoverSocket() has been called.'); + //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Reached line ' . __LINE__ . ' after discoverSocket() has been called.'); // We have to put this socket in our registry, so get an instance $registryInstance = SocketRegistry::createSocketRegistry(); @@ -374,7 +377,8 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R $helperInstance = Registry::getRegistry()->getInstance('connection'); // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.'); + //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: stateInstance=' . $helperInstance->getStateInstance()); + //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Reached line ' . __LINE__ . ' before isSocketRegistered() has been called.'); // Is it not there? if ((is_resource($socketResource)) && (!$registryInstance->isSocketRegistered($helperInstance, $socketResource))) { @@ -392,19 +396,19 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R } // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.'); + //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Reached line ' . __LINE__ . ' after isSocketRegistered() has been called.'); // Make sure the connection is up $helperInstance->getStateInstance()->validatePeerStateConnected(); // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.'); + //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Reached line ' . __LINE__ . ' after validatePeerStateConnected() has been called.'); // Enqueue it again on the out-going queue, the connection is up and working at this point $this->getStackerInstance()->pushNamed(self::STACKER_NAME_OUTGOING, $packageData); // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('NETWORK-PACKAGE: Reached line ' . __LINE__ . ' after pushNamed() has been called.'); + //* NOISY-DEBUG: */ $this->debugOutput('PACKAGE: Reached line ' . __LINE__ . ' after pushNamed() has been called.'); } /** @@ -577,7 +581,7 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R // Sanity check if we have packages declared if (!$this->isPackageDeclared()) { // This is not fatal but should be avoided - // @TODO Add some logging here + $this->debugOutput('PACKAGE: No package has been declared, but ' . __METHOD__ . ' has been called!'); return; } // END - if diff --git a/docs/TODOs.txt b/docs/TODOs.txt index 1613cdbb8..34b5f2bbc 100644 --- a/docs/TODOs.txt +++ b/docs/TODOs.txt @@ -23,7 +23,6 @@ ./application/hub/main/database/wrapper/states/class_PeerStateLookupDatabaseWrapper.php:177: * @todo Unfinished area ./application/hub/main/database/wrapper/states/class_PeerStateLookupDatabaseWrapper.php:219: * @todo Unfinished area ./application/hub/main/discovery/package/class_PackageRecipientDiscovery.php:100: * @todo Add some validation of recipient field, e.g. ip:port is found -./application/hub/main/discovery/socket/class_PackageSocketDiscovery.php:102: // @TODO We may need some logging here ./application/hub/main/factories/socket/class_SocketFactory.php:10: * @todo Find an interface for hub helper ./application/hub/main/filter/bootstrap/chat/class_ChatBootstrapGenericActivationFilter.php:54: * @todo Maybe we want to do somthing more here? ./application/hub/main/filter/bootstrap/cruncher/class_CruncherBootstrapBufferQueueInitializerFilter.php:54: * @todo 0% done @@ -51,11 +50,11 @@ ./application/hub/main/helper/connection/class_BaseConnectionHelper.php:182: // @TODO Move this to the socket error handler ./application/hub/main/helper/connection/class_BaseConnectionHelper.php:210: * @todo Rewrite the while() loop to a iterator to not let the software stay very long here ./application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php:10: * @todo Find an interface for hub helper -./application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php:143: * @todo We may want to implement a filter for ease notification of other objects like our pool -./application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php:47: * @todo $errorCode/-Message are now in handleSocketError()'s call-back methods -./application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php:84: // @TODO The whole resolving part should be moved out and made more configurable +./application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php:145: * @todo We may want to implement a filter for ease notification of other objects like our pool +./application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php:49: * @todo $errorCode/-Message are now in handleSocketError()'s call-back methods +./application/hub/main/helper/connection/tcp/class_TcpConnectionHelper.php:86: // @TODO The whole resolving part should be moved out and made more configurable ./application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php:10: * @todo Find an interface for hub helper -./application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php:54: * @todo Implement a filter for ease notification of other objects like the pool +./application/hub/main/helper/connection/udp/class_UdpConnectionHelper.php:56: * @todo Implement a filter for ease notification of other objects like the pool ./application/hub/main/helper/hub/announcement/class_HubDescriptorHelper.php:10: * @todo Find an interface for hub helper ./application/hub/main/helper/hub/announcement/class_HubDescriptorHelper.php:61: * @todo Rewrite the ->renderXmlContent() call to no arguments ./application/hub/main/helper/hub/connection/class_HubSelfConnectHelper.php:10: * @todo Find an interface for hub helper @@ -91,12 +90,11 @@ ./application/hub/main/package/class_NetworkPackage.php:23: * @todo Needs to add functionality for handling the object's type ./application/hub/main/package/class_NetworkPackage.php:244: * @todo $helperInstance is unused ./application/hub/main/package/class_NetworkPackage.php:248: // @TODO crc32() is very weak, but it needs to be fast -./application/hub/main/package/class_NetworkPackage.php:426: // @TODO We may want to do somthing more here? -./application/hub/main/package/class_NetworkPackage.php:554: // @TODO Add some logging here -./application/hub/main/package/class_NetworkPackage.php:580: // @TODO Add some logging here -./application/hub/main/package/class_NetworkPackage.php:684: // @TODO Add some logging here -./application/hub/main/package/class_NetworkPackage.php:783: // @TODO Add some content here -./application/hub/main/package/class_NetworkPackage.php:822: * @todo This may be enchanced for outgoing packages? +./application/hub/main/package/class_NetworkPackage.php:430: // @TODO We may want to do somthing more here? +./application/hub/main/package/class_NetworkPackage.php:558: // @TODO Add some logging here +./application/hub/main/package/class_NetworkPackage.php:688: // @TODO Add some logging here +./application/hub/main/package/class_NetworkPackage.php:787: // @TODO Add some content here +./application/hub/main/package/class_NetworkPackage.php:826: * @todo This may be enchanced for outgoing packages? ./application/hub/main/package/fragmenter/class_PackageFragmenter.php:274: * @todo Implement a way to send non-announcement packages with extra-salt ./application/hub/main/package/fragmenter/class_PackageFragmenter.php:442: * @todo $helperInstance is unused ./application/hub/main/producer/cruncher/keys/class_CruncherKeyProducer.php:106: // @TODO Do something with it -- 2.39.5