From b1a7ac5fdb4d5a74b6dc5914f0835115a62cbe06 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Wed, 8 Dec 2010 16:54:08 +0000 Subject: [PATCH] [TASK] Implemented countActiveConnections(). --- Classes/Client/ClientManager.php | 10 +++++++++- Classes/Connection/ConnectionPool.php | 13 +++++++++++++ Testcode/Client/ClientManagerTest.php | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Classes/Client/ClientManager.php b/Classes/Client/ClientManager.php index 080ab9f..54a446b 100644 --- a/Classes/Client/ClientManager.php +++ b/Classes/Client/ClientManager.php @@ -61,7 +61,6 @@ class Client_ClientManager { } /** - * TODO: Implement a function that is able to count active connections instead of sockets! * @see Connection_ConnectionPool * @return int */ @@ -69,6 +68,15 @@ class Client_ClientManager { return $this->connectionPool->countSockets(); } + /** + * Returns the amount of connected connection handlers. + * @see Connection_ConnectionPool + * @return int + */ + public function countActiveConnections() { + return $this->connectionPool->countActiveConnections(); + } + /** * Main worker function. * Processes incoming data, calls clients and much more. diff --git a/Classes/Connection/ConnectionPool.php b/Classes/Connection/ConnectionPool.php index a9eb714..3b69156 100644 --- a/Classes/Connection/ConnectionPool.php +++ b/Classes/Connection/ConnectionPool.php @@ -180,5 +180,18 @@ class Connection_ConnectionPool { public function countSockets() { return $this->socketPool->countSockets(); } + + /** + * Returns the amount of active connections. + * A connection is active when the handler is connected. + * @return int + */ + public function countActiveConnections() { + $count = 0; + foreach($this->connectionHandlers AS $connectionHandler) { + if($connectionHandler->isConnected() === TRUE) $count++; + } + return $count; + } } ?> \ No newline at end of file diff --git a/Testcode/Client/ClientManagerTest.php b/Testcode/Client/ClientManagerTest.php index 89b7c09..69838e9 100644 --- a/Testcode/Client/ClientManagerTest.php +++ b/Testcode/Client/ClientManagerTest.php @@ -37,7 +37,7 @@ $srv = $clientManager->createTcpConnection("srv", "jpt"); $srv->bind("localhost", 7777); $srv->listen(); -while($clientManager->countSockets() > 0) { +while($clientManager->countActiveConnections() > 0) { $clientManager->work(); } ?> \ No newline at end of file