--- /dev/null
+<?php
+/**
+ * A helper class for maintaining connection statistics, no instance is
+ * required to use this class.
+ *
+ * @author Roland Haeder <webmaster@ship-simu.org>
+ * @version 0.0.0
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
+ * @license GNU GPL 3.0 or any newer version
+ * @link http://www.ship-simu.org
+ * @todo Find an interface for hub helper
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class ConnectionStatisticsHelper extends BaseHubSystem {
+ /**
+ * Statistics array
+ */
+ private static $connectionStatistics = array(
+ // Statistics for TCP connections
+ 'tcp' => array(
+ // Tried connection attempts
+ 'retry_count' => array(),
+ ),
+ // Statistics for UDP connections
+ 'udp' => array(
+ 'retry_count' => array(),
+ )
+ );
+
+ /**
+ * Protected constructor
+ *
+ * @return void
+ */
+ protected function __construct () {
+ parent::__construct(__CLASS__);
+ }
+
+ /**
+ * Checks wether the retry count has reached a configured limit for given
+ * connection.
+ *
+ * @param $helperInstance An instance of a ConnectionHelper class
+ * @return $isExhausted Wether the retry count has been reached
+ */
+ public static function isConnectRetryExhausted (ConnectionHelper $helperInstance) {
+ //* DEBUG: */ $helperInstance->debugOutput(__METHOD__ . ':helperInstance=' . $helperInstance->__toString() . ' - ENTERED!');
+ // Construct config entry
+ $configEntry = $helperInstance->getProtocol() . '_connect_retry_max';
+
+ // Check it out
+ $isExhausted = (
+ (
+ isset(self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'])
+ ) && (
+ self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'] >= $helperInstance->getConfigInstance()->getConfigEntry($configEntry)
+ )
+ );
+
+ // Return it
+ //* DEBUG: */ $helperInstance->debugOutput(__METHOD__ . ':helperInstance=' . $helperInstance->__toString() . ',isExhausted=' . intval($isExhausted) . ' - EXIT!');
+ return $isExhausted;
+ }
+
+ /**
+ * Increaes connect-retry count for given connection
+ *
+ * @param $helperInstance An instance of a ConnectionHelper class
+ * @return void
+ */
+ public static function increaseConnectRetry (ConnectionHelper $helperInstance) {
+ //* DEBUG: */ $helperInstance->debugOutput(__METHOD__ . ':helperInstance=' . $helperInstance->__toString() . ' - ENTERED!');
+ // Is the counter there
+ if (!isset(self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'])) {
+ // First attempt
+ //* DEBUG: */ $helperInstance->debugOutput(__METHOD__ . ':helperInstance=' . $helperInstance->__toString() . ' - FIRST!');
+ self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count'] = 1;
+ } else {
+ // Next attempt
+ //* DEBUG: */ $helperInstance->debugOutput(__METHOD__ . ':helperInstance=' . $helperInstance->__toString() . ' - INCREMENT!');
+ self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['retry_count']++;
+ }
+
+ // Create/update 'last_update' for later purging
+ self::$connectionStatistics[$helperInstance->getProtocol()][$helperInstance->__toString()]['last_update'] = time();
+ }
+}
+
+// [EOF]
+?>