From b47743ccc1caf6c6e416395bbab615076ccd5378 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 19 May 2012 16:56:47 +0000 Subject: [PATCH] Added verfication of connection type in addPeer(), added more debug --- .../hub/interfaces/pool/peer/class_PoolablePeer.php | 1 + .../hub/main/listener/tcp/class_TcpListener.php | 8 +++++++- application/hub/main/lists/class_BaseList.php | 8 +++++++- application/hub/main/pools/class_BasePool.php | 4 ++-- .../hub/main/pools/peer/class_DefaultPeerPool.php | 12 +++++++++++- 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/application/hub/interfaces/pool/peer/class_PoolablePeer.php b/application/hub/interfaces/pool/peer/class_PoolablePeer.php index 769a13edc..ad81095c2 100644 --- a/application/hub/interfaces/pool/peer/class_PoolablePeer.php +++ b/application/hub/interfaces/pool/peer/class_PoolablePeer.php @@ -29,6 +29,7 @@ interface PoolablePeer extends Poolable, SocketTag { * @param $connectionType Type of connection, can only be 'incoming', 'outgoing' or 'server' * @return void * @throws InvalidSocketException If the given resource is invalid or errorous + * @throws InvalidConnectionTypeException If the provided connection type is not valid */ function addPeer ($socketResource, $connectionType); diff --git a/application/hub/main/listener/tcp/class_TcpListener.php b/application/hub/main/listener/tcp/class_TcpListener.php index 00f78b185..25604efa5 100644 --- a/application/hub/main/listener/tcp/class_TcpListener.php +++ b/application/hub/main/listener/tcp/class_TcpListener.php @@ -216,10 +216,16 @@ class TcpListener extends BaseListener implements Listenable { // Some new peers found? if ($left < 1) { + // Debug message + //* EXTREME-NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: left=' . $left . ',server=' . $this->getSocketResource() . ',readers=' . print_r($readers, true)); + // Nothing new found return; } // END - if + // Debug message + /* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER:server=' . $this->getSocketResource() . ',readers=' . print_r($readers, true)); + // Do we have changed peers? if (in_array($this->getSocketResource(), $readers)) { /* @@ -232,7 +238,7 @@ class TcpListener extends BaseListener implements Listenable { $newSocket = socket_accept($this->getSocketResource()); // Debug message - //* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: newSocket=' . $newSocket . ',server=' .$this->getSocketResource()); + /* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: newSocket=' . $newSocket . ',server=' .$this->getSocketResource()); // Array for timeout settings $options = array( diff --git a/application/hub/main/lists/class_BaseList.php b/application/hub/main/lists/class_BaseList.php index cdf4514c1..7fe351e45 100644 --- a/application/hub/main/lists/class_BaseList.php +++ b/application/hub/main/lists/class_BaseList.php @@ -266,9 +266,15 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable { } elseif ((is_array($entry)) && (isset($entry['id']))) { // Supported array found $entry2 = crc32($entry['id']) . ':' . count($entry); + } elseif ((is_array($entry)) && (isset($entry[BasePool::SOCKET_ARRAY_RESOURCE])) && (isset($entry[BasePool::SOCKET_ARRAY_CONN_TYPE]))) { + // Is a socket resource array + $entry2 = crc32($entry[BasePool::SOCKET_ARRAY_RESOURCE] . ':' . $entry[BasePool::SOCKET_ARRAY_CONN_TYPE]); + } elseif (is_array($entry)) { + // Is a generic array + $entry2 = gettype($entry) . ':' . count($entry); } else { // Unsupported type detected - $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: entry type ' . gettype($entry) . ' is unsupported.'); + $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Entry type ' . gettype($entry) . ' is unsupported.'); // @TODO Extend this somehow? $entry2 = gettype($entry); diff --git a/application/hub/main/pools/class_BasePool.php b/application/hub/main/pools/class_BasePool.php index ca2243d49..708c4e5af 100644 --- a/application/hub/main/pools/class_BasePool.php +++ b/application/hub/main/pools/class_BasePool.php @@ -73,7 +73,7 @@ class BasePool extends BaseHubSystem implements Visitable { * * @param $group Name of the pool group * @param $poolSegment Name of the pool segment - * @param $instance An instance of a class we should add to the pool + * @param $instance An instance of a class that should bed added to the pool * @return void */ protected final function addInstance ($group, $poolName, Visitable $instance) { @@ -90,7 +90,7 @@ class BasePool extends BaseHubSystem implements Visitable { /** * Adds an entry to the pool * - * @param $poolEntry The new pool entry we should add + * @param $poolEntry The new pool entry that should be added * @return void */ protected final function addPoolEntry ($poolEntry) { diff --git a/application/hub/main/pools/peer/class_DefaultPeerPool.php b/application/hub/main/pools/peer/class_DefaultPeerPool.php index b0d762161..42fc143a6 100644 --- a/application/hub/main/pools/peer/class_DefaultPeerPool.php +++ b/application/hub/main/pools/peer/class_DefaultPeerPool.php @@ -90,11 +90,21 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { * @param $connectionType Type of connection, can only be 'incoming', 'outgoing' or 'server' * @return void * @throws InvalidSocketException If the given resource is invalid or errorous + * @throws InvalidConnectionTypeException If the provided connection type is not valid */ public function addPeer ($socketResource, $connectionType) { + // Debug message + //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',connectionType=' . $connectionType . ' - ENTERED!'); + // Validate the socket $this->validateSocket($socketResource); + // Is the connection type valid? + if (!$this->isValidConnectionType($connectionType)) { + // Is not a valid connection type! + throw new InvalidConnectionTypeException(array($this, $connectionType), self::EXCEPTION_INVALID_CONNECTION_TYPE); + } // END - if + // Default is this peer's IP $peerName = '0.0.0.0'; @@ -118,7 +128,7 @@ class DefaultPeerPool extends BasePool implements PoolablePeer { } // Debug message - $this->debugOutput('POOL: Adding peer ' . $peerName . ',socketResource=' . $socketResource); + $this->debugOutput('POOL: Adding peer ' . $peerName . ',socketResource=' . $socketResource . ',type=' . $connectionType); // Construct the array $socketArray = array( -- 2.39.5